-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate.js
executable file
·179 lines (143 loc) · 4.76 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
#!/usr/bin/env node
import { execSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import chalk from 'chalk';
import prompts from 'prompts';
import readdir from 'recursive-readdir';
import YAML from 'yaml';
import {
getSelfVersion,
toValidPackageName,
ignoreFiles,
} from './src/lib/utils.js';
import {
WIZARD_DIRNAME,
PROJECT_FILE_PATHNAME,
} from './src/lib/filemap.js';
import {
warn,
blankLine,
info,
success,
} from './src/lib/console.js';
const version = getSelfVersion();
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? (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) {
warn(`"${targetDir}" directory exists and is not empty, aborting...`);
process.exit(1);
}
const templatesDir = path.join(WIZARD_DIRNAME, 'app-templates');
// const templatesMetas = JSON.parse(fs.readFileSync(path.join(templatesDir, 'metas.json')));
const options = {
name: path.basename(targetWorkingDir),
createVersion: version,
language: 'js',
configFormat: 'yaml',
};
const templateDir = path.join(templatesDir, options.language);
const files = await readdir(templateDir, ignoreFiles);
fs.mkdirSync(targetWorkingDir, { recursive: true });
blankLine();
info(`Scaffolding application in "${targetWorkingDir}" directory`);
for (let src of files) {
const file = path.relative(templateDir, src);
const dest = path.join(targetWorkingDir, file);
fs.mkdirSync(path.dirname(dest), { recursive: true });
switch (file) {
case 'package.json': {
const pkg = JSON.parse(fs.readFileSync(src));
pkg.name = toValidPackageName(options.name);
fs.writeFileSync(dest, JSON.stringify(pkg, null, 2));
break;
}
case 'README.md': {
let readme = fs.readFileSync(src).toString();
readme = readme.replace('# `[app-name]`', `# \`${options.name}\``);
fs.writeFileSync(dest, readme);
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;
}
// 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
// --------------------------------------------------------------------
['gitignore', 'npmignore'].forEach((filename) => {
const content = fs.readFileSync(path.join(WIZARD_DIRNAME, 'project-files', filename));
fs.writeFileSync(path.join(targetWorkingDir, `.${filename}`), content);
});
// write options in .soundworks file
fs.writeFileSync(path.join(targetWorkingDir, PROJECT_FILE_PATHNAME), JSON.stringify(options, null, 2));
info(`Installing dependencies`);
blankLine();
const execOptions = {
cwd: targetWorkingDir,
stdio: 'inherit',
};
// install itself as a dev dependency
execSync(`npm install --save-dev @soundworks/create --silent`, execOptions);
if (debug) {
execSync(`npm link @soundworks/create`, execOptions);
}
// launch init wizard
execSync(`npx soundworks --init`, execOptions);
// recap & next steps
success('Your project is ready!');
blankLine();
info('next steps:');
const relative = path.relative(process.cwd(), targetWorkingDir);
let i = 1;
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')}`);