-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
148 lines (127 loc) · 3.75 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env zx
/* global $, fs, path, argv, echo, spinner */
const name = argv._[0]
if (!name) {
echo`Error: please specify the app name`
process.exit(1)
}
const p = (...args) => path.join(name, ...args)
async function patchFiles(files, ...replacers) {
for (const file of [files].flat()) {
let contents = await fs.readFile(file, 'utf8')
for (const [pattern, replacement] of replacers) {
contents = contents.replace(pattern, replacement)
}
await fs.writeFile(file, contents)
}
}
async function patchPackage(...dependencies) {
const version = async (dep) => (await $`npm show ${dep} version`).stdout.trim()
const file = p('package.json')
let pkg = await fs.readJson(file)
for (const dep of dependencies) {
pkg.devDependencies[dep.slice(1)] =
dep.charAt(0) === '+' ? `^${await version(dep.slice(1))}` : undefined
}
await fs.writeJson(file, pkg, { spaces: 2 })
}
let done
const loading = spinner('patching...', () => new Promise((resolve) => (done = resolve)))
const favicon = await fetch('https://cdn.jsdelivr.net/gh/zerodevx/sveltekit-starter/favicon.png')
.then((r) => r.blob())
.then((b) => b.arrayBuffer())
await fs.writeFile(p('static', 'favicon.png'), Buffer.from(favicon))
await patchPackage(
'+tailwindcss',
'+autoprefixer',
'+@tailwindcss/typography',
'+@fontsource-variable/inter',
'+@iconify/tailwind',
'+@iconify-json/mdi',
'+prettier-plugin-tailwindcss',
'-@sveltejs/adapter-auto',
'+@sveltejs/adapter-static'
)
await fs.writeFile(
p('tailwind.config.js'),
`import { addIconSelectors } from '@iconify/tailwind'
import typography from '@tailwindcss/typography'
import dt from 'tailwindcss/defaultTheme'
/** @type {import('tailwindcss').Config} */
export default {
content: ['./src/**/*.{html,js,svelte,ts}'],
theme: {
extend: {
fontFamily: {
sans: ['Inter Variable', ...dt.fontFamily.sans]
}
}
},
plugins: [addIconSelectors(['mdi']), typography]
}`
)
await fs.writeFile(
p('postcss.config.js'),
`/** @type {import('postcss-load-config').Config} */
export default {
plugins: {
tailwindcss: {},
autoprefixer: {}
}
}`
)
await fs.writeFile(
p('src', 'app.css'),
`/* Write your global styles here, in PostCSS syntax */
@tailwind base;
@tailwind components;
@tailwind utilities;`
)
await fs.writeFile(
p('src', 'routes', '+layout.svelte'),
`<script>
import '@fontsource-variable/inter'
import '../app.css'
</script>
<slot />`
)
await fs.writeFile(p('src', 'routes', '+layout.js'), `export const prerender = true\n`)
await patchFiles(p('src', 'routes', '+page.svelte'), [
`</h1>`,
`</h1>\n<span class="iconify mdi--heart text-xl text-red-600 animate-pulse" />\n`
])
await patchFiles(
p('svelte.config.js'),
[`import`, `import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';\nimport`],
[
`adapter-auto';`,
`adapter-static';\nimport { readFileSync } from 'node:fs';\n\nconst { version: name } = JSON.parse(readFileSync(new URL('package.json', import.meta.url), 'utf8'));`
],
[`adapter()`, `adapter({fallback:'404.html'}), version:{name}`],
[`\n};`, `, preprocess: [vitePreprocess()]\n};`]
)
await patchFiles(p('eslint.config.js'), [
`languageOptions`,
`rules:{'no-tabs':'error','no-unexpected-multiline':'error'}, languageOptions`
])
const prettier = await fs.readJson(p('.prettierrc'))
prettier.plugins = [...prettier.plugins, 'prettier-plugin-tailwindcss']
await fs.writeJson(p('.prettierrc'), {
...prettier,
printWidth: 100,
useTabs: false,
semi: false,
singleQuote: true,
trailingComma: 'none',
proseWrap: 'always',
svelteSortOrder: 'options-scripts-markup-styles',
svelteIndentScriptAndStyle: false
})
done()
await loading
echo`
All done! Complete the setup with:
$ cd ${name}
$ npm update --save
$ npm run format
`