balena-cli/lib/app-oclif.ts
Lucian 4d389bb6cc Implement full command testing, beginning with "balena version"
This also modifies the core CLI to be fed command programatically, which
is useful for being able to do thing like mock endpoints with tools like
"nock", and provide an easier debugging experience.
The tests utilise a "runCommand" helper that intercepts and captures
stdout/stderr writes and returns them once the command has finished
running. At this point the test implementation can parse the
stdout/stderr logs and assess nock interceptions to determine if the
command ran correctly.
This change also homogenises debug messages to start with `[debug]`,
however this is not strictly enforced by linting rules.

Change-type: minor
Signed-off-by: Lucian <lucian.buzzo@gmail.com>
2019-08-12 14:50:41 +01:00

54 lines
1.4 KiB
TypeScript

/**
* @license
* Copyright 2019 Balena Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Main } from '@oclif/command';
import { ExitError } from '@oclif/errors';
import { AppOptions } from './app';
import { handleError } from './errors';
class CustomMain extends Main {
protected _helpOverride(): boolean {
// Disable oclif's default handler for the 'version' command
if (['-v', '--version', 'version'].includes(this.argv[0])) {
return false;
} else {
return super._helpOverride();
}
}
}
/**
* oclif CLI entrypoint
*/
export function run(command: string[], options: AppOptions) {
return CustomMain.run(command).then(
() => {
if (!options.noFlush) {
return require('@oclif/command/flush');
}
},
(error: Error) => {
// oclif sometimes exits with ExitError code 0 (not an error)
if (error instanceof ExitError && error.oclif.exit === 0) {
return;
}
handleError(error);
},
);
}