Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -14,11 +14,79 @@ const parseIfJson = (str: string) => {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
const publishRegistry = (options: { execPath: string, registry: string, tag?: string, config: any, env: any }) => {
|
||||
const packageJson = path.resolve(options.execPath, 'package.json');
|
||||
let cmd = '';
|
||||
const config = options.config || {};
|
||||
const execPath = options.execPath;
|
||||
const registry = options.registry;
|
||||
const setEnv = options.env || {};
|
||||
switch (registry) {
|
||||
case 'npm':
|
||||
cmd = 'npm publish -s --registry https://registry.npmjs.org';
|
||||
break;
|
||||
case 'cnb':
|
||||
cmd = 'npm publish -s --registry https://npm.cnb.cool/kevisual/registry/-/packages/';
|
||||
break;
|
||||
default:
|
||||
cmd = 'npm publish -s --registry https://registry.npmjs.org';
|
||||
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 pkg = fs.readFileSync(packageJson, 'utf-8');
|
||||
const pkgJson = parseIfJson(pkg);
|
||||
const version = pkgJson?.version as string;
|
||||
if (version && options?.tag) {
|
||||
let tag = String(version).split('-')[1] || '';
|
||||
if (tag) {
|
||||
if (tag.includes('.')) {
|
||||
tag = tag.split('.')[0];
|
||||
}
|
||||
cmd = `${cmd} --tag ${tag}`;
|
||||
}
|
||||
}
|
||||
console.log(chalk.green(cmd));
|
||||
|
||||
const child = spawn(cmd, {
|
||||
shell: true,
|
||||
cwd: execPath,
|
||||
env: {
|
||||
...process.env, // 保留当前环境变量
|
||||
...tokenEnv,
|
||||
...setEnv,
|
||||
},
|
||||
});
|
||||
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 command = new Command('npm').description('npm command show publish and set .npmrc').action(async (options) => { });
|
||||
const publish = new Command('publish')
|
||||
.argument('[registry]')
|
||||
.option('-p --proxy', 'proxy')
|
||||
.option('-t, --tag', 'tag')
|
||||
.option('-u, --update', 'update new version')
|
||||
.description('publish npm')
|
||||
.action(async (registry, options) => {
|
||||
if (!registry) {
|
||||
@@ -26,8 +94,8 @@ const publish = new Command('publish')
|
||||
message: 'Select the registry to publish',
|
||||
choices: [
|
||||
{
|
||||
name: 'me',
|
||||
value: 'me',
|
||||
name: 'all',
|
||||
value: 'all',
|
||||
},
|
||||
{
|
||||
name: 'npm',
|
||||
@@ -41,7 +109,6 @@ const publish = new Command('publish')
|
||||
});
|
||||
}
|
||||
const config = getConfig();
|
||||
let cmd = '';
|
||||
const execPath = process.cwd();
|
||||
let setEnv = {};
|
||||
const proxyEnv = {
|
||||
@@ -55,70 +122,15 @@ const publish = new Command('publish')
|
||||
...proxyEnv,
|
||||
};
|
||||
}
|
||||
if (registry) {
|
||||
const packageJson = path.resolve(execPath, 'package.json');
|
||||
switch (registry) {
|
||||
case 'me':
|
||||
cmd = 'npm publish -s --registry https://npm.xiongxiao.me';
|
||||
break;
|
||||
case 'npm':
|
||||
cmd = 'npm publish -s --registry https://registry.npmjs.org';
|
||||
break;
|
||||
case 'cnb':
|
||||
cmd = 'npm publish -s --registry https://npm.cnb.cool/kevisual/registry/-/packages/';
|
||||
break;
|
||||
default:
|
||||
cmd = 'npm publish -s --registry https://npm.xiongxiao.me';
|
||||
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 pkg = fs.readFileSync(packageJson, 'utf-8');
|
||||
const pkgJson = parseIfJson(pkg);
|
||||
const version = pkgJson?.version as string;
|
||||
if (version && options?.tag) {
|
||||
let tag = String(version).split('-')[1] || '';
|
||||
if (tag) {
|
||||
if (tag.includes('.')) {
|
||||
tag = tag.split('.')[0];
|
||||
}
|
||||
cmd = `${cmd} --tag ${tag}`;
|
||||
}
|
||||
}
|
||||
console.log(chalk.green(cmd));
|
||||
if (options?.update) {
|
||||
patchFunc({ directory: execPath });
|
||||
}
|
||||
|
||||
const child = spawn(cmd, {
|
||||
shell: true,
|
||||
cwd: execPath,
|
||||
env: {
|
||||
...process.env, // 保留当前环境变量
|
||||
...tokenEnv,
|
||||
...setEnv,
|
||||
},
|
||||
});
|
||||
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'));
|
||||
}
|
||||
if (registry === 'all') {
|
||||
publishRegistry({ execPath, registry: 'npm', config, env: setEnv });
|
||||
publishRegistry({ execPath, registry: 'cnb', config, env: setEnv });
|
||||
} else {
|
||||
publishRegistry({ execPath, registry, tag: options?.tag, config, env: setEnv });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -142,8 +154,7 @@ const npmrc = new Command('set')
|
||||
const config = getConfig();
|
||||
const npmrcContent =
|
||||
config?.npmrc ||
|
||||
`//npm.xiongxiao.me/:_authToken=\${ME_NPM_TOKEN}
|
||||
//npm.cnb.cool/kevisual/registry/-/packages/:_authToken=\${CNB_API_KEY}
|
||||
`/npm.cnb.cool/kevisual/registry/-/packages/:_authToken=\${CNB_API_KEY}
|
||||
//registry.npmjs.org/:_authToken=\${NPM_TOKEN}
|
||||
`;
|
||||
const execPath = process.cwd();
|
||||
@@ -210,9 +221,8 @@ const install = new Command('install')
|
||||
});
|
||||
command.addCommand(install);
|
||||
|
||||
// npm patch
|
||||
const patch = new Command('patch').description('npm patch 发布补丁版本').action(async () => {
|
||||
const cwd = process.cwd();
|
||||
const patchFunc = (opts?: { directory?: string }) => {
|
||||
const cwd = opts?.directory || process.cwd();
|
||||
const packageJson = path.resolve(cwd, 'package.json');
|
||||
if (fileIsExist(packageJson)) {
|
||||
const pkg = fs.readFileSync(packageJson, 'utf-8');
|
||||
@@ -229,6 +239,10 @@ const patch = new Command('patch').description('npm patch 发布补丁版本').a
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// npm patch
|
||||
const patch = new Command('patch').description('npm patch 发布补丁版本').action(async () => {
|
||||
patchFunc();
|
||||
});
|
||||
command.addCommand(patch);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user