Skip to content

Commit

Permalink
feat: easy upgrade command and fix easy clean in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli committed Mar 12, 2018
1 parent baa06be commit a1d0fd5
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 10 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ Usage: easy [command] [options]

init [options] init webpack config or boilerplate for Vue/React/Weex
install npm install
print [env] [options] print webpack config, support print by env or config node key
build [env] webpack building
server [env] webpack building and start server
clean [env] clean webpack cache
open [dir] open webpack cache dir
kill [port] kill port, such as 7001 or 7001,9000,9001
print [env] [options] print webpack config, support print by env or config node key
build [env] webpack building
server [env] webpack building and start server
clean [env] clean webpack cache
open [dir] open webpack cache dir
upgrade upgrade project easywebpack relation package to latest version
kill [port] kill port, such as 7001 or 7001,9000,9001



Expand Down
8 changes: 8 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

'use strict';
const path = require('path');
const fs = require('fs');
const program = require('commander');
const chalk = require('chalk');
const _ = require('lodash.get');
Expand Down Expand Up @@ -51,6 +52,13 @@ program
builder.getWebpackConfig(config, option);
});

program
.command('upgrade')
.description('upgrade project package to latest version')
.action(() => {
require('../lib/upgrade')(baseDir);
});

program
.command('print [env]')
.option('-n, --node [key]', 'print webpack config info by config node key, example: [module/module.rules/plugins] and so on')
Expand Down
28 changes: 25 additions & 3 deletions lib/tool.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
'use strict';
const os = require('os');
const fs = require('fs');
const shell = require('shelljs');
const opn = require('opn');
const kill = require('kill-port');
exports.rm = filepath => {
const dirs = Array.isArray(filepath) ? filepath : [filepath];
dirs.forEach(dir => {
const result = shell.exec(`rm -rf ${dir}`);
if (result.code === 0) {
if(os.platform() === 'win32') {
exports.deleteFile(dir);
console.log(`remove [ ${dir} ] success`);
} else {
console.log(`remove [ ${dir} ] failed`);
const result = shell.exec(`rm -rf ${dir}`);
if (result.code === 0) {
console.log(`remove [ ${dir} ] success`);
} else {
console.log(`remove [ ${dir} ] failed`);
}
}
});
};

exports.deleteFile = filepath => {
if (fs.existsSync(filepath)) {
const files = fs.readdirSync(filepath);
files.forEach((file, index) => {
const curPath = filepath + "/" + file;
if (fs.statSync(curPath).isDirectory()) {
exports.deleteFile(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(filepath);
}
}

exports.open = filepath => {
opn(filepath);
};
Expand Down
78 changes: 78 additions & 0 deletions lib/upgrade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
module.exports = baseDir => {
const pkgFile = path.join(baseDir, 'package.json');
const pkgJSON = require(pkgFile);
const removePackages = [
'babel-core',
'babel-loader',
'babel-eslint',
'postcss-loader',
'eslint-loader',
'vue-template-compiler',
'server-side-render-resource',
'vue-server-renderer',
'webpack-manifest-normalize',
'html-webpack-plugin',
'directory-named-webpack-plugin',
'progress-bar-webpack-plugin',
'webpack-manifest-resource-plugin'
];
const upgradePackages = {
'easywebpack-vue': {
version: '^4.0.0'
},
'easywebpack-react': {
version: '^4.0.0'
},
'easywebpack-html': {
version: '^4.0.0'
},
'easywebpack-js': {
version: '^4.0.0'
},
'egg-webpack': {
version: '^4.0.0'
},
'ts-loader': {
version: '^4.0.0'
},
};

// remove and update dependencies
Object.keys(pkgJSON.dependencies).forEach(name => {
if(removePackages.indexOf(name) > -1) {
delete pkgJSON.dependencies[name];
}
if(upgradePackages[name]) {
pkgJSON.dependencies[name] = upgradePackages[name].version ;
}
});

// remove and update devDependencies
Object.keys(pkgJSON.devDependencies).forEach(name => {
if(removePackages.indexOf(name) > -1) {
delete pkgJSON.devDependencies[name];
}
if(upgradePackages[name]) {
pkgJSON.devDependencies[name] = upgradePackages[name].version ;
}
});

// remove npm lock file
const packageLockFile = path.join(baseDir, 'package-lock.json');
if(fs.existsSync(packageLockFile)) {
fs.unlinkSync(packageLockFile);
}

// remove yarn lock file
const yarnLockFile = path.join(baseDir, 'yarn.lock');
if(fs.existsSync(yarnLockFile)) {
fs.unlinkSync(yarnLockFile);
}
fs.writeFileSync(pkgFile, JSON.stringify(pkgJSON, null, 2));
console.log(chalk.green('-- 1. upgrade package.json successfully'));
console.log(chalk.red('-- 2. please reinstall the dependencies with npm install or yarn install'));
}
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = {
return null;
}
},

initOption(program, option, config = {}) {
const target = program.web ? 'web' : (program.node ? 'node' : undefined);
return Object.assign({}, {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easywebpack-cli",
"version": "3.8.0",
"version": "3.9.0",
"description": "Webpack Building Command Line And Boilerplate Init Tool for easywebpack, Support Vue/React/Weex",
"bin": {
"easywebpack": "bin/cli.js",
Expand Down

0 comments on commit a1d0fd5

Please # to comment.