generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
153 lines (137 loc) · 5.01 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
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
#!/usr/bin/env node
import path from 'path';
import fs from 'fs-extra';
import minimist from 'minimist';
import prompts from 'prompts';
import { fileURLToPath } from 'url';
import { createRequire } from 'node:module';
import { cloneExample } from './actions/cloneExample.js';
const argv = minimist(process.argv.slice(2));
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url);
if (argv.t && argv.template) {
console.error(`Error: illegal options: specify either -t or --template, not both.`)
process.exit(1)
}
if (argv.s && argv.sync) {
console.error(`Error: illegal options: specify either -s or --sync, not both.`)
process.exit(1)
}
if (argv.e && argv.endpoints) {
console.error(`Error: illegal options: specify either -e or --endpoint, not both.`)
process.exit(1)
}
if (argv.ex && argv.example) {
console.error(`Error: illegal options: specify either -ex or --example, not both.`)
process.exit(1)
}
let template = argv.t || argv.template;
let sync = argv.s || argv.sync;
let endpoints = argv.e?.join(', ') || argv.endpoints;
let example = argv.ex || argv.example;
if (example && (template || sync || endpoints)) {
console.error(`Example app ${example} was selected, but isn't configurable. Other flags will be ignored.`)
}
async function init() {
const context = {
targetDir: argv._[0] || '.',
cwd: process.cwd(),
root: path.join(process.cwd(), argv._[0] || '.'),
}
if (example) {
await cloneExample(example, context);
}
else {
if (!template || !sync || !endpoints) {
await (async () => {
let options = [];
if (!template) {
options.push({
type: 'select',
name: 'template',
message: 'Choose a template:',
choices: [
{ title: 'Vanilla Vite TS', value: 'vanilla-vite-ts' }
],
})
}
if (!sync) {
options.push({
type: 'text',
name: 'sync',
message: 'Set your DWN sync interval, eg. 2s; 2m; 2h; 2d; (Default set if not provided: 2m)',
initial: '',
validate: sync => /^[0-9][a-z]/.test(sync) || sync === '' ? true : 'Please enter response as a valid time format eg. 2s; 2m; 2h; 2d'
})
}
if (!endpoints) {
options.push({
type: 'text',
name: 'endpoints',
message: 'Set DWN endpoints (Default set if not provided)',
initial: '',
validate: endpoints => /^https?:\/\//.test(endpoints) || endpoints === '' ? true : 'Please enter a valid URL'
})
}
const response = await prompts(options, { onCancel: () => { console.error(`Project terminated.`); process.exit(1) }});
template = template || response.template;
sync = sync || response.sync;
endpoints = endpoints || response.endpoints;
})();
}
const renameFiles = {
_gitignore: '.gitignore',
}
console.log(`Scaffolding project in ${context.root}...`)
await fs.ensureDir(context.root)
const existing = await fs.readdir(context.root)
if (existing.length) {
console.error(`Error: target directory is not empty.`)
process.exit(1)
}
const templateDir = path.join(
__dirname,
`template-${template || 'vanilla-vite-ts'}` // -t or --template=template-vanilla-vite-ts
)
const write = async (file, content) => {
const targetPath = path.join(context.root, renameFiles[file] ?? file)
if (content) {
await fs.writeFile(targetPath, content)
} else {
await fs.copy(path.join(templateDir, file), targetPath)
}
}
const files = await fs.readdir(templateDir)
for (const file of files) {
if (file === 'package.json') {
const pkg = require(path.join(templateDir, `package.json`))
pkg.name = path.basename(context.root)
await write(file, JSON.stringify(pkg, null, 2))
} else if (file === 'web5-config.ts' && (sync || endpoints)) {
fs.readFile(path.join(templateDir, `web5-config.ts`), 'utf8', async function (err, data) {
if (err) {
return console.log(err);
}
const dwnSync = (sync) ? `sync: '${sync}'` : undefined // -s or --sync=2m
const dwnEndpoints = (endpoints) ? `dwnEndpoints: ['${endpoints}']` : undefined // -e for each endpoint or --endpoints=https://test.com,https://test2.com
const concat = dwnSync && dwnEndpoints ? dwnSync + ', ' + dwnEndpoints : undefined
const result = data.replace(/Web5.connect\(\)/g, `Web5.connect({ ${concat ?? dwnSync ?? dwnEndpoints } })`);
await write(file, result)
});
} else {
await write(file, undefined)
}
}
}
console.log(`\nDone. Now run:\n`)
if (context.root !== context.cwd) {
console.log(` cd ${path.relative(context.cwd, context.root)}`)
}
console.log(` npm install`)
console.log(` npm run dev`)
console.log()
}
init().catch((e) => {
console.error(e)
})