1
mirror of https://github.com/importantimport/urara.git synced 2024-09-16 17:18:40 +08:00
urara/urara.ts

180 lines
4.7 KiB
TypeScript
Raw Permalink Normal View History

2022-01-02 13:45:21 +08:00
/**
* Urara.TS
* Version: Any
2022-01-02 13:45:21 +08:00
*/
2021-11-04 15:34:01 +08:00
import chalk from 'chalk'
import chokidar from 'chokidar'
2024-09-11 18:43:52 +08:00
import fs from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
2021-11-04 15:34:01 +08:00
const config = {
catch: ['ENOENT', 'EEXIST'],
2023-01-30 20:59:03 +08:00
extensions: {
images: ['jpg', 'png', 'webp', 'avif'],
2023-01-30 20:59:03 +08:00
posts: ['md'],
},
images: [''],
}
2023-01-30 20:59:03 +08:00
const check = (ext: string) => (config.extensions.posts.includes(ext) ? 'src/routes' : 'static')
const log = (color: string, msg: string, dest?: Error | string) =>
2024-09-11 18:43:52 +08:00
// eslint-disable-next-line no-console
2021-11-04 15:34:01 +08:00
console.log(
chalk.dim(`${new Date().toLocaleTimeString()} `)
+ chalk.magentaBright.bold('[urara] ')
+ chalk[color](`${msg} `)
+ chalk.dim(dest ?? ''),
2021-11-04 15:34:01 +08:00
)
const error = (err: { code: string, message: unknown }) => {
if (config.catch.includes(err.code)) {
2024-09-11 18:43:52 +08:00
// eslint-disable-next-line no-console
console.log(
chalk.dim(`${new Date().toLocaleTimeString()} `)
+ chalk.redBright.bold('[urara] ')
+ chalk.red('error ')
+ chalk.dim(err.message),
)
}
else {
throw err
}
2021-11-04 15:34:01 +08:00
}
const cpFile = (src: string, { dest = path.join(check(path.parse(src).ext.slice(1)), src.slice(6)), stat = 'copy' } = {}) =>
2023-01-30 20:59:03 +08:00
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)
2023-01-30 20:59:03 +08:00
: fs
.copyFile(src, dest)
.then(() => log('green', `${stat} file`, dest))
.catch(error)
2021-11-04 15:34:01 +08:00
const rmFile = (src: string, { dest = path.join(check(path.parse(src).ext.slice(1)), src.slice(6)) } = {}) =>
2023-01-30 20:59:03 +08:00
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)
2023-01-30 20:59:03 +08:00
: fs
.rm(dest)
.then(() => log('yellow', 'remove file', dest))
.catch(error)
2021-11-04 15:34:01 +08:00
2024-09-11 18:43:52 +08:00
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))],
} = {},
) => {
dest.forEach(path =>
fs
.mkdir(path)
.then(() => log('green', 'make dir', path))
.catch(error),
)
}
const cpDir = (src: string) =>
fs.readdir(src, { withFileTypes: true }).then(files =>
files.forEach((file) => {
const dest = path.join(src, file.name)
if (file.isDirectory()) {
mkDir(dest)
cpDir(dest)
}
else if (file.name.startsWith('.')) {
log('cyan', 'ignore file', dest)
}
else {
cpFile(dest)
}
}),
)
2021-11-04 15:34:01 +08:00
2023-01-30 20:59:03 +08:00
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))],
} = {},
2023-01-30 20:59:03 +08:00
) => {
dest.forEach(path =>
fs
.rm(path, { force: true, recursive: true })
.then(() => log('yellow', 'remove dir', path))
.catch(error),
)
2021-11-04 15:34:01 +08:00
}
const cleanDir = (src: string) =>
fs.readdir(src, { withFileTypes: true }).then((files) => {
files.forEach((file) => {
const dest = path.join(src, file.name)
2024-09-11 18:43:52 +08:00
// eslint-disable-next-line ts/no-unused-expressions
file.isDirectory()
? rmDir(dest)
: file.name.startsWith('.')
? log('cyan', 'ignore file', dest)
: rmFile(dest)
})
})
const build = () => {
mkDir('static', { dest: ['static'] })
2023-01-30 20:59:03 +08:00
mkDir('src/static', { dest: ['src/static'] })
cpDir('urara')
}
const clean = () => {
cleanDir('urara')
rmDir('static', { dest: ['static'] })
2023-01-30 20:59:03 +08:00
rmDir('src/static', { dest: ['src/static'] })
2021-11-04 15:34:01 +08:00
}
switch (process.argv[2]) {
case 'watch':
{
const watcher = chokidar.watch('urara', {
ignored: (file: string) => path.basename(file).startsWith('.'),
2021-11-04 15:34:01 +08:00
})
watcher
.on('add', file => cpFile(file))
2022-01-02 13:45:21 +08:00
.on('change', file => cpFile(file, { stat: 'update' }))
.on('unlink', file => rmFile(file))
.on('addDir', dir => mkDir(dir))
.on('unlinkDir', dir => rmDir(dir))
.on('error', error => log('red', 'error', error))
2022-01-02 13:45:21 +08:00
.on('ready', () => log('cyan', 'copy complete. ready for changes'))
process
.on('SIGINT', () => {
log('red', 'sigint')
clean()
watcher?.close()
})
.on('SIGTERM', () => {
log('red', 'sigterm')
watcher?.close()
})
.on('exit', () => {
log('red', 'exit')
})
2021-11-04 15:34:01 +08:00
}
break
case 'build':
build()
2021-11-04 15:34:01 +08:00
break
case 'clean':
clean()
2021-11-04 15:34:01 +08:00
break
default:
log('red', 'error', 'invalid arguments')
2021-11-04 15:34:01 +08:00
break
}