-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.js
executable file
·220 lines (173 loc) · 5.67 KB
/
create.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
#!/usr/bin/env node
import { execSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import chalk from 'chalk';
import { mkdirp } from 'mkdirp';
import prompts from 'prompts';
import readdir from 'recursive-readdir';
import YAML from 'yaml';
import {
getSelfVersion,
toValidPackageName,
ignoreFiles,
} from './lib/utils.js';
const version = getSelfVersion();
const __dirname = path.dirname(fileURLToPath(import.meta.url));
let debug = false; // will link itself at the end of the installation
if (process.argv[2] == '--debug' || process.argv[3] == '--debug') {
console.log(chalk.yellow('> Run create in debug mode'));
debug = true;
}
console.log(`\
${chalk.gray(`[@soundworks/create#v${version}]`)}
${chalk.yellow('> welcome to soundworks')}
- documentation: ${chalk.cyan('https://soundworks.dev')}
- issues: ${chalk.cyan('https://github.com/collective-soundworks/soundworks/issues')}
`);
let targetDir;
if (process.argv[2] && process.argv[2] !== '--debug') {
targetDir = process.argv[2];
} else {
targetDir = '.';
}
if (targetDir === '.') {
const result = await prompts([
{
type: 'text',
name: 'dir',
message: 'Where should we create your project?\n (leave blank to use current directory)',
},
]);
if (result.dir) {
targetDir = result.dir;
}
}
const targetWorkingDir = path.isAbsolute(targetDir) ?
targetDir : path.normalize(path.join(process.cwd(), targetDir));
if (fs.existsSync(targetWorkingDir) && fs.readdirSync(targetWorkingDir).length > 0) {
console.log(chalk.red(`> "${targetDir}" directory exists and is not empty, aborting...`));
process.exit(1);
}
const templatesDir = path.join(__dirname, 'app-templates');
// const templatesMetas = JSON.parse(fs.readFileSync(path.join(templatesDir, 'metas.json')));
const options = {};
options.createVersion = version;
options.name = path.basename(targetWorkingDir);
options.eslint = true;
options.language = 'js';
options.configFormat = 'yaml';
const templateDir = path.join(templatesDir, options.language);
const files = await readdir(templateDir, ignoreFiles);
await mkdirp(targetWorkingDir);
console.log('');
console.log(`> creating ${options.language} template in:`, targetWorkingDir);
for (let src of files) {
const file = path.relative(templateDir, src);
const dest = path.join(targetWorkingDir, file);
await mkdirp(path.dirname(dest));
switch (file) {
case 'package.json': {
const pkg = JSON.parse(fs.readFileSync(src));
pkg.name = toValidPackageName(options.name);
if (options.eslint) {
pkg.scripts.lint = `eslint .`;
}
fs.writeFileSync(dest, JSON.stringify(pkg, null, 2));
break;
}
case 'config/application.yaml': {
const obj = YAML.parse(fs.readFileSync(src).toString());
// ovewrite
obj.name = options.name;
obj.author = '';
obj.clients = {};
fs.writeFileSync(dest, YAML.stringify(obj));
break;
}
case 'README.md': {
let readme = fs.readFileSync(src).toString();
readme = readme.replace('# `[app-name]`', `# \`${options.name}\``);
fs.writeFileSync(dest, readme);
break;
}
// just copy the file without modification
default: {
fs.copyFileSync(src, dest);
break;
}
}
}
// --------------------------------------------------------------------
// npm has a weird behavior regarding `.gitignore` files which are
// automatically renamed to `.npmignore`.
// Note 31-10-2023: the .gitignore and .npmrc files seems to be completely
// removed from the package altogether (test w/ `npm pack`).
// So we are just re-creating them from scratch later
// --------------------------------------------------------------------
// create .gitignore file
fs.writeFileSync(path.join(targetWorkingDir, '.gitignore'), `\
# build files and dependencies
/node_modules
.build
.data
# ignore all environment config files
/config/env-*
# junk files
package-lock.json
.DS_Store
Thumbs.db
# TLS certificates
/**/*.pem
`);
// create .npmrc file
fs.writeFileSync(path.join(targetWorkingDir, '.npmrc'), `\
package-lock=false
`);
if (options.eslint === true) {
const src = path.join(__dirname, 'build-tools', '.eslintrc');
fs.writeFileSync(path.join(targetWorkingDir, '.eslintrc'), `\
{
"extends": "@ircam",
}`
);
}
// write options in .soundworks file
fs.writeFileSync(path.join(targetWorkingDir, '.soundworks'), JSON.stringify(options, null, 2));
console.log(`> installing dependencies`);
console.log('');
const execOptions = {
cwd: targetWorkingDir,
stdio: 'inherit',
};
// install itself as a dev dependency
const devDeps = ['@soundworks/create'];
if (options.eslint === true) {
devDeps.push('eslint');
devDeps.push('@ircam/eslint-config');
}
// this will install other deps as well
execSync(`npm install --save-dev ${devDeps.join(' ')} --silent`, execOptions);
if (debug) {
execSync(`npm link @soundworks/create`, execOptions);
}
// launch init wizard
execSync(`npx soundworks --init`, execOptions);
// recap & next steps
console.log(chalk.yellow('> your project is ready!'));
console.log(` ✔ ${options.language === 'js' ? 'JavaScript' : 'TypeScript'}`);
if (options.eslint) {
console.log(' ✔ eslint');
}
console.log('')
console.log(chalk.yellow('> next steps:'));
let i = 1;
const relative = path.relative(process.cwd(), targetWorkingDir);
if (relative !== '') {
console.log(` ${i++}: ${chalk.cyan(`cd ${relative}`)}`);
}
console.log(` ${i++}: ${chalk.cyan('git init && git add -A && git commit -m "first commit"')} (optional)`);
console.log(` ${i++}: ${chalk.cyan('npm run dev')}`);
console.log('')
console.log(`- to close the dev server, press ${chalk.bold(chalk.cyan('Ctrl-C'))}`);