Skip to content

Commit

Permalink
feat: add bin script
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Aug 15, 2020
1 parent 40c2796 commit e91f3b4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
54 changes: 54 additions & 0 deletions bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env node
const { homedir } = require('os');
const { normalize, resolve } = require('path');
const { premove } = require('./sync');

const argv = process.argv.slice(2);

if (argv.includes('--help') || argv.includes('-h')) {
let msg = '\n Description\n Remove all items recursively\n';
msg += '\n Usage\n $ premove [options] [...paths]\n';
msg += '\n Options';
msg += '\n --cwd Directory to resolve from (default ".")';
msg += '\n --help Displays this message\n';
console.log(msg); process.exit(0);
}

let cwd = resolve('.');
let idx = argv.indexOf('--cwd');

if (idx !== -1) {
let num = 1;
if ((idx + 1) !== argv.length) {
cwd = resolve(cwd, argv[idx + num++]);
}
argv.splice(idx, num);
}

const home = homedir();
const isFlag = /^-{1,2}/;
const isRoot = /^(\/|[a-zA-Z]:\\)$/;

let i=0, tmp, dirs=[];
for (; i < argv.length; i++) {
tmp = argv[i];
if (!tmp || isFlag.test(tmp)) continue;

tmp = normalize(resolve(cwd, tmp));
if (tmp === home || isRoot.test(tmp)) {
console.error('! will not remove home and/or root directory :: "%s"', argv[i]);
continue;
}

if (!tmp.startsWith(cwd)) {
console.error('! will not remove items outside `--cwd` scope :: "%s"', argv[i]);
continue;
}

dirs.push(tmp);
}

dirs.sort(); // length
for (i=0; i < dirs.length;) {
premove(dirs[i++]);
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"email": "luke.edwards05@gmail.com",
"url": "https://lukeed.com"
},
"bin": {
"premove": "./bin.js"
},
"exports": {
".": {
"import": "./dist/index.mjs",
Expand All @@ -24,6 +27,7 @@
},
"files": [
"*.d.ts",
"bin.js",
"dist",
"sync"
],
Expand Down

0 comments on commit e91f3b4

Please sign in to comment.