feat: add config set and npm config
This commit is contained in:
214
src/command/npm.ts
Normal file
214
src/command/npm.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
import { program, Command } from '@/program.ts';
|
||||
import { chalk } from '../module/chalk.ts';
|
||||
import path from 'path';
|
||||
import { spawn } from 'child_process';
|
||||
import { fileIsExist } from '@/uitls/file.ts';
|
||||
import { getConfig } from '@/module/get-config.ts';
|
||||
import fs from 'fs';
|
||||
import inquirer from 'inquirer';
|
||||
|
||||
const command = new Command('npm')
|
||||
.description('npm command show publish and set .npmrc')
|
||||
.option('-p --publish <publish>', 'publish')
|
||||
.action(async (options) => {
|
||||
const { publish } = options || {};
|
||||
const config = getConfig();
|
||||
let cmd = '';
|
||||
const execPath = process.cwd();
|
||||
if (publish) {
|
||||
const packageJson = path.resolve(execPath, 'package.json');
|
||||
switch (publish) {
|
||||
case 'me':
|
||||
cmd = 'npm publish --registry https://npm.xiongxiao.me';
|
||||
console.log(chalk.green(cmd));
|
||||
break;
|
||||
case 'npm':
|
||||
cmd = 'npm publish --registry https://registry.npmjs.org';
|
||||
console.log(chalk.green(cmd));
|
||||
break;
|
||||
default:
|
||||
cmd = 'npm publish --registry https://npm.xiongxiao.me';
|
||||
console.log(chalk.green(cmd));
|
||||
break;
|
||||
}
|
||||
if (fileIsExist(packageJson)) {
|
||||
const keys = Object.keys(config).filter((key) => key.includes('NPM_TOKEN'));
|
||||
const tokenEnv = keys.reduce((prev, key) => {
|
||||
return {
|
||||
...prev,
|
||||
[key]: config[key],
|
||||
};
|
||||
}, {});
|
||||
const child = spawn(cmd, {
|
||||
shell: true,
|
||||
cwd: execPath,
|
||||
env: {
|
||||
...process.env, // 保留当前环境变量
|
||||
...tokenEnv,
|
||||
},
|
||||
});
|
||||
child.stdout.on('data', (data) => {
|
||||
console.log(chalk.green(`${data}`));
|
||||
});
|
||||
child.stderr.on('data', (data) => {
|
||||
// 过滤掉 'npm notice' 或者其他信息
|
||||
if (data.toString().includes('npm notice')) {
|
||||
console.log(chalk.yellow(`notice: ${data}`));
|
||||
} else {
|
||||
console.error(`stderr: ${data}`);
|
||||
}
|
||||
});
|
||||
child.on('close', (code) => {
|
||||
// console.log(`child process exited with code ${code}`);
|
||||
});
|
||||
} else {
|
||||
console.error(chalk.red('package.json not found'));
|
||||
}
|
||||
}
|
||||
});
|
||||
const publish = new Command('publish')
|
||||
.argument('[registry]')
|
||||
.description('publish npm')
|
||||
.action(async (registry) => {
|
||||
const answer = await inquirer.prompt([
|
||||
{
|
||||
type: 'list',
|
||||
name: 'publish',
|
||||
message: 'Select the registry to publish',
|
||||
choices: [
|
||||
{
|
||||
name: 'me',
|
||||
value: 'me',
|
||||
},
|
||||
{
|
||||
name: 'npm',
|
||||
value: 'npm',
|
||||
},
|
||||
],
|
||||
when: !registry,
|
||||
},
|
||||
]);
|
||||
registry = registry || answer.publish;
|
||||
const config = getConfig();
|
||||
let cmd = '';
|
||||
const execPath = process.cwd();
|
||||
if (registry) {
|
||||
const packageJson = path.resolve(execPath, 'package.json');
|
||||
switch (registry) {
|
||||
case 'me':
|
||||
cmd = 'npm publish --registry https://npm.xiongxiao.me';
|
||||
console.log(chalk.green(cmd));
|
||||
break;
|
||||
case 'npm':
|
||||
cmd = 'npm publish --registry https://registry.npmjs.org';
|
||||
console.log(chalk.green(cmd));
|
||||
break;
|
||||
default:
|
||||
cmd = 'npm publish --registry https://npm.xiongxiao.me';
|
||||
console.log(chalk.green(cmd));
|
||||
break;
|
||||
}
|
||||
if (fileIsExist(packageJson)) {
|
||||
const keys = Object.keys(config).filter((key) => key.includes('NPM_TOKEN'));
|
||||
const tokenEnv = keys.reduce((prev, key) => {
|
||||
return {
|
||||
...prev,
|
||||
[key]: config[key],
|
||||
};
|
||||
}, {});
|
||||
const child = spawn(cmd, {
|
||||
shell: true,
|
||||
cwd: execPath,
|
||||
env: {
|
||||
...process.env, // 保留当前环境变量
|
||||
...tokenEnv,
|
||||
},
|
||||
});
|
||||
child.stdout.on('data', (data) => {
|
||||
console.log(chalk.green(`${data}`));
|
||||
});
|
||||
child.stderr.on('data', (data) => {
|
||||
// 过滤掉 'npm notice' 或者其他信息
|
||||
if (data.toString().includes('npm notice')) {
|
||||
console.log(chalk.yellow(`notice: ${data}`));
|
||||
} else {
|
||||
console.error(`stderr: ${data}`);
|
||||
}
|
||||
});
|
||||
child.on('close', (code) => {
|
||||
// console.log(`child process exited with code ${code}`);
|
||||
});
|
||||
} else {
|
||||
console.error(chalk.red('package.json not found'));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
command.addCommand(publish);
|
||||
|
||||
const getnpmrc = new Command('get').action(async () => {
|
||||
const execPath = process.cwd();
|
||||
const npmrcPath = path.resolve(execPath, '.npmrc');
|
||||
if (fileIsExist(npmrcPath)) {
|
||||
const npmrcContent = fs.readFileSync(npmrcPath, 'utf-8');
|
||||
// console.log(chalk.green('get .npmrc success'));
|
||||
console.log(npmrcContent);
|
||||
}
|
||||
});
|
||||
command.addCommand(getnpmrc);
|
||||
|
||||
const npmrc = new Command('set')
|
||||
.description('set .npmrc')
|
||||
.option('-f <force>')
|
||||
.action(async (options) => {
|
||||
const config = getConfig();
|
||||
const npmrcContent =
|
||||
config?.npmrc ||
|
||||
`//npm.xiongxiao.me/:_authToken=\${ME_NPM_TOKEN}
|
||||
@abearxiong:registry=https://npm.pkg.github.com
|
||||
//registry.npmjs.org/:_authToken=\${NPM_TOKEN}
|
||||
@kevisual:registry=https://npm.xiongxiao.me`;
|
||||
const execPath = process.cwd();
|
||||
const npmrcPath = path.resolve(execPath, '.npmrc');
|
||||
let writeFlag = false;
|
||||
if (fileIsExist(npmrcPath)) {
|
||||
if (options.force) {
|
||||
writeFlag = true;
|
||||
} else {
|
||||
const answer = await inquirer.prompt([
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'confirm',
|
||||
message: `Are you sure you want to overwrite the .npmrc file?`,
|
||||
default: false,
|
||||
},
|
||||
]);
|
||||
if (answer.confirm) {
|
||||
writeFlag = true;
|
||||
}
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
writeFlag = true;
|
||||
}
|
||||
if (writeFlag) {
|
||||
fs.writeFileSync(npmrcPath, npmrcContent);
|
||||
console.log(chalk.green('write .npmrc success'));
|
||||
}
|
||||
});
|
||||
command.addCommand(npmrc);
|
||||
|
||||
const remove = new Command('remove').description('remove .npmrc').action(async () => {
|
||||
const execPath = process.cwd();
|
||||
const npmrcPath = path.resolve(execPath, '.npmrc');
|
||||
if (fileIsExist(npmrcPath)) {
|
||||
fs.unlinkSync(npmrcPath);
|
||||
console.log(chalk.green('remove .npmrc success'));
|
||||
} else {
|
||||
console.log(chalk.green('.npmrc success'));
|
||||
}
|
||||
});
|
||||
command.addCommand(remove);
|
||||
|
||||
//
|
||||
program.addCommand(command);
|
||||
Reference in New Issue
Block a user