UNPKG

4.66 kBJavaScriptView Raw
1var fileUtil = require('./fileUtil');
2var stringUtil = require('./stringUtil');
3var configUtil = require('./configUtil');
4var tip = require('./tip');
5
6var cmdMap = {
7 '--version': tip.showVersion,
8 '-version': tip.showVersion,
9 '-v': tip.showVersion,
10 '-help': tip.showHelp,
11 '--help': tip.showHelp,
12 'init': configUtil.initConfig
13};
14
15// 模块和模块下文件的路径连接符
16var joinChar = '~';
17
18//配置的默认目标文件夹路径引用符
19var defaultDestDirRefChar = '!';
20
21var nameRE = /^[$a-zA-z_][$a-zA-z_0-9]+/;
22
23var cmdName = 'autocode';
24
25function parse(sections) {
26 sections = sections.slice(2);
27
28 var arg1 = sections.shift();
29
30 if(arg1 == null){
31 tip.showHelp();
32 return;
33 }
34
35 var f = cmdMap[arg1];
36 if (f) {
37 f();
38 return;
39 }
40
41 var config = configUtil.getConfig();
42
43 var mod = parseMod(arg1);
44
45 var dest;
46 var arg2 = sections.shift();
47
48 dest = arg2 == null ? {
49 destDir: getDestDir(config, mod.modName)
50 } : parseDest(arg2, mod.modName);
51
52
53 var arg3 = sections.shift();
54 var newModName = dest.newModName,
55 force = dest.force;
56
57 if (arg3) {
58 if (arg3 === '--force' || arg3 === '-force') {
59 force = true;
60 } else if (!newModName && nameRE.test(arg3)) {
61 newModName = arg3;
62 }
63 }
64
65 if (!force) {
66 var arg4 = sections.shift();
67 if (arg4 && (arg4 === '--force' || arg4 === '-force')) {
68 force = true;
69 }
70 }
71
72 var date = new Date();
73
74 config.__date__ = date.getFullYear() + '/' + formatNum(date.getMonth() + 1) + '/' + formatNum(date.getDate()) + ' ' + formatNum(date.getHours()) + ':' + formatNum(date.getMinutes());
75 config.__moduleprefix__ = newModName;
76
77 if (mod.filePath && dest.filePath) {
78 fileUtil.copyFile(mod.filePath, dest.filePath, config, null, force);
79 } else if (mod.filePath) {
80 var destFilePath = newModName ? dest.destDir + '/' + stringUtil.xapitalize(newModName) + '/' + mod.fileName : dest.destDir + '/' + mod.fileName;
81 fileUtil.copyFile(mod.filePath, destFilePath, config, null, force);
82 } else if (dest.filePath) {
83 console.error('dest dir `' + dest.filePath + '` is not a dir, you could not copy dir into a file!');
84 } else {
85 fileUtil.copyDir(mod.modTplDir, newModName ? dest.destDir + '/' + stringUtil.xapitalize(newModName) : dest.destDir, config, null, force);
86 }
87}
88
89function formatNum(num) {
90 return num < 10 ? '0' + num : num;
91}
92
93function parseDest(arg, modName) {
94 var sections, defaultDestDirRef, newModName, lastSlashIndex, filePath, force;
95 var config = configUtil.getConfig();
96 var destDir = getDestDir(config, modName);
97
98 if (arg.indexOf('/') !== -1) {
99 //绝对路径, 目标目录
100 destDir = arg;
101 } else if (arg.indexOf(joinChar) !== -1) {
102 //相对模块默认目标目录的路径
103 sections = arg.split(joinChar);
104 defaultDestDirRef = sections.shift();
105 if (defaultDestDirRef !== defaultDestDirRefChar) {
106 console.error('the relative path of dest file(or dir) must begin with `' + defaultDestDirRefChar + '` !');
107 return;
108 }
109 sections.unshift(destDir);
110 destDir = sections.join('/');
111 } else if (nameRE.test(arg)) {
112 //需要生成的实际模块名
113 newModName = arg;
114 } else if (arg === '--force' || arg === '-force') {
115 force = true;
116 }
117
118 lastSlashIndex = destDir.lastIndexOf('/');
119 if (destDir.lastIndexOf('.') > lastSlashIndex) {
120 // modDir 实际是一个文件
121 filePath = destDir;
122 destDir = destDir.substring(0, lastSlashIndex);
123 }
124
125 return {
126 destDir: destDir,
127 filePath: filePath,
128 newModName: newModName,
129 force: force
130 };
131}
132
133
134
135function parseMod(arg) {
136 var sections, modName, filePath, fileName, lastSlashIndex;
137 var config = configUtil.getConfig();
138
139 if (arg.indexOf('/') !== -1) {
140 //绝对路径
141 modDir = arg;
142 } else if (arg.indexOf(joinChar) !== -1) {
143 //相对模块路径
144 sections = arg.split(joinChar);
145 modName = sections.shift();
146 modDir = config.tplDir ? config.tplDir + '/' + modName : modName;
147 sections.unshift(modDir);
148 modDir = sections.join('/');
149 } else {
150 //模块名
151 modName = arg;
152 modDir = config.tplDir ? config.tplDir + '/' + modName : modName;
153 }
154
155 lastSlashIndex = modDir.lastIndexOf('/');
156 if (modDir.lastIndexOf('.') > lastSlashIndex) {
157 // modDir 实际是一个文件
158 filePath = modDir;
159 fileName = filePath.substring(lastSlashIndex + 1);
160 modDir = modDir.substring(0, lastSlashIndex);
161 }
162
163 return {
164 modTplDir: modDir,
165 modName: modName,
166 filePath: filePath,
167 fileName: fileName
168 };
169}
170
171function getDestDir(config, modName) {
172 return config.destDir && config.destDir[modName] || '';
173}
174
175module.exports = {
176 parse: parse
177};
\No newline at end of file