UNPKG

12.2 kBJavaScriptView Raw
1(function() {
2 // require
3 /*
4 excludeInclude(source)
5 formatArgument(arg)
6 getPlugin(name)
7 normalizePath(string)
8 normalizePathToArray(source)
9 wrapList(list)
10 */
11 /*
12 fetchGitHub_(name)
13 */
14 var $, _, excludeInclude, fetchGitHub_, formatArgument, getPlugin, gulp, i, j, key, kleur, len, len1, listKey, normalizePath, normalizePathToArray, path, string, wrapList,
15 indexOf = [].indexOf;
16
17 path = require('path');
18
19 gulp = require('gulp');
20
21 kleur = require('kleur');
22
23 $ = require('estus-flask');
24
25 ({_} = $);
26
27 // variable
28 $.os = (function() {
29 switch (false) {
30 case !~(string = process.platform).search('darwin'):
31 return 'macos';
32 case !~string.search('win'):
33 return 'windows';
34 default:
35 return 'linux';
36 }
37 })();
38
39 $.path = {
40 base: process.cwd(),
41 home: require('os').homedir()
42 };
43
44 $.plugin = {};
45
46 // return
47 module.exports = $;
48
49 excludeInclude = function(source) {
50 source = formatArgument(source);
51 source.push('!**/include/**');
52 return source = _.uniq(source); // return
53 };
54
55 formatArgument = function(arg) {
56 var type;
57 switch (type = $.type(arg)) {
58 case 'array':
59 return _.clone(arg);
60 case 'string':
61 return [arg];
62 default:
63 throw new Error(`invalid type '${type}'`);
64 }
65 };
66
67 getPlugin = function(name) {
68 var base1;
69 if (name === 'uglify') {
70 return (base1 = $.plugin).uglify || (base1.uglify = (function() {
71 var composer, uglifyEs;
72 uglifyEs = require('uglify-es');
73 composer = require('gulp-uglify/composer');
74 return composer(uglifyEs, console);
75 })());
76 }
77 throw new Error(`invalid plugin '${name}'`);
78 };
79
80 normalizePath = function(string) {
81 var isIgnore;
82 if ('string' !== $.type(string)) {
83 return null;
84 }
85 // check isIgnore
86 if (string[0] === '!') {
87 isIgnore = true;
88 string = string.slice(1);
89 }
90 // replace . & ~
91 string = string.replace(/\.{2}/g, '__parent_directory__');
92 string = (function() {
93 switch (string[0]) {
94 case '.':
95 return string.replace(/\./, $.path.base);
96 case '~':
97 return string.replace(/~/, $.path.home);
98 default:
99 return string;
100 }
101 })();
102 string = string.replace(/__parent_directory__/g, '..');
103 // replace ../ to ./../ at start
104 if (string[0] === '.' && string[1] === '.') {
105 string = `${$.path.base}/${string}`;
106 }
107 // normalize
108 string = path.normalize(string).replace(/\\/g, '/');
109 // absolute
110 if (!path.isAbsolute(string)) {
111 string = `${$.path.base}/${string}`;
112 }
113 // ignore
114 if (isIgnore) {
115 string = `!${string}`;
116 }
117 // return
118 return _.trimEnd(string, '/');
119 };
120
121 normalizePathToArray = function(source) {
122 var groupSource, i, len, results;
123 groupSource = formatArgument(source);
124 results = [];
125 for (i = 0, len = groupSource.length; i < len; i++) {
126 source = groupSource[i];
127 results.push(normalizePath(source));
128 }
129 return results;
130 };
131
132 wrapList = function(list) {
133 var key;
134 if (!list) {
135 return '';
136 }
137 list = (function() {
138 switch ($.type(list)) {
139 case 'array':
140 return _.clone(list);
141 case 'string':
142 return [list];
143 default:
144 throw new Error('invalid type');
145 }
146 })();
147 return ((function() {
148 var i, len, results;
149 results = [];
150 for (i = 0, len = list.length; i < len; i++) {
151 key = list[i];
152 results.push(`'${key}'`);
153 }
154 return results;
155 })()).join(', ');
156 };
157
158 // return
159 $.fn = {excludeInclude, formatArgument, getPlugin, normalizePath, normalizePathToArray, wrapList};
160
161 /*
162 fn.require(source)
163 */
164 $.fn.require = function(source) {
165 return require(normalizePath(source));
166 };
167
168 /*
169 reload(source)
170 watch(source)
171 yargs()
172 */
173 $.reload = function(source) {
174 var livereload;
175 if (!source) {
176 throw new Error('invalid source');
177 }
178 source = normalizePathToArray(source);
179 // require
180 livereload = require('gulp-livereload');
181 livereload.listen();
182 $.watch(source).pipe(livereload());
183 return $; // return
184 };
185
186 $.watch = require('gulp-watch');
187
188 $.yargs = require('yargs');
189
190 $.argv = $.yargs.argv;
191
192 fetchGitHub_ = async function(name) {
193 var source;
194 source = normalizePath(`./../${(name.split('/')[1])}`);
195 if ((await $.isExisted_(source))) {
196 return (await $.exec_([`cd ${source}`, 'git fetch', 'git pull']));
197 }
198 return (await $.exec_(`git clone https://github.com/${name}.git ${source}`));
199 };
200
201 /*
202 task(name, [fn])
203 */
204 // task
205 $.task = function(...arg) {
206 var _fn, fn, mapResult, mapTask, name, type;
207 [name, fn] = arg;
208 mapTask = gulp._registry._tasks;
209 // get task list
210 if (!name) {
211 mapResult = {};
212 for (name in mapTask) {
213 mapResult[name] = mapTask[name].unwrap();
214 }
215 return mapResult;
216 }
217 // get function via name
218 if (!fn) {
219 return mapTask[name].unwrap();
220 }
221 // set new task
222 type = $.type(fn);
223 if (type !== 'async function' && type !== 'function') {
224 $.info('warning', `invalid type of '${name}()': '${type}'`);
225 }
226 if (type !== 'async function') {
227 // generate a wrapper
228 _fn = fn;
229 fn = async function() {
230 await new Promise(function(resolve) {
231 return resolve();
232 });
233 return _fn();
234 };
235 }
236 return gulp.task(name, fn);
237 };
238
239 // added default tasks
240 /*
241 default()
242 gurumin()
243 kokoro()
244 noop()
245 prune()
246 update()
247 */
248 $.task('default', async function() {
249 var list, name;
250 list = _.keys(gulp._registry._tasks);
251 list.sort();
252 $.info('task', wrapList(list));
253 name = (await $.prompt_({
254 id: 'default-gulp',
255 type: 'autocomplete',
256 list: list,
257 message: 'task'
258 }));
259 if (indexOf.call(list, name) < 0) {
260 throw new Error(`invalid task '${name}'`);
261 }
262 await $.task(name)();
263 return $; // return
264 });
265
266 $.task('gurumin', async function() {
267 await fetchGitHub_('phonowell/gurumin');
268 await $.chain($).remove_('./gurumin').copy_('./../gurumin/source/**/*', './gurumin');
269 return $; // return
270 });
271
272 $.task('kokoro', async function() {
273 var LIST, filename, i, isSame, len, listClean, source, target;
274 await fetchGitHub_('phonowell/kokoro');
275 // clean
276 listClean = ['./coffeelint.yaml', './coffeelint.yml', './stylint.yaml', './stylintrc.yml'];
277 $.info.pause('kokoro');
278 await $.remove_(listClean);
279 $.info.resume('kokoro');
280 // copy
281 LIST = ['.gitignore', '.npmignore', '.stylintrc', 'coffeelint.json', 'license.md', 'tslint.json'];
282 for (i = 0, len = LIST.length; i < len; i++) {
283 filename = LIST[i];
284 source = `./../kokoro/${filename}`;
285 target = `./${filename}`;
286 isSame = (await $.isSame_([source, target]));
287 if (isSame === true) {
288 continue;
289 }
290 await $.copy_(source, './');
291 await $.exec_(`git add -f ${$.path.base}/${filename}`);
292 }
293 return $; // return
294 });
295
296 $.task('noop', function() {
297 return null;
298 });
299
300 // https://github.com/tj/node-prune
301 $.task('prune', async function() {
302 var base, line, listDirectory, listExtension, listFile, listSource;
303 // await $.exec_ 'npm prune'
304 base = './node_modules';
305 // file
306 listFile = ['.DS_Store', '.appveyor.yml', '.babelrc', '.coveralls.yml', '.documentup.json', '.editorconfig', '.eslintignore', '.eslintrc', '.eslintrc.js', '.eslintrc.json', '.eslintrc.yml', '.flowconfig', '.gitattributes', '.gitlab-ci.yml', '.htmllintrc', '.jshintrc', '.lint', '.npmignore', '.stylelintrc', '.stylelintrc.js', '.stylelintrc.json', '.stylelintrc.yaml', '.stylelintrc.yml', '.tern-project', '.travis.yml', '.yarn-integrity', '.yarn-metadata.json', '.yarnclean', '.yo-rc.json', 'AUTHORS', 'CHANGES', 'CONTRIBUTORS', 'Gruntfile.js', 'Gulpfile.js', 'LICENCE', 'LICENCE-MIT', 'LICENCE.BSD', 'LICENCE.txt', 'LICENSE', 'LICENSE-MIT', 'LICENSE.BSD', 'LICENSE.txt', 'Makefile', '_config.yml', 'appveyor.yml', 'changelog', 'circle.yml', 'eslint', 'gulpfile.js', 'htmllint.js', 'jest.config.js', 'karma.conf.js', 'licence', 'license', 'stylelint.config.js', 'tsconfig.json'];
307 listSource = (function() {
308 var i, len, results;
309 results = [];
310 for (i = 0, len = listFile.length; i < len; i++) {
311 line = listFile[i];
312 results.push(`${base}/**/${line}`);
313 }
314 return results;
315 })();
316 await $.remove_(listSource);
317 // directory
318 listDirectory = ['.circleci', '.github', '.idea', '.nyc_output', '.vscode', '__tests__', 'assets', 'coverage', 'doc', 'docs', 'example', 'examples', 'images', 'powered-test', 'test', 'tests', 'website'];
319 listSource = (function() {
320 var i, len, results;
321 results = [];
322 for (i = 0, len = listDirectory.length; i < len; i++) {
323 line = listDirectory[i];
324 results.push(`${base}/**/${line}`);
325 }
326 return results;
327 })();
328 await $.remove_(listSource);
329 // extension
330 listExtension = ['.coffee', '.jst', '.markdown', '.md', '.mkd', '.swp', '.tgz', '.ts'];
331 listSource = (function() {
332 var i, len, results;
333 results = [];
334 for (i = 0, len = listExtension.length; i < len; i++) {
335 line = listExtension[i];
336 results.push(`${base}/**/*${line}`);
337 }
338 return results;
339 })();
340 await $.remove_(listSource);
341 return $; // return
342 });
343
344 $.task('update', async function() {
345 var registry;
346 ({registry} = $.argv);
347 await $.update_({registry});
348 return $; // return
349 });
350
351 $.chain = function(...arg) {
352 var fn;
353 fn = require('achain');
354 return fn(...arg);
355 };
356
357 /*
358 getName(source)
359 */
360 $.getName = function(source) {
361 /*
362 basename
363 dirname
364 extname
365 filename
366 */
367 var basename, dirname, extname, filename;
368 if (!((source != null ? source.length : void 0) || source > 0)) {
369 throw new Error(`invalid source '${source}'`);
370 }
371 source = source.replace(/\\/g, '/');
372 extname = path.extname(source);
373 basename = path.basename(source, extname);
374 dirname = path.dirname(source);
375 filename = `${basename}${extname}`;
376 // return
377 return {basename, dirname, extname, filename};
378 };
379
380 /*
381 getBasename(source)
382 getDirname(source)
383 getExtname(source)
384 getFilename(source)
385 */
386 $.getBasename = function(source) {
387 var basename;
388 ({basename} = $.getName(source));
389 return basename; // return
390 };
391
392 $.getDirname = function(source) {
393 var dirname;
394 ({dirname} = $.getName(source));
395 return dirname; // return
396 };
397
398 $.getExtname = function(source) {
399 var extname;
400 ({extname} = $.getName(source));
401 return extname; // return
402 };
403
404 $.getFilename = function(source) {
405 var filename;
406 ({filename} = $.getName(source));
407 return filename; // return
408 };
409
410
411 // important, never change
412 listKey = ['backup_', 'clean_', 'compile_', 'copy_', 'delay_', 'download_', 'exec_', 'isExisted_', 'isSame_', 'link_', 'lint_', 'mkdir_', 'move_', 'prompt_', 'read_', 'recover_', 'remove_', 'rename_', 'say_', 'source_', 'ssh', 'stat_', 'update_', 'walk_', 'write_', 'zip_'];
413
414 for (i = 0, len = listKey.length; i < len; i++) {
415 key = listKey[i];
416 (function(key) {
417 // function
418 return $[key] = key[key.length - 1] !== '_' ? function(...arg) {
419 var fn;
420 fn = require(`./build/${key}.js`);
421 fn = $[key] = fn($);
422 return fn(...arg);
423 // async function
424 } : async function(...arg) {
425 var fn, fn_;
426 fn = require(`./build/${key}.js`);
427 fn_ = $[key] = fn($);
428 return (await fn_(...arg));
429 };
430 })(key);
431 }
432
433 listKey = ['backup', 'compile', 'copy', 'delay', 'download', 'exec', 'isExisted', 'isSame', 'link', 'lint', 'mkdir', 'move', 'prompt', 'read', 'recover', 'remove', 'rename', 'say', 'source', 'stat', 'update', 'walk', 'write', 'zip'];
434
435 for (j = 0, len1 = listKey.length; j < len1; j++) {
436 key = listKey[j];
437 $[`${key}Async`] = $[`${key}_`];
438 }
439
440}).call(this);