kn-cli 1.0.13 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kn-cli",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/readme.md CHANGED
@@ -17,6 +17,11 @@
17
17
 
18
18
 
19
19
  # 更新日志
20
+ * 1.0.14
21
+ 1. 对打包过程优化,判断node_modules是否存在及清理必要
22
+ 2. 在打包前输出cli版本号
23
+
24
+
20
25
  * 1.0.13
21
26
  1. 对打包时resolve.modules寻找node_modules的路径优先级进行调整
22
27
 
package/src/build.js CHANGED
@@ -26,14 +26,16 @@ const ROOT=path.resolve(__dirname,"../");
26
26
  module.exports= async (options={})=> {
27
27
  const {type='dev'} = options;
28
28
  const projectPath = process.cwd();
29
+ const projectName = path.parse(projectPath).base;
29
30
 
30
31
  // 用户项目文件夹下的public目录
31
32
  const sourceDir = path.resolve(projectPath,'public');
32
33
  // 用户项目文件夹下的package.json
33
34
  const projectPackagePath = path.resolve(projectPath,'package.json');
34
-
35
+ const tempDir = path.resolve(ROOT,'temp');
36
+ const tempProjectDir = path.resolve(tempDir,projectName);
35
37
  // 构建模板
36
- const tempWebpackDir = path.resolve(ROOT,"temp/_webpack/");
38
+ const tempWebpackDir = path.resolve(tempProjectDir,"_webpack");
37
39
  const tempWebpackPackagePath = path.resolve(tempWebpackDir,'package.json');
38
40
  const tempWebpackConfigPath = path.resolve(tempWebpackDir,'webpack.config.js');
39
41
  const tempStartShell = path.resolve(tempWebpackDir,'start.sh');
@@ -65,29 +67,51 @@ module.exports= async (options={})=> {
65
67
  },
66
68
  ]);
67
69
  }
70
+ let logCount='';
68
71
  const tasks = new Listr([
69
72
  {...TaskNodeVersion},
70
73
  {
71
74
  title:"创建临时缓存文件夹",
72
75
  task:async (ctx,task)=>{
73
- const tempDir = path.resolve(ROOT,"temp");
74
76
  try{
75
- fs.statSync(tempDir);
77
+ const exists = fs.existsSync(tempWebpackDir);
78
+ if(exists==false){
79
+ task.title=`创建临时缓存文件夹成功${tempWebpackDir}`;
80
+ shelljs.mkdir(tempDir);
81
+ shelljs.mkdir(tempProjectDir);
82
+ if(answer){
83
+ answer.install='需要';
84
+ }
85
+ return;
86
+ }
87
+ const hasNodeModules = fs.existsSync(path.resolve(tempWebpackDir,'node_modules'));
88
+ if(!hasNodeModules){
89
+ if(answer){
90
+ answer.install='需要';
91
+ }
92
+ }
76
93
  task.title='清理旧的缓存文件';
77
94
  // 保留node_modules,以提高编译效率
78
- await cleanDir(tempDir,function(file){
79
- task.out=`${tempDir}`;
80
- if(type=='dev'&&/node_modules/.test(file)){
81
- return true;
95
+ await cleanDir(tempWebpackDir+'/',{
96
+ exclude:function(file){
97
+ if(type=='dev'&&/node_modules/.test(file)){
98
+ if(answer?.install=='不需要'){
99
+ return true;
100
+ }
101
+ }
102
+ logCount+=`清除:${file}\n`
103
+ return false;
82
104
  }
83
- return false;
84
105
  });
85
- task.title='清理缓存完毕';
106
+ if(logCount){
107
+ task.output=`清除文件:${logCount}`;
108
+ }
109
+ task.title=`清理缓存完毕${tempWebpackDir}`;
86
110
  }catch(err){
87
- task.title=`创建临时缓存文件夹成功`;
88
- shelljs.mkdir(tempDir);
111
+ task.output=err.message;
89
112
  }
90
- }
113
+ },
114
+ options: { persistentOutput: true }
91
115
  },
92
116
  {
93
117
  title:"缓冲",
@@ -205,16 +229,16 @@ module.exports= async (options={})=> {
205
229
  let build_env='';
206
230
  let mock='';
207
231
  let noSkipNpmInstall='';
208
- switch(answer.type){
232
+ switch(answer?.type){
209
233
  case '本地调试':{build_env='localdebug'}break;
210
234
  case '测试环境':{build_env='dev'}break;
211
235
  case '生产环境':{build_env='prod'}break;
212
236
  }
213
- switch(answer.mock){
237
+ switch(answer?.mock){
214
238
  case '打开':{mock='1'}break;
215
239
  case '不打开':{mock='0'}break;
216
240
  }
217
- switch(answer.install){
241
+ switch(answer?.install){
218
242
  case '需要':{noSkipNpmInstall='1'}break;
219
243
  case '不需要':{noSkipNpmInstall='0'}break;
220
244
  }
package/src/cli.js CHANGED
@@ -14,11 +14,13 @@ const build = require('./build');
14
14
 
15
15
  module.exports=async (args)=> {
16
16
  let options = parseArgumentsIntoOptions(args);
17
+ let logVersion = chalk.green(`当前cli版本为 : ${pjson.version}`)
18
+
17
19
  // 打印版本信息
18
20
  if (get(options, "version")) {
19
- let log = chalk.green(`当前cli版本为 : ${pjson.version}`)
20
- return console.log(log);
21
+ return console.log(logVersion);
21
22
  }
23
+ console.log(logVersion);
22
24
  console.log(options);
23
25
  if(get(options,"install")){
24
26
  const req = await createProject(options);
@@ -19,16 +19,23 @@ module.exports = {
19
19
  cleanDir:function(dir,option){
20
20
  const {exclude} = option;
21
21
  return new Promise(resolve=>{
22
+ let filename='';
22
23
  try{
23
24
  var files = fs.readdirSync(dir);
24
25
  files.forEach(file=>{
25
26
  if(exclude(file) == false){
26
- fs.rmdirSync(path.resolve(dir,file));
27
+ filename = path.resolve(dir,file);
28
+ const fileStat = fs.statSync(filename);
29
+ if(fileStat.isDirectory()){
30
+ fs.rmdirSync(filename,{recursive:true,force:true});
31
+ }else{
32
+ fs.rmSync(filename,{force:true});
33
+ }
27
34
  }
28
35
  })
29
36
  resolve();
30
37
  }catch(err){
31
- throw new Error(err.message);
38
+ throw new Error(err);
32
39
  }
33
40
  })
34
41
  },