Skip to content

Commit

Permalink
feat: 优化传统模式可维护性
Browse files Browse the repository at this point in the history
  • Loading branch information
zkz098 committed Sep 21, 2024
1 parent 1143cde commit 8c635f0
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions toolbox/compiler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,12 @@ const CONFIG = {
legacyScript: false,
}

async function deleteFileRecursive(dir) {
const files = await fs.readdir(dir)
for (const f of files) {
const fullPath = path.join(dir, f)
const stat = await fs.lstat(fullPath)
if (stat.isDirectory()) {
await deleteFileRecursive(fullPath)
} else {
const fileExt = path.extname(f)
if (fileExt === '.ts' || fileExt === '.json' || fileExt === '.tsbuildinfo') {
await fs.unlink(fullPath)
}
}
}
}
console.log('ShokaX ToolBox - Compiler')
console.log('Start compiling...')

let hexoRoot = path.join(import.meta.url, './../../../../').trim()
if (hexoRoot.startsWith('file:/')) {
hexoRoot = hexoRoot.slice(5); // 去除 'file://'
} else if (hexoRoot.startsWith('file:\\')) {
hexoRoot = hexoRoot.slice(8); // 去除 'file:\'
}
const entryPoints = await glob('./scripts/**/*.ts');
const jsons = await glob('./scripts/**/*.json');

if (CONFIG.legacyScript) {
console.log('Simulating legacy script compiler...')
let sPath = path.join(import.meta.url, './../../scripts/').trim()
Expand All @@ -51,14 +33,18 @@ if (CONFIG.legacyScript) {
cwd: sPath
}, async (code, stdout, stderr) => {
console.log('Deleting typescript files...')
await deleteFileRecursive(sPath)
for (const entry of entryPoints) {
await fs.unlink(entry)
}
for (const entry of jsons) {
await fs.unlink(entry)
}
console.log('Finished compiling.')
})
})
} else {
console.log('RUN THIS SCRIPT IN YOUR SHOKAX THEME ROOT DIRECTORY!')
console.log('Using esbuild compiler...')
const entryPoints = glob.sync('./scripts/**/*.ts');
buildSync({
entryPoints: entryPoints,
outdir: 'scripts',
Expand All @@ -68,7 +54,12 @@ if (CONFIG.legacyScript) {
platform: 'node',
loader: { '.ts': 'ts' },
})
await deleteFileRecursive('./scripts/')
entryPoints.forEach(async (entry) => {
await fs.unlink(entry)
})
jsons.forEach(async (entry)=>{
await fs.unlink(entry)
})
console.log('Finished compiling.')
}

Expand Down

0 comments on commit 8c635f0

Please sign in to comment.