-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
141 lines (119 loc) · 3.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env node
var pathUtil = require('path');
var fs = require('fs-extra');
var inquirer = require('inquirer');
var facade = require('commander');
var pkg = require('./package.json');
facade
.command('release')
.description('release a pokemon')
.action(handlerRelease)
facade
.command('list')
.description('list current pokemon')
.action(showList)
facade
.command('kill')
.description('kill a pokemon')
.action(handlerKill)
facade
.command('catch')
.description('run setup commands for all envs')
.option("-n, --name [name]", "set nickname of the pokemon")
.option("-p, --path [path]", "set path of source folder")
.action(handlerCatch);
facade
.version(pkg.version)
.usage('catch')
.option('-f, --force', 'processing in force [TODO]')
.option('-r, --rename', 'rename the pokemon [TODO]')
.option('-m, --merge', 'merge two pokemon [TODO]')
.parse(process.argv);
if (!facade.args.length) facade.help();
function showList(type, cb, nickname) {
var isDictionary = function(name) {
return fs.lstatSync(name).isDirectory();
}
inquirer.prompt([
{
type: "list",
name: "pokemon",
message: "pick a pokemon",
choices: type == 'catch' ?
fs.readdirSync(process.cwd()).filter(isDictionary) :
fs.readdirSync(pathUtil.join(__dirname, 'pokemon'))
}
], function( answers ) {
cb && cb( answers.pokemon,nickname )
});
}
function temp() {
console.log('still in building');
}
//如果不是文件夹 抛出异常
function catchPoke(name, nickname, path) {
var wild = (path && path!= process.cwd()) ?
pathUtil.normalize(path) :
pathUtil.join(process.cwd(), name);
var nick = nickname ?
pathUtil.join(__dirname, 'pokemon', nickname) :
pathUtil.join(__dirname, 'pokemon', name ? name : pathUtil.basename(path));
var nickExits = fs.existsSync(nick),
wildExits = fs.existsSync(wild);
if (wildExits) {
if (!nickExits) {
fs.copy(wild, nick, function(err) {
if (err) {
console.log('error when copy', err)
}
console.log('OKK')
})
} else {
console.log('already exits');
}
}else{
console.log(wild + ' hasn\'t exit');
};
}
function handlerRelease() {
showList( 'release', releasePoke )
}
function handlerCatch(options) {
var name = ((typeof options.name)=='boolean')? false : options.name;
var path = ((typeof options.path)=='boolean')? false : options.path;
if (path) {
path = options.path || process.cwd();
catchPoke('', name, path);
}else{
showList( 'catch', catchPoke, name);
};
}
function releasePoke(name, path) {
var poke = pathUtil.join(__dirname, 'pokemon/' + name);
var exits = fs.existsSync(poke);
if (exits) {
fs.copy(poke, pathUtil.join(process.cwd(), name), function(err) {
if (err) {
console.log('error when copy')
}
console.log('OKK')
})
} else {
console.log('not exits');
}
}
function handlerKill() {
showList( 'kill', killPoke )
}
function killPoke(name) {
var poke = pathUtil.join(__dirname, 'pokemon/' + name);
var exits = fs.existsSync(poke);
if (exits) {
fs.remove(poke, function(err){
if (err) return console.error(err);
console.log("success!")
});
} else {
console.log('no ' + name);
}
}