Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
♻️ Clean up bin/lisky to increase readability
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Mar 19, 2018
1 parent 748d494 commit c9ec05a
Showing 1 changed file with 103 additions and 70 deletions.
173 changes: 103 additions & 70 deletions bin/lisky
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
#!/usr/bin/env node
'use strict';

var _os = require('os');

var _os2 = _interopRequireDefault(_os);

var _lockfile = require('lockfile');

var _lockfile2 = _interopRequireDefault(_lockfile);

var _semver = require('semver');

var _semver2 = _interopRequireDefault(_semver);

var _package = require('../package.json');

var _package2 = _interopRequireDefault(_package);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/*
* LiskHQ/lisky
* Copyright © 2017 Lisk Foundation
Expand All @@ -34,59 +14,112 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* Removal or modification of this copyright notice is prohibited.
*
*/
var nonInteractiveLiskyArg = process.argv[1];
// eslint-disable-next-line import/order

var nonInteractiveCommandArg = process.argv[2];

process.env.LISKY_CONFIG_DIR = process.env.LISKY_CONFIG_DIR || _os2.default.homedir() + '/.lisky';
var configLockfilePath = process.env.LISKY_CONFIG_DIR + '/config.lock';

process.env.NON_INTERACTIVE_MODE = !(nonInteractiveLiskyArg.endsWith('lisky') && process.argv.length === 2);

function exit(code) {
process.exit(code || 0);
}
const os = require('os');
const lockfile = require('lockfile');
const semver = require('semver');
const packageJSON = require('../package.json');

// eslint-disable-next-line import/extensions,import/no-unresolved
const lisky = require('../dist').default;
// eslint-disable-next-line import/extensions,import/no-unresolved
const execFile = require('../dist/execFile').default;

const nonInteractiveLiskyArg = process.argv[1];
const nonInteractiveCommandArg = process.argv[2];
const configLockfilePath = `${process.env.LISKY_CONFIG_DIR}/config.lock`;

const errorNodeVersion = (expected, actual) =>
Error(`ERROR: Requires Node.js version ${expected}, but was started with version ${actual}.`);

const exit = code => process.exit(code || 0);

const showWarn = msg => console.warn('\x1b[33m', msg, '\x1b[0m');
const showError = error => console.error('\x1b[31m', error.message, '\x1b[0m');

const showVersion = version => console.info(version);
const execClean = path => {
showWarn('WARNING: Attempting to remove configuration lockfile. I hope you know what you’re doing.');
lockfile.unlockSync(path);
};

const isFileInput = command => {
// eslint-disable-next-line no-underscore-dangle
const firstCommandWords = lisky.commands.map(c => c._name.split(' ')[0]);
return firstCommandWords.indexOf(command) === -1;
};

const setEnvironment = () => {
process.env.LISKY_CONFIG_DIR =
process.env.LISKY_CONFIG_DIR || `${os.homedir()}/.lisky`;

process.env.NON_INTERACTIVE_MODE = !(
nonInteractiveLiskyArg.endsWith('lisky') && process.argv.length === 2
);
};

const checkNodeVersion = (expected, actual) => {
if (!semver.satisfies(actual, expected)) {
throw errorNodeVersion(semver.clean(expected), semver.clean(actual));
}
};

const handleBasicCommands = (command, lockFilePath, version) => {
switch (command) {
case 'clean':
execClean(lockFilePath);
return true;
case '--version':
case '-v':
showVersion(version);
return true;
default:
return false;
}
};

if (!_semver2.default.satisfies(process.version, _package2.default.engines.node)) {
console.error('\x1b[31m', 'ERROR: Requires Node.js version ' + _semver2.default.clean(_package2.default.engines.node) + ', but was started with version ' + _semver2.default.clean(process.version) + '.', '\x1b[0m');
exit();
}
const handleFileInput = (liskyInstnce, command, options, exitFn) => {
try {
execFile(liskyInstnce, command, options, exitFn);
return true;
} catch (e) {
return false;
}
};

switch (process.argv[2]) {
case 'clean':
console.warn('\x1b[33m', 'WARNING: Attempting to remove configuration lockfile. I hope you know what you’re doing.', '\x1b[0m');
_lockfile2.default.unlockSync(configLockfilePath);
const run = () => {
setEnvironment();
try {
checkNodeVersion(packageJSON.engines.node, process.version);
} catch (err) {
showError(err);
exit();
break;
case '--version':
case '-v':
console.info(_package2.default.version);
}

const handled = handleBasicCommands(
nonInteractiveCommandArg,
configLockfilePath,
packageJSON.version);
if (handled) {
exit();
break;
default:
// continue...
}

var lisky = require('../dist').default;
var execFile = require('../dist/exec_file').default;

// eslint-disable-next-line no-underscore-dangle
var firstCommandWords = lisky.commands.map(function (c) {
return c._name.split(' ')[0];
});

var commandArgIsFilePath = false;
if (firstCommandWords.indexOf(nonInteractiveCommandArg) === -1) {
commandArgIsFilePath = true;
try {
var nonInteractiveOptions = process.argv.slice(3);
execFile(lisky, nonInteractiveCommandArg, nonInteractiveOptions, exit);
} catch (error) {
commandArgIsFilePath = false;
}
}

if (!commandArgIsFilePath) {
module.exports = process.env.NON_INTERACTIVE_MODE === 'true' ? lisky.parse(process.argv) : lisky;
}
const commandArgIsFilePath = isFileInput(nonInteractiveCommandArg);
let fileHandled = false;
if (commandArgIsFilePath) {
const nonInteractiveOptions = process.argv.slice(3);
fileHandled = handleFileInput(
lisky,
nonInteractiveCommandArg,
nonInteractiveOptions,
exit);
}
if (!commandArgIsFilePath || !fileHandled) {
return process.env.NON_INTERACTIVE_MODE === 'true'
? lisky.parse(process.argv)
: lisky;
}
return null;
};

module.export = run();

0 comments on commit c9ec05a

Please sign in to comment.