recursive-copy-cli
Version:
CLI for [recursive-copy](https://github.com/timkendrick/recursive-copy)
161 lines (160 loc) • 6.68 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.getYargsInstance = void 0;
var console_1 = __importDefault(require("console"));
var yargs_1 = __importDefault(require("yargs/yargs"));
var rename_params_to_fn_1 = require("./rename-params-to-fn");
var transform_to_fn_1 = require("./transform-to-fn");
var filter_coerce_1 = require("./filter-coerce");
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports
var packageJSON = require('../../package.json');
/* eslint-enable @typescript-eslint/method-signature-style */
var failHandler = function (msg, err, yargsObj) {
yargsObj.showHelp();
// eslint-disable-next-line no-console
console_1["default"].error();
// eslint-disable-next-line no-console
console_1["default"].error(msg);
// if (err) throw err as Error; // preserve stack
// Explicitly exit, else yargs will pass
yargsObj.exit(1, err !== null && err !== void 0 ? err : new Error(msg));
};
// Handle exceptions thrown by middleware, since they are not handled by yargs
function gracefulMiddleware(middleware) {
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
return function (argv, yargsObj) {
try {
middleware(argv, yargsObj);
}
catch (error) {
var msg = '';
if (error instanceof Error) {
msg = error.message;
}
failHandler(msg, error, yargsObj);
}
};
}
function getYargsInstance() {
var yargsInstance = yargs_1["default"]();
yargsInstance.parserConfiguration({
'parse-numbers': false,
'strip-aliased': true,
'strip-dashed': true
});
yargsInstance
.usage('$0 <src> <dest>', '',
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
function (yargsObj) {
return yargsObj
.positional('src', {
description: 'Source file/folder path',
type: 'string'
})
.positional('dest', {
description: 'Destination file/folder path',
type: 'string'
})
.demandCommand(0, 0, '', 'Too many arguments: got $0 unknown arguments')
.example("$0 srcPath destPath -r pascalcase", 'Renames files using the pascalcase module')
.example("$0 srcPath destPath -p '::' '-'", 'Renames someFile::name.ext to someFile-name.ext')
.example("$0 srcPath destPath -p '/(.*)-(.*)\\.(.*)/g' '$2-$1.$3'", 'Renames author::title.ext to title-author.ext')
.example("$0 srcPath destPath -f '*.json' '/\\*.js$/'", 'Only Copies json & js files')
.example("$0 srcPath destPath -f \"*.js\" -t some-transform-module", 'modify the contents of js files')
.epilogue("Use --no-<option> to toggle boolean options. eg: --no-overwrite or --no-w\n\n When specifying a module, you could specify a global module, local module or provide the path to file.\n eg: ./someFolder/pascalcase/index.js in case of file or node_modules/pascalcase in case of local module\n\n For more help refer " + packageJSON.homepage);
} // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions
)
.strictCommands();
yargsInstance
.option('overwrite', {
alias: 'w',
description: 'Whether to overwrite destination files [Default: false]',
type: 'boolean'
})
.option('expand', {
alias: 'e',
description: 'Whether to expand symbolic links [Default: false]',
type: 'boolean'
})
.option('dot', {
alias: 'd',
description: 'Whether to copy files beginning with a .(dot) [Default: false]',
type: 'boolean'
})
.option('junk', {
alias: 'j',
description: 'Whether to copy OS junk files (e.g. .DS_Store, Thumbs.db) [Default: false]',
type: 'boolean'
})
.option('filter', {
alias: 'f',
description: 'Filter regular expression / glob that determines which files to copy (uses maximatch)',
type: 'array',
requiresArg: true,
coerce: filter_coerce_1.filterCoerce
})
.option('transform-module', {
alias: 't',
description: 'Function that returns a transform stream used to modify file contents',
type: 'array',
requiresArg: true
})
.option('concurrency', {
alias: 'c',
description: 'Maximum number of simultaneous copy operations [Default: 255]',
type: 'number',
requiresArg: true,
coerce: function (value) {
if (value !== undefined && isNaN(value)) {
throw new Error('Error: Invalid concurrency option');
}
return value;
}
})
.option('debug', {
alias: 'v',
description: 'Whether to log debug information [Default: false]',
type: 'boolean'
});
yargsInstance
.option('renameModule', {
alias: 'r',
description: 'renames source paths using the module',
type: 'array',
conflicts: 'renamePattern',
requiresArg: true
})
.option('renamePattern', {
alias: 'p',
description: 'renames patterns in source paths. eg: :: -',
type: 'array',
nargs: 2,
requiresArg: true
});
yargsInstance.middleware([
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
gracefulMiddleware(function (argv) {
// remove all undefined values
Object.keys(argv).forEach(function (key) {
if (argv[key] === undefined) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete argv[key];
}
});
}),
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
gracefulMiddleware(function (argv) {
rename_params_to_fn_1.renameParamsToFunction(argv);
}),
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
gracefulMiddleware(function (argv) {
transform_to_fn_1.transformParamsToFunction(argv);
}),
]);
yargsInstance.wrap(yargsInstance.terminalWidth()).showHelpOnFail(true).strict().strictOptions();
return yargsInstance;
}
exports.getYargsInstance = getYargsInstance;