urara fix from Sevichecc

This commit is contained in:
matthieu42morin 2024-04-29 15:44:44 +02:00
parent b687c154b0
commit 910b62cd1b
1 changed files with 94 additions and 122 deletions

View File

@ -9,15 +9,11 @@ import chokidar from 'chokidar'
import chalk from 'chalk'
const config = {
extensions: {
posts: ['md'],
images: ['jpg', 'png', 'webp', 'avif']
},
images: [''],
extensions: ['md'],
catch: ['ENOENT', 'EEXIST']
}
const check = (ext: string) => (config.extensions.posts.includes(ext) ? 'src/routes' : 'static')
const check = (ext: string) => (config.extensions.includes(ext) ? 'src/routes' : 'static')
const log = (color: string, msg: string, dest?: string | Error) =>
console.log(
@ -41,25 +37,13 @@ const error = (err: { code: string; message: unknown }) => {
}
const cpFile = (src: string, { stat = 'copy', dest = path.join(check(path.parse(src).ext.slice(1)), src.slice(6)) } = {}) =>
config.extensions.images.includes(path.parse(src).ext.slice(1))
? fs
.copyFile(src, path.join('src/static', src.slice(6)))
.then(() => fs.copyFile(src, path.join('static', src.slice(6))))
.then(() => log('green', `${stat} file`, dest))
.catch(error)
: fs
fs
.copyFile(src, dest)
.then(() => log('green', `${stat} file`, dest))
.catch(error)
const rmFile = (src: string, { dest = path.join(check(path.parse(src).ext.slice(1)), src.slice(6)) } = {}) =>
config.extensions.images.includes(path.parse(src).ext.slice(1))
? fs
.rm(path.join('src/static', src.slice(6)))
.then(() => fs.rm(path.join('static', src.slice(6))))
.then(() => log('yellow', 'remove file', dest))
.catch(error)
: fs
fs
.rm(dest)
.then(() => log('yellow', 'remove file', dest))
.catch(error)
@ -79,12 +63,7 @@ const cpDir = (src: string) =>
})
)
const mkDir = (
src: string,
{
dest = [path.join('src/routes', src.slice(6)), path.join('static', src.slice(6)), path.join('src/static', src.slice(6))]
} = {}
) => {
const mkDir = (src: string, { dest = [path.join('src/routes', src.slice(6)), path.join('static', src.slice(6))] } = {}) => {
dest.forEach(path =>
fs
.mkdir(path)
@ -93,12 +72,7 @@ const mkDir = (
)
}
const rmDir = (
src: string,
{
dest = [path.join('src/routes', src.slice(6)), path.join('static', src.slice(6)), path.join('src/static', src.slice(6))]
} = {}
) => {
const rmDir = (src: string, { dest = [path.join('src/routes', src.slice(6)), path.join('static', src.slice(6))] } = {}) => {
dest.forEach(path =>
fs
.rm(path, { force: true, recursive: true })
@ -117,14 +91,12 @@ const cleanDir = (src: string) =>
const build = () => {
mkDir('static', { dest: ['static'] })
mkDir('src/static', { dest: ['src/static'] })
cpDir('urara')
}
const clean = () => {
cleanDir('urara')
rmDir('static', { dest: ['static'] })
rmDir('src/static', { dest: ['src/static'] })
}
switch (process.argv[2]) {