Skip to content

Commit

Permalink
feat(shell): accept pathed name
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd committed Sep 27, 2018
1 parent 5071856 commit 7e6c713
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
35 changes: 33 additions & 2 deletions packages/@ionic/cli-framework/src/utils/__tests__/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,21 @@ describe('@ionic/cli-framework', () => {
jest.resetAllMocks();
});

it('should set name and args attributes', async () => {
it('should set attributes', async () => {
const name = 'cmd';
const args = ['foo', 'bar', 'baz'];
const cmd = new ShellCommand(name, args);
expect(cmd.name).toEqual(name);
expect(cmd.path).not.toBeDefined();
expect(cmd.args).toEqual(args);
});

it('should set attributes for pathed name', async () => {
const name = path.resolve('/path', 'to', 'cmd');
const args = ['foo', 'bar', 'baz'];
const cmd = new ShellCommand(name, args);
expect(cmd.name).toEqual('cmd');
expect(cmd.path).toEqual(name);
expect(cmd.args).toEqual(args);
});

Expand Down Expand Up @@ -111,6 +121,14 @@ describe('@ionic/cli-framework', () => {
expect(result).toEqual('cmd foo bar baz');
});

it('should bashify command and args with pathed name', async () => {
const name = path.resolve('/path', 'to', 'cmd');
const args = ['foo', 'bar', 'baz'];
const cmd = new ShellCommand(name, args);
const result = cmd.bashify();
expect(result).toEqual('cmd foo bar baz');
});

it('should bashify command and args with spaces', async () => {
const name = 'cmd';
const args = ['foo bar baz'];
Expand All @@ -127,7 +145,7 @@ describe('@ionic/cli-framework', () => {
expect(result).toEqual('cmd "foo \\"bar\\" baz"');
});

it('should call spawn with correct args and return child process', async () => {
it('should call spawn with correct program/args', async () => {
const result = {};
mockCrossSpawn.mockImplementation(() => result);
const name = 'cmd';
Expand All @@ -140,6 +158,19 @@ describe('@ionic/cli-framework', () => {
expect(mockCrossSpawn).toHaveBeenCalledWith(name, args, expectedOptions);
});

it('should call spawn with correct program/args with pathed name', async () => {
const result = {};
mockCrossSpawn.mockImplementation(() => result);
const name = path.resolve('/path', 'to', 'cmd');
const args = ['foo', 'bar', 'baz'];
const options = { env: { PATH: '' } };
const cmd = new ShellCommand(name, args, options);
const expectedOptions = { env: createProcessEnv(options.env) };
expect(cmd.spawn()).toBe(result);
expect(mockCrossSpawn).toHaveBeenCalledTimes(1);
expect(mockCrossSpawn).toHaveBeenCalledWith(name, args, expectedOptions);
});

it('should pipe stdout and stderr in run()', async () => {
const cmd = new ShellCommand('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
Expand Down
10 changes: 9 additions & 1 deletion packages/@ionic/cli-framework/src/utils/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ export function expandTildePath(p: string) {
export interface ShellCommandOptions extends SpawnOptions {}

export class ShellCommand {
protected readonly path?: string;
protected _options: SpawnOptions;

constructor(public name: string, public args: ReadonlyArray<string>, options: ShellCommandOptions = {}) {
const i = name.lastIndexOf(path.sep);

if (i >= 0) {
this.name = name.substring(i + 1);
this.path = name;
}

this._options = options;
}

Expand Down Expand Up @@ -129,7 +137,7 @@ export class ShellCommand {
}

spawn(): ChildProcess {
return spawn(this.name, this.args, this.options);
return spawn(this.path ? this.path : this.name, this.args, this.options);
}

bashify(): string {
Expand Down

0 comments on commit 7e6c713

Please sign in to comment.