-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
95 lines (76 loc) · 2.62 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
95
//--------------------------------------------------------
//-- Grow project
//--------------------------------------------------------
"use strict";
const chalk = require("chalk");
const figures = require("figures");
const inquirer = require("inquirer");
const replaceInFile = require("replace-in-file");
const fss = require("@absolunet/fss");
const LOCAL = fss.realpath(".");
const NWAYO = `${LOCAL}/nwayo`;
const CONFIG_ORIGINAL = `${NWAYO}/nwayo.yaml`;
const BUNDLE = `${NWAYO}/bundles/site/site.yaml`;
const CONFIG = `${LOCAL}/nwayo.yaml`;
const ROOT = fss.realpath(__dirname);
const BOILERPLATE = `${ROOT}/boilerplate`;
const PACKAGE = fss.readJson(`${ROOT}/package.json`);
const echo = console.log; // eslint-disable-line no-console
const error = (message) => {
console.error(chalk.red(`\n ${figures.cross} ${message}`)); // eslint-disable-line no-console
process.exit(); // eslint-disable-line node/no-process-exit, unicorn/no-process-exit
};
class GrowProject {
async cli() {
if (fss.exists(NWAYO)) {
error(`There is already a 'nwayo' folder`);
}
if (fss.exists(CONFIG)) {
error(`There is already a 'nwayo.yaml' file`);
}
const { projectId } = await inquirer.prompt([
{
name: "projectId",
message: `What is your project's id ?`,
type: "input",
validate: (id) => {
if (id.length >= 3) {
if (/^(?<kebab1>[a-z][a-z0-9]*)(?<kebab2>-[a-z0-9]+)*$/u.test(id)) {
return true;
}
return "The project id must be kebab-case";
}
return "The project id must be at least 3 characters long";
},
},
]);
echo(chalk.blue(`\n${figures.play} Generating project ${figures.ellipsis}`));
// Duplicate boilerplate
fss.copy(BOILERPLATE, NWAYO);
fss.rename(`${NWAYO}/-gitignore`, `${NWAYO}/.gitignore`);
// Configuration
const config = fss.readYaml(CONFIG_ORIGINAL);
fss.writeYaml(CONFIG, config);
fss.remove(CONFIG_ORIGINAL);
// Bundle
const bundle = fss.readYaml(BUNDLE);
bundle.output.konstan = "../pub/build";
bundle.output.build = "../pub/build";
fss.writeYaml(BUNDLE, bundle);
// Change project name
await replaceInFile({
files: [`${NWAYO}/package.json`, `${NWAYO}/vendor/package.json`],
from: "PROJECT_NAME",
to: projectId,
});
// Confirmation
echo(`
${chalk.green(`${figures.tick} ${chalk.bold(projectId)} via nwayo ${PACKAGE.version} created!`)}
1. If not already present, install nwayo CLI via ${chalk.yellow("npm i -g @absolunet/nwayo-cli")}
2. Run ${chalk.yellow("npm install")} in the nwayo root folder
3. Run ${chalk.yellow("nwayo rebuild")} in the nwayo root folder
4. Enjoy !
`);
}
}
module.exports = new GrowProject();