forked from fluffynuts/PeanutButter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
190 lines (171 loc) · 5.32 KB
/
gulpfile.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
Welcome new user!
To get started, copy this gulpfile.js to the root of your repo and run:
`node gulpfile.js`
You should be guided through the basic setup. More information in README.md. In
particular, I highly recommend reading about how to use `local-tasks` to extend
and / or override the default task-set.
*/
var
fs = require("fs"),
path = require("path"),
gulpTasksFolder = "gulp-tasks", // if you cloned elsewhere, you"ll need to modify this
requireModule = global.requireModule = function(mod) {
var modulePath = [".", gulpTasksFolder, "modules", mod].join("/");
if (fs.existsSync(modulePath + ".js")) {
return require(modulePath);
} else {
return require(mod);
}
};
if (!fs.existsSync(gulpTasksFolder)) {
console.error("Either clone `gulp-tasks` to the `gulp-tasks` folder or modify this script to avoid sadness");
process.exit(2);
}
let autoWorking = null;
function pauseWhilstWorking() {
const
args = process.argv,
lastTwo = args.slice(args.length - 2),
runningGulp = isGulpJs(lastTwo[0]),
task = lastTwo[1];
if (!runningGulp || !task) {
return;
}
autoWorking = true;
try {
const localGulp = require("gulp");
localGulp.task(task, function() {
console.log(`--- taking over your ${task} task whilst we do some bootstrapping ---`);
return new Promise(function watchWorker(resolve, reject) {
if (!autoWorking) {
resolve();
}
setTimeout(function() {
watchWorker(resolve, reject);
}, 500);
});
});
} catch (e) {
/* suppress: may not have deps installed yet */
}
}
function isGulpJs(filePath) {
return path.basename(filePath) === "gulp.js";
}
if (!fs.existsSync("package.json")) {
pauseWhilstWorking();
console.log(
"You need to set up a package.json first. I'll run `npm init` for you (:"
);
initializeNpm().then(() => autoWorking = false);
} else if (mustInstallDeps()) {
pauseWhilstWorking();
console.log(
"Now we just need to install the dependencies required for gulp-tasks to run (:"
);
installGulpTaskDependencies().then(() => {
console.log("You're good to go with `gulp-tasks`. Try running `npm run gulp build`");
autoWorking = false;
});
} else {
bootstrapGulp();
}
function requiredDeps() {
var starter = require([".", gulpTasksFolder, "start", "package.json"].join("/"));
return Object.keys(starter.devDependencies);
}
function mustInstallDeps() {
var
package = require("./package.json"),
devDeps = package.devDependencies || {},
haveDeps = Object.keys(devDeps),
needDeps = requiredDeps();
return needDeps.reduce((acc, cur) => {
return acc || haveDeps.indexOf(cur) == -1;
}, false);
}
function initializeNpm() {
var spawn = requireModule("spawn");
runNpmWith(["init"])
.then(() => spawn("cmd", [ "/c", "node", process.argv[1]]));
}
function addMissingScript(package, name, script) {
package.scripts[name] = package.scripts[name] || script;
}
function installGulpTaskDependencies() {
var
findFirstMissing = function() {
var args = Array.from(arguments);
return args.reduce((acc, cur) => acc || (fs.existsSync(cur) ? acc : cur), undefined);
},
deps = requiredDeps(),
package = require("./package.json"),
buildTools = findFirstMissing("tools", "build-tools", ".tools", ".build-tools"),
prepend = `cross-env BUILD_TOOLS_FOLDER=${buildTools}`;
addMissingScript(package, "gulp", `${prepend} gulp`);
addMissingScript(package, "test", "run-s \"gulp test-dotnet\"");
fs.writeFileSync("package.json", JSON.stringify(package, null, 4), { encoding: "utf8" });
return runNpmWith(["install", "--save-dev"].concat(deps));
}
function testBin(cmds, pkg) {
if (!Array.isArray(cmds)) {
cmds = [ cmds ];
}
cmds.forEach(cmd => {
const
expected = path.join("node_modules", ".bin", "cmd"),
modPath = path.join("node_modules", pkg || cmds[0]);
if (!fs.existsSync(expected)) {
if (fs.existsSync(modPath)) {
try {
} catch (e) {
fs.renameSync(modPath, `${modPath}.b0rked.${new Date().getTime()}`);
}
}
}
});
}
function bootstrapGulp() {
var importNpmTasks = requireModule("import-npm-tasks");
try {
importNpmTasks();
var requireDir = require("require-dir");
requireDir("gulp-tasks");
["override-tasks", "local-tasks"].forEach(function(dirname) {
if (fs.existsSync(dirname)) {
requireDir(dirname);
}
});
} catch (e) {
if (shouldDump(e)) {
console.error(e);
} else {
if (!process.env.DEBUG) {
console.log(
"Error occurred. For more info, set the DEBUG environment variable (eg set DEBUG=*)."
);
}
}
process.exit(1);
}
function shouldDump(e) {
return process.env.ALWAYS_DUMP_GULP_ERRORS || probablyNotReportedByGulp(e);
}
function probablyNotReportedByGulp(e) {
var message = (e || "").toString().toLowerCase();
return ["cannot find module", "referenceerror", "syntaxerror"].reduce(
(acc, cur) => {
return acc || message.indexOf(cur) > -1;
},
false
);
}
}
function runNpmWith(args) {
var spawn = requireModule("spawn");
testBin(["run-p", "run-s"], "npm-run-all");
testBin("cross-env");
testBin("gulp");
return spawn("cmd", ["/c", "npm"].concat(args));
}