From 2d3a4b0a0e598d6544dd443834a0fba35a36f6c6 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Fri, 10 Mar 2017 22:08:04 +0100 Subject: [PATCH] add smart sourcing of files with bash --- dist/index.d.ts | 1 + dist/index.js | 3 +- dist/smartshell.classes.smartshell.d.ts | 14 +++++++++- dist/smartshell.classes.smartshell.js | 36 ++++++++++++++++++++---- test/test.js | 10 ++++++- test/test.ts | 13 +++++++-- ts/index.ts | 1 + ts/smartshell.classes.smartshell.ts | 37 +++++++++++++++++++++---- 8 files changed, 100 insertions(+), 15 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index f724545..760ab72 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1 +1,2 @@ export * from './smartshell.wrap'; +export * from './smartshell.classes.smartshell'; diff --git a/dist/index.js b/dist/index.js index 75053c3..a34922e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4,4 +4,5 @@ function __export(m) { } Object.defineProperty(exports, "__esModule", { value: true }); __export(require("./smartshell.wrap")); -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHVDQUFpQyJ9 \ No newline at end of file +__export(require("./smartshell.classes.smartshell")); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHVDQUFpQztBQUNqQyxxREFBK0MifQ== \ No newline at end of file diff --git a/dist/smartshell.classes.smartshell.d.ts b/dist/smartshell.classes.smartshell.d.ts index 075ddb5..f833a73 100644 --- a/dist/smartshell.classes.smartshell.d.ts +++ b/dist/smartshell.classes.smartshell.d.ts @@ -1,11 +1,23 @@ +import * as smartshellWrap from './smartshell.wrap'; export declare type TExecutor = 'sh' | 'bash'; export interface ISmartshellContructorOptions { executor: TExecutor; sourceFilePaths: string[]; } export declare class Smartshell { - sourceFiles: string[]; + executor: TExecutor; + sourceFileArray: string[]; constructor(optionsArg: ISmartshellContructorOptions); addSourceFiles(sourceFilePathsArray: string[]): void; cleanSourceFiles(): void; + /** + * executes silently and returns IExecResult + * @param commandArg + */ + execSilent(commandArg: string): Promise; + /** + * creates the final sourcing string + * @param commandArg + */ + private createExecString(commandArg); } diff --git a/dist/smartshell.classes.smartshell.js b/dist/smartshell.classes.smartshell.js index 4039ddc..20cd074 100644 --- a/dist/smartshell.classes.smartshell.js +++ b/dist/smartshell.classes.smartshell.js @@ -1,20 +1,46 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); +const smartshellWrap = require("./smartshell.wrap"); class Smartshell { constructor(optionsArg) { - this.sourceFiles = []; + this.sourceFileArray = []; + this.executor = optionsArg.executor; for (let sourceFilePath of optionsArg.sourceFilePaths) { - this.sourceFiles.push(sourceFilePath); + this.sourceFileArray.push(sourceFilePath); } } addSourceFiles(sourceFilePathsArray) { for (let sourceFilePath of sourceFilePathsArray) { - this.sourceFiles.push(sourceFilePath); + this.sourceFileArray.push(sourceFilePath); } } cleanSourceFiles() { - this.sourceFiles = []; + this.sourceFileArray = []; + } + /** + * executes silently and returns IExecResult + * @param commandArg + */ + execSilent(commandArg) { + let execCommand = this.createExecString(commandArg); + return smartshellWrap.execSilent(commandArg); + } + /** + * creates the final sourcing string + * @param commandArg + */ + createExecString(commandArg) { + if (this.executor === 'bash') { + let sourceString = ''; + for (let sourceFilePath of this.sourceFileArray) { + sourceString = sourceString + `source ${sourceFilePath} && `; + } + return `bash -c '${sourceString} ${commandArg}'`; + } + else { + return commandArg; + } } } exports.Smartshell = Smartshell; -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21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0FBQztZQUNELE1BQU0sQ0FBQyxZQUFZLFlBQVksSUFBSSxVQUFVLEdBQUcsQ0FBQTtRQUNsRCxDQUFDO1FBQUMsSUFBSSxDQUFDLENBQUM7WUFDTixNQUFNLENBQUMsVUFBVSxDQUFBO1FBQ25CLENBQUM7SUFDSCxDQUFDO0NBQ0Y7QUE1Q0QsZ0NBNENDIn0= \ No newline at end of file diff --git a/test/test.js b/test/test.js index 09bd536..ab96535 100644 --- a/test/test.js +++ b/test/test.js @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); require("typings-test"); const smartchai_1 = require("smartchai"); const smartshell = require("../dist/index"); +let testSmartshell; describe('smartshell', function () { it('it should run async', function () { this.timeout(1000000); @@ -22,5 +23,12 @@ describe('smartshell', function () { }); return execStreamingResponse.finalPromise; }); + it('should create a Smartshell instance', function () { + testSmartshell = new smartshell.Smartshell({ + executor: 'bash', + sourceFilePaths: [] + }); + smartchai_1.expect(testSmartshell).to.be.instanceof(smartshell.Smartshell); + }); }); -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSx3QkFBcUI7QUFDckIseUNBQWtDO0FBRWxDLDRDQUEyQztBQUUzQyxRQUFRLENBQUMsWUFBWSxFQUFFO0lBQ3JCLEVBQUUsQ0FBQyxxQkFBcUIsRUFBRTtRQUN4QixJQUFJLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxDQUFBO1FBQ3JCLE1BQU0sQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLFFBQVEsQ0FBQyxDQUFDLElBQUksQ0FBQyxDQUFDLFVBQVU7WUFDL0Msa0JBQU0sQ0FBQyxVQUFVLENBQUMsTUFBTSxDQUFDLENBQUMsRUFBRSxDQUFDLEtBQUssQ0FBQyxVQUF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DLEVBQUU7UUFDeEMsY0FBYyxHQUFHLElBQUksVUFBVSxDQUFDLFVBQVUsQ0FBQztZQUN6QyxRQUFRLEVBQUUsTUFBTTtZQUNoQixlQUFlLEVBQUUsRUFBRTtTQUNwQixDQUFDLENBQUE7UUFDRixrQkFBTSxDQUFDLGNBQWMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLFVBQVUsQ0FBQyxVQUFVLENBQUMsQ0FBQTtJQUNoRSxDQUFDLENBQUMsQ0FBQTtBQUNKLENBQUMsQ0FBQyxDQUFBIn0= \ No newline at end of file diff --git a/test/test.ts b/test/test.ts index 560bf5c..9bdc905 100644 --- a/test/test.ts +++ b/test/test.ts @@ -3,6 +3,8 @@ import { expect } from 'smartchai' import * as smartshell from '../dist/index' +let testSmartshell: smartshell.Smartshell + describe('smartshell', function () { it('it should run async', function () { this.timeout(1000000) @@ -10,16 +12,23 @@ describe('smartshell', function () { expect(execResult.stdout).to.match(/[0-9\.]*/) }) }) - it('should run async and silent', function() { + it('should run async and silent', function () { return smartshell.execSilent('npm -v').then((execResult) => { expect(execResult.stdout).to.match(/[0-9\.]*/) }) }) - it('should stream a shell execution', function() { + it('should stream a shell execution', function () { let execStreamingResponse = smartshell.execStreaming('npm -v') execStreamingResponse.childProcess.stdout.on('data', (data) => { console.log('Received ' + data) }) return execStreamingResponse.finalPromise }) + it('should create a Smartshell instance', function () { + testSmartshell = new smartshell.Smartshell({ + executor: 'bash', + sourceFilePaths: [] + }) + expect(testSmartshell).to.be.instanceof(smartshell.Smartshell) + }) }) diff --git a/ts/index.ts b/ts/index.ts index ce0d1fb..a1a16ab 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1 +1,2 @@ export * from './smartshell.wrap' +export * from './smartshell.classes.smartshell' diff --git a/ts/smartshell.classes.smartshell.ts b/ts/smartshell.classes.smartshell.ts index a4158d3..d3f4ec0 100644 --- a/ts/smartshell.classes.smartshell.ts +++ b/ts/smartshell.classes.smartshell.ts @@ -10,20 +10,47 @@ export interface ISmartshellContructorOptions { } export class Smartshell { - sourceFiles: string[] = [] + executor: TExecutor + sourceFileArray: string[] = [] constructor (optionsArg: ISmartshellContructorOptions) { + this.executor = optionsArg.executor for (let sourceFilePath of optionsArg.sourceFilePaths) { - this.sourceFiles.push(sourceFilePath) + this.sourceFileArray.push(sourceFilePath) } } addSourceFiles(sourceFilePathsArray: string[]) { for(let sourceFilePath of sourceFilePathsArray) { - this.sourceFiles.push(sourceFilePath) + this.sourceFileArray.push(sourceFilePath) } } cleanSourceFiles () { - this.sourceFiles = [] + this.sourceFileArray = [] } -} \ No newline at end of file + + /** + * executes silently and returns IExecResult + * @param commandArg + */ + execSilent(commandArg: string) { + let execCommand = this.createExecString(commandArg) + return smartshellWrap.execSilent(commandArg) + } + + /** + * creates the final sourcing string + * @param commandArg + */ + 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}'` + } else { + return commandArg + } + } +}