@@ -4,8 +4,8 @@ import { create } from 'create-svelte' // @latest
44
55$ . verbose = false
66
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 ( ) ) {
99 let contents = await fs . readFile ( file , 'utf8' )
1010 for ( const [ pattern , replacement ] of replacers ) {
1111 contents = contents . replace ( pattern , replacement )
@@ -14,9 +14,15 @@ export async function patchFiles(filepaths, ...replacers) {
1414 }
1515}
1616
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 } )
2026}
2127
2228export async function addBaseTemplate ( { name, template } ) {
@@ -37,11 +43,17 @@ export async function addBaseTemplate({ name, template }) {
3743
3844export async function addTailwindcss ( { name } ) {
3945 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+ ] )
4051}
4152
4253export 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 ) ) ,
4557 printWidth : 100 ,
4658 useTabs : false ,
4759 semi : false ,
@@ -61,16 +73,40 @@ export async function addEslint({ name }) {
6173}
6274
6375export 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' )
6878 await fs . outputFile (
6979 path . join ( name , 'src' , 'routes' , '+layout.js' ) ,
7080 `export const prerender = true\n`
7181 )
7282}
7383
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+
74110void ( async function ( ) {
75111 const opts = {
76112 name : argv . _ [ 0 ] ,
@@ -85,16 +121,13 @@ void (async function () {
85121 process . exit ( 1 )
86122 }
87123
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` )
98131
99132 echo `\nAll done! Complete the setup with:\n`
100133 echo `$ cd ${ opts . name } `
0 commit comments