Skip to content

Commit

Permalink
feat(utils/exec): passes environment variables w/ state envs and path…
Browse files Browse the repository at this point in the history
…s by default; returns a promise
  • Loading branch information
rafamel committed Apr 22, 2019
1 parent 44516dc commit 6ed1eef
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 93 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"fs-extra": "^7.0.1",
"lodash.mergewith": "^4.6.1",
"loglevel": "^1.6.1",
"manage-path": "^2.0.0",
"promist": "^0.5.3",
"slimconf": "^0.9.0",
"spawn-command": "0.0.2-1",
Expand Down
12 changes: 12 additions & 0 deletions src/@types/manage-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare module 'manage-path' {
export interface IAlterPath {
unshift(...arr: Array<string | string[]>): string;
push(...arr: Array<string | string[]>): string;
get(): string;
restore(): string;
}

export default function managePath(envs: {
[key: string]: string | undefined;
}): IAlterPath;
}
2 changes: 1 addition & 1 deletion src/commands/run/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function trunk(task: TScript): Promise<void> {
}
if (typeof task === 'string') {
logger.debug('Command exec: ' + task);
return (await exec(task)).promise;
return exec(task);
}
if (typeof task === 'function') {
logger.debug('Run function' + task.name ? ` ${task.name}` : '');
Expand Down
29 changes: 0 additions & 29 deletions src/utils/exec.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/utils/exec/get-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import state from '~/state';
import manage from 'manage-path';
import { IOfType } from '~/types';

export default async function getEnv(): Promise<IOfType<string | undefined>> {
const env: IOfType<string | undefined> = Object.assign(
{},
process.env,
state.get('env')
);
const alter = manage(env);
alter.unshift(await state.paths());

return env;
}
26 changes: 26 additions & 0 deletions src/utils/exec/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sc from 'spawn-command';
import { SpawnOptions } from 'child_process';
import { DEFAULT_STDIO } from '~/constants';
import logger from '~/utils/logger';
import { rejects } from 'errorish';
import getEnv from './get-env';

export default async function exec(
command: string,
options?: SpawnOptions
): Promise<void> {
const opts: SpawnOptions = Object.assign({}, options);

if (!opts.stdio) opts.stdio = DEFAULT_STDIO;
if (!opts.env) opts.env = await getEnv();

logger.debug('Executing: ' + command);
const ps = sc(command, opts);

return new Promise((resolve: (arg: void) => void, reject) => {
ps.on('close', (code: number) => {
return code ? reject(Error(`Failed: ${command}`)) : resolve();
});
ps.on('error', (err: any) => reject(err));
}).catch(rejects);
}
61 changes: 0 additions & 61 deletions test/utils/exec.test.ts

This file was deleted.

0 comments on commit 6ed1eef

Please sign in to comment.