UNPKG

makedirp

Version:

The 'mkdir -p' command implementation for nodejs.Make directory recursively.

89 lines (84 loc) 2.57 kB
#!/usr/bin/env node import { createInterface } from 'readline'; import { mkdirpSync } from './index.mjs'; import 'node:fs'; import 'node:path'; import '@lxf2513/mkdir-recursive'; const name = "makedirp"; const version = "1.3.3"; const log = (str) => { console.log("\x1B[31m%s\x1B[0m", str); }; const helpInfo = () => { log(`Try '${name} --help' for more information.`); }; const help = `${name} ${version} Usage: ${name} [OPTION]... DIRECTORY... Create the DIRECTORY(ies), if they do not already exist. Options: -- Treat all subsequent arguments as DIRECTORY(ies) -h, --help display this help and exit -v, --version output version information and exit -m=MODE, --mode=MODE set file mode (as in chmod), not a=rwx - umask `; const validArgs = ["--", "-h", "-v", "-m", "--help", "--version", "--mode"]; const main = async (...args2) => { const _args = args2.filter((i) => i.trim()); let paths = []; let options = []; const splitChar = "--"; const idx = _args.findIndex((arg) => arg === splitChar); if (!_args.length || _args.length === 1 && idx > -1) { log(`${name}: missing operand`); helpInfo(); return 1; } if (idx > -1 && _args.length > 1) { options = _args.slice(0, idx); paths = _args.slice(idx).filter((a) => a !== splitChar); } if (idx < 0 && _args.length) { options = _args.filter((a) => /^-/.test(a)); paths = _args.filter((a) => !/^-/.test(a)); } let mode = void 0; if (options.length) { const arg = options.length === 1 ? options[0] : options.filter((o) => o !== "-m" && o !== "--mode")[0]; const setMode = arg.includes("-m=") || arg.includes("--mode="); if (validArgs.includes(arg) || setMode) { if (arg === "-h" || arg === "--help") { console.log(help); return 0; } else if (arg === "-v" || arg === "--version") { console.log(`${name} ${version}`); return 0; } else if (setMode) { mode = parseInt(arg.replace(/^(-m|--mode)=/, ""), 8); if (isNaN(mode)) { log(`invalid mode '${mode}'. It must be an octal number.`); helpInfo(); return 1; } } } else { log(`unknown option: ${arg}`); helpInfo(); return 1; } } const rl = createInterface({ input: process.stdin, output: process.stdout }); mkdirpSync(paths, mode); rl.close(); return 0; }; const args = process.argv.slice(2); main(...args).then( (code) => process.exit(code), (err) => { log(err); process.exit(1); } );