From 2a751714d76947a5648cd84d7b2cc725ecc98dba Mon Sep 17 00:00:00 2001 From: 0xB917 <50108258+kwaa@users.noreply.github.com> Date: Sun, 2 Jan 2022 23:28:18 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20urara.js=20cleanDir()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit clean files by detecting urara folder #4 --- urara.js | 76 ++++++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/urara.js b/urara.js index 5e6a6580..8e226a9d 100644 --- a/urara.js +++ b/urara.js @@ -43,63 +43,51 @@ const cpFile = (src, { stat = 'copy', dest = path.join(check(path.parse(src).ext .then(log('green', `${stat} file`, dest)) .catch(error) -const rmFile = (src, dest = path.join(check(path.parse(src).ext.slice(1)), src.slice(6))) => +const rmFile = (src, { dest = path.join(check(path.parse(src).ext.slice(1)), src.slice(6)) } = {}) => fs.rm(dest).then(log('yellow', 'remove file', dest)).catch(error) const cpDir = src => fs.readdir(src, { withFileTypes: true }).then(files => files.forEach(file => { const dest = path.join(src, file.name) - file.isDirectory() - ? mkDir(dest).then(cpDir(dest)) - : file.name.startsWith('.') - ? log('cyan', 'ignore file', dest) - : cpFile(dest) + if (file.isDirectory()) { + mkDir(dest) + cpDir(dest) + } else if (file.name.startsWith('.')) { + log('cyan', 'ignore file', dest) + } else { + cpFile(dest) + } }) ) -const mkDir = src => - fs - .mkdir(path.join('src/routes', src.slice(6)), { recursive: true }) - .then( - fs - .mkdir(path.join('static', src.slice(6)), { recursive: true }) - .then(log('green', 'make dir', path.join('static', src.slice(6)))) - .catch(error) - ) - .then(log('green', 'make dir', path.join('src/routes', src.slice(6)))) - .catch(error) - -const rmDir = src => - fs - .rm(path.join('src/routes', src.slice(6)), { force: true, recursive: true }) - .then( - fs - .rm(path.join('static', src.slice(6)), { force: true, recursive: true }) - .then(log('yellow', 'remove dir', path.join('static', src.slice(6)))) - .catch(error) - ) - .then(log('yellow', 'remove dir', path.join('src/routes', src.slice(6)))) - .catch(error) +const mkDir = (src, { dest = [path.join('src/routes', src.slice(6)), path.join('static', src.slice(6))] } = {}) => { + dest.forEach(path => fs.mkdir(path).then(log('green', 'make dir', path)).catch(error)) +} -const build = async () => { - fs.mkdir('static').then(log('green', 'make dir', 'static')).catch(error) - cpDir('urara').catch(error) +const rmDir = (src, { dest = [path.join('src/routes', src.slice(6)), path.join('static', src.slice(6))] } = {}) => { + dest.forEach(path => fs.rm(path, { force: true, recursive: true }).then(log('yellow', 'remove dir', path)).catch(error)) } -const clean = async () => { - fs.rm('static', { recursive: true }).then(log('yellow', 'remove dir', 'static')).catch(error) - fs.readdir('src/routes', { withFileTypes: true }).then(files => +const cleanDir = src => + fs.readdir(src, { withFileTypes: true }).then(files => { files.forEach(file => { - if (file.isDirectory()) { - const dest = path.join('src/routes', file.name) - fs.rm(dest, { recursive: true }).then(log('yellow', 'remove dir', dest)).catch(error) - } + const dest = path.join(src, file.name) + file.isDirectory() ? rmDir(dest) : file.name.startsWith('.') ? log('cyan', 'ignore file', dest) : rmFile(dest) }) - ) + }) + +const build = () => { + mkDir('static', { dest: ['static'] }) + cpDir('urara') +} + +const clean = () => { + cleanDir('urara') + rmDir('static', { dest: ['static'] }) } -const rename = async () => { +const rename = () => { fs.readdir('build', { withFileTypes: true }).then(files => files.forEach(file => { if (file.isDirectory() && config.rename.includes(file.name)) { @@ -145,13 +133,13 @@ switch (process.argv[2]) { } break case 'build': - build().then(log('cyan', 'copy complete.')) + build() break case 'clean': - clean().then(log('cyan', 'clean complete.')) + clean() break case 'rename': - rename().then(log('cyan', 'rename complete.')) + rename() break default: log('red', 'error', 'invalid arguments')