Skip to content

Commit 9bb1c33

Browse files
author
caoli
committed
init
0 parents  commit 9bb1c33

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fis3-command-npm
2+
====================
3+
4+
- fis3 npm install 安装node_modules 依赖
5+
6+
- fis3 npm start 启动bin/www
7+
8+
- fis3 npm app.js node app.js 启动应用

index.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
exports.name = 'npm';
2+
exports.desc = 'description of npm';
3+
exports.options = {
4+
'-h, --help': 'print this help message',
5+
'--files' : 'some options.'
6+
};
7+
8+
exports.run = function(argv, cli) {
9+
// 如果输入为 fis3 npm -h
10+
// 或者 fis3 npm --help
11+
// 则输出帮助信息。
12+
if (argv.h || argv.help) {
13+
return cli.help(exports.name, exports.options);
14+
}
15+
16+
17+
var exec = require('child_process').exec;
18+
19+
var fisReleaseDir = fis.project.getTempPath('/www');
20+
//console.log('>>>app run dir:' + fisReleaseDir);
21+
22+
var cmdRun = function(cmdRunStr){
23+
exec(cmdRunStr, {cwd: fisReleaseDir}, function(err,stdout,stderr){
24+
if(err) {
25+
console.log('>>>'+cmdRunStr+' error:' + err);
26+
} else {
27+
console.log('>>>'+cmdRunStr+' success');
28+
}
29+
});
30+
};
31+
32+
33+
var safeRun = function(cmdRunStr){
34+
if(cmdRunStr.endsWith('.js')) {
35+
var command = cmdRunStr.split(' ')[1];
36+
has(fisReleaseDir + '/' + command).then(function (isExist) {
37+
if (isExist) {
38+
cmdRun(cmdRunStr);
39+
} else {
40+
console.log('>>>invalid file:' + command);
41+
}
42+
});
43+
}else{
44+
cmdRun(cmdRunStr);
45+
}
46+
}
47+
48+
var has = function(path){
49+
return new Promise(function (resolve, reject) {
50+
fs.stat(path, function(err, stat) {
51+
if (err == null && stat.isDirectory()) {
52+
resolve(true);
53+
}else if (err == null && stat.isFile()) {
54+
resolve(true);
55+
}else{
56+
resolve(false);
57+
}
58+
});
59+
});
60+
}
61+
62+
var fs = require('fs');
63+
64+
var params = argv ? argv['_']||[] : [];
65+
66+
if (params.length >= 2) {
67+
var command = params[1];
68+
if (command == 'start') {
69+
safeRun('npm start');
70+
} else if (command == 'install') {
71+
safeRun('npm install');
72+
} else if(command.endsWith('.js')) {
73+
safeRun('node '+ command)
74+
}else{
75+
console.log('>>>npm install invalid command:' + command);
76+
}
77+
} else {
78+
79+
has(fisReleaseDir + '/node_modules').then(function (installed) {
80+
//console.log('>>>' + installed);
81+
if (installed) {
82+
safeRun('node app.js')
83+
} else {
84+
safeRun('npm install');
85+
safeRun('node app.js')
86+
}
87+
});
88+
}
89+
90+
91+
};

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "fis3-command-npm",
3+
"version": "0.0.1",
4+
"description": "fis3 自动安装node_modules",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/hubcarl/fis3-command-npm"
12+
},
13+
"keywords": [
14+
"fis3",
15+
"server"
16+
],
17+
"author": "sky",
18+
"bugs": {
19+
"url": "https://github.com/hubcarl/fis3-command-npm/issues"
20+
},
21+
"homepage": "https://github.com/hubcarl/fis3-command-npm"
22+
}

0 commit comments

Comments
 (0)