-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaffold.js
56 lines (48 loc) · 1.72 KB
/
scaffold.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
import degit from "degit";
import { spawn } from "child_process";
function handleDepInstallationExit(exitCode, directoryName) {
if (exitCode === 0) {
console.log(
"\nProject dependencies installed successfully! You can enter your new project directory now by running"
);
console.log(`\ncd ${directoryName}`);
console.log("\nThen you can start the development server using:");
console.log("\nnpm start");
} else {
console.log("\nProject dependency installtion failed");
}
}
function handleTemplateCloneSuccess(directoryname, skipDepsInstall) {
if (skipDepsInstall) {
console.log(
"\nProject template cloned successfully! You can enter your new project directory now by running"
);
console.log(`\ncd ${directoryname}`);
return;
}
console.log("Project template cloned successfully, installing deps:");
const installDeps = spawn("npm", ["install"], {
cwd: directoryname,
stdio: "inherit",
});
installDeps.on("exit", (exitCode) =>
handleDepInstallationExit(exitCode, directoryname)
);
}
function handleTemplateCloneFailure(error, directoryName) {
console.error("Error encountered when trying to clone template.");
if (error.code === "DEST_NOT_EMPTY") {
console.error(
`It looks like ${directoryName} already exists.\nEither choose a new directory name or remove the directory`
);
} else {
console.error("\nError:\n", error);
}
}
export function scaffold(directoryName, skipDepsInstall) {
const template = degit("wise-king-sullyman/create-TOP-react-app-template");
template.clone(directoryName).then(
() => handleTemplateCloneSuccess(directoryName, skipDepsInstall),
(error) => handleTemplateCloneFailure(error, directoryName)
);
}