import { validDirectory } from './Utils/fsutils' ; export interface CommandInterface { evaluateAsLine ( expression , callback ) : void ; evaluate ( expression , callback ) : void ; execute ( expression ) : void ; } export class Commands { private static escape ( path ) { return path . replace ( /([\\"])/g , "\\$1" ) ; } public static addFolder ( ci , sourceFolder ) { ci . execute ( `addpath("${ Commands . escape ( sourceFolder ) }")` ) ; } public static cwd ( ci , newWorkingDirectory ) { if ( validDirectory ( newWorkingDirectory ) ) { ci . execute ( `cd "${ Commands . escape ( newWorkingDirectory ) }"` ) ; } } private static readonly CLEAN_REGEX = /^\s*ans =\s*/ ; public static getValue ( str ) { return str . replace ( Commands . CLEAN_REGEX , '' ) . trim ( ) ; } public static pwd ( ci , callback ) { ci . evaluateAsLine ( 'pwd' , ( line ) => { callback ( Commands . getValue ( line ) ) ; } ) ; } public static pushd ( ci , dir , callback ) { Commands . pwd ( ci , ( oldWorkingDirectory ) => { Commands . cwd ( ci , dir ) ; callback ( ci , ( ci ) => { Commands . cwd ( ci , oldWorkingDirectory ) ; } ) ; } ) ; } }