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