1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 | const {fs,path,cwdPath,program,tip,pathInfo,getType,fwsConfig,npmPackage,getLocalIp} = {
|
4 | fs:require('fs'),
|
5 | path:require('path'),
|
6 | cwdPath:process.cwd(),
|
7 | program:require('commander'),
|
8 | tip:require('./lib/tip'),
|
9 | pathInfo:require('./lib/getPathInfo'),
|
10 | getType:require('./lib/getType'),
|
11 | fwsConfig:require('./config'),
|
12 | npmPackage:require('./package.json'),
|
13 | getLocalIp:require('./lib/getLocalIp')
|
14 | };
|
15 |
|
16 |
|
17 | String.prototype.replaceAll = function(substr,replacement){
|
18 | let re = /\*|\.|\?|\+|\$|\^|\[|\]|\(|\)|\{|\}|\||\\|\//g,
|
19 | newReS = substr.replace(re,(item,index)=>{
|
20 | return '\\'+item;
|
21 | }),
|
22 | newRe = new RegExp(newReS,'g');
|
23 | return this.replace(newRe,replacement);
|
24 | };
|
25 |
|
26 |
|
27 |
|
28 | program.version(npmPackage.version);
|
29 |
|
30 |
|
31 | global.fws = {
|
32 | 'fwsPath':path.join(__dirname,path.sep),
|
33 | 'taskPath':path.join(__dirname,'task'+path.sep),
|
34 | 'tplPath':path.join(__dirname,'tpl'+path.sep),
|
35 | 'tplConfigPath':path.join(__dirname,'tpl','_config'+path.sep),
|
36 | 'cmdPath':cwdPath,
|
37 | 'srcPath':path.join(cwdPath,'src'+path.sep),
|
38 | 'devPath':path.join(cwdPath,'dev'+path.sep),
|
39 | 'distPath':path.join(cwdPath,'dist'+path.sep),
|
40 | 'config':fwsConfig,
|
41 | 'require':(module)=>{
|
42 | delete require.cache[require.resolve(module)];
|
43 | return require(module);
|
44 | },
|
45 | 'globalReplace':{
|
46 | '$$localhost':getLocalIp()
|
47 | },
|
48 | 'cache':{}
|
49 | };
|
50 |
|
51 | global.fws_spriteTime ={};
|
52 |
|
53 |
|
54 | fws.config.update_author = fwsConfig.author;
|
55 | fws.config.update_mail = fwsConfig.mail;
|
56 |
|
57 |
|
58 | let fwsConfigPath = path.join(cwdPath,'fws_config.js');
|
59 | if(pathInfo(fwsConfigPath).type === 'file'){
|
60 | let fwsConfigData = require(fwsConfigPath);
|
61 | for(let i in fwsConfigData){
|
62 | fws.config[i] = fwsConfigData[i];
|
63 | };
|
64 | };
|
65 |
|
66 |
|
67 |
|
68 | if(pathInfo(path.join(__dirname,'/task')).type === 'dir'){
|
69 | let taskDirList = fs.readdirSync(fws.taskPath),
|
70 | task = {},
|
71 | taskList = [];
|
72 |
|
73 |
|
74 | taskDirList.forEach((item,index)=>{
|
75 | if(path.extname(item).toLowerCase() === '.js'){
|
76 | taskList.push(item);
|
77 | };
|
78 | });
|
79 |
|
80 | for(let index=0,len = taskList.length; index<len; index++){
|
81 | let item = taskList[index],
|
82 | taskFile = path.join(fws.taskPath,item),
|
83 | extName = path.extname(taskFile),
|
84 | fileName = path.basename(taskFile,extName),
|
85 | taskContent = require(taskFile).regTask;
|
86 |
|
87 |
|
88 | if(taskContent){
|
89 |
|
90 | if(taskContent.command && getType(taskContent.command) === 'string'){
|
91 | task[fileName] = program.command(`${fileName} ${taskContent.command}`);
|
92 | }else{
|
93 | tip.error(`任务 "${taskFile}" regTask.command 无效`);
|
94 | continue;
|
95 | };
|
96 |
|
97 |
|
98 | if(taskContent.description && getType(taskContent.description) === 'string'){
|
99 | task[fileName].description(taskContent.description);
|
100 | };
|
101 |
|
102 |
|
103 | if(taskContent.option && getType(taskContent.option) === 'array'){
|
104 | taskContent.option.forEach((item,index)=>{
|
105 | task[fileName].option.apply(task[fileName],item);
|
106 | });
|
107 | };
|
108 |
|
109 |
|
110 | if(taskContent.action && getType(taskContent.action) === 'function'){
|
111 | task[fileName].action((name,options)=>{
|
112 | let task = new taskContent.action(name,options);
|
113 | task.init();
|
114 | });
|
115 | }else{
|
116 | tip.error(`任务 "${taskFile}" regTask.action 必须是一个函数`);
|
117 | };
|
118 |
|
119 |
|
120 | if(taskContent.help && getType(taskContent.help) === 'function'){
|
121 | task[fileName].on('--help',(...arg)=>{
|
122 | taskContent.help(arg);
|
123 | });
|
124 | };
|
125 |
|
126 | }else{
|
127 | tip.error(`"${taskFile}" 不是一个有效的任务插件,请检查插件暴露参数。`);
|
128 | };
|
129 |
|
130 | };
|
131 |
|
132 | }else{
|
133 | tip.error(`任务目录 ${fws.taskPath} 好像不存在,请检查……`);
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 | program.on('--help',()=>{
|
139 | console.log(` 意见反馈:`);
|
140 | console.log(` ------------------------------------------------------------`);
|
141 | console.log(' @单炒饭 https://github.com/sbfkcel/fws/wiki');
|
142 | });
|
143 |
|
144 |
|
145 | program.parse(process.argv);
|
146 |
|
147 |
|
148 | if (!process.argv.splice(2).length) {
|
149 | program.outputHelp();
|
150 | };
|