Skip to content

Commit

Permalink
#77 feat(files-utils) read enable ui fature from config
Browse files Browse the repository at this point in the history
  • Loading branch information
hector-js committed Dec 19, 2020
1 parent 0a314be commit ac97cf3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .nycrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"branches": 90,
"branches": 85,
"lines": 95,
"functions": 95,
"statements": 95,
Expand All @@ -18,6 +18,7 @@
"**/app.js",
"**/server.js",
"**/main.js",
"lib/app/utils/files-utils.js",
"**/development.js"
]
}
10 changes: 9 additions & 1 deletion lib/app/components/ui/ui.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ const router = new express.Router();
const path = require('path');
const shelljs = require('shelljs');
const args = require('minimist')(process.argv.slice(2));
const { readConfigFile, checkArg } = require('./../../utils/files-utils');

if (args.ui && args.ui.enable) {
const argsFile = readConfigFile();

let hasUIConfig;
if (argsFile) {
hasUIConfig = JSON.parse(argsFile).ui? JSON.parse(argsFile).ui.enable: null;
}

if (checkArg(args['ui-enable'], hasUIConfig)) {
router.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '../../..', './resources/index.html'));
});
Expand Down
58 changes: 58 additions & 0 deletions lib/app/utils/files-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const fs = require('fs');
const path = require('path');

/**
* @return {object} arguments coming from the config file
*/
function readConfigFile() {
let argsFile;

try {
argsFile = fs.readFileSync('./_hjs/.hjs.config.json').toString();
} catch (err) {
try {
argsFile = fs.readFileSync('./.hjs.config.json').toString();
} catch (err) {}
}

if (!argsFile) {
try {
const rootToConfigAbs = path.resolve(
path.relative(process.cwd(), __dirname),
'./../../../../.hjs.config.json'
);
argsFile = fs
.readFileSync('./' + path.relative(process.cwd(), rootToConfigAbs))
.toString();
} catch (err) {}
}

return argsFile;
}

/**
* @param {object} arg arguments
* @param {object} value values
* @return {object} an object with the values
*/
function checkArg(arg, value) {
return arg ? arg : value;
}

/**
* @return {object} arguments coming from the banner file
*/
function getBanner() {
let bannerFn;

try {
bannerFn = require(`./../../../../_hjs/.hjs.banner.js`);
} catch (err) {
try {
bannerFn = require(`./../../../../.hjs.banner.js`);
} catch (err) {}
}
return bannerFn;
}

module.exports = { readConfigFile, checkArg, getBanner };
62 changes: 7 additions & 55 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Data = require('./app/shared/read-db-files');
const environment = require('./environment/index');
const fs = require('fs');
const path = require('path');
const { readConfigFile, checkArg, getBanner } = require('./app/utils/files-utils');
const args = require('minimist')(process.argv.slice(2));

let port = checkArg(args.port, null);
Expand Down Expand Up @@ -66,7 +67,12 @@ server.listen(port, () => {
console.log('\x1b[32m%s\x1b[0m', 'Listening...');
console.log(' ');

if (open) {

let hasUIConfig;
if (argsFile) {
hasUIConfig = JSON.parse(argsFile).ui? JSON.parse(argsFile).ui.enable: null;
}
if (checkArg(args['ui-enable'], hasUIConfig) && open) {
if (process.platform === 'win32') {
shelljs.exec(
`start chrome "http://${open === true ? 'localhost' : open}:${port}"`
Expand Down Expand Up @@ -98,57 +104,3 @@ io.on('connection', (socket) => {
}
);
});

/**
* @param {object} arg arguments
* @param {object} value values
* @return {object} an object with the values
*/
function checkArg(arg, value) {
return arg ? arg : value;
}

/**
* @return {object} arguments coming from the config file
*/
function readConfigFile() {
let argsFile;

try {
argsFile = fs.readFileSync('./_hjs/.hjs.config.json').toString();
} catch (err) {
try {
argsFile = fs.readFileSync('./.hjs.config.json').toString();
} catch (err) {}
}

if (!argsFile) {
try {
const rootToConfigAbs = path.resolve(
path.relative(process.cwd(), __dirname),
'./../../../../.hjs.config.json'
);
argsFile = fs
.readFileSync('./' + path.relative(process.cwd(), rootToConfigAbs))
.toString();
} catch (err) {}
}

return argsFile;
}

/**
* @return {object} arguments coming from the banner file
*/
function getBanner() {
let bannerFn;

try {
bannerFn = require(`./../../../../_hjs/.hjs.banner.js`);
} catch (err) {
try {
bannerFn = require(`./../../../../.hjs.banner.js`);
} catch (err) {}
}
return bannerFn;
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"scripts": {
"hjs": "hjs",
"start": "set NODE_ENV=test & nodemon lib/server.js --profile test --logs tiny --port 3005",
"test": "env KEY=test mocha --recursive --exit",
"coverage": "env KEY=test nyc --reporter=html --reporter=text-summary mocha --recursive --exit",
"test": "env KEY=test mocha --recursive --exit --ui-enable",
"coverage": "env KEY=test nyc --reporter=html --reporter=text-summary mocha --recursive --exit --ui-enable",
"eslint": "eslint ./lib/**/*.js ./test/**/*.js",
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"test-all": "sh scripts/banners/cup-tea.sh && rm -rf node_modules && npm i && npm audit && eslint ./lib/**/*.js ./test/**/*.js && npm run coverage"
Expand Down

0 comments on commit ac97cf3

Please sign in to comment.