@amihhs/quickly-dev-cli
Version:
CLI for Quickly build a local development environment
111 lines (104 loc) • 3.61 kB
JavaScript
import path from 'path';
import { cac } from 'cac';
import { red, green } from 'colorette';
import consola from 'consola';
import fs from 'node:fs';
import { addDirToEnvPath } from '@pnpm/os.env.path-extender';
const version = "0.0.4-beta.2";
const CLI_NAME = "quickly-dev-cli";
const SIMPLE_NAME = "qdev";
function copyCli(currentLocation, targetDir) {
const execName = path.basename(currentLocation).replace(CLI_NAME, SIMPLE_NAME).replace(/(-|win|linux|macos|linuxstatic|x64|x86|arm64)/g, "");
const newExecPath = path.join(targetDir, execName);
if (path.relative(newExecPath, currentLocation) === "")
return;
fs.mkdirSync(targetDir, { recursive: true });
fs.copyFileSync(currentLocation, newExecPath);
}
function getExecPath() {
if (process.pkg != null) {
return process.execPath;
}
return process.cwd();
}
async function handler(opts) {
const execPath = getExecPath();
if (execPath.match(/\.[cm]?js$/) == null)
copyCli(execPath, opts.homeDir);
try {
const report = await addDirToEnvPath(opts.homeDir, {
configSectionName: SIMPLE_NAME,
proxyVarName: "QDEV_HOME",
overwrite: opts.force,
position: "start"
});
return renderSetupOutput(report);
} catch (err) {
consola.error(red(err));
throw err;
}
}
function renderSetupOutput(report) {
if (report.oldSettings === report.newSettings)
return "No changes to the environment were made. Everything is already up to date.";
const output = [];
if (report.configFile)
output.push(reportConfigChange(report.configFile));
output.push(`Next configuration changes were made:
${report.newSettings}`);
if (report.configFile == null) {
output.push("Setup complete. Open a new terminal to start using pnpm.");
} else if (report.configFile.changeType !== "skipped") {
output.push(`To start using ${SIMPLE_NAME}, run:
source ${report.configFile.path}
`);
}
return output.join("\n\n");
}
function reportConfigChange(configReport) {
switch (configReport.changeType) {
case "created":
return `Created ${configReport.path}`;
case "appended":
return `Appended new lines to ${configReport.path}`;
case "modified":
return `Replaced configuration in ${configReport.path}`;
case "skipped":
return `Configuration already up to date in ${configReport.path}`;
}
}
async function startCli(cwd = process.cwd(), argv = process.argv, _options = {}) {
const cli = cac(SIMPLE_NAME);
cli.command("setup", "setup quickly-dev-cli to your system").action(async (_dir, _options2) => {
const USER_HOME = process.env.HOME || process.env.USERPROFILE;
const output = await handler({
homeDir: path.join(USER_HOME || cwd, CLI_NAME),
force: true
});
consola.info(green(`<<setup complete>>
${output}`));
});
cli.command("start", "Start build a local development environment").action(async (_dir, _options2) => {
console.log("<<start command>>", "Welcome use quickly-dev-cli, but now it han't any function, please wait for the next version.");
});
cli.help();
cli.version(version);
cli.parse(argv, { run: false });
await cli.runMatchedCommand();
}
class PrettyError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
if (typeof Error.captureStackTrace === "function")
Error.captureStackTrace(this, this.constructor);
else
this.stack = new Error(message).stack;
}
}
function handleError(error) {
if (error instanceof PrettyError)
consola.error(error.message);
process.exitCode = 1;
}
startCli().catch(handleError);