|
| 1 | +#!/usr/bin/env zx |
| 2 | + |
| 3 | +import { create } from 'create-svelte' // @latest |
| 4 | + |
| 5 | +export async function patchFiles(filepaths, ...replacers) { |
| 6 | + for (const file of [filepaths].flat()) { |
| 7 | + let contents = await fs.readFile(file, 'utf8') |
| 8 | + for (const [pattern, replacement] of replacers) { |
| 9 | + contents = contents.replace(pattern, replacement) |
| 10 | + } |
| 11 | + await fs.writeFile(file, contents) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +export async function addBaseTemplate({ name, template }) { |
| 16 | + await fs.remove(name) |
| 17 | + await create(name, { |
| 18 | + name, |
| 19 | + template, |
| 20 | + types: 'checkjs', |
| 21 | + prettier: true, |
| 22 | + eslint: true, |
| 23 | + playwright: true, |
| 24 | + vitest: false |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +export async function addTailwindcss({ name }) { |
| 29 | + await $`cd ${name} && npx svelte-add@latest tailwindcss`.quiet() |
| 30 | +} |
| 31 | + |
| 32 | +export async function addPrettier({ name }) { |
| 33 | + await fs.writeJson(path.join(name, '.prettierrc'), { |
| 34 | + ...(await fs.readJson(path.join(name, '.prettierrc'))), |
| 35 | + printWidth: 100, |
| 36 | + useTabs: false, |
| 37 | + semi: false, |
| 38 | + singleQuote: true, |
| 39 | + trailingComma: 'none', |
| 40 | + proseWrap: 'always', |
| 41 | + svelteSortOrder: 'options-scripts-markup-styles', |
| 42 | + svelteIndentScriptAndStyle: false |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +export async function addEslint({ name }) { |
| 47 | + await patchFiles(path.join(name, '.eslintrc.cjs'), [ |
| 48 | + `}\n};`, |
| 49 | + `},\n\trules: { 'no-tabs': 'error', 'no-unexpected-multiline': 'error' }\n};` |
| 50 | + ]) |
| 51 | +} |
| 52 | + |
| 53 | +export async function addAdapterStatic({ name }) { |
| 54 | + await patchFiles( |
| 55 | + [path.join(name, 'package.json'), path.join(name, 'svelte.config.js')], |
| 56 | + [`adapter-auto`, `adapter-static`] |
| 57 | + ) |
| 58 | + await fs.outputFile( |
| 59 | + path.join(name, 'src', 'routes', '+layout.js'), |
| 60 | + `export const prerender = true\n` |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +void (async function () { |
| 65 | + const opts = { |
| 66 | + name: argv._[0], |
| 67 | + template: argv.t || 'skeleton' |
| 68 | + } |
| 69 | + if (!opts.name) { |
| 70 | + echo`Error: please specify the app name` |
| 71 | + process.exit(1) |
| 72 | + } |
| 73 | + if ((await fs.pathExists(opts.name)) && !argv.o) { |
| 74 | + echo`Error: path ${path.sep}${opts.name} already exists, specify -o flag to overwrite` |
| 75 | + process.exit(1) |
| 76 | + } |
| 77 | + |
| 78 | + await addBaseTemplate(opts) |
| 79 | + echo`- created ${opts.template} template` |
| 80 | + await addTailwindcss(opts) |
| 81 | + echo`- added tailwindcss` |
| 82 | + await addPrettier(opts) |
| 83 | + echo`- patched prettier config` |
| 84 | + await addEslint(opts) |
| 85 | + echo`- patched eslint config` |
| 86 | + await addAdapterStatic(opts) |
| 87 | + echo`- added adapter-static` |
| 88 | + |
| 89 | + echo`\nAll done! Complete the setup with:\n` |
| 90 | + echo`$ cd ${opts.name} && npm i && npm run format` |
| 91 | +})() |
0 commit comments