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

Commit

Permalink
♻️ Changed namings
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Mar 19, 2018
1 parent c9ec05a commit 3dcdf8c
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 121 deletions.
144 changes: 76 additions & 68 deletions bin/lisky
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
#!/usr/bin/env node
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

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 @@ -14,112 +38,96 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
const os = require('os');
const lockfile = require('lockfile');
const semver = require('semver');
const packageJSON = require('../package.json');
var nonInteractiveLiskyArg = process.argv[1];
var nonInteractiveCommandArg = process.argv[2];
var nonInteractiveOptions = process.argv.slice(3);

// 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);
var exit = function exit(code) {
return 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');
var printWarning = function printWarning(message) {
return console.warn('\x1b[33m', message, '\x1b[0m');
};
var printError = function printError(message) {
return console.error('\x1b[31m', message, '\x1b[0m');
};
var printVersion = function printVersion(version) {
return console.info(version);
};

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);
var execClean = function execClean(path) {
printWarning('WARNING: Attempting to remove configuration lockfile. I hope you know what you’re doing.');
_lockfile2.default.unlockSync(path);
};

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

const setEnvironment = () => {
process.env.LISKY_CONFIG_DIR =
process.env.LISKY_CONFIG_DIR || `${os.homedir()}/.lisky`;
var setEnvironment = function setEnvironment() {
process.env.LISKY_CONFIG_DIR = process.env.LISKY_CONFIG_DIR || _os2.default.homedir() + '/.lisky';

process.env.NON_INTERACTIVE_MODE = !(
nonInteractiveLiskyArg.endsWith('lisky') && process.argv.length === 2
);
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));
var checkNodeVersion = function checkNodeVersion(expected, actual) {
if (!_semver2.default.satisfies(actual, expected)) {
throw new Error('ERROR: Requires Node.js version ' + _semver2.default.clean(expected) + ', but was started with version ' + _semver2.default.clean(actual) + '.');
}
};

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

