cmder-cli
Version:
namespace shell commands cli
99 lines (74 loc) • 3.15 kB
JavaScript
const
fs = require('fs')
,cliColor = require('cli-color')
;
module.exports = function() {
const
{ clientDir } = process.cmder
,path = clientDir + 'package.json'
;
// already inject
if( fs.existsSync( clientDir + 'cmder-commands.js' ) ) return;
if( fs.existsSync( path ) ) {
let packageJson = fs.readFileSync( path , 'utf-8' ) ;
try {
packageJson = JSON.parse( packageJson ) ;
const scripts = packageJson['scripts'] ;
if( !(typeof scripts === 'object') ) {
scripts = {};
}
scripts['cmder'] = `node cmder-commands.js`
packageJson[ 'scripts' ] = scripts;
let formattageJSON = JSON.stringify(packageJson) ;
formattageJSON = formattageJSON.split('')
.map( char => {
if( /^,|\{$/.test(char) ) {
return char + '\n\t' ;
} else if( char === '}' ) {
return '\n'+char + '\n' ;
}
return char;
} ).join('') ;
console.log( cliColor.green('\t`npm run cmder` is inject with success ✅') );
fs.writeFileSync( path , formattageJSON ) ;
fs.appendFileSync( clientDir + "cmder-commands.js" ,
`const cmder = require('cmder') ;
/**
* @object cmder
* @method {async function} exec
* @params {string} path - tree path commands group to exec
* @params {string="/"} separator - custom your separator ressource path exemple: \`namespace=>wrapName=>commandsGroupName\` **use with** \`string: separator = "=>"\`
* @params {boolean=true} shell - try **spawn** a \`powershell\` terminal before exec commands list
* @return {Promise} - \`{array<Response>}\` of stdout commands \`cmder.exec( 'namespace/wrapName/commandsGroupName' ).then( array<Response>: responses => : void )\`
* @exemple cmder.exec('namespace/wrapName/commandsGroupName')
*/
// cmder.exec
/**
* @object cmder
* @method exists
* @params {string} path - tree path commands group to check exists
* @params {string="/"} separator - custom your separator ressource path exemple: \`namespace=>wrapName=>commandsGroupName\` **use with** \`string: separator = "=>"\`
* @return {boolean}
* @exemple cmder.exists('namespace/wrapName/commandsGroupName')
*
*/
// cmder.exists
/**
* @object cmder
* @method out
* @params {array<Response} - array Response for log the \`stdout\` of previous commands list exec
* @return void
*/
// cmder.out
// cmder.exec('namespace/test')
`
) ;
console.log( cliColor.green('\ncmder-commands.js is spawn with success ✅') );
} catch( SyntaxError ) {
console.log( "syntaxError" );
// package.json SyntaxError
}
} else {
// package.json not exists
}
}