Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
feat(cli): implement help, display version
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jul 10, 2018
1 parent d614551 commit 5d07778
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
#!/usr/bin/env node
import chalk from 'chalk';
import * as commandLineArgs from 'command-line-args';

/**
* Supported output style. Mirrors `style_option_strings[]` in sassc
* (https://github.com/sass/sassc/blob/6a64d0569205bfcf0794a473f97affcb60b22fcc/sassc.c#L184-L189)
*/
enum StyleOption {
SASS_STYLE_COMPRESSED = 'compressed',
SASS_STYLE_COMPACT = 'compact',
SASS_STYLE_EXPANDED = 'expanded',
SASS_STYLE_NESTED = 'nested'
}

/**
* Definitions of available command line args.
*/
const optionDefinitions = [
{ name: 'stdin', alias: 's', description: 'Read input from standard input instead of an input file.' },
{
Expand All @@ -22,18 +30,43 @@ const optionDefinitions = [
{ name: 'omit-map-comment', alias: 'M', description: 'Omits the source map url comment.' },
{ name: 'precision', alias: 'p', description: 'Set the precision for numbers.' },
{ name: 'sass', alias: 'a', description: 'Treat input as indented syntax.' },
{ name: 'version', alias: 'v', description: 'Display compiled versions.' },
{ name: 'version', alias: 'v', description: 'Display compiled versions.', type: Boolean },
{ name: 'help', alias: 'h', description: 'Display this help message.', type: Boolean }
];

/**
* Get usage definition for library version.
*
*/
const buildDisplayVersion = async () => {
const { loadModule } = await import('./loadModule');
const sassFactory = await loadModule();
const { libsassAsm, libsass, sassLang, sass2scss } = await sassFactory.getVersion();

return {
header: chalk.reset('Version'),
content: [
{ name: 'libsass-asm', summary: libsassAsm },
{ name: 'libsass', summary: libsass },
{ name: 'sass', summary: sassLang },
{ name: 'sass2scss', summary: sass2scss }
]
};
};

(async () => {
const options = commandLineArgs(optionDefinitions, { camelCase: true });

if (options.help || Object.keys(options).length === 0) {
const commandLineUsage = await import('command-line-usage');
const usage = commandLineUsage([{ header: 'Options', optionList: optionDefinitions }]);
const displayHelp = options.help || Object.keys(options).length === 0;
const displayVersion = options.version;

if (displayHelp || displayVersion) {
const cmdUsage = await import('command-line-usage');
const usageDefinition = displayHelp
? { header: chalk.reset('Options'), optionList: optionDefinitions }
: await buildDisplayVersion();
const usage = cmdUsage([usageDefinition]);
console.log(usage);
} else if (options.version) {
return;
}
})();

0 comments on commit 5d07778

Please sign in to comment.