create-tateru-cli
Version:
Create basic Tateru CLI project from template files
86 lines (82 loc) • 3.05 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.commands = commands;
const node_fs_1 = require("node:fs");
const node_path_1 = __importDefault(require("node:path"));
const DEFAULT_TEMPLATE_NAME = 'gulp-vanilla';
const DEFAULT_DIRECTORY = 'tateru-cli-starter';
const HELP_MESSAGE = `
Create a new Tateru CLI project with ease.
Usage:
npx create-tateru-cli [OPTIONS] [ARGS] [DIRECTORY]
Options:
-t, --template NAME Set build environment (dev or prod). Default is dev.
-h, --help Display help and usage details.
-V, --version Display Tateru CLI version.
Available templates:
gulp-vanilla Basic template with gulp workflow.
`;
/**
* Prints the help message.
*/
function printHelp() {
console.log(HELP_MESSAGE);
}
/**
* Parses command-line arguments and returns options.
* If --help or --version is provided, it prints output and exits the process.
*
* @param basePath The base path of the project. Used to locale package.json file.
*/
function commands(basePath) {
return new Promise((resolve, reject) => {
const args = process.argv.slice(2);
const options = {
template: DEFAULT_TEMPLATE_NAME,
directory: DEFAULT_DIRECTORY,
};
// Get package.json data
const packageJsonPath = node_path_1.default.resolve(basePath, 'package.json');
const packageJson = JSON.parse((0, node_fs_1.readFileSync)(packageJsonPath, 'utf-8'));
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case '--help':
case '-h':
printHelp();
resolve(null);
break;
case '--version':
case '-V':
console.log(packageJson.version);
resolve(null);
break;
case '--template':
case '-t':
if (args[i + 1] && !args[i + 1].startsWith('-')) {
options.template = args[i + 1] || DEFAULT_TEMPLATE_NAME;
i++;
}
else {
console.error('Error: Missing value for --template.');
reject();
}
break;
default:
if (arg.startsWith('-')) {
console.error(`Error: Unknown option "${arg}"\n`);
console.log(`Run "npm create tateru-cli@latest -- --help" to see available options.\n`);
reject();
}
if (!arg.startsWith('-')) {
options.directory = arg;
}
break;
}
}
resolve(options);
});
}