feat: add config set and npm config
This commit is contained in:
@@ -2,16 +2,22 @@ import { program as app, Command } from '@/program.ts';
|
||||
import { checkFileExists, getConfig, writeConfig } from '@/module/index.ts';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { chalk } from '@/module/chalk.ts';
|
||||
import inquirer from 'inquirer';
|
||||
|
||||
const command = new Command('config')
|
||||
.description('')
|
||||
.option('-d, --dev <dev>', 'Specify dev')
|
||||
.option('-l --list', 'list config')
|
||||
.option('-s --set <set>', 'set config')
|
||||
.option('-g --get <get>', 'get config')
|
||||
.option('-r --remove <remove>', 'remove config')
|
||||
.option('-v --value <value>', 'value')
|
||||
.option('-w --workdir <path>', 'web config')
|
||||
.action(async (options) => {
|
||||
const { dev, list, workdir } = options || {};
|
||||
let config = getConfig();
|
||||
let flag = false;
|
||||
const execPath = process.cwd();
|
||||
if (dev === 'true' || dev === 'false') {
|
||||
flag = true;
|
||||
const config = getConfig();
|
||||
@@ -21,9 +27,8 @@ const command = new Command('config')
|
||||
config.dev = false;
|
||||
}
|
||||
}
|
||||
if (options.workdir) {
|
||||
if (workdir) {
|
||||
let finalPath: string;
|
||||
flag = true;
|
||||
const workdir = options.workdir;
|
||||
|
||||
if (workdir.startsWith('/')) {
|
||||
@@ -31,21 +36,118 @@ const command = new Command('config')
|
||||
finalPath = workdir;
|
||||
} else {
|
||||
// 否则,处理为相对路径
|
||||
finalPath = path.resolve(workdir);
|
||||
finalPath = path.join(execPath, workdir);
|
||||
}
|
||||
if (!checkFileExists(finalPath)) {
|
||||
console.log('路径不存在');
|
||||
fs.mkdirSync(finalPath, { recursive: true });
|
||||
}
|
||||
config.workdir = finalPath;
|
||||
const answers = await inquirer.prompt([
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'confirm',
|
||||
message: `Are you sure you want to set the workdir to: ${finalPath}?`,
|
||||
default: false,
|
||||
},
|
||||
]);
|
||||
if (answers.confirm) {
|
||||
flag = true;
|
||||
config.workdir = finalPath;
|
||||
console.log(chalk.green(`set workdir success:`, finalPath));
|
||||
} else {
|
||||
console.log('Cancel set workdir');
|
||||
}
|
||||
}
|
||||
if (list) {
|
||||
const config = getConfig();
|
||||
console.log('config', config);
|
||||
if (options.set) {
|
||||
const key = options.set;
|
||||
let value = options.value;
|
||||
const answer = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'value',
|
||||
message: `Enter your ${key}:(current: ${config[key]})`,
|
||||
when: () => !value,
|
||||
},
|
||||
]);
|
||||
value = answer.value || value;
|
||||
if (key && value) {
|
||||
flag = true;
|
||||
config[key] = value;
|
||||
}
|
||||
}
|
||||
if (options.remove) {
|
||||
const key = options.remove;
|
||||
if (key) {
|
||||
flag = true;
|
||||
delete config[key];
|
||||
}
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
writeConfig(config);
|
||||
}
|
||||
});
|
||||
|
||||
const setCommand = new Command('set')
|
||||
.argument('<key>')
|
||||
.argument('[value]', 'value', 'not_input')
|
||||
.description('set config')
|
||||
.action(async (key, value) => {
|
||||
const config = getConfig();
|
||||
if (!key) {
|
||||
console.log('key is empty');
|
||||
return;
|
||||
}
|
||||
let flag = false;
|
||||
const answer = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'value',
|
||||
message: `Enter your ${key}:(current: ${config[key]})`,
|
||||
when: () => value === 'not_input',
|
||||
},
|
||||
]);
|
||||
value = answer.value || value;
|
||||
if (key && value) {
|
||||
flag = true;
|
||||
console.log(chalk.green(`set ${key} success:`, value));
|
||||
config[key] = value;
|
||||
}
|
||||
if (flag) {
|
||||
writeConfig(config);
|
||||
}
|
||||
});
|
||||
|
||||
command.addCommand(setCommand);
|
||||
const getCommand = new Command('get')
|
||||
.argument('[key]', 'key')
|
||||
.description('get config')
|
||||
.action(async (key) => {
|
||||
const config = getConfig();
|
||||
const keys = Object.keys(config);
|
||||
const answer = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'key',
|
||||
message: `Enter your key:(keys: ${JSON.stringify(keys)})`,
|
||||
when: () => !key,
|
||||
},
|
||||
]);
|
||||
key = answer.key || key;
|
||||
|
||||
if (config[key]) {
|
||||
console.log(chalk.green(`get ${key}:`));
|
||||
console.log(config[key]);
|
||||
} else {
|
||||
console.log(chalk.red(`not found ${key}`));
|
||||
}
|
||||
});
|
||||
command.addCommand(getCommand);
|
||||
|
||||
const list = new Command('list').action(async () => {
|
||||
const config = getConfig();
|
||||
console.log(chalk.green('config', JSON.stringify(config, null, 2)));
|
||||
});
|
||||
command.addCommand(list);
|
||||
|
||||
app.addCommand(command);
|
||||
|
||||
Reference in New Issue
Block a user