forked from devloco/create-react-wptheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateReactWpTheme.js
428 lines (378 loc) · 13.5 KB
/
createReactWpTheme.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
* Copyright (c) 2018-present, https://github.com/devloco
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// create-react-wptheme is installed globally on people's computers. This means
// that it is extremely difficult to have them upgrade the version and
// because there's only one global version installed, it is very prone to
// breaking changes.
//
// The only job of create-react-wptheme is to init the repository and then
// forward all the commands to the local version of create-react-wptheme.
//
// If you need to add a new command, please add it to the scripts/ folder.
//
// The only reason to modify this file is to add more warnings and
// troubleshooting information for the `create-react-wptheme` command.
//
// Do not make breaking changes! We absolutely don't want to have to
// tell people to update their global version of create-react-wptheme.
//
// Also be careful with new language features.
// This file must work on Node 6+.
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"use strict";
const validateProjectName = require("validate-npm-package-name");
const chalk = require("chalk");
const commander = require("commander");
const fs = require("fs-extra");
const path = require("path");
const execSync = require("child_process").execSync;
const spawn = require("cross-spawn");
const semver = require("semver");
const dns = require("dns");
const url = require("url");
const envinfo = require("envinfo");
const packageJson = require("./package.json");
// Check this!!!!
const getScriptsPath = function() {
return scriptsFromNpm();
};
const scriptsFromNpm = function() {
return {
scriptsPath: "@devloco/react-scripts-wptheme",
callback: function() {}
};
};
const scriptsFromGit = function() {
const tempFolderName = "temp";
fs.ensureDirSync(tempFolderName);
process.chdir(tempFolderName);
const tempPath = process.cwd();
console.log(chalk.magenta("Cloning react-scripts-wptheme from GitHub..."));
execSync("git clone https://github.com/devloco/react-scripts-wptheme.git");
process.chdir("..");
let scriptsPath = "file:" + path.join(tempPath, "react-scripts-wptheme");
return {
scriptsPath: scriptsPath,
callback: function() {
deleteFolderRecursive(tempPath);
}
};
};
const scriptsFromFile = function() {
return {
scriptsPath: "file:E:\\WPDev\\github\\devloco\\react-scripts-wptheme",
callback: function() {}
};
};
let projectName;
const program = new commander.Command(packageJson.name)
.version(packageJson.version)
.arguments("<project-directory>")
.usage(`${chalk.green("<project-directory>")} [options]`)
.action((name) => {
projectName = name;
})
.option("--info", "print environment debug info (ignore 'package.json not found' errors)")
.option("--use-npm", "force downloading packages using npm instead of yarn (if both are installed)")
.option("--verbose", "force create-react-app to print additional logs (NOTE: create-react-wptheme is always verbose)")
.option("--version", "print version")
.allowUnknownOption()
.parse(process.argv);
if (typeof projectName === "undefined") {
if (program.info) {
envinfo.print({
packages: ["react-scripts-wptheme"],
noNativeIDE: true,
duplicates: true
});
process.exit(0);
}
console.error("Please specify the project directory:");
console.log(` ${chalk.cyan(program.name())} ${chalk.green("<project-directory>")}`);
console.log();
console.log("For example:");
console.log(` ${chalk.cyan(program.name())} ${chalk.green("my-react-wptheme")}`);
console.log();
process.exit(1);
}
console.log(program.name() + " version: " + chalk.magenta(packageJson.version));
createApp(projectName, program.useNpm, program.verbose);
function createApp(name, useNpm, verbose) {
if (!semver.satisfies(process.version, ">=8.0.0")) {
console.log(chalk.yellow(`You are using Node ${process.version}. Please update to Node 8 or higher for the best experience.\n`));
process.exit(1);
}
const useYarn = useNpm ? false : shouldUseYarn();
if (!useYarn) {
const npmInfo = checkNpmVersion();
if (!npmInfo.hasMinNpm) {
if (npmInfo.npmVersion) {
console.log(chalk.yellow(`You are using npm ${npmInfo.npmVersion}. Please update to npm 3 or higher for the best experience.\n`));
process.exit(1);
}
}
}
const root = path.resolve(name);
const appName = path.basename(root);
checkAppName(appName);
console.log(`Creating a new React WP theme in ${chalk.green(root)}.`);
console.log();
fs.ensureDirSync(name);
if (!isSafeToCreateProjectIn(root, name)) {
process.exit(1);
}
const originalDirectory = process.cwd();
process.chdir(root);
if (!useYarn && !checkThatNpmCanReadCwd()) {
process.exit(1);
}
run(root, appName, originalDirectory, verbose, useNpm, useYarn);
}
function run(root, appName, originalDirectory, verbose, useNpm, useYarn) {
const packageToInstall = "create-react-app";
return Promise.resolve(packageToInstall)
.then((packageName) =>
checkIfOnline(useYarn).then((isOnline) => ({
isOnline: isOnline,
packageName: packageName
}))
)
.then((info) => {
if (!info.isOnline) {
abortCommand(chalk.yellow("You appear to be offline."));
}
let packageName = info.packageName;
return createReactApp("react-src", useNpm, verbose).then(() => packageName);
})
.catch((reason) => {
console.log();
console.log("Aborting installation.");
if (reason.command) {
console.log(` ${chalk.cyan(reason.command)} has failed.`);
} else {
console.log(chalk.red("Unexpected error."), reason);
console.log("Please report it as a bug here:");
console.log("https://github.com/devloco/create-react-wptheme/issues");
}
console.log();
console.log("Done.");
process.exit(1);
});
}
function abortCommand(additonalMsg) {
return new Promise((resolve, reject) => {
console.log(chalk.red("Aborting installation."));
if (typeof additonalMsg === "string") {
console.log(additonalMsg);
}
process.exit(0);
});
}
function createReactApp(appName, useNpm, verbose) {
return new Promise((resolve, reject) => {
let command = "npx";
let args = [];
args.push("create-react-app");
args.push(appName);
if (verbose) {
args.push("--verbose");
}
if (useNpm) {
args.push("--use-npm");
}
let scriptsPath = getScriptsPath();
args.push("--scripts-version");
args.push(scriptsPath.scriptsPath);
const child = spawn(command, args, { stdio: "inherit" })
.on("error", function(err) {
console.log(`createReactWpTheme.js ERROR for command: ${command} ${args.join(" ")}`);
throw err;
})
.on("close", (code) => {
if (code !== 0) {
reject({
command: `${command} ${args.join(" ")}`
});
return;
}
scriptsPath.callback();
resolve();
});
});
}
function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file) {
let curPath = path + "/" + file;
if (fs.statSync(curPath).isDirectory()) {
// recurse
deleteFolderRecursive(curPath);
} else {
// delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
function checkNpmVersion() {
let hasMinNpm = false;
let npmVersion = null;
try {
npmVersion = execSync("npm --version")
.toString()
.trim();
hasMinNpm = semver.gte(npmVersion, "3.0.0");
} catch (err) {
// ignore
}
return {
hasMinNpm: hasMinNpm,
npmVersion: npmVersion
};
}
function checkAppName(appName) {
const validationResult = validateProjectName(appName);
if (!validationResult.validForNewPackages) {
console.error(`Could not create a project called ${chalk.red(`"${appName}"`)} because of npm naming restrictions:`);
printValidationResults(validationResult.errors);
printValidationResults(validationResult.warnings);
process.exit(1);
}
}
function printValidationResults(results) {
if (typeof results !== "undefined") {
results.forEach((error) => {
console.error(chalk.red(` * ${error}`));
});
}
}
// If project only contains files generated by GH, it’s safe.
// We also special case IJ-based products .idea because it integrates with CRA:
// https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-243446094
function isSafeToCreateProjectIn(root, name) {
const validFiles = [
".DS_Store",
"Thumbs.db",
".git",
".gitignore",
".idea",
"README.md",
"LICENSE",
"web.iml",
".hg",
".hgignore",
".hgcheck",
".npmignore",
"mkdocs.yml",
"docs",
".travis.yml",
".gitlab-ci.yml",
".gitattributes"
];
console.log();
const conflicts = fs.readdirSync(root).filter((file) => !validFiles.includes(file));
if (conflicts.length < 1) {
return true;
}
console.log(`The directory ${chalk.green(name)} contains files that could conflict:`);
console.log();
for (const file of conflicts) {
console.log(` ${file}`);
}
console.log();
console.log("Either try using a new directory name, or remove the files listed above.");
return false;
}
function checkThatNpmCanReadCwd() {
const cwd = process.cwd();
let childOutput = null;
try {
// Note: intentionally using spawn over exec since
// the problem doesn't reproduce otherwise.
// `npm config list` is the only reliable way I could find
// to reproduce the wrong path. Just printing process.cwd()
// in a Node process was not enough.
childOutput = spawn.sync("npm", ["config", "list"]).output.join("");
} catch (err) {
// Something went wrong spawning node.
// Not great, but it means we can't do this check.
// We might fail later on, but let's continue.
return true;
}
if (typeof childOutput !== "string") {
return true;
}
const lines = childOutput.split("\n");
// `npm config list` output includes the following line:
// "; cwd = C:\path\to\current\dir" (unquoted)
// I couldn't find an easier way to get it.
const prefix = "; cwd = ";
const line = lines.find((line) => line.indexOf(prefix) === 0);
if (typeof line !== "string") {
// Fail gracefully. They could remove it.
return true;
}
const npmCWD = line.substring(prefix.length);
if (npmCWD === cwd) {
return true;
}
console.error(
chalk.red(
`Could not start an npm process in the right directory.\n\n` +
`The current directory is: ${chalk.bold(cwd)}\n` +
`However, a newly started npm process runs in: ${chalk.bold(npmCWD)}\n\n` +
`This is probably caused by a misconfigured system terminal shell.`
)
);
if (process.platform === "win32") {
console.error(
chalk.red(`On Windows, this can usually be fixed by running:\n\n`) +
` ${chalk.cyan("reg")} delete "HKCU\\Software\\Microsoft\\Command Processor" /v AutoRun /f\n` +
` ${chalk.cyan("reg")} delete "HKLM\\Software\\Microsoft\\Command Processor" /v AutoRun /f\n\n` +
chalk.red(`Try to run the above two lines in the terminal.\n`) +
chalk.red(`To learn more about this problem, read: https://blogs.msdn.microsoft.com/oldnewthing/20071121-00/?p=24433/`)
);
}
return false;
}
function shouldUseYarn() {
try {
execSync("yarnpkg --version", { stdio: "ignore" });
return true;
} catch (e) {
return false;
}
}
function checkIfOnline(useYarn) {
if (!useYarn) {
// Don't ping the Yarn registry.
// We'll just assume the best case.
return Promise.resolve(true);
}
return new Promise((resolve) => {
dns.lookup("registry.yarnpkg.com", (err) => {
let proxy;
if (err != null && (proxy = getProxy())) {
// If a proxy is defined, we likely can't resolve external hostnames.
// Try to resolve the proxy name as an indication of a connection.
dns.lookup(url.parse(proxy).hostname, (proxyErr) => {
resolve(proxyErr == null);
});
} else {
resolve(err == null);
}
});
});
}