1
1
#!/usr/bin/env zx
2
2
3
- import { create } from 'create-svelte' // @latest
4
-
5
3
$ . verbose = false
6
4
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
-
12
5
async function patchFiles ( files , ...replacers ) {
13
6
for ( const file of [ files ] . flat ( ) ) {
14
7
let contents = await fs . readFile ( file , 'utf8' )
@@ -30,32 +23,28 @@ async function patchPackage(name, ...dependencies) {
30
23
await fs . writeJson ( file , pkg , { spaces : 2 } )
31
24
}
32
25
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 ( ) => {
44
33
await fetch ( 'https://raw.githubusercontent.com/zerodevx/sveltekit-starter/main/favicon.png' ) . then (
45
34
( r ) => r . body . pipe ( fs . createWriteStream ( path . join ( name , 'static' , 'favicon.png' ) ) )
46
35
)
47
- }
36
+ } )
48
37
49
- export async function addTailwindcss ( { name } ) {
38
+ await spinner ( 'add tailwindcss' , async ( ) => {
50
39
await $ `cd ${ name } && npx -y svelte-add@latest tailwindcss`
51
40
await patchPackage ( name , '+@tailwindcss/typography' )
52
41
await patchFiles ( path . join ( name , 'tailwind.config.cjs' ) , [
53
42
'plugins: []' ,
54
- `plugins: [${ SHIM_R } ('@tailwindcss/typography')]`
43
+ `plugins: [require ('@tailwindcss/typography')]`
55
44
] )
56
- }
45
+ } )
57
46
58
- export async function addPrettier ( { name } ) {
47
+ await spinner ( 'patch prettier' , async ( ) => {
59
48
const file = path . join ( name , '.prettierrc' )
60
49
await fs . writeJson ( file , {
61
50
...( await fs . readJson ( file ) ) ,
@@ -68,75 +57,54 @@ export async function addPrettier({ name }) {
68
57
svelteSortOrder : 'options-scripts-markup-styles' ,
69
58
svelteIndentScriptAndStyle : false
70
59
} )
71
- }
60
+ } )
72
61
73
- export async function addEslint ( { name } ) {
62
+ await spinner ( 'patch eslint' , async ( ) => {
74
63
await patchFiles ( path . join ( name , '.eslintrc.cjs' ) , [
75
64
`}\n};` ,
76
65
`},\n\trules: { 'no-tabs': 'error', 'no-unexpected-multiline': 'error' }\n};`
77
66
] )
78
- }
67
+ } )
79
68
80
- export async function addAdapterStatic ( { name } ) {
69
+ await spinner ( 'add adapter-static' , async ( ) => {
81
70
await patchFiles ( path . join ( name , 'svelte.config.js' ) , [ `adapter-auto` , `adapter-static` ] )
82
71
await patchPackage ( name , '-@sveltejs/adapter-auto' , '+@sveltejs/adapter-static' )
83
72
await fs . outputFile (
84
73
path . join ( name , 'src' , 'routes' , '+layout.js' ) ,
85
74
`export const prerender = true\n`
86
75
)
87
- }
76
+ } )
88
77
89
- export async function addFontsource ( { name } ) {
78
+ await spinner ( 'add fontsource' , async ( ) => {
90
79
await patchPackage ( name , '+@fontsource/inter' )
91
80
await patchFiles ( path . join ( name , 'src' , 'routes' , '+layout.svelte' ) , [
92
81
`<script>` ,
93
- `<script>${ SHIM_I } '@fontsource/inter/variable.css';`
82
+ `<script>import '@fontsource/inter/variable.css';`
94
83
] )
95
84
await patchFiles (
96
85
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` ] ,
98
87
[ `extend: {}` , `extend: { fontFamily: { sans: ['InterVariable', ...dt.fontFamily.sans] } }` ]
99
88
)
100
- }
89
+ } )
101
90
102
- export async function addIconify ( { name } ) {
91
+ await spinner ( 'add iconify' , async ( ) => {
103
92
await patchPackage ( name , '+@iconify/svelte' , '+@iconify-icons/mdi' )
104
93
await fs . outputFile (
105
94
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`
107
96
)
108
97
await patchFiles (
109
98
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>` ] ,
111
100
[ `</p>` , `</p>\n\n<Icon class="w-12 h-12" icon='check' />\n` ]
112
101
)
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
+ } )
135
103
136
104
echo `
137
105
All done! Complete the setup with:
138
106
139
- $ cd ${ opts . name }
107
+ $ cd ${ name }
140
108
$ npx npm-check-updates -u
141
109
$ npm i
142
110
$ npm run format
0 commit comments