#!/usr/bin/env node const { cosmiconfig } = require("cosmiconfig"); const clearPath = require("../lib"); const { info } = console; /** * ---------------------------------------------------------- */ const filterConfig = (config) => { if (typeof config === "string") { return config.split(" "); } return config; }; const displayError = (chalk, title) => { info(chalk.red("*"), chalk.bold.red(title)); info( chalk.gray( "* read https://github.com/vkiss/clear-path/blob/main/README.md#configuration to learn how to configure clear-path.js", ), ); }; const getRoutine = (args) => { for (const arg of args) { if (arg.split("=")[0] === "--routine") { return arg.split("=")[1]; } } return undefined; }; const clearPathArrayOrString = (chalk, pathsToDelete, options) => { if (typeof pathsToDelete === "string") { clearPath(pathsToDelete, options); return; } if (Array.isArray(pathsToDelete)) { for (const path of pathsToDelete) { clearPath(path, options); } return; } return displayError(chalk, "wrong configuration"); }; const runScript = async () => { const chalk = (await import("chalk")).default; const { argv } = process; const isSilent = argv.includes("--silent"); // Script start message if (!isSilent) { info(chalk.gray("> clear-path.js")); } const config = { silent: false, callback: false, }; config.silent = isSilent; const explorer = cosmiconfig("clearpath"); explorer .search() .then((result) => { if (result === null) { return displayError(chalk, "configuration missing"); } const pathsToDelete = filterConfig(result.config); if (pathsToDelete.routine) { const argRoutine = getRoutine(argv.slice(2)); if (argRoutine) { const routineToRun = pathsToDelete.routine[argRoutine]; if (routineToRun) { return clearPathArrayOrString(chalk, routineToRun, config); } if (pathsToDelete.routine.default) { return clearPathArrayOrString(chalk, pathsToDelete.routine.default, config); } return displayError(chalk, `routine '${argRoutine}' not found`); } return displayError(chalk, "missing routine parameter"); } clearPathArrayOrString(chalk, pathsToDelete, config); }) .catch((error) => { info(chalk.red(error)); }); }; runScript();