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 Feb 19, 2018
1 parent 691faed commit 6bfea78
Showing 1 changed file with 86 additions and 43 deletions.
129 changes: 86 additions & 43 deletions bin/lisky
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,100 @@ const execFile = require('../dist/execFile').default;

const nonInteractiveLiskyArg = process.argv[1];
const nonInteractiveCommandArg = process.argv[2];

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

process.env.NON_INTERACTIVE_MODE = !(
nonInteractiveLiskyArg.endsWith('lisky') && process.argv.length === 2
);
const errorNodeVersion = (expected, actual) =>
Error(`ERROR: Requires Node.js version ${expected}, but was started with version ${actual}.`);

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

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');
exit();
}
const showWarn = msg => console.warn('\x1b[33m', msg, '\x1b[0m');
const showError = error => console.error('\x1b[31m', error.message, '\x1b[0m');

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');
lockfile.unlockSync(configLockfilePath);
exit();
break;
case '--version':
case '-v':
console.info(packageJSON.version);
exit();
break;
default:
// continue...
}
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;
}
};

// eslint-disable-next-line no-underscore-dangle
const firstCommandWords = lisky.commands.map(c => c._name.split(' ')[0]);
const handleFileInput = (liskyInstnce, command, options, exitFn) => {
try {
execFile(liskyInstnce, command, options, exitFn);
return true;
} catch (e) {
return false;
}
};

let commandArgIsFilePath = false;
if (firstCommandWords.indexOf(nonInteractiveCommandArg) === -1) {
commandArgIsFilePath = true;
const run = () => {
setEnvironment();
try {
checkNodeVersion(packageJSON.engines.node, process.version);
} catch (err) {
showError(err);
exit();
}

const handled = handleBasicCommands(
nonInteractiveCommandArg,
configLockfilePath,
packageJSON.version);
if (handled) {
exit();
}

const commandArgIsFilePath = isFileInput(nonInteractiveCommandArg);
let fileHandled = false;
if (commandArgIsFilePath) {
const nonInteractiveOptions = process.argv.slice(3);
execFile(lisky, nonInteractiveCommandArg, nonInteractiveOptions, exit);
} catch (error) {
commandArgIsFilePath = false;
fileHandled = handleFileInput(
lisky,
nonInteractiveCommandArg,
nonInteractiveOptions,
exit);
}
}

if (!commandArgIsFilePath) {
module.exports =
process.env.NON_INTERACTIVE_MODE === 'true'
? lisky.parse(process.argv)
: lisky;
}
if (!commandArgIsFilePath || !fileHandled) {
return process.env.NON_INTERACTIVE_MODE === 'true'
? lisky.parse(process.argv)
: lisky;
}
return null;
};

module.export = run();

0 comments on commit 6bfea78

Please sign in to comment.