Skip to content

Commit 8e28b1d

Browse files
committed
Refactor usage to two-steps
1 parent ce591eb commit 8e28b1d

File tree

2 files changed

+50
-68
lines changed

2 files changed

+50
-68
lines changed

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,36 @@ So let's get started.
1616

1717
## Usage
1818

19-
This combo-patcher uses [zx](https://github.com/google/zx), so make sure that's installed - then
20-
run:
19+
### Create Svelte
20+
21+
Create the Svelte template using `create-svelte` with these recommended options:
22+
23+
- [x] Skeleton project
24+
- [x] JavaScript with JSDoc comments
25+
- [x] ESLint, Prettier and Playwight/Vitest
2126

2227
```
23-
$ zx -i https://raw.githubusercontent.com/zerodevx/sveltekit-starter/main/index.mjs <app-name> [flags]
28+
$ npm create svelte@latest my-app
2429
```
2530

26-
This generates the starter based on the last known SvelteKit decisions, then saves the output to
27-
`/app-name`.
31+
### Run combo-patcher
32+
33+
Run the combo-patcher. This uses [zx](https://github.com/google/zx), so make sure that's installed.
2834

29-
### Flags
35+
```
36+
$ zx https://raw.githubusercontent.com/zerodevx/sveltekit-starter/main/index.mjs my-app
37+
```
3038

31-
| Flag | Type | Default | Description |
32-
| ---- | -------------------------------------- | ---------- | ---------------------------------- |
33-
| -o | boolean | false | Overwrite existing path if exists |
34-
| -t | ('skeleton'\|'skeletonlib'\|'default') | 'skeleton' | Base template from `create-svelte` |
39+
### Update dependencies
40+
41+
Prepare the starter based on the last known SvelteKit decisions.
42+
43+
```
44+
$ cd my-app
45+
$ npx npm-check-updates -u
46+
$ npm i
47+
$ npm run format
48+
```
3549

3650
## Opinions
3751

index.mjs

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
#!/usr/bin/env zx
22

3-
import { create } from 'create-svelte' // @latest
4-
53
$.verbose = false
64

7-
// Fix stupid `zx -i` logic (https://github.com/google/zx/blob/main/src/deps.ts)
8-
const SHIM_I = 'imp' + 'ort'
9-
const SHIM_F = 'fr' + 'om'
10-
const SHIM_R = 'req' + 'uire'
11-
125
async function patchFiles(files, ...replacers) {
136
for (const file of [files].flat()) {
147
let contents = await fs.readFile(file, 'utf8')
@@ -30,32 +23,28 @@ async function patchPackage(name, ...dependencies) {
3023
await fs.writeJson(file, pkg, { spaces: 2 })
3124
}
3225

33-
export async function addBaseTemplate({ name, template }) {
34-
await fs.remove(name)
35-
await create(name, {
36-
name,
37-
template,
38-
types: 'checkjs',
39-
prettier: true,
40-
eslint: true,
41-
playwright: true,
42-
vitest: false
43-
})
26+
const name = argv._[0]
27+
if (!name) {
28+
echo`Error: please specify the app name`
29+
process.exit(1)
30+
}
31+
32+
await spinner('replace favicon', async () => {
4433
await fetch('https://raw.githubusercontent.com/zerodevx/sveltekit-starter/main/favicon.png').then(
4534
(r) => r.body.pipe(fs.createWriteStream(path.join(name, 'static', 'favicon.png')))
4635
)
47-
}
36+
})
4837

49-
export async function addTailwindcss({ name }) {
38+
await spinner('add tailwindcss', async () => {
5039
await $`cd ${name} && npx -y svelte-add@latest tailwindcss`
5140
await patchPackage(name, '+@tailwindcss/typography')
5241
await patchFiles(path.join(name, 'tailwind.config.cjs'), [
5342
'plugins: []',
54-
`plugins: [${SHIM_R}('@tailwindcss/typography')]`
43+
`plugins: [require('@tailwindcss/typography')]`
5544
])
56-
}
45+
})
5746

58-
export async function addPrettier({ name }) {
47+
await spinner('patch prettier', async () => {
5948
const file = path.join(name, '.prettierrc')
6049
await fs.writeJson(file, {
6150
...(await fs.readJson(file)),
@@ -68,75 +57,54 @@ export async function addPrettier({ name }) {
6857
svelteSortOrder: 'options-scripts-markup-styles',
6958
svelteIndentScriptAndStyle: false
7059
})
71-
}
60+
})
7261

73-
export async function addEslint({ name }) {
62+
await spinner('patch eslint', async () => {
7463
await patchFiles(path.join(name, '.eslintrc.cjs'), [
7564
`}\n};`,
7665
`},\n\trules: { 'no-tabs': 'error', 'no-unexpected-multiline': 'error' }\n};`
7766
])
78-
}
67+
})
7968

80-
export async function addAdapterStatic({ name }) {
69+
await spinner('add adapter-static', async () => {
8170
await patchFiles(path.join(name, 'svelte.config.js'), [`adapter-auto`, `adapter-static`])
8271
await patchPackage(name, '-@sveltejs/adapter-auto', '+@sveltejs/adapter-static')
8372
await fs.outputFile(
8473
path.join(name, 'src', 'routes', '+layout.js'),
8574
`export const prerender = true\n`
8675
)
87-
}
76+
})
8877

89-
export async function addFontsource({ name }) {
78+
await spinner('add fontsource', async () => {
9079
await patchPackage(name, '+@fontsource/inter')
9180
await patchFiles(path.join(name, 'src', 'routes', '+layout.svelte'), [
9281
`<script>`,
93-
`<script>${SHIM_I} '@fontsource/inter/variable.css';`
82+
`<script>import '@fontsource/inter/variable.css';`
9483
])
9584
await patchFiles(
9685
path.join(name, 'tailwind.config.cjs'),
97-
[`const config`, `const dt = ${SHIM_R}('tailwindcss/defaultTheme');\n\nconst config`],
86+
[`const config`, `const dt = require('tailwindcss/defaultTheme');\n\nconst config`],
9887
[`extend: {}`, `extend: { fontFamily: { sans: ['InterVariable', ...dt.fontFamily.sans] } }`]
9988
)
100-
}
89+
})
10190

102-
export async function addIconify({ name }) {
91+
await spinner('add iconify', async () => {
10392
await patchPackage(name, '+@iconify/svelte', '+@iconify-icons/mdi')
10493
await fs.outputFile(
10594
path.join(name, 'src', 'lib', 'icons.js'),
106-
`${SHIM_I} Icon, { addIcon } ${SHIM_F} '@iconify/svelte/dist/OfflineIcon.svelte';\n${SHIM_I} check ${SHIM_F} '@iconify-icons/mdi/check';\n\naddIcon('check', check);\n\nexport { Icon as default }\n`
95+
`import Icon, { addIcon } from '@iconify/svelte/dist/OfflineIcon.svelte';\nimport check from '@iconify-icons/mdi/check';\n\naddIcon('check', check);\n\nexport { Icon as default }\n`
10796
)
10897
await patchFiles(
10998
path.join(name, 'src', 'routes', '+page.svelte'),
110-
[`<h1>`, `<script>${SHIM_I} Icon ${SHIM_F} '$lib/icons'</script>\n\n<h1>`],
99+
[`<h1>`, `<script>import Icon from '$lib/icons'</script>\n\n<h1>`],
111100
[`</p>`, `</p>\n\n<Icon class="w-12 h-12" icon='check' />\n`]
112101
)
113-
}
114-
115-
const opts = {
116-
name: argv._[0],
117-
template: argv.t || 'skeleton'
118-
}
119-
if (!opts.name) {
120-
echo`Error: please specify the app name`
121-
process.exit(1)
122-
}
123-
if ((await fs.pathExists(opts.name)) && !argv.o) {
124-
echo`Error: path ${path.sep}${opts.name} already exists, specify -o flag to overwrite`
125-
process.exit(1)
126-
}
127-
128-
await addBaseTemplate(opts).then(() => echo`- created ${opts.template} template`)
129-
await addTailwindcss(opts).then(() => echo`- added tailwindcss`)
130-
await addPrettier(opts).then(() => echo`- patched prettier config`)
131-
await addEslint(opts).then(() => echo`- patched eslint config`)
132-
await addAdapterStatic(opts).then(() => echo`- added adapter-static`)
133-
await addFontsource(opts).then(() => echo`- added fontsource`)
134-
await addIconify(opts).then(() => echo`- added iconify`)
102+
})
135103

136104
echo`
137105
All done! Complete the setup with:
138106
139-
$ cd ${opts.name}
107+
$ cd ${name}
140108
$ npx npm-check-updates -u
141109
$ npm i
142110
$ npm run format

0 commit comments

Comments
 (0)