|
1 | 1 | #!/usr/bin/env node
|
2 | 2 | import fs from 'fs';
|
3 | 3 | import path from 'path';
|
4 |
| -import { fileURLToPath } from 'url'; |
5 | 4 | import { bold, cyan, gray, green, red } from 'kleur/colors';
|
6 | 5 | import prompts from 'prompts';
|
7 |
| -import { mkdirp, copy } from './utils.js'; |
| 6 | +import { create } from './index.js'; |
| 7 | +import { dist } from './utils.js'; |
8 | 8 |
|
9 | 9 | // prettier-ignore
|
10 | 10 | const disclaimer = `
|
@@ -36,8 +36,6 @@ async function main() {
|
36 | 36 | process.exit(1);
|
37 | 37 | }
|
38 | 38 | }
|
39 |
| - } else { |
40 |
| - mkdirp(cwd); |
41 | 39 | }
|
42 | 40 |
|
43 | 41 | const options = /** @type {import('./types/internal').Options} */ (
|
@@ -83,10 +81,7 @@ async function main() {
|
83 | 81 | ])
|
84 | 82 | );
|
85 | 83 |
|
86 |
| - const name = path.basename(path.resolve(cwd)); |
87 |
| - |
88 |
| - write_template_files(options.template, options.typescript, name, cwd); |
89 |
| - write_common_files(cwd, options, name); |
| 84 | + await create(cwd, options); |
90 | 85 |
|
91 | 86 | console.log(bold(green('\nYour project is ready!')));
|
92 | 87 |
|
@@ -126,131 +121,4 @@ async function main() {
|
126 | 121 | console.log(`\nStuck? Visit us at ${cyan('https://svelte.dev/chat')}\n`);
|
127 | 122 | }
|
128 | 123 |
|
129 |
| -/** |
130 |
| - * @param {string} template |
131 |
| - * @param {boolean} typescript |
132 |
| - * @param {string} name |
133 |
| - * @param {string} cwd |
134 |
| - */ |
135 |
| -function write_template_files(template, typescript, name, cwd) { |
136 |
| - const dir = dist(`templates/${template}`); |
137 |
| - copy(`${dir}/assets`, cwd, (name) => name.replace('DOT-', '.')); |
138 |
| - copy(`${dir}/package.json`, `${cwd}/package.json`); |
139 |
| - |
140 |
| - const manifest = `${dir}/files.${typescript ? 'ts' : 'js'}.json`; |
141 |
| - const files = /** @type {import('./types/internal').File[]} */ ( |
142 |
| - JSON.parse(fs.readFileSync(manifest, 'utf-8')) |
143 |
| - ); |
144 |
| - |
145 |
| - files.forEach((file) => { |
146 |
| - const dest = path.join(cwd, file.name); |
147 |
| - mkdirp(path.dirname(dest)); |
148 |
| - |
149 |
| - fs.writeFileSync(dest, file.contents.replace(/~TODO~/g, name)); |
150 |
| - }); |
151 |
| -} |
152 |
| - |
153 |
| -/** |
154 |
| - * |
155 |
| - * @param {string} cwd |
156 |
| - * @param {import('./types/internal').Options} options |
157 |
| - * @param {string} name |
158 |
| - */ |
159 |
| -function write_common_files(cwd, options, name) { |
160 |
| - const shared = dist('shared.json'); |
161 |
| - const { files } = /** @type {import('./types/internal').Common} */ ( |
162 |
| - JSON.parse(fs.readFileSync(shared, 'utf-8')) |
163 |
| - ); |
164 |
| - |
165 |
| - const pkg_file = path.join(cwd, 'package.json'); |
166 |
| - const pkg = /** @type {any} */ (JSON.parse(fs.readFileSync(pkg_file, 'utf-8'))); |
167 |
| - |
168 |
| - files.forEach((file) => { |
169 |
| - const include = file.include.every((condition) => matchesCondition(condition, options)); |
170 |
| - const exclude = file.exclude.some((condition) => matchesCondition(condition, options)); |
171 |
| - |
172 |
| - if (exclude || !include) return; |
173 |
| - |
174 |
| - if (file.name === 'package.json') { |
175 |
| - const new_pkg = JSON.parse(file.contents); |
176 |
| - merge(pkg, new_pkg); |
177 |
| - } else { |
178 |
| - const dest = path.join(cwd, file.name); |
179 |
| - mkdirp(path.dirname(dest)); |
180 |
| - fs.writeFileSync(dest, file.contents); |
181 |
| - } |
182 |
| - }); |
183 |
| - |
184 |
| - pkg.dependencies = sort_keys(pkg.dependencies); |
185 |
| - pkg.devDependencies = sort_keys(pkg.devDependencies); |
186 |
| - pkg.name = toValidPackageName(name); |
187 |
| - |
188 |
| - fs.writeFileSync(pkg_file, JSON.stringify(pkg, null, ' ')); |
189 |
| -} |
190 |
| - |
191 |
| -/** |
192 |
| - * @param {import('./types/internal').Condition} condition |
193 |
| - * @param {import('./types/internal').Options} options |
194 |
| - * @returns {boolean} |
195 |
| - */ |
196 |
| -function matchesCondition(condition, options) { |
197 |
| - return condition === 'default' || condition === 'skeleton' |
198 |
| - ? options.template === condition |
199 |
| - : options[condition]; |
200 |
| -} |
201 |
| - |
202 |
| -/** |
203 |
| - * @param {any} target |
204 |
| - * @param {any} source |
205 |
| - */ |
206 |
| -function merge(target, source) { |
207 |
| - for (const key in source) { |
208 |
| - if (key in target) { |
209 |
| - const target_value = target[key]; |
210 |
| - const source_value = source[key]; |
211 |
| - |
212 |
| - if ( |
213 |
| - typeof source_value !== typeof target_value || |
214 |
| - Array.isArray(source_value) !== Array.isArray(target_value) |
215 |
| - ) { |
216 |
| - throw new Error('Mismatched values'); |
217 |
| - } |
218 |
| - |
219 |
| - merge(target_value, source_value); |
220 |
| - } else { |
221 |
| - target[key] = source[key]; |
222 |
| - } |
223 |
| - } |
224 |
| -} |
225 |
| - |
226 |
| -/** @param {Record<string, any>} obj */ |
227 |
| -function sort_keys(obj) { |
228 |
| - if (!obj) return; |
229 |
| - |
230 |
| - /** @type {Record<string, any>} */ |
231 |
| - const sorted = {}; |
232 |
| - Object.keys(obj) |
233 |
| - .sort() |
234 |
| - .forEach((key) => { |
235 |
| - sorted[key] = obj[key]; |
236 |
| - }); |
237 |
| - |
238 |
| - return sorted; |
239 |
| -} |
240 |
| - |
241 |
| -/** @param {string} path */ |
242 |
| -function dist(path) { |
243 |
| - return fileURLToPath(new URL(`./dist/${path}`, import.meta.url).href); |
244 |
| -} |
245 |
| - |
246 |
| -/** @param {string} name */ |
247 |
| -function toValidPackageName(name) { |
248 |
| - return name |
249 |
| - .trim() |
250 |
| - .toLowerCase() |
251 |
| - .replace(/\s+/g, '-') |
252 |
| - .replace(/^[._]/, '') |
253 |
| - .replace(/[^a-z0-9~.-]+/g, '-'); |
254 |
| -} |
255 |
| - |
256 | 124 | main();
|
0 commit comments