@@ -4,8 +4,8 @@ import { create } from 'create-svelte' // @latest
4
4
5
5
$ . verbose = false
6
6
7
- export async function patchFiles ( filepaths , ...replacers ) {
8
- for ( const file of [ filepaths ] . flat ( ) ) {
7
+ async function patchFiles ( files , ...replacers ) {
8
+ for ( const file of [ files ] . flat ( ) ) {
9
9
let contents = await fs . readFile ( file , 'utf8' )
10
10
for ( const [ pattern , replacement ] of replacers ) {
11
11
contents = contents . replace ( pattern , replacement )
@@ -14,9 +14,15 @@ export async function patchFiles(filepaths, ...replacers) {
14
14
}
15
15
}
16
16
17
- export async function getVersion ( pkg ) {
18
- const version = await $ `npm show ${ pkg } version`
19
- return version . toString ( )
17
+ async function patchPackage ( name , ...dependencies ) {
18
+ const version = async ( dep ) => ( await $ `npm show ${ dep } version` ) . stdout . trim ( )
19
+ const file = path . join ( name , 'package.json' )
20
+ let pkg = await fs . readJson ( file )
21
+ for ( const dep of dependencies ) {
22
+ pkg . devDependencies [ dep . slice ( 1 ) ] =
23
+ dep . charAt ( 0 ) === '+' ? `^${ await version ( dep . slice ( 1 ) ) } ` : undefined
24
+ }
25
+ await fs . writeJson ( file , pkg , { spaces : 2 } )
20
26
}
21
27
22
28
export async function addBaseTemplate ( { name, template } ) {
@@ -37,11 +43,17 @@ export async function addBaseTemplate({ name, template }) {
37
43
38
44
export async function addTailwindcss ( { name } ) {
39
45
await $ `cd ${ name } && npx -y svelte-add@latest tailwindcss`
46
+ await patchPackage ( name , '+@tailwindcss/typography' )
47
+ await patchFiles ( path . join ( name , 'tailwind.config.cjs' ) , [
48
+ 'plugins: []' ,
49
+ `plugins: [require('@tailwindcss/typography')]`
50
+ ] )
40
51
}
41
52
42
53
export async function addPrettier ( { name } ) {
43
- await fs . writeJson ( path . join ( name , '.prettierrc' ) , {
44
- ...( await fs . readJson ( path . join ( name , '.prettierrc' ) ) ) ,
54
+ const file = path . join ( name , '.prettierrc' )
55
+ await fs . writeJson ( file , {
56
+ ...( await fs . readJson ( file ) ) ,
45
57
printWidth : 100 ,
46
58
useTabs : false ,
47
59
semi : false ,
@@ -61,16 +73,40 @@ export async function addEslint({ name }) {
61
73
}
62
74
63
75
export async function addAdapterStatic ( { name } ) {
64
- await patchFiles (
65
- [ path . join ( name , 'package.json' ) , path . join ( name , 'svelte.config.js' ) ] ,
66
- [ `adapter-auto` , `adapter-static` ]
67
- )
76
+ await patchFiles ( path . join ( name , 'svelte.config.js' ) , [ `adapter-auto` , `adapter-static` ] )
77
+ await patchPackage ( name , '-@sveltejs/adapter-auto' , '+@sveltejs/adapter-static' )
68
78
await fs . outputFile (
69
79
path . join ( name , 'src' , 'routes' , '+layout.js' ) ,
70
80
`export const prerender = true\n`
71
81
)
72
82
}
73
83
84
+ export async function addFontsource ( { name } ) {
85
+ await patchPackage ( name , '+@fontsource/inter' )
86
+ await patchFiles ( path . join ( name , 'src' , 'routes' , '+layout.svelte' ) , [
87
+ `<script>` ,
88
+ `<script>import '@fontsource/inter/variable.css';`
89
+ ] )
90
+ await patchFiles (
91
+ path . join ( name , 'tailwind.config.cjs' ) ,
92
+ [ `const config` , `const dt = require('tailwindcss/defaultTheme');\n\nconst config` ] ,
93
+ [ `extend: {}` , `extend: { fontFamily: { sans: ['InterVariable', ...dt.fontFamily.sans] } }` ]
94
+ )
95
+ }
96
+
97
+ export async function addIconify ( { name } ) {
98
+ await patchPackage ( name , '+@iconify/svelte' , '+@iconify-icons/mdi' )
99
+ await fs . outputFile (
100
+ path . join ( name , 'src' , 'lib' , 'icons.js' ) ,
101
+ `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`
102
+ )
103
+ await patchFiles (
104
+ path . join ( name , 'src' , 'routes' , '+page.svelte' ) ,
105
+ [ `<h1>` , `<script>import Icon from '$lib/icons'</script>\n\n<h1>` ] ,
106
+ [ `</p>` , `</p>\n\n<Icon class="w-12 h-12" icon='check' />\n` ]
107
+ )
108
+ }
109
+
74
110
void ( async function ( ) {
75
111
const opts = {
76
112
name : argv . _ [ 0 ] ,
@@ -85,16 +121,13 @@ void (async function () {
85
121
process . exit ( 1 )
86
122
}
87
123
88
- await addBaseTemplate ( opts )
89
- echo `- created ${ opts . template } template`
90
- await addTailwindcss ( opts )
91
- echo `- added tailwindcss`
92
- await addPrettier ( opts )
93
- echo `- patched prettier config`
94
- await addEslint ( opts )
95
- echo `- patched eslint config`
96
- await addAdapterStatic ( opts )
97
- echo `- added adapter-static`
124
+ await addBaseTemplate ( opts ) . then ( ( ) => echo `- created ${ opts . template } template` )
125
+ await addTailwindcss ( opts ) . then ( ( ) => echo `- added tailwindcss` )
126
+ await addPrettier ( opts ) . then ( ( ) => echo `- patched prettier config` )
127
+ await addEslint ( opts ) . then ( ( ) => echo `- patched eslint config` )
128
+ await addAdapterStatic ( opts ) . then ( ( ) => echo `- added adapter-static` )
129
+ await addFontsource ( opts ) . then ( ( ) => echo `- added fontsource` )
130
+ await addIconify ( opts ) . then ( ( ) => echo `- added iconify` )
98
131
99
132
echo `\nAll done! Complete the setup with:\n`
100
133
echo `$ cd ${ opts . name } `
0 commit comments