Skip to content

Commit

Permalink
Fix exit code issue for YML parsing (#171)
Browse files Browse the repository at this point in the history
* Fix exitcode issue for YML parsing

* Set exitcode to 1 When opration is not successfuly

* Update tests

* Update tests

* Update tests

* update tests
  • Loading branch information
timqian authored Mar 30, 2022
1 parent b67fd2e commit fa830b1
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/commands/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -109,7 +109,7 @@ module.exports = async (config, cli, command, globalTencentCredentials = default
cli.log(
`Serverless: ${chalk.yellow('未指定授权名称,请通过 --profile 指定要删除的授权名称')}`
);
process.exit();
process.exit(1);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -205,7 +205,7 @@ module.exports = async (config, cli, command) => {
{ command: 'dev' }
);
console.log('');
process.exit();
process.exit(1);
} else {
cli.log(msg, 'grey');
}
Expand Down
10 changes: 5 additions & 5 deletions src/commands/invoke/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -87,7 +87,7 @@ module.exports = async (config, cli, command) => {
outcome: 'failure',
failure_reason: '不能同时指定 data 与 path, 请检查后重试',
});
process.exit();
process.exit(1);
}

if (path || p) {
Expand All @@ -101,7 +101,7 @@ module.exports = async (config, cli, command) => {
outcome: 'failure',
failure_reason: '找不到指定的路径文件, 请检查后重试',
});
process.exit();
process.exit(1);
}
}

Expand All @@ -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;
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/commands/invoke/invoke-local/runNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/invoke/invoke-local/runPhp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/invoke/invoke-local/runPython.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ module.exports = async () => {
},
{ command: 'auto' }
);
process.exit();
process.exit(1);
}
if (Array.isArray(instances) && instances.length > 0) {
isLinkingInstance = true;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ TraceId: ${traceId}`;
console.log();
console.log(chalk.gray('可以使用 --noValidation 跳过 serverless 应用配置校验'));
if (errors.length > 0) {
process.exit();
process.exit(1);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/libs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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._;
}
Expand Down Expand Up @@ -185,7 +186,7 @@ module.exports = () => {
}
const cli = new CLI({});
cli.logError(e, { command: args._[0] });
process.exit();
process.exit(1);
}
return config;
};
6 changes: 3 additions & 3 deletions src/libs/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);

Expand Down Expand Up @@ -182,7 +182,7 @@ const standaloneUpgrade = async (options) => {
e
);

process.exit(-1);
process.exit(1);
} finally {
cliProgressFooter.updateProgress();
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/utils/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/libs/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit fa830b1

Please sign in to comment.