Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Commit

Permalink
feat(cli): exit with error if command is not found SHELL-1570
Browse files Browse the repository at this point in the history
  • Loading branch information
KalleV committed Jul 10, 2018
1 parent c8f3b6a commit 872f0d8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
25 changes: 12 additions & 13 deletions lib/cli/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import flatiron = require('flatiron')
import path = require('path')
import _ = require('lodash')
import {isPackageSync} from "./utils"
import {checkVersion} from "../check-self-update"
import labShare from '../labshare'
import loaderPlugin = require('./loader-plugin')

const {app} = flatiron,
cwd = process.cwd(),
lscRoot = path.join(__dirname, '..', '..');
const {app} = flatiron;
const cwd = process.cwd();
const lscRoot = path.join(__dirname, '..', '..');

export interface IStartOptions {
directories?: string[]
Expand All @@ -31,12 +30,12 @@ interface IPackageJson {
* @param {string} pattern - The CLI module pattern to search for (glob syntax)
* @param {Array<Function>} initModules - Array of custom initializer functions
*/
export function start({
main = cwd,
directories = [lscRoot],
pattern = '{src/cli,cli}/*.js',
initFunctions = []
}: IStartOptions) {
export async function start({
main = cwd,
directories = [lscRoot],
pattern = '{src/cli,cli}/*.js',
initFunctions = []
}: IStartOptions) {
let pkg: IPackageJson;

checkVersion({name: 'lsc', logger: app.log});
Expand Down Expand Up @@ -77,9 +76,9 @@ export function start({

global.LabShare = labShare;

app.start(error => {
if (_.isError(error)) {
app.log.error(error.stack);
app.start((error) => {
if (error) {
app.log.error(error.stack || 'Command not found!');
process.exit(1);
}
});
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"snyk-protect": "snyk protect",
"prepublish": "npm run snyk-protect",
"semantic-release": "semantic-release",
"prepare": "npm run snyk-protect"
"prepare": "npm run snyk-protect",
"lsc": "lsc"
},
"bin": {
"lsc": "./lib/bin/lsc.js"
Expand Down Expand Up @@ -66,16 +67,16 @@
"devDependencies": {
"@commitlint/cli": "^7.0.0",
"@commitlint/config-conventional": "^7.0.1",
"@labshare/semantic-release-config": "^1.0.0",
"@types/jasmine": "^2.6.0",
"coveralls": "^3.0.1",
"husky": "^0.14.3",
"istanbul": "^0.4.2",
"jasmine": "3.1.0",
"jasmine-core": "^3.1.0",
"mock-fs": "^4.4.1",
"typescript": "^2.5.3",
"semantic-release": "^15.5.0",
"@labshare/semantic-release-config": "^1.0.0"
"typescript": "^2.5.3"
},
"snyk": true,
"release": {
Expand Down
30 changes: 30 additions & 0 deletions test/lib/unit/bin/lsc_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {exec} from 'child_process';
import * as path from "path";
import {getPackageManifest} from "../../../../lib/cli";

describe('LSC', () => {

const cwd = path.join(__dirname, '..', '..', '..', '..');
const binPath = getPackageManifest(cwd).bin.lsc;

it('runs commands', (done: DoneFn) => {
exec(`node ${binPath} --version`, {cwd}, (error: Error) => {
if (error) {
done.fail(error);
return;
}

done();
});
});

it('exits with an error if the command is not recognized', (done: DoneFn) => {
exec(`node ${binPath}`, {cwd}, (error: any) => {
expect(error.message).toContain('Command not found');
expect(error.code).not.toBe(0);

done();
});
});

});

0 comments on commit 872f0d8

Please sign in to comment.