fix(dependencies): remove shelljs and go node native
This commit is contained in:
@ -1,2 +1,2 @@
|
||||
export * from './smartshell.wrap'
|
||||
export * from './smartshell.classes.smartshell'
|
||||
export * from './smartshell.wrap';
|
||||
export * from './smartshell.classes.smartshell';
|
||||
|
@ -1,65 +1,64 @@
|
||||
import * as plugins from './smartshell.plugins'
|
||||
import * as smartshellWrap from './smartshell.wrap'
|
||||
import * as plugins from './smartshell.plugins';
|
||||
import * as smartshellWrap from './smartshell.wrap';
|
||||
|
||||
export type TExecutor = 'sh' | 'bash'
|
||||
export type TExecutor = 'sh' | 'bash';
|
||||
|
||||
export interface ISmartshellContructorOptions {
|
||||
executor: TExecutor
|
||||
sourceFilePaths: string[]
|
||||
|
||||
executor: TExecutor;
|
||||
sourceFilePaths: string[];
|
||||
}
|
||||
|
||||
export class Smartshell {
|
||||
executor: TExecutor
|
||||
sourceFileArray: string[] = []
|
||||
constructor (optionsArg: ISmartshellContructorOptions) {
|
||||
this.executor = optionsArg.executor
|
||||
executor: TExecutor;
|
||||
sourceFileArray: string[] = [];
|
||||
constructor(optionsArg: ISmartshellContructorOptions) {
|
||||
this.executor = optionsArg.executor;
|
||||
for (let sourceFilePath of optionsArg.sourceFilePaths) {
|
||||
this.sourceFileArray.push(sourceFilePath)
|
||||
this.sourceFileArray.push(sourceFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
addSourceFiles (sourceFilePathsArray: string[]) {
|
||||
addSourceFiles(sourceFilePathsArray: string[]) {
|
||||
for (let sourceFilePath of sourceFilePathsArray) {
|
||||
this.sourceFileArray.push(sourceFilePath)
|
||||
this.sourceFileArray.push(sourceFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
cleanSourceFiles () {
|
||||
this.sourceFileArray = []
|
||||
cleanSourceFiles() {
|
||||
this.sourceFileArray = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* executes silently and returns IExecResult
|
||||
* @param commandArg
|
||||
*/
|
||||
async execSilent (commandArg: string) {
|
||||
let execCommand = this.createExecString(commandArg)
|
||||
return await smartshellWrap.execSilent(execCommand)
|
||||
async execSilent(commandArg: string) {
|
||||
let execCommand = this.createExecString(commandArg);
|
||||
return await smartshellWrap.execSilent(execCommand);
|
||||
}
|
||||
|
||||
/**
|
||||
* executes and returns IExecResult
|
||||
* @param commandArg
|
||||
*/
|
||||
async exec (commandArg: string) {
|
||||
let execCommand = this.createExecString(commandArg)
|
||||
return await smartshellWrap.exec(execCommand)
|
||||
async exec(commandArg: string) {
|
||||
let execCommand = this.createExecString(commandArg);
|
||||
return await smartshellWrap.exec(execCommand);
|
||||
}
|
||||
|
||||
/**
|
||||
* creates the final sourcing string
|
||||
* @param commandArg
|
||||
*/
|
||||
private createExecString (commandArg): string {
|
||||
private createExecString(commandArg): string {
|
||||
if (this.executor === 'bash') {
|
||||
let sourceString = ''
|
||||
let sourceString = '';
|
||||
for (let sourceFilePath of this.sourceFileArray) {
|
||||
sourceString = sourceString + `source ${sourceFilePath} && `
|
||||
sourceString = sourceString + `source ${sourceFilePath} && `;
|
||||
}
|
||||
return `bash -c '${sourceString} ${commandArg}'`
|
||||
return `bash -c '${sourceString} ${commandArg}'`;
|
||||
} else {
|
||||
return commandArg
|
||||
return commandArg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,4 @@
|
||||
import * as shelljs from 'shelljs'
|
||||
import * as smartq from 'smartq'
|
||||
import * as which from 'which'
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as which from 'which';
|
||||
|
||||
export {
|
||||
shelljs,
|
||||
smartq,
|
||||
which
|
||||
}
|
||||
export { smartpromise, which };
|
||||
|
@ -1,45 +1,81 @@
|
||||
import * as plugins from './smartshell.plugins'
|
||||
import * as plugins from "./smartshell.plugins";
|
||||
|
||||
// interfaces
|
||||
import { ChildProcess } from 'child_process'
|
||||
import { Deferred } from 'smartq'
|
||||
import * as cp from "child_process";
|
||||
import { Deferred } from "@pushrocks/smartpromise";
|
||||
|
||||
/**
|
||||
* interface for ExecResult
|
||||
*/
|
||||
export interface IExecResult {
|
||||
exitCode: number,
|
||||
stdout: string
|
||||
exitCode: number;
|
||||
stdout: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* interface for streaming ExecResult
|
||||
*/
|
||||
export interface IExecResultStreaming {
|
||||
childProcess: ChildProcess,
|
||||
finalPromise: Promise<IExecResult>
|
||||
childProcess: cp.ChildProcess;
|
||||
finalPromise: Promise<IExecResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* import path
|
||||
* imports path into the shell from env if available and returns it with
|
||||
*/
|
||||
let importPath = (stringArg): string => {
|
||||
let importEnvVarPath = (stringArg): string => {
|
||||
if (process.env.SMARTSHELL_PATH) {
|
||||
let commandResult = `PATH=${process.env.SMARTSHELL_PATH} && ${stringArg}`
|
||||
let commandResult = `PATH=${process.env.SMARTSHELL_PATH} && ${stringArg}`;
|
||||
// console.log(commandResult)
|
||||
return commandResult
|
||||
return commandResult;
|
||||
} else {
|
||||
return stringArg
|
||||
return stringArg;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* executes a given command async
|
||||
* @param commandStringArg
|
||||
*/
|
||||
export let exec = (commandStringArg: string, silentArg: boolean = false, strictArg = false): Promise<IExecResult> => {
|
||||
let done = plugins.smartq.defer<IExecResult>()
|
||||
plugins.shelljs.exec(importPath(commandStringArg), { async: true, silent: silentArg }, (code, stdout, stderr) => {
|
||||
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 !== '')
|
||||
@ -57,76 +93,112 @@ export let exec = (commandStringArg: string, silentArg: boolean = false, strictA
|
||||
exitCode: code,
|
||||
stdout: stdout
|
||||
})
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
})*/
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
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.smartq.defer<IExecResult>()
|
||||
let execChildProcess = plugins.shelljs.exec(importPath(commandStringArg), {async: true, silent: silentArg}, (code, stdout, stderr) => {
|
||||
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: stdout
|
||||
})
|
||||
})
|
||||
stdout: logStore
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
childProcess: execChildProcess,
|
||||
finalPromise: childProcessEnded.promise
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export let execStreamingSilent = (commandStringArg: string) => {
|
||||
return execStreaming(commandStringArg, true)
|
||||
}
|
||||
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.smartq.defer()
|
||||
let execStreamingResult = execStreaming(commandStringArg, silentArg)
|
||||
execStreamingResult.childProcess.stdout.on('data', (stdOutChunk: string) => {
|
||||
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()
|
||||
done.resolve();
|
||||
}
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
export let execAndWaitForLineSilent = (commandStringArg: string, regexArg: RegExp) => {
|
||||
execAndWaitForLine(commandStringArg, regexArg, true)
|
||||
}
|
||||
export let execAndWaitForLineSilent = (
|
||||
commandStringArg: string,
|
||||
regexArg: RegExp
|
||||
) => {
|
||||
execAndWaitForLine(commandStringArg, regexArg, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* get a path
|
||||
*/
|
||||
export let which = (cmd: string): Promise<string> => {
|
||||
let done = plugins.smartq.defer<string>()
|
||||
let done = plugins.smartpromise.defer<string>();
|
||||
plugins.which(cmd, (err, path: string) => {
|
||||
if (err) {
|
||||
done.reject(err)
|
||||
done.reject(err);
|
||||
}
|
||||
done.resolve(path)
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
done.resolve(path);
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
|
Reference in New Issue
Block a user