1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const fs = require('fs'),
|
7 | path = require('path'),
|
8 | os = require('os'),
|
9 | prompt = require('prompt'),
|
10 | fsa = require('fs-extra'),
|
11 | util = require('./utils'),
|
12 | argv = require('minimist')(process.argv.slice(2));
|
13 |
|
14 | let tools = {
|
15 | regexp: /(\{\{[\s\S]*?\}\}|@[\s\S]*?)[\/|\\]*/gi,
|
16 |
|
17 | getProjectName: function () {
|
18 | return String(argv._[1]) || tools.getArg(['p', 'project']) || path.parse(process.cwd())['name'];
|
19 | },
|
20 |
|
21 | getConfig: function (cwdir) {
|
22 | let projectPath = cwdir || process.cwd(),
|
23 | configFile = path.resolve(projectPath, 'gupack-config.js'),
|
24 | cfgFile = argv['f'] || argv['gupackfile'];
|
25 | if (cfgFile) {
|
26 | configFile = path.isAbsolute(cfgFile) ? cfgFile : path.resolve(projectPath, cfgFile);
|
27 | }
|
28 |
|
29 | if (fs.existsSync(configFile)) {
|
30 | return require(configFile);
|
31 | } else {
|
32 |
|
33 | tools.log.red(`× 不是有效的项目:缺少配置文件(gupack-config.js) \n`);
|
34 | process.exit(1);
|
35 | }
|
36 | },
|
37 |
|
38 | isInSourcePath: function () {
|
39 | return fs.existsSync('./gupack-config.js');
|
40 | },
|
41 |
|
42 | setCWD: function (cwd) {
|
43 | process.chdir(cwd);
|
44 | },
|
45 |
|
46 | isAbsolutePath: function (p) {
|
47 | return path.isAbsolute(p) || ':' === p.slice(1, 2);
|
48 | },
|
49 |
|
50 |
|
51 | getBuildPath: function (base, buildpath) {
|
52 | if (!tools.isAbsolutePath(buildpath)) {
|
53 |
|
54 | return path.resolve(base, buildpath);
|
55 | } else {
|
56 | return buildpath;
|
57 | }
|
58 | },
|
59 |
|
60 | getStaticPath: function (filePath) {
|
61 | return filePath.replace(/^.{1,2}[\/\\]{1,2}/g, '');
|
62 | },
|
63 |
|
64 | getFileContent: function (filePath) {
|
65 | let content;
|
66 | if (fs.existsSync(filePath)) {
|
67 | content = fs.readFileSync(filePath, 'utf8');
|
68 | }
|
69 | return content || '';
|
70 | },
|
71 |
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | replaceVar: function (content, regx, value) {
|
80 | regx = regx || /\{\{\@[^\{]*?\}\}/gi;
|
81 | return (content = content.replace(regx, (m, $1) => {
|
82 | return value;
|
83 | }));
|
84 | },
|
85 |
|
86 | getTime: function () {
|
87 | let date = new Date();
|
88 | return `${fn(date.getHours())}:${fn(date.getMinutes())}:${fn(date.getSeconds())}`;
|
89 |
|
90 | function fn(n) {
|
91 | return n < 10 ? `0${n}` : n;
|
92 | }
|
93 | },
|
94 |
|
95 | prompt: function (message, cb) {
|
96 | return new Promise(function (resolve, reject) {
|
97 | prompt.get(
|
98 | [{
|
99 | name: 'ok',
|
100 | message: message
|
101 | }],
|
102 | (err, result) => {
|
103 | if (/^y|yes|ok|\u662f$/i.test(result.ok)) {
|
104 | cb && cb();
|
105 | resolve.apply(prompt, [result.ok]);
|
106 | } else {
|
107 | reject('Aborting');
|
108 | prompt.stop();
|
109 | }
|
110 | }
|
111 | );
|
112 | });
|
113 | },
|
114 |
|
115 | cmdify: function (command) {
|
116 | if (os.platform() !== 'win32') {
|
117 | return command;
|
118 | }
|
119 | command = command.split(' ');
|
120 | command[0] += '.cmd';
|
121 | return command.join(' ');
|
122 | },
|
123 |
|
124 | pathRegxs: [
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | /url\([\"\']?([^\"\']*)[\"\']?\)/gi,
|
130 | /(?:img|audio|video|script)[\S\s]*?src=[\"\']?([^\"\']*)[\"\']?/gi,
|
131 | /(?:link)[\S\s]*?href=[\"\']?([^\"\']*)[\"\']?/gi
|
132 | ],
|
133 |
|
134 | fillAlign: function (text, max, dir = 'right', char = ' ') {
|
135 | let s = text,
|
136 | l = max - s.length;
|
137 | for (let i = l; l > 0; l--) {
|
138 | s = dir === 'left' ? s + char : char + s;
|
139 | }
|
140 | return s;
|
141 | },
|
142 |
|
143 | hasArg: function (ags) {
|
144 | return !!tools.getArg(ags);
|
145 | },
|
146 | getArg: function (ags) {
|
147 | if (util.isString(ags)) return ags in argv && argv[ags];
|
148 | if (util.isArray(ags) && ags.length > 0) {
|
149 | for (let rs, i = 0; i < ags.length; ++i) {
|
150 | rs = argv[ags[i]];
|
151 |
|
152 |
|
153 | if (rs === 0) return String(rs);
|
154 | if (rs) return rs;
|
155 | }
|
156 | }
|
157 | return null;
|
158 | },
|
159 |
|
160 | parseArgv: function (argv) {
|
161 | argv = argv || process.argv.slice(2);
|
162 | let rs = {
|
163 | _: []
|
164 | };
|
165 | let index = 0;
|
166 | while (argv && argv.length > 0) {
|
167 | let aKey = argv[0],
|
168 | aName = argv[1];
|
169 | if (/-+/g.test(aKey)) {
|
170 | rs[aKey.replace(/-+/g, '')] = /-+/g.test(aName) ? true : aName;
|
171 | } else {
|
172 | aName && (rs[aKey] = /-+/g.test(aName) ? true : aName);
|
173 | rs._.push(aKey);
|
174 | }
|
175 | argv.splice(0, 1);
|
176 | }
|
177 | for (let key in rs) {
|
178 | if (rs._.indexOf(key) > -1) {
|
179 | delete rs[key];
|
180 | rs._ = rs._.filter(item => item === key);
|
181 | }
|
182 | }
|
183 | return rs;
|
184 | },
|
185 |
|
186 | fs: fs,
|
187 | fsa: fsa,
|
188 | Path: path,
|
189 | argv: argv
|
190 | };
|
191 |
|
192 | let styles = {
|
193 | red: [31, 39],
|
194 | error: [31, 39],
|
195 | green: [32, 39],
|
196 | success: [32, 39],
|
197 | blue: [36, 0],
|
198 | info: [36, 0],
|
199 | yellow: [33, 39],
|
200 | gray: [90, 39],
|
201 | start: [90, 39],
|
202 | end: [90, 39],
|
203 | magenta: [35, 39],
|
204 | cyan: [36, 39]
|
205 | };
|
206 | tools['log'] = console.log;
|
207 | tools['msg'] = {};
|
208 |
|
209 | for (let name in styles) {
|
210 | let fn = styles[name],
|
211 | open = '\x1b[' + fn[0] + 'm',
|
212 | close = '\x1b[' + fn[1] + 'm';
|
213 | inject(name, open, close);
|
214 | }
|
215 |
|
216 | function inject(name, open, close) {
|
217 | tools['log'][name] = (msg) => {
|
218 | console.log(open + (msg || '') + close);
|
219 |
|
220 | if (['error', 'end'].indexOf(name) > -1) {
|
221 | return process.exit(0);
|
222 | }
|
223 | };
|
224 | tools['msg'][name] = (msg) => {
|
225 | return open + msg + close;
|
226 | };
|
227 | }
|
228 |
|
229 | module.exports = tools; |
\ | No newline at end of file |