Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add exit code #21

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Changelog
## v1.8.0
- Add exit code to CLI in case of error.

## v1.7.0
- Add CLI paramerter `--use-env` to automatically load environment variables as variables ([#17](https://github.com/qetza/replacetokens/issues/17)).
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ Example: `'@**/*.(json|yaml);!vars.local.json' '$VARS' '{ "var1": "inline", "var

Show version number.

### Exit codes
The replacetokens CLI returns an exit code to indicate whether it succeeded or not.

| Exit code | Description |
|-|-|
| `0` | Completed successfully. |
| `1` | At least on error appended. |

## API
Install replace tokens as a module:
```
Expand Down
48 changes: 24 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qetza/replacetokens",
"version": "1.7.0",
"version": "1.8.0",
"description": "replace tokens in files",
"author": "Guillaume ROUCHON",
"license": "MIT",
Expand Down
4 changes: 3 additions & 1 deletion src/bin/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function run() {
// parse arguments
var argv = await yargs(process.argv.slice(2))
.scriptName('replacetokens')
.version('1.7.0')
.version('1.8.0')
.usage('$0 [args]')
.help()
.options({
Expand Down Expand Up @@ -168,6 +168,8 @@ export async function run() {
_warn.apply(null, args);
};
console.error = function (...args) {
process.exitCode = 1; // mark process failed on any error log attempt, don't stop execution

if (logLevel > LogLevels.Error) return;

if (logColor) {
Expand Down
102 changes: 102 additions & 0 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,4 +919,106 @@ describe('run', () => {
await fs.rm(tmp, { force: true, recursive: true });
}
});

it('exit code: success', async () => {
// arrange
const exitCode = process.exitCode;

await fs.mkdir(tmp, { recursive: true });

process.env.REPLACETOKENS_TESTS_VARS = '{ "var3": "env" }';

try {
process.exitCode = 0;

let input = path.join(tmp, 'file.txt');
await fs.copyFile(path.join(data, 'file.txt'), input);

input = path.resolve(input);

jest.replaceProperty(process, 'argv', [
'node',
'index.js',
'--sources',
input.replace(/\\/g, '/'),
'--variables',
'{ "var1": "args" } // comment',
'@**/vars.(json|yml|yaml)',
'@**/var.jsonc',
'$REPLACETOKENS_TESTS_VARS',
'["array", { "var4": "array" }]',
'{ "VAR_YML2": "inline", "var_yaml1": "inline" }',
'--root',
data,
'--log-level',
'debug'
]);

loadVariablesSpy.mockRestore();
replaceTokensSpy.mockRestore();

// act
await run();

// assert
expect(process.exitCode).toEqual(0);
} finally {
process.exitCode = exitCode;

delete process.env.REPLACETOKENS_TESTS_VARS;

await fs.rm(tmp, { force: true, recursive: true });
}
});

it('exit code: failure', async () => {
// arrange
const exitCode = process.exitCode;

await fs.mkdir(tmp, { recursive: true });

try {
process.exitCode = 0;

let input = path.join(tmp, 'file.txt');
await fs.copyFile(path.join(data, 'file.txt'), input);

input = path.resolve(input);

jest.replaceProperty(process, 'argv', [
'node',
'index.js',
'--sources',
input.replace(/\\/g, '/'),
'--variables',
'{ "var1": "args" } // comment',
'@**/vars.(json|yml|yaml)',
'@**/var.jsonc',
'$REPLACETOKENS_TESTS_VARS',
'["array", { "var4": "array" }]',
'{ "VAR_YML2": "inline", "var_yaml1": "inline" }',
'--root',
data,
'--log-level',
'off',
'--missing-var-log',
'error'
]);

loadVariablesSpy.mockRestore();
replaceTokensSpy.mockRestore();

// act
await run();

// assert
expect(process.exitCode).toEqual(1);
} finally {
process.exitCode = exitCode;

delete process.env.REPLACETOKENS_TESTS_VARS;

await fs.rm(tmp, { force: true, recursive: true });
}
});
});