UNPKG

es2049package

Version:

ECMAScript 2049 package: zero-configuration libraries and command-line utilies by Harald Rudell

55 lines (44 loc) 1.99 kB
#!/usr/bin/env node import fs from 'fs-extra'; import path from 'path'; /* © 2017-present Harald Rudell <harald.rudell@gmail.com> (http://www.haraldrudell.com) This source code is licensed under the ISC-style license found in the LICENSE file in the root directory of this source tree. */ async function clean(args) { if (typeof args === 'string') args = [args];else if (!Array.isArray(args) || !args.length) throw new Error('clean: argument not non-empty string or array'); for (let [index, s] of args) { const st = typeof s; if (st !== 'string' || !s) throw new Error(`clean: index ${index}: not non-empty string: ${st}`); } console.log(`clean: ${args.join(' ')}…`); const projectDir = fs.realpathSync(process.cwd()); // project directory without symlinks await Promise.all(args.map(s => removeIfExist(path.resolve(projectDir, s)))); console.log('clean completed successfully.'); } async function removeIfExist(p) { if (await fs.exists(p)) await fs.remove(p); } /* © 2017-present Harald Rudell <harald.rudell@gmail.com> (http://www.haraldrudell.com) This source code is licensed under the ISC-style license found in the LICENSE file in the root directory of this source tree. */ const m = 'cleanbin'; let debug; doClean(process.argv.slice(2)).catch(onReject); async function doClean(argv) { for (let [ix, arg] of argv.entries()) arg === '-debug' && (debug = true) && argv.splice(ix, 1); const entries = argv.length ? argv : await readJsonRollupClean(); debug && console.log(`${m} entries: ${entries} argv.length: ${argv.length}`); return clean(entries); } async function readJsonRollupClean() { const json = JSON.parse((await fs.readFile(path.resolve('package.json'), 'utf8'))); return json && json.rollup && json.rollup.clean; } function onReject(e) { debug && console.error(`${m} error handler:`); if (!(e instanceof Error)) e = new Error(`Error value: ${typeof e} ${e}`); console.error(!debug ? e.message : e); process.exit(1); }