node-cli-plus
Enhanced CLI configurable for a specific project.
Install and place .nodeplus file in project root directory to configure.
Setup
-
npm install node-cli-plus --save-dev
-
Create a .nodeplus file in the project root. See below for basic example.
-
Run in project with npx nodeplus
Example nodeplus config file
require('module-alias/register');
const path = require('path');
const {path: rootPath} = require('app-root-path');
const {config: envConfig} = require('dotenv');
envConfig({path: path.join(rootPath, `./config/env/.env`)});
const fetchNew = require('isomorphic-fetch');
fetch = fetchNew;
global.temp = null;
const apiFetch = (route, method = 'GET', body = {}, verbose = false) => {
let reqRoute = route.match(/^\//g) ? `http://localhost:${process.env['PORT']}${route}` : route;
if (!reqRoute.match(/^https?:\/\//g)) {
reqRoute = `http://${reqRoute}`;
}
if (verbose) console.log('apiFetch :: request route (reqRoute):', reqRoute);
const isBody = body && Object.keys(body).length > 0;
if (verbose) console.log('apiFetch :: isBody?', isBody);
let resStore;
const headers = {
...(isBody ? {Accept: 'application/json', 'Content-Type': 'application/json'} : {})
};
if (verbose) {
console.log(`apiFetch :: headers:`, headers);
console.log(`apiFetch :: body:`, body);
}
return (
fetch(reqRoute, {
headers,
method,
mode: 'cors',
body: isBody ? JSON.stringify(body) : undefined,
credentials: 'include'
})
.then(res => {
console.log(`res:`, res);
resStore = res;
return res.text();
})
.catch(err => {
console.log(`err:`, err, `err keys:`, Object.keys(err));
if (err && err.type === 'invalid-json') return resStore.text();
throw err;
})
.then(data => {
console.log(data);
return data;
})
.catch(err => console.log(`Failed to fetch from route "${route}" with error:`, err))
);
};
apiFetch['__repl_description__'] = `Make API request to local server, with all config (e.g. auth headers) take care of.`;
const arraySort = require('array-sort');
const mathJs = require('mathjs');
mathJs['__repl_description__'] = `Public module for handling complex mathematical operations.`
module.exports = {
apiFetch,
arraySort,
mathJs,
__essential_defs__: ['apiFetch', 'mathJs'],
};