const handleFileInput = (liskyInstnce, command, options, exitFn) => {
try {
execFile(liskyInstnce, command, options, exitFn);
return true;
} catch (e) {
return false;
}
var getLiskyInstanceByMode = function getLiskyInstanceByMode(liskyInstance, nonInteractiveMode) {
return nonInteractiveMode ? liskyInstance.parse(process.argv) : liskyInstance;
};

const run = () => {
var run = function run() {
setEnvironment();
try {
checkNodeVersion(packageJSON.engines.node, process.version);
} catch (err) {
showError(err);
checkNodeVersion(_package2.default.engines.node, process.version);
} catch (error) {
printError(error.message);
exit();
}

const handled = handleBasicCommands(
nonInteractiveCommandArg,
configLockfilePath,
packageJSON.version);
if (handled) {
if (handleBasicCommands(nonInteractiveCommandArg, process.env.LISKY_CONFIG_DIR + '/config.lock', _package2.default.version)) {
exit();
}

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;
// Dynamically required, otherwise it starts the CLI before handling above codes
// eslint-disable-next-line global-require
var lisky = require('../dist').default;
// eslint-disable-next-line global-require
var execFile = require('../dist/exec_file').default;
if (isUnknownCommand(lisky, nonInteractiveCommandArg)) {
try {
// "execFile" throws error when it "nonInteractiveCommandArg" is not a filepath
return execFile(lisky, nonInteractiveCommandArg, nonInteractiveOptions, exit);
} catch (error) {
return getLiskyInstanceByMode(lisky, process.env.NON_INTERACTIVE_MODE === 'true');
}
}
return null;
return getLiskyInstanceByMode(lisky, process.env.NON_INTERACTIVE_MODE === 'true');
};

module.export = run();
run();

exports.default = run;
147 changes: 94 additions & 53 deletions bin/lisky.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,74 +17,115 @@
import os from 'os';
import lockfile from 'lockfile';
import semver from 'semver';
// eslint-disable-next-line import/order
import packageJSON from '../package.json';

const nonInteractiveLiskyArg = process.argv[1];
const nonInteractiveCommandArg = process.argv[2];
const nonInteractiveOptions = process.argv.slice(3);

process.env.LISKY_CONFIG_DIR =
process.env.LISKY_CONFIG_DIR || `${os.homedir()}/.lisky`;
const configLockfilePath = `${process.env.LISKY_CONFIG_DIR}/config.lock`;
const exit = code => process.exit(code || 0);

process.env.NON_INTERACTIVE_MODE = !(
nonInteractiveLiskyArg.endsWith('lisky') && process.argv.length === 2
);
const printWarning = message => console.warn('\x1b[33m', message, '\x1b[0m');
const printError = message => console.error('\x1b[31m', message, '\x1b[0m');
const printVersion = version => console.info(version);

function exit(code) {
process.exit(code || 0);
}
const execClean = path => {
printWarning(
'WARNING: Attempting to remove configuration lockfile. I hope you know what you’re doing.',
);
lockfile.unlockSync(path);
};

const isUnknownCommand = (liskyInstance, command) => {
const firstCommandWords = liskyInstance.commands.map(
// eslint-disable-next-line no-underscore-dangle
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`;

if (!semver.satisfies(process.version, packageJSON.engines.node)) {
console.error(
'\x1b[31m',
`ERROR: Requires Node.js version ${semver.clean(
packageJSON.engines.node,
)}, but was started with version ${semver.clean(process.version)}.`,
'\x1b[0m',
process.env.NON_INTERACTIVE_MODE = !(
nonInteractiveLiskyArg.endsWith('lisky') && process.argv.length === 2
);
exit();
}
};

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',
const checkNodeVersion = (expected, actual) => {
if (!semver.satisfies(actual, expected)) {
throw new Error(
`ERROR: Requires Node.js version ${semver.clean(
expected,
)}, but was started with version ${semver.clean(actual)}.`,
);
lockfile.unlockSync(configLockfilePath);
exit();
break;
case '--version':
case '-v':
console.info(packageJSON.version);
exit();
break;
default:
// continue...
}
}
};

const lisky = require('../dist').default;
const execFile = require('../dist/exec_file').default;
const handleBasicCommands = (command, lockFilePath, version) => {
switch (command) {
case 'clean':
execClean(lockFilePath);
return true;
case '--version':
case '-v':
printVersion(version);
return true;
default:
return false;
}
};

// eslint-disable-next-line no-underscore-dangle
const firstCommandWords = lisky.commands.map(c => c._name.split(' ')[0]);
const getLiskyInstanceByMode = (liskyInstance, nonInteractiveMode) =>
nonInteractiveMode ? liskyInstance.parse(process.argv) : liskyInstance;

let commandArgIsFilePath = false;
if (firstCommandWords.indexOf(nonInteractiveCommandArg) === -1) {
commandArgIsFilePath = true;
const run = () => {
setEnvironment();
try {
const nonInteractiveOptions = process.argv.slice(3);
execFile(lisky, nonInteractiveCommandArg, nonInteractiveOptions, exit);
checkNodeVersion(packageJSON.engines.node, process.version);
} catch (error) {
commandArgIsFilePath = false;
printError(error.message);
exit();
}

if (
handleBasicCommands(
nonInteractiveCommandArg,
`${process.env.LISKY_CONFIG_DIR}/config.lock`,
packageJSON.version,
)
) {
exit();
}
}

if (!commandArgIsFilePath) {
module.exports =
process.env.NON_INTERACTIVE_MODE === 'true'
? lisky.parse(process.argv)
: lisky;
}
// Dynamically required, otherwise it starts the CLI before handling above codes
// eslint-disable-next-line global-require
const lisky = require('../dist').default;
// eslint-disable-next-line global-require
const execFile = require('../dist/exec_file').default;
if (isUnknownCommand(lisky, nonInteractiveCommandArg)) {
try {
// "execFile" throws error when it "nonInteractiveCommandArg" is not a filepath
return execFile(
lisky,
nonInteractiveCommandArg,
nonInteractiveOptions,
exit,
);
} catch (error) {
return getLiskyInstanceByMode(
lisky,
process.env.NON_INTERACTIVE_MODE === 'true',
);
}
}
return getLiskyInstanceByMode(
lisky,
process.env.NON_INTERACTIVE_MODE === 'true',
);
};

run();

export default run;

0 comments on commit 3dcdf8c

Please sign in to comment.