-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
94 lines (74 loc) · 2.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import gulpHelp from 'gulp-help';
import yargs from 'yargs';
import runSequence from 'run-sequence';
import loadPlugins from 'gulp-load-plugins';
import assignPolyfill from 'lodash.assign';
const DIR_NAME = yargs.argv.directory || 'gulp';
const CWD = process.cwd();
const GULP_DIR = path.resolve(`${CWD}/${DIR_NAME}`);
const GULP_CONFIG_PATH = path.resolve(`${GULP_DIR}/config/gulp.conf.js`);
const $ = loadPlugins({
config: path.resolve(`${CWD}/package.json`)
});
let gulpInstance;
let originalGulpConfig;
Object.assign = Object.assign || assignPolyfill;
try {
originalGulpConfig = require(GULP_CONFIG_PATH);
} catch (e) {
originalGulpConfig = {};
}
const config = Object.assign({}, originalGulpConfig, { args: yargs.argv });
log(chalk.yellow.bold('Original gulp config', JSON.stringify(originalGulpConfig)));
log(chalk.yellow.bold('Merged gulp config', JSON.stringify(config)));
export function load(_gulp) {
if (!_gulp) {
throw new Error('An instance of gulp must be provided!');
}
gulpInstance = gulpHelp(_gulp);
fs.readdirSync(GULP_DIR).forEach((file) => {
if ((/.js$/).test(file)) {
const filepath = `${GULP_DIR}/${file.split('.')[0]}`;
log(chalk.magenta.bold('Loading file:', filepath));
require(filepath)(this, gulpInstance, $, config);
log(chalk.green.bold('Loaded file:', filepath));
}
});
return this;
}
export function task(name, description, subtasks, args = {}) {
if (isObject(name)) {
const options = name;
name = options.name;
description = options.description;
subtasks = options.subtasks;
args = options.args;
}
log(chalk.magenta.bold('Loading task:', name));
gulpInstance.task(name, description, (done) => runSequence.use(gulpInstance).apply(runSequence, subtasks.concat([done])), { options: args });
log(chalk.green.bold('Loaded task:', name));
return this;
}
export function subtask(name, ...args) {
log(chalk.magenta.bold('Loading subtask:', name));
gulpInstance.task.apply(gulpInstance, [name, false].concat(args));
log(chalk.green.bold('Loaded task:', name));
return this;
}
function log(msg) {
if (config.args.debug) {
console.log(msg);
}
}
function isObject(value) {
const type = typeof value;
return !!value && (type === 'object' || type === 'function');
}
export default {
load,
task,
subtask
};