From fa830b1977409f484514d6ba5a340b177c08c46e Mon Sep 17 00:00:00 2001 From: Tim Qian Date: Wed, 30 Mar 2022 13:32:58 +0800 Subject: [PATCH] Fix exit code issue for YML parsing (#171) * Fix exitcode issue for YML parsing * Set exitcode to 1 When opration is not successfuly * Update tests * Update tests * Update tests * update tests --- src/commands/credentials.js | 4 ++-- src/commands/dev.js | 4 ++-- src/commands/invoke/index.js | 10 +++++----- src/commands/invoke/invoke-local/runNode.js | 2 +- src/commands/invoke/invoke-local/runPhp.js | 2 +- src/commands/invoke/invoke-local/runPython.js | 2 +- src/commands/logs.js | 4 ++-- src/libs/auto.js | 2 +- src/libs/cli.js | 2 +- src/libs/config.js | 7 ++++--- src/libs/standalone.js | 6 +++--- src/libs/utils/basic.js | 2 +- tests/libs/cli.test.js | 2 +- 13 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/commands/credentials.js b/src/commands/credentials.js index 04edbc2..fd5c81a 100644 --- a/src/commands/credentials.js +++ b/src/commands/credentials.js @@ -67,7 +67,7 @@ module.exports = async (config, cli, command, globalTencentCredentials = default cli.log( `Serverless: ${chalk.yellow(`授权信息 ${profile} 已存在,请使用 --overwrite 进行覆写`)}` ); - process.exit(); + process.exit(1); } credContent[profile] = { ...credContent[profile], @@ -109,7 +109,7 @@ module.exports = async (config, cli, command, globalTencentCredentials = default cli.log( `Serverless: ${chalk.yellow('未指定授权名称,请通过 --profile 指定要删除的授权名称')}` ); - process.exit(); + process.exit(1); } } diff --git a/src/commands/dev.js b/src/commands/dev.js index 3a23c44..681c3cb 100644 --- a/src/commands/dev.js +++ b/src/commands/dev.js @@ -173,7 +173,7 @@ module.exports = async (config, cli, command) => { cli.log( `Serverless: ${chalk.yellow('该命令暂不支持对多组件进行调用, 使用 --target 指定执行目录')}` ); - process.exit(); + process.exit(1); } const projectFile = await utils.checkBasicConfigValidation(instanceDir); @@ -205,7 +205,7 @@ module.exports = async (config, cli, command) => { { command: 'dev' } ); console.log(''); - process.exit(); + process.exit(1); } else { cli.log(msg, 'grey'); } diff --git a/src/commands/invoke/index.js b/src/commands/invoke/index.js index b182b2e..6877e36 100644 --- a/src/commands/invoke/index.js +++ b/src/commands/invoke/index.js @@ -60,7 +60,7 @@ module.exports = async (config, cli, command) => { instanceDir = await utils.getDirForInvokeCommand(instanceDir, functionAlias); } catch (e) { cli.log(`Serverless: ${chalk.yellow(e.message)}`); - process.exit(); + process.exit(1); } } @@ -87,7 +87,7 @@ module.exports = async (config, cli, command) => { outcome: 'failure', failure_reason: '不能同时指定 data 与 path, 请检查后重试', }); - process.exit(); + process.exit(1); } if (path || p) { @@ -101,7 +101,7 @@ module.exports = async (config, cli, command) => { outcome: 'failure', failure_reason: '找不到指定的路径文件, 请检查后重试', }); - process.exit(); + process.exit(1); } } @@ -113,7 +113,7 @@ module.exports = async (config, cli, command) => { outcome: 'failure', failure_reason: '传入的 data 不是序列化 JSON, 请检查后重试', }); - process.exit(); + process.exit(1); } const componentType = instanceYaml && instanceYaml.component; @@ -129,7 +129,7 @@ module.exports = async (config, cli, command) => { outcome: 'failure', failure_reason: 'Inovke 命令仅能在 scf 或者 multi-scf 组件目录中调用', }); - process.exit(); + process.exit(1); } const sdk = new ServerlessSDK({ diff --git a/src/commands/invoke/invoke-local/runNode.js b/src/commands/invoke/invoke-local/runNode.js index 6adb456..f2c45b2 100644 --- a/src/commands/invoke/invoke-local/runNode.js +++ b/src/commands/invoke/invoke-local/runNode.js @@ -41,7 +41,7 @@ test() fse.unlinkSync(tempNodeFile); fse.unlinkSync(tempResFile); colorLog(`调用错误\n\n ${errData.join('\n').toString()}`, 'red', cli); - process.exit(); + process.exit(1); }); res.on('close', () => { diff --git a/src/commands/invoke/invoke-local/runPhp.js b/src/commands/invoke/invoke-local/runPhp.js index 2a39fbd..192eb4e 100644 --- a/src/commands/invoke/invoke-local/runPhp.js +++ b/src/commands/invoke/invoke-local/runPhp.js @@ -51,7 +51,7 @@ module.exports = async (event, context, handlerFile, handlerFunc, cli) => { fse.unlinkSync(tempPhpFile); fse.unlinkSync(tempResFile); colorLog(`调用错误\n\n ${errData.join('\n').toString()}`, 'red', cli); - process.exit(); + process.exit(1); }); res.on('close', () => { diff --git a/src/commands/invoke/invoke-local/runPython.js b/src/commands/invoke/invoke-local/runPython.js index 60b3199..fdd070d 100644 --- a/src/commands/invoke/invoke-local/runPython.js +++ b/src/commands/invoke/invoke-local/runPython.js @@ -39,7 +39,7 @@ f.close()`; fse.unlinkSync(tempPyFile); fse.unlinkSync(tempResFile); colorLog(`调用错误\n\n ${errData.join('\n').toString()}`, 'red', cli); - process.exit(); + process.exit(1); }); res.on('close', () => { diff --git a/src/commands/logs.js b/src/commands/logs.js index dc7c890..11c1682 100644 --- a/src/commands/logs.js +++ b/src/commands/logs.js @@ -70,7 +70,7 @@ module.exports = async (config, cli, command) => { .format('YYYY-MM-DD HH:mm:ss'); } else if (!dayjs(startTime).isValid()) { cli.log(`Serverless: ${chalk.yellow('指定时间格式不正确,请检查后重试')}`); - process.exit(); + process.exit(1); } else { startTimeValue = dayjs(startTime).tz('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss'); } @@ -90,7 +90,7 @@ module.exports = async (config, cli, command) => { cli.log( `Serverless: ${chalk.yellow('该命令暂不支持对多组件进行调用,请使用 --target 指定组件实例')}` ); - process.exit(); + process.exit(1); } await utils.checkBasicConfigValidation(instanceDir); await utils.login(config); diff --git a/src/libs/auto.js b/src/libs/auto.js index aa7e05f..6507b45 100644 --- a/src/libs/auto.js +++ b/src/libs/auto.js @@ -345,7 +345,7 @@ module.exports = async () => { }, { command: 'auto' } ); - process.exit(); + process.exit(1); } if (Array.isArray(instances) && instances.length > 0) { isLinkingInstance = true; diff --git a/src/libs/cli.js b/src/libs/cli.js index beb52ed..5e67b24 100644 --- a/src/libs/cli.js +++ b/src/libs/cli.js @@ -355,7 +355,7 @@ TraceId: ${traceId}`; console.log(); console.log(chalk.gray('可以使用 --noValidation 跳过 serverless 应用配置校验')); if (errors.length > 0) { - process.exit(); + process.exit(1); } } diff --git a/src/libs/config.js b/src/libs/config.js index 15e7038..6724eb1 100644 --- a/src/libs/config.js +++ b/src/libs/config.js @@ -17,8 +17,6 @@ const CLI = require('./cli'); module.exports = () => { const args = minimist(process.argv.slice(2)); - const instanceConfig = loadInstanceConfig(process.cwd()); - const stage = args.stage || (instanceConfig && instanceConfig.stage) || 'dev'; const params = []; if (args._[1]) { params.push(args._[1]); @@ -35,6 +33,9 @@ module.exports = () => { const config = { ...args, params }; try { + const instanceConfig = loadInstanceConfig(process.cwd()); + const stage = args.stage || (instanceConfig && instanceConfig.stage) || 'dev'; + if (config._) { delete config._; } @@ -185,7 +186,7 @@ module.exports = () => { } const cli = new CLI({}); cli.logError(e, { command: args._[0] }); - process.exit(); + process.exit(1); } return config; }; diff --git a/src/libs/standalone.js b/src/libs/standalone.js index bbc4dfd..85b54c5 100644 --- a/src/libs/standalone.js +++ b/src/libs/standalone.js @@ -114,7 +114,7 @@ const standaloneUpgrade = async (options) => { if (semver.major(latestVersion) > semver.major(version) && !options.major) { console.log(red('Serverless CLI 有新版本发布,无法自动升级,请手动安装最新版本。')); - process.exit(); + process.exit(1); } const getCliProgressFooter = require('cli-progress-footer'); @@ -134,7 +134,7 @@ const standaloneUpgrade = async (options) => { if (answer === undefined) { await writePerference({ standaloneUpgradeExpireDate: Date.now() }); // record the date that users choose don't upgrade console.log('\n超时无响应,已取消升级。'); - process.exit(); + process.exit(1); } }, 5000); @@ -182,7 +182,7 @@ const standaloneUpgrade = async (options) => { e ); - process.exit(-1); + process.exit(1); } finally { cliProgressFooter.updateProgress(); } diff --git a/src/libs/utils/basic.js b/src/libs/utils/basic.js index 9166b57..434daad 100644 --- a/src/libs/utils/basic.js +++ b/src/libs/utils/basic.js @@ -642,7 +642,7 @@ const executeGraph = async (allComponents, command, graph, cli, sdk, options) => // Don't try to deploy other instances if it's a permission issue if (error.code === 'ResourceNotFound.Role') { - process.exit(); + process.exit(1); } return null; } diff --git a/tests/libs/cli.test.js b/tests/libs/cli.test.js index 5d0347e..f66c72f 100644 --- a/tests/libs/cli.test.js +++ b/tests/libs/cli.test.js @@ -85,7 +85,7 @@ describe('Test CLI functions', () => { expect(console.log.mock.calls[1][0]).toMatch('scf 组件校验结果: 错误 1 警告 0 规则版本 v0.0.1'); expect(console.log.mock.calls[3][0]).toMatch('inputs.region'); - expect(mockExit).toHaveBeenCalledWith(); + expect(mockExit).toHaveBeenCalledWith(1); }); test('isSessionActive', () => {