kn-cli 1.0.120 → 1.0.123
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 +1 -1
- package/src/clear.js +64 -0
- package/src/cli.js +24 -1
- package/src/help.js +11 -0
- package/src/utils/index.js +2 -2
- package/src/utils/parseArgumentsIntoOptions.js +11 -0
package/package.json
CHANGED
package/src/clear.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
|
|
2
|
+
const path= require( "path");
|
|
3
|
+
const ROOT=path.resolve(__dirname,"../");
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const inquirer = require('inquirer');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
module.exports=async ()=> {
|
|
9
|
+
const sourceDir = process.cwd()
|
|
10
|
+
const tempDir = path.resolve(ROOT,'temp');
|
|
11
|
+
const now = new Date();
|
|
12
|
+
const threeMonthsAgo = new Date();
|
|
13
|
+
threeMonthsAgo.setMonth(now.getMonth() - 3);
|
|
14
|
+
let needClears=[];
|
|
15
|
+
try{
|
|
16
|
+
var files = fs.readdirSync(tempDir,{withFileTypes:true});
|
|
17
|
+
files.forEach(file=>{
|
|
18
|
+
if(/^[.]/.test(file.name))return;
|
|
19
|
+
const filename = path.resolve(tempDir,file.name,'_webpack');
|
|
20
|
+
if(!fs.existsSync(filename))return;
|
|
21
|
+
const fileStat = fs.statSync(filename);
|
|
22
|
+
if(fileStat.isDirectory()){
|
|
23
|
+
const isOlderThan3Months = fileStat.mtime < threeMonthsAgo;
|
|
24
|
+
if(isOlderThan3Months){
|
|
25
|
+
needClears.push(file.name)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
if(needClears.length>0){
|
|
30
|
+
console.log(`以下缓存目录可以被清理:`);
|
|
31
|
+
needClears.forEach(file=>{
|
|
32
|
+
console.log(`${file}`);
|
|
33
|
+
})
|
|
34
|
+
answer = await inquirer.prompt([
|
|
35
|
+
{
|
|
36
|
+
name:'isClear',
|
|
37
|
+
type:'list',
|
|
38
|
+
message:'请选择是否清理这些项目的缓存:',
|
|
39
|
+
choices:['放弃清理','清理'],
|
|
40
|
+
default:'放弃清理'
|
|
41
|
+
},
|
|
42
|
+
]);
|
|
43
|
+
if(answer.isClear == '清理'){
|
|
44
|
+
for(let dir of needClears){
|
|
45
|
+
try{
|
|
46
|
+
const filename = path.resolve(tempDir,dir);
|
|
47
|
+
console.log(`清理[${dir}]...`)
|
|
48
|
+
fs.rmdirSync(filename,{recursive:true,force:true});
|
|
49
|
+
console.log(`清理[${dir}]完毕`)
|
|
50
|
+
}catch(ex2){
|
|
51
|
+
console.log(`清理失败:`,ex2)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
}else{
|
|
58
|
+
console.log('没有可清理的项目')
|
|
59
|
+
}
|
|
60
|
+
}catch(err){
|
|
61
|
+
throw new Error(err);
|
|
62
|
+
}
|
|
63
|
+
return 1;
|
|
64
|
+
}
|
package/src/cli.js
CHANGED
|
@@ -5,6 +5,10 @@ const chalk = require("chalk");
|
|
|
5
5
|
const createProject = require("./create");
|
|
6
6
|
const build = require('./build');
|
|
7
7
|
const getTool = require("./getTools");
|
|
8
|
+
const clear = require("./clear");
|
|
9
|
+
const help = require("./help");
|
|
10
|
+
|
|
11
|
+
|
|
8
12
|
|
|
9
13
|
// const path= require("path");
|
|
10
14
|
// const ROOT=path.resolve(__dirname,"../");
|
|
@@ -23,7 +27,7 @@ module.exports=async (args)=> {
|
|
|
23
27
|
return console.log(pjson.version);
|
|
24
28
|
}
|
|
25
29
|
console.log(logVersion);
|
|
26
|
-
console.log(options);
|
|
30
|
+
// console.log(options);
|
|
27
31
|
if(get(options,"install")){
|
|
28
32
|
const req = await createProject(options);
|
|
29
33
|
if(!req){
|
|
@@ -57,6 +61,25 @@ module.exports=async (args)=> {
|
|
|
57
61
|
process.exit(2);
|
|
58
62
|
}
|
|
59
63
|
return 1;
|
|
64
|
+
}else if(get(options,"clear")){
|
|
65
|
+
const req = await clear(options);
|
|
66
|
+
if(!req){
|
|
67
|
+
process.exit(2);
|
|
68
|
+
}
|
|
69
|
+
return 1;
|
|
70
|
+
}else if(get(options,"help")){
|
|
71
|
+
const req = await help(options);
|
|
72
|
+
if(!req){
|
|
73
|
+
process.exit(2);
|
|
74
|
+
}
|
|
75
|
+
return 1;
|
|
76
|
+
}else{
|
|
77
|
+
console.log('命令格式错误,请参考下面:')
|
|
78
|
+
const req = await help(options);
|
|
79
|
+
if(!req){
|
|
80
|
+
process.exit(2);
|
|
81
|
+
}
|
|
82
|
+
return 1;
|
|
60
83
|
}
|
|
61
84
|
|
|
62
85
|
}
|
package/src/help.js
ADDED
package/src/utils/index.js
CHANGED
|
@@ -16,14 +16,14 @@ module.exports = {
|
|
|
16
16
|
})
|
|
17
17
|
})
|
|
18
18
|
},
|
|
19
|
-
cleanDir:function(dir,option){
|
|
19
|
+
cleanDir:function(dir,option={}){
|
|
20
20
|
const {exclude} = option;
|
|
21
21
|
return new Promise(resolve=>{
|
|
22
22
|
let filename='';
|
|
23
23
|
try{
|
|
24
24
|
var files = fs.readdirSync(dir);
|
|
25
25
|
files.forEach(file=>{
|
|
26
|
-
if(exclude(file) == false){
|
|
26
|
+
if(!exclude || exclude(file) == false){
|
|
27
27
|
filename = path.resolve(dir,file);
|
|
28
28
|
const fileStat = fs.statSync(filename);
|
|
29
29
|
if(fileStat.isDirectory()){
|
|
@@ -12,6 +12,14 @@ module.exports=function(rawArgs) {
|
|
|
12
12
|
"--report":Boolean,
|
|
13
13
|
"-v": "--version",//-v 转译成 --version
|
|
14
14
|
"--tool":Boolean,
|
|
15
|
+
"--clear":Boolean,// 清理缓存空间
|
|
16
|
+
"--help":Boolean,// 帮助,
|
|
17
|
+
"-h": "--help",
|
|
18
|
+
"--h":"--help",
|
|
19
|
+
"-help": "--help",
|
|
20
|
+
"-?": "--help",
|
|
21
|
+
"--?": "--help",
|
|
22
|
+
|
|
15
23
|
},
|
|
16
24
|
{
|
|
17
25
|
permissive:true,
|
|
@@ -29,5 +37,8 @@ module.exports=function(rawArgs) {
|
|
|
29
37
|
test: get(args, "--test", false),
|
|
30
38
|
report: get(args, "--report", false),
|
|
31
39
|
tool: get(args, "--tool", false),
|
|
40
|
+
clear: get(args, "--clear", false),
|
|
41
|
+
help: get(args, "--help", false),
|
|
42
|
+
|
|
32
43
|
};
|
|
33
44
|
}
|