/
init.js
242 lines (196 loc) · 5.97 KB
/
init.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
#!/usr/bin/env node
const {
packageManager,
getRunCommand,
getPackageManagerInstallPage,
getInstallCommand,
} = require('../lib/packageManager');
const chalk = require('chalk');
const fs = require('node:fs/promises');
const { posix: path } = require('node:path');
const { isEmptyDir } = require('../lib/isEmptyDir');
const validatePackageName = require('validate-npm-package-name');
const dedent = require('dedent');
const { setCwd } = require('../lib/cwd');
const prettierWrite = require('../lib/runPrettier').write;
const esLintFix = require('../lib/runESLint').fix;
const configure = require('../lib/configure');
const install = require('../lib/install');
const banner = require('../lib/banner');
const toPosixPath = require('../lib/toPosixPath');
const trace = require('debug')('sku:init');
const { fdir: Fdir } = require('fdir');
const ejs = require('ejs');
const args = require('../config/args');
const removeLeadingUnderscoreFromFileName = (filePath) => {
const basename = path.basename(filePath);
if (basename.startsWith('_')) {
const normalizedFileName = basename.replace(/^_/, '');
const dirname = path.dirname(filePath);
return path.join(dirname, normalizedFileName);
}
return filePath;
};
const getTemplateFileDestinationFromRoot =
(projectRoot, templateDirectory) => (filePath) => {
const normalizedFilePath = removeLeadingUnderscoreFromFileName(filePath);
const filePathRelativeToTemplate =
normalizedFilePath.split(templateDirectory)[1];
return path.join(projectRoot, filePathRelativeToTemplate);
};
(async () => {
const projectName = args.argv[0];
const usage = () => {
console.log('Usage:');
console.log(
` ${chalk.cyan('sku init')} ${chalk.green(
'<project-directory>',
)} [--verbose]`,
);
console.log();
console.log('For example:');
console.log(` ${chalk.cyan('sku init')} ${chalk.green('my-seek-ui')}`);
process.exit(1);
};
if (args.argv.indexOf('--help') >= 0) {
usage();
process.exit(0);
}
if (!projectName) {
usage();
process.exit(1);
}
const root = path.resolve(projectName);
setCwd(root);
trace(`Creating project "${projectName}" in "${root}"`);
const appName = path.basename(root);
const reservedNames = [
'react',
'react-dom',
'sku',
'braid-design-system',
].sort();
const {
validForNewPackages,
errors = [],
warnings = [],
} = validatePackageName(appName);
if (!validForNewPackages) {
console.error(dedent`
Could not create a project called ${chalk.red(`"${appName}"`)} \
because of npm naming restrictions:
`);
const results = [...errors, ...warnings];
results.forEach((result) => console.error(chalk.red(` * ${result}`)));
process.exit(1);
}
if (reservedNames.indexOf(appName) >= 0) {
console.error(
chalk.red(dedent`
We cannot create a project called \
${chalk.green(appName)} \
because a dependency with the same name exists.
Due to the way npm works, the following names are not allowed:
${chalk.cyan(reservedNames.map((depName) => ` ${depName}`).join('\n'))}
Please choose a different project name.
`),
);
process.exit(1);
}
await fs.mkdir(projectName, { recursive: true });
if (!isEmptyDir(root)) {
console.log(`The directory ${chalk.green(projectName)} is not empty.`);
process.exit(1);
}
console.log(`Creating a new sku project in ${chalk.green(root)}.`);
console.log();
const packageJson = {
name: appName,
version: '0.1.0',
private: true,
scripts: {
start: 'sku start',
test: 'sku test',
build: 'sku build',
serve: 'sku serve',
lint: 'sku lint',
format: 'sku format',
},
};
const packageJsonString = JSON.stringify(packageJson, null, 2);
await fs.writeFile(path.join(root, 'package.json'), packageJsonString);
process.chdir(root);
const templateDirectory = path.join(toPosixPath(__dirname), '../template');
const templateFiles = await new Fdir()
.withBasePath()
.crawl(templateDirectory)
.withPromise();
const getTemplateFileDestination = getTemplateFileDestinationFromRoot(
root,
templateDirectory,
);
const templateData = {
appName,
gettingStartedDocs: dedent`
First of all, make sure you've installed [${packageManager}](${getPackageManagerInstallPage()}).
Then, install dependencies:
\`\`\`sh
$ ${getInstallCommand()}
\`\`\`
`,
startScript: getRunCommand('start'),
testScript: getRunCommand('test'),
lintScript: getRunCommand('lint'),
formatScript: getRunCommand('format'),
buildScript: getRunCommand('build'),
};
await Promise.all(
templateFiles.map(async (file) => {
const fileContents = await ejs.renderFile(file, templateData);
const destination = getTemplateFileDestination(file);
// Ensure folders exist before writing files to them
await fs.mkdir(path.dirname(destination), { recursive: true });
await fs.writeFile(destination, fileContents);
}),
);
const deps = ['braid-design-system', 'react', 'react-dom'];
const devDeps = [
'@vanilla-extract/css',
'sku',
'@types/react',
'@types/react-dom',
];
console.log(
`Installing packages with ${chalk.bold(
packageManager,
)}. This might take a couple of minutes.`,
);
console.log(
`Installing ${deps
.concat(devDeps)
.map((x) => chalk.cyan(x))
.join(', ')}...`,
);
console.log();
const logLevel = args.argv.indexOf('--verbose') >= 0 ? 'verbose' : 'regular';
await install({ deps, logLevel });
await install({
deps: devDeps,
type: 'dev',
logLevel,
exact: false,
});
await configure();
await esLintFix();
await prettierWrite();
const nextSteps = [
`${chalk.cyan('cd')} ${projectName}`,
`${chalk.cyan(getRunCommand('start'))}`,
]
.filter(Boolean)
.join('\n');
banner('info', `${projectName} created`, [
'Get started by running:',
nextSteps,
]);
})();