This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initial-setup.cjs
executable file
·99 lines (88 loc) · 2.83 KB
/
initial-setup.cjs
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
const fs = require('fs/promises');
const child_process = require('child_process');
const ERR_SELF_INIT = 'Init called on project template itself';
async function run_command(command) {
return new Promise((resolve, reject) => {
child_process.exec(command, (err, stdout) => {
if (err) {
reject(err);
} else {
resolve(stdout ? stdout.trim() : '');
}
});
});
}
async function edit_file(filename, replacer) {
const content = await fs.readFile(filename, 'utf-8');
await fs.writeFile(filename, replacer(content), 'utf-8');
}
async function main() {
const user = await run_command('git config user.name');
const gitURL = await run_command('git remote get-url origin');
const githubProject = gitURL.slice(gitURL.indexOf('github.com') + 11, -4);
if (githubProject === 'svitejs/project-template') {
throw ERR_SELF_INIT;
}
const mainPackage = githubProject.substring(githubProject.indexOf('/') + 1);
const replaceValues = (c) => {
return c
.replace(/svitejs\/project-template/g, githubProject)
.replace(/packages\/project-template/g, `packages/${mainPackage}`)
.replace(/project-template/g, mainPackage)
.replace(/~~AUTHOR~~/g, user)
.replace(/~~YEAR~~/g, new Date().getFullYear());
};
console.log(`first install: updating template to match repo "${githubProject}"`);
await fs.unlink('README.md');
await fs.rename('README.tpl.md', 'README.md');
await fs.unlink('LICENSE');
await fs.rename('LICENSE.tpl', 'LICENSE');
await fs.rename('.github/workflows.tpl', '.github/workflows');
await Promise.all(
[
'.changeset/config.json',
'.github/ISSUE_TEMPLATE/bug_report.yml',
'.github/ISSUE_TEMPLATE/feature_request.yml',
'.github/ISSUE_TEMPLATE/config.yml',
'.github/workflows/release.yml',
'packages/playground/README.md',
'packages/project-template/package.json',
'packages/project-template/LICENSE',
'package.json',
'README.md',
'LICENSE'
].map((f) => edit_file(f, replaceValues))
);
await fs.rename('packages/project-template', `packages/${mainPackage}`);
await edit_file('package.json', (c) => {
return c.replace('project-template-monorepo', `${mainPackage}-monorepo`);
});
await edit_file('.gitignore', (c) => {
return c.replace('pnpm-lock.yaml', 'pnpm-lock.yaml\n!/pnpm-lock.yaml');
});
}
async function cleanup() {
try {
await fs.unlink('scripts/initial-setup.cjs');
await edit_file('package.json', (c) => {
const pkg = JSON.parse(c);
delete pkg.scripts.setup;
return JSON.stringify(pkg, null, 2) + '\n';
});
} catch (e) {
console.error('cleanup failed', e);
}
}
async function install_deps() {
console.log('installing dependencies');
return child_process.spawn('pnpm', ['install'], { stdio: 'inherit' });
}
main()
.then(cleanup)
.then(install_deps)
.catch((e) => {
if (e !== ERR_SELF_INIT) {
console.error('initial setup failed', e);
process.exit(1);
}
});