Compare commits
34 Commits
Author | SHA1 | Date | |
---|---|---|---|
a6f164602c | |||
f8457ad58d | |||
8ceac74a1f | |||
f63c4456bf | |||
32c33cad5b | |||
0758e58fa4 | |||
e981ca6bdd | |||
2853a48708 | |||
389f8f4a52 | |||
c1a537f052 | |||
4701341c0c | |||
d5d347721a | |||
6a047d6333 | |||
6e7b4dd18e | |||
debc5f1524 | |||
8d03561762 | |||
f516a6e753 | |||
1c4f6a8bf9 | |||
4fe3775904 | |||
dcee2d6233 | |||
39d457382f | |||
e6ed457501 | |||
0a6484762c | |||
baa3e4e6e9 | |||
0d68361381 | |||
286d80328c | |||
2a71f00ab0 | |||
80a44a43cb | |||
5ea780acda | |||
7c9cf6e70d | |||
697f789b55 | |||
f452964779 | |||
f2db8dc41f | |||
2ca593297c |
@ -26,6 +26,7 @@ mirror:
|
|||||||
snyk:
|
snyk:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci command npm install -g snyk
|
- npmci command npm install -g snyk
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command snyk test
|
||||||
@ -36,21 +37,11 @@ snyk:
|
|||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install lts
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
@ -62,6 +53,7 @@ testLTS:
|
|||||||
testSTABLE:
|
testSTABLE:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
@ -117,8 +109,10 @@ pages:
|
|||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-node:npmci
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g npmpage
|
- npmci command npm install -g typedoc typescript
|
||||||
- npmci command npmpage
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
@ -129,13 +123,3 @@ pages:
|
|||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
|
||||||
windowsCompatibility:
|
|
||||||
image: stefanscherer/node-windows:10-build-tools
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npm install & npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- windows
|
|
||||||
allow_failure: true
|
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
# smartshell
|
|
||||||
|
|
||||||
shell actions designed as promises
|
|
||||||
|
|
||||||
## Availabililty
|
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/smartshell)
|
|
||||||
[](https://GitLab.com/pushrocks/smartshell)
|
|
||||||
[](https://github.com/pushrocks/smartshell)
|
|
||||||
[](https://pushrocks.gitlab.io/smartshell/)
|
|
||||||
|
|
||||||
## Status for master
|
|
||||||
|
|
||||||
[](https://GitLab.com/pushrocks/smartshell/commits/master)
|
|
||||||
[](https://GitLab.com/pushrocks/smartshell/commits/master)
|
|
||||||
[](https://www.npmjs.com/package/smartshell)
|
|
||||||
[](https://david-dm.org/pushrocks/smartshell)
|
|
||||||
[](https://www.bithound.io/github/pushrocks/smartshell/master/dependencies/npm)
|
|
||||||
[](https://www.bithound.io/github/pushrocks/smartshell)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](http://standardjs.com/)
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
||||||
|
|
||||||
[](https://push.rocks)
|
|
@ -1,9 +1,7 @@
|
|||||||
{
|
{
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [
|
"npmGlobalTools": [],
|
||||||
"npmts",
|
"npmAccessLevel": "public",
|
||||||
"ts-node"
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
],
|
|
||||||
"npmAccessLevel": "public"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
1303
package-lock.json
generated
1303
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartshell",
|
"name": "@pushrocks/smartshell",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "1.0.21",
|
"version": "2.0.16",
|
||||||
"description": "shell actions designed as promises",
|
"description": "shell actions designed as promises",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tsrun test/test.ts)",
|
"test": "(tstest test/)",
|
||||||
"build": "(npmts)"
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -24,12 +24,17 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartshell#README",
|
"homepage": "https://gitlab.com/pushrocks/smartshell#README",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsrun": "^1.1.9",
|
"@gitzone/tsbuild": "^2.1.11",
|
||||||
"@pushrocks/tapbundle": "^3.0.1",
|
"@gitzone/tsrun": "^1.2.6",
|
||||||
"@types/node": "^10.5.2"
|
"@gitzone/tstest": "^1.0.20",
|
||||||
|
"@pushrocks/tapbundle": "^3.0.9",
|
||||||
|
"@types/node": "^12.0.2",
|
||||||
|
"tslint": "^5.16.0",
|
||||||
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartexit": "^1.0.8",
|
||||||
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"@types/which": "^1.3.1",
|
"@types/which": "^1.3.1",
|
||||||
"which": "^1.3.1"
|
"which": "^1.3.1"
|
||||||
}
|
}
|
||||||
|
58
test/test.ts
58
test/test.ts
@ -1,37 +1,10 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
import * as smartshell from '../ts/index';
|
import * as smartshell from '../ts';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
let testSmartshell: smartshell.Smartshell;
|
let testSmartshell: smartshell.Smartshell;
|
||||||
|
|
||||||
tap.test('smartshell should run async', async () => {
|
|
||||||
let execResult = await smartshell.exec('npm -v');
|
|
||||||
expect(execResult.stdout).to.match(/[0-9\.]*/);
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('smartshell should run async and silent', async () => {
|
|
||||||
let execResult = await smartshell.execSilent('npm -v');
|
|
||||||
expect(execResult.stdout).to.match(/[0-9\.]*/);
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('smartshell should stream a shell execution', async () => {
|
|
||||||
let done = smartpromise.defer();
|
|
||||||
let execStreamingResponse = smartshell.execStreaming('npm -v');
|
|
||||||
execStreamingResponse.childProcess.stdout.on('data', data => {
|
|
||||||
done.resolve(data);
|
|
||||||
});
|
|
||||||
let data = await done.promise;
|
|
||||||
expect(data).to.match(/[0-9\.]*/);
|
|
||||||
await execStreamingResponse.finalPromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('it should execute and wait for a line in the output', async () => {
|
|
||||||
await smartshell.execAndWaitForLine('echo "5.0.4"', /5.0.4/);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Smartshell class
|
|
||||||
|
|
||||||
tap.test('smartshell should create a Smartshell instance', async () => {
|
tap.test('smartshell should create a Smartshell instance', async () => {
|
||||||
testSmartshell = new smartshell.Smartshell({
|
testSmartshell = new smartshell.Smartshell({
|
||||||
executor: 'bash',
|
executor: 'bash',
|
||||||
@ -40,12 +13,41 @@ tap.test('smartshell should create a Smartshell instance', async () => {
|
|||||||
expect(testSmartshell).to.be.instanceof(smartshell.Smartshell);
|
expect(testSmartshell).to.be.instanceof(smartshell.Smartshell);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('smartshell should run async', async () => {
|
||||||
|
let execResult = await testSmartshell.exec('npm -v');
|
||||||
|
expect(execResult.stdout).to.match(/[0-9\.]*/);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('smartshell should run async and silent', async () => {
|
||||||
|
let execResult = await testSmartshell.execSilent('npm -v');
|
||||||
|
expect(execResult.stdout).to.match(/[0-9\.]*/);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('smartshell should stream a shell execution', async () => {
|
||||||
|
let done = smartpromise.defer();
|
||||||
|
let execStreamingResponse = await testSmartshell.execStreaming('npm -v');
|
||||||
|
execStreamingResponse.childProcess.stdout.on('data', data => {
|
||||||
|
done.resolve(data);
|
||||||
|
});
|
||||||
|
let data = await done.promise;
|
||||||
|
expect(data).to.match(/[0-9\.]*/);
|
||||||
|
await execStreamingResponse.finalPromise;
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('it should execute and wait for a line in the output', async () => {
|
||||||
|
await testSmartshell.execAndWaitForLine('echo "5.0.4"', /5.0.4/);
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('smartshell should run async', async () => {
|
tap.test('smartshell should run async', async () => {
|
||||||
return testSmartshell.execSilent('sleep 1 && npm -v').then(async execResult => {
|
return testSmartshell.execSilent('sleep 1 && npm -v').then(async execResult => {
|
||||||
console.log(execResult.stdout);
|
console.log(execResult.stdout);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to find git', async () => {
|
||||||
|
testSmartshell.exec('git --version');
|
||||||
|
});
|
||||||
|
|
||||||
tap.start({
|
tap.start({
|
||||||
throwOnError: true
|
throwOnError: true
|
||||||
});
|
});
|
||||||
|
@ -1,2 +1 @@
|
|||||||
export * from './smartshell.wrap';
|
|
||||||
export * from './smartshell.classes.smartshell';
|
export * from './smartshell.classes.smartshell';
|
||||||
|
103
ts/smartshell.classes.shellenv.ts
Normal file
103
ts/smartshell.classes.shellenv.ts
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
export type TExecutor = 'sh' | 'bash';
|
||||||
|
|
||||||
|
export interface IShellEnvContructorOptions {
|
||||||
|
executor: TExecutor;
|
||||||
|
sourceFilePaths?: string[];
|
||||||
|
pathDirectories?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ShellEnv {
|
||||||
|
executor: TExecutor;
|
||||||
|
sourceFileArray: string[] = [];
|
||||||
|
pathDirArray: string[] = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* constructor for the shellenv
|
||||||
|
*/
|
||||||
|
constructor(optionsArg: IShellEnvContructorOptions) {
|
||||||
|
this.executor = optionsArg.executor;
|
||||||
|
|
||||||
|
// add sourcefiles
|
||||||
|
if (optionsArg.sourceFilePaths) {
|
||||||
|
this.sourceFileArray = this.sourceFileArray.concat(optionsArg.sourceFilePaths);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add pathDirectories
|
||||||
|
if (optionsArg.pathDirectories) {
|
||||||
|
this.pathDirArray = this.pathDirArray.concat(optionsArg.pathDirectories);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* imports path into the shell from env if available and returns it with
|
||||||
|
*/
|
||||||
|
private _setPath(commandStringArg): string {
|
||||||
|
let commandResult = commandStringArg;
|
||||||
|
let commandPaths: string[] = [];
|
||||||
|
commandPaths = commandPaths.concat(process.env.PATH.split(':'));
|
||||||
|
if (process.env.SMARTSHELL_PATH) {
|
||||||
|
commandPaths = commandPaths.concat(process.env.SMARTSHELL_PATH.split(':'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// lets filter for unwanted paths
|
||||||
|
// Windows WSL
|
||||||
|
commandPaths = commandPaths.filter(commandPathArg => {
|
||||||
|
const filterResult =
|
||||||
|
!commandPathArg.startsWith('/mnt/c/') &&
|
||||||
|
!commandPathArg.startsWith('Files/1E') &&
|
||||||
|
!commandPathArg.includes(' ');
|
||||||
|
if (!filterResult) {
|
||||||
|
// console.log(`${commandPathArg} will be filtered!`);
|
||||||
|
}
|
||||||
|
return filterResult;
|
||||||
|
});
|
||||||
|
|
||||||
|
commandResult = `PATH=${commandPaths.join(':')} && ${commandStringArg}`;
|
||||||
|
return commandResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add files that are going to be sourced when running a command
|
||||||
|
* @param sourceFilePathsArray
|
||||||
|
*/
|
||||||
|
addSourceFiles(sourceFilePathsArray: string[]) {
|
||||||
|
for (let sourceFilePath of sourceFilePathsArray) {
|
||||||
|
this.sourceFileArray.push(sourceFilePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cleans the source files array
|
||||||
|
*/
|
||||||
|
cleanSourceFiles() {
|
||||||
|
this.sourceFileArray = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public createEnvExecString(commandArg: string): string {
|
||||||
|
let commandResult = '';
|
||||||
|
let sourceString = '';
|
||||||
|
|
||||||
|
// deal with sourcestring
|
||||||
|
for (const sourceFilePath of this.sourceFileArray) {
|
||||||
|
sourceString = sourceString + `source ${sourceFilePath} && `;
|
||||||
|
}
|
||||||
|
|
||||||
|
// deal with avaiable path
|
||||||
|
let pathString = 'PATH=$PATH';
|
||||||
|
for (const pathDir of this.pathDirArray) {
|
||||||
|
pathString += `:${pathDir}`;
|
||||||
|
}
|
||||||
|
pathString += ` && `;
|
||||||
|
|
||||||
|
switch (this.executor) {
|
||||||
|
case 'bash':
|
||||||
|
commandResult = `bash -c '${pathString}${sourceString}${commandArg}'`;
|
||||||
|
break;
|
||||||
|
case 'sh':
|
||||||
|
commandResult = `${pathString}${sourceString}${commandArg}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
commandResult = this._setPath(commandResult);
|
||||||
|
return commandResult;
|
||||||
|
}
|
||||||
|
}
|
38
ts/smartshell.classes.shelllog.ts
Normal file
38
ts/smartshell.classes.shelllog.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import * as plugins from './smartshell.plugins';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a log handler for spawned logs
|
||||||
|
* making sure the process doesn't run out of memory
|
||||||
|
*/
|
||||||
|
export class ShellLog {
|
||||||
|
logStore = Buffer.from('');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* log data to console
|
||||||
|
* @param dataArg
|
||||||
|
*/
|
||||||
|
logToConsole(dataArg: string | Buffer): void {
|
||||||
|
// make sure we have the data as string
|
||||||
|
process.stdout.write(dataArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add data to Buffer for later consumption
|
||||||
|
* @param dataArg
|
||||||
|
*/
|
||||||
|
addToBuffer(dataArg: string | Buffer): void {
|
||||||
|
// make sure we have the data as Buffer
|
||||||
|
const dataBuffer: Buffer = (() => {
|
||||||
|
if (!Buffer.isBuffer(dataArg)) {
|
||||||
|
return Buffer.from(dataArg);
|
||||||
|
}
|
||||||
|
return dataArg;
|
||||||
|
})();
|
||||||
|
this.logStore = Buffer.concat([this.logStore, dataBuffer]);
|
||||||
|
}
|
||||||
|
|
||||||
|
logAndAdd(dataArg: string | Buffer): void {
|
||||||
|
this.logToConsole(dataArg);
|
||||||
|
this.addToBuffer(dataArg);
|
||||||
|
}
|
||||||
|
}
|
@ -1,64 +1,158 @@
|
|||||||
|
// -- imports --
|
||||||
import * as plugins from './smartshell.plugins';
|
import * as plugins from './smartshell.plugins';
|
||||||
import * as smartshellWrap from './smartshell.wrap';
|
import { ShellEnv, IShellEnvContructorOptions, TExecutor } from './smartshell.classes.shellenv';
|
||||||
|
import { ShellLog } from './smartshell.classes.shelllog';
|
||||||
|
|
||||||
export type TExecutor = 'sh' | 'bash';
|
import * as cp from 'child_process';
|
||||||
|
import { Deferred } from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
export interface ISmartshellContructorOptions {
|
// -- interfaces --
|
||||||
executor: TExecutor;
|
/**
|
||||||
sourceFilePaths: string[];
|
* interface for ExecResult
|
||||||
|
*/
|
||||||
|
export interface IExecResult {
|
||||||
|
exitCode: number;
|
||||||
|
stdout: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* interface for streaming ExecResult
|
||||||
|
*/
|
||||||
|
export interface IExecResultStreaming {
|
||||||
|
childProcess: cp.ChildProcess;
|
||||||
|
finalPromise: Promise<IExecResult>;
|
||||||
|
kill: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- SmartShell --
|
||||||
export class Smartshell {
|
export class Smartshell {
|
||||||
executor: TExecutor;
|
public shellEnv: ShellEnv;
|
||||||
sourceFileArray: string[] = [];
|
public smartexit = new plugins.smartexit.SmartExit();
|
||||||
constructor(optionsArg: ISmartshellContructorOptions) {
|
|
||||||
this.executor = optionsArg.executor;
|
|
||||||
for (let sourceFilePath of optionsArg.sourceFilePaths) {
|
|
||||||
this.sourceFileArray.push(sourceFilePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
addSourceFiles(sourceFilePathsArray: string[]) {
|
constructor(optionsArg: IShellEnvContructorOptions) {
|
||||||
for (let sourceFilePath of sourceFilePathsArray) {
|
this.shellEnv = new ShellEnv(optionsArg);
|
||||||
this.sourceFileArray.push(sourceFilePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanSourceFiles() {
|
|
||||||
this.sourceFileArray = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* executes silently and returns IExecResult
|
* executes a given command async
|
||||||
* @param commandArg
|
* @param commandStringArg
|
||||||
*/
|
*/
|
||||||
async execSilent(commandArg: string) {
|
private async _exec(
|
||||||
let execCommand = this.createExecString(commandArg);
|
commandStringArg: string,
|
||||||
return await smartshellWrap.execSilent(execCommand);
|
silentArg: boolean = false,
|
||||||
}
|
strictArg = false,
|
||||||
|
streamingArg = false
|
||||||
|
): Promise<IExecResult | IExecResultStreaming> {
|
||||||
|
// flow control promises
|
||||||
|
const done = plugins.smartpromise.defer<IExecResult | IExecResultStreaming>();
|
||||||
|
const childProcessEnded = plugins.smartpromise.defer<IExecResult>();
|
||||||
|
// build commandToExecute
|
||||||
|
let commandToExecute = commandStringArg;
|
||||||
|
commandToExecute = this.shellEnv.createEnvExecString(commandStringArg);
|
||||||
|
const spawnlogInstance = new ShellLog();
|
||||||
|
const execChildProcess = cp.spawn(commandToExecute, [], {
|
||||||
|
shell: true,
|
||||||
|
env: process.env,
|
||||||
|
detached: true
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
this.smartexit.addProcess(execChildProcess);
|
||||||
* executes and returns IExecResult
|
|
||||||
* @param commandArg
|
|
||||||
*/
|
|
||||||
async exec(commandArg: string) {
|
|
||||||
let execCommand = this.createExecString(commandArg);
|
|
||||||
return await smartshellWrap.exec(execCommand);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
execChildProcess.stdout.on('data', data => {
|
||||||
* creates the final sourcing string
|
if (!silentArg) {
|
||||||
* @param commandArg
|
spawnlogInstance.logToConsole(data);
|
||||||
*/
|
|
||||||
private createExecString(commandArg): string {
|
|
||||||
if (this.executor === 'bash') {
|
|
||||||
let sourceString = '';
|
|
||||||
for (let sourceFilePath of this.sourceFileArray) {
|
|
||||||
sourceString = sourceString + `source ${sourceFilePath} && `;
|
|
||||||
}
|
}
|
||||||
return `bash -c '${sourceString} ${commandArg}'`;
|
spawnlogInstance.addToBuffer(data);
|
||||||
} else {
|
});
|
||||||
return commandArg;
|
execChildProcess.stderr.on('data', data => {
|
||||||
|
if (!silentArg) {
|
||||||
|
spawnlogInstance.logToConsole(data);
|
||||||
|
}
|
||||||
|
spawnlogInstance.addToBuffer(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (streamingArg) {
|
||||||
|
done.resolve({
|
||||||
|
childProcess: execChildProcess,
|
||||||
|
finalPromise: childProcessEnded.promise,
|
||||||
|
kill: () => {
|
||||||
|
// this notation with the - kills the whole process group
|
||||||
|
process.kill(-execChildProcess.pid);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
execChildProcess.on('exit', (code, signal) => {
|
||||||
|
this.smartexit.removeProcess(execChildProcess);
|
||||||
|
if (strictArg && code === 1) {
|
||||||
|
done.reject();
|
||||||
|
}
|
||||||
|
|
||||||
|
const execResult = {
|
||||||
|
exitCode: code,
|
||||||
|
stdout: spawnlogInstance.logStore.toString()
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!streamingArg) {
|
||||||
|
done.resolve(execResult);
|
||||||
|
}
|
||||||
|
childProcessEnded.resolve(execResult);
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await done.promise;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async exec(commandStringArg: string): Promise<IExecResult> {
|
||||||
|
return (await this._exec(commandStringArg, false)) as IExecResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* executes a given command async and silent
|
||||||
|
* @param commandStringArg
|
||||||
|
*/
|
||||||
|
async execSilent(commandStringArg: string): Promise<IExecResult> {
|
||||||
|
return (await this._exec(commandStringArg, true)) as IExecResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* executes a command async and strict, meaning it rejects the promise if something happens
|
||||||
|
*/
|
||||||
|
async execStrict(commandStringArg: string): Promise<IExecResult> {
|
||||||
|
return (await this._exec(commandStringArg, true, true)) as IExecResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* executes a command and allows you to stream output
|
||||||
|
*/
|
||||||
|
async execStreaming(
|
||||||
|
commandStringArg: string,
|
||||||
|
silentArg: boolean = false
|
||||||
|
): Promise<IExecResultStreaming> {
|
||||||
|
return (await this._exec(commandStringArg, silentArg, false, true)) as IExecResultStreaming;
|
||||||
|
}
|
||||||
|
|
||||||
|
async execStreamingSilent(commandStringArg: string) {
|
||||||
|
return (await this.execStreaming(commandStringArg, true)) as IExecResultStreaming;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* executes a command and returns promise that will be fullfilled once an putput line matches RegexArg
|
||||||
|
* @param commandStringArg
|
||||||
|
* @param regexArg
|
||||||
|
*/
|
||||||
|
async execAndWaitForLine(commandStringArg: string, regexArg: RegExp, silentArg: boolean = false) {
|
||||||
|
let done = plugins.smartpromise.defer();
|
||||||
|
let execStreamingResult = await this.execStreaming(commandStringArg, silentArg);
|
||||||
|
execStreamingResult.childProcess.stdout.on('data', (stdOutChunk: string) => {
|
||||||
|
if (regexArg.test(stdOutChunk)) {
|
||||||
|
done.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
async execAndWaitForLineSilent(commandStringArg: string, regexArg: RegExp) {
|
||||||
|
this.execAndWaitForLine(commandStringArg, regexArg, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
|
import * as smartexit from '@pushrocks/smartexit';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as which from 'which';
|
import * as which from 'which';
|
||||||
|
|
||||||
export { smartpromise, which };
|
export { smartexit, smartpromise, which };
|
||||||
|
@ -1,204 +0,0 @@
|
|||||||
import * as plugins from "./smartshell.plugins";
|
|
||||||
|
|
||||||
// interfaces
|
|
||||||
import * as cp from "child_process";
|
|
||||||
import { Deferred } from "@pushrocks/smartpromise";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* interface for ExecResult
|
|
||||||
*/
|
|
||||||
export interface IExecResult {
|
|
||||||
exitCode: number;
|
|
||||||
stdout: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* interface for streaming ExecResult
|
|
||||||
*/
|
|
||||||
export interface IExecResultStreaming {
|
|
||||||
childProcess: cp.ChildProcess;
|
|
||||||
finalPromise: Promise<IExecResult>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* imports path into the shell from env if available and returns it with
|
|
||||||
*/
|
|
||||||
let importEnvVarPath = (stringArg): string => {
|
|
||||||
if (process.env.SMARTSHELL_PATH) {
|
|
||||||
let commandResult = `PATH=${process.env.SMARTSHELL_PATH} && ${stringArg}`;
|
|
||||||
// console.log(commandResult)
|
|
||||||
return commandResult;
|
|
||||||
} else {
|
|
||||||
return stringArg;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* executes a given command async
|
|
||||||
* @param commandStringArg
|
|
||||||
*/
|
|
||||||
export let exec = (
|
|
||||||
commandStringArg: string,
|
|
||||||
silentArg: boolean = false,
|
|
||||||
strictArg = false
|
|
||||||
): Promise<IExecResult> => {
|
|
||||||
let done = plugins.smartpromise.defer<IExecResult>();
|
|
||||||
const commandToExecute = importEnvVarPath(commandStringArg);
|
|
||||||
try {
|
|
||||||
const execChildProcess = cp.exec(commandToExecute, {
|
|
||||||
timeout: null,
|
|
||||||
maxBuffer: 1000000,
|
|
||||||
env: process.env
|
|
||||||
});
|
|
||||||
|
|
||||||
let logStore = "";
|
|
||||||
|
|
||||||
execChildProcess.stdout.on("data", (data: string) => {
|
|
||||||
if (!silentArg) {
|
|
||||||
console.log(data);
|
|
||||||
}
|
|
||||||
logStore += data;
|
|
||||||
});
|
|
||||||
execChildProcess.stderr.on("data", data => {
|
|
||||||
if (!silentArg) {
|
|
||||||
console.log(data);
|
|
||||||
}
|
|
||||||
logStore += data;
|
|
||||||
});
|
|
||||||
execChildProcess.on("exit", (code, signal) => {
|
|
||||||
done.resolve({
|
|
||||||
exitCode: code,
|
|
||||||
stdout: logStore
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
const error = e;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*plugins.shelljs.exec(importEnvVarPath(commandStringArg), { async: true, silent: silentArg }, (code, stdout, stderr) => {
|
|
||||||
if (
|
|
||||||
stderr
|
|
||||||
&& (stderr !== '')
|
|
||||||
&& (!silentArg || strictArg)
|
|
||||||
&& (process.env.DEBUG === 'true')
|
|
||||||
) {
|
|
||||||
console.log('StdErr found.')
|
|
||||||
console.log(stderr)
|
|
||||||
}
|
|
||||||
if (strictArg) {
|
|
||||||
done.reject(new Error(stderr))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
done.resolve({
|
|
||||||
exitCode: code,
|
|
||||||
stdout: stdout
|
|
||||||
})
|
|
||||||
})*/
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* executes a given command async and silent
|
|
||||||
* @param commandStringArg
|
|
||||||
*/
|
|
||||||
export let execSilent = async (
|
|
||||||
commandStringArg: string
|
|
||||||
): Promise<IExecResult> => {
|
|
||||||
return await exec(commandStringArg, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* executes strict, meaning it rejects the promise if something happens
|
|
||||||
*/
|
|
||||||
export let execStrict = async (
|
|
||||||
commandStringArg: string
|
|
||||||
): Promise<IExecResult> => {
|
|
||||||
return await exec(commandStringArg, true, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* executes a command and allws you to stream output
|
|
||||||
*/
|
|
||||||
export let execStreaming = (
|
|
||||||
commandStringArg: string,
|
|
||||||
silentArg: boolean = false
|
|
||||||
) => {
|
|
||||||
let childProcessEnded = plugins.smartpromise.defer<IExecResult>();
|
|
||||||
const commandToExecute = importEnvVarPath(commandStringArg);
|
|
||||||
let execChildProcess = cp.exec(commandToExecute, {
|
|
||||||
timeout: null,
|
|
||||||
maxBuffer: 1000000,
|
|
||||||
env: process.env
|
|
||||||
});
|
|
||||||
|
|
||||||
let logStore = "";
|
|
||||||
|
|
||||||
execChildProcess.stdout.on("data", (data: string) => {
|
|
||||||
if (!silentArg) {
|
|
||||||
console.log(data);
|
|
||||||
}
|
|
||||||
logStore += data;
|
|
||||||
});
|
|
||||||
execChildProcess.stderr.on("data", data => {
|
|
||||||
if (!silentArg) {
|
|
||||||
console.log(data);
|
|
||||||
}
|
|
||||||
logStore += data;
|
|
||||||
});
|
|
||||||
execChildProcess.on("exit", (code, signal) => {
|
|
||||||
childProcessEnded.resolve({
|
|
||||||
exitCode: code,
|
|
||||||
stdout: logStore
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
childProcess: execChildProcess,
|
|
||||||
finalPromise: childProcessEnded.promise
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export let execStreamingSilent = (commandStringArg: string) => {
|
|
||||||
return execStreaming(commandStringArg, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* executes a command and returns promise that will be fullfilled once an putput line matches RegexArg
|
|
||||||
* @param commandStringArg
|
|
||||||
* @param regexArg
|
|
||||||
*/
|
|
||||||
export let execAndWaitForLine = (
|
|
||||||
commandStringArg: string,
|
|
||||||
regexArg: RegExp,
|
|
||||||
silentArg: boolean = false
|
|
||||||
) => {
|
|
||||||
let done = plugins.smartpromise.defer();
|
|
||||||
let execStreamingResult = execStreaming(commandStringArg, silentArg);
|
|
||||||
execStreamingResult.childProcess.stdout.on("data", (stdOutChunk: string) => {
|
|
||||||
if (regexArg.test(stdOutChunk)) {
|
|
||||||
done.resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
export let execAndWaitForLineSilent = (
|
|
||||||
commandStringArg: string,
|
|
||||||
regexArg: RegExp
|
|
||||||
) => {
|
|
||||||
execAndWaitForLine(commandStringArg, regexArg, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get a path
|
|
||||||
*/
|
|
||||||
export let which = (cmd: string): Promise<string> => {
|
|
||||||
let done = plugins.smartpromise.defer<string>();
|
|
||||||
plugins.which(cmd, (err, path: string) => {
|
|
||||||
if (err) {
|
|
||||||
done.reject(err);
|
|
||||||
}
|
|
||||||
done.resolve(path);
|
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
|||||||
{
|
{
|
||||||
"extends": "tslint-config-standard"
|
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||||
|
"rules": {
|
||||||
|
"semicolon": [true, "always"],
|
||||||
|
"no-console": false,
|
||||||
|
"ordered-imports": false,
|
||||||
|
"object-literal-sort-keys": false,
|
||||||
|
"member-ordering": {
|
||||||
|
"options":{
|
||||||
|
"order": [
|
||||||
|
"static-method"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"defaultSeverity": "warning"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user