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

Commit 5d07778

Browse files
committed
feat(cli): implement help, display version
1 parent d614551 commit 5d07778

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

src/cli.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
#!/usr/bin/env node
2+
import chalk from 'chalk';
23
import * as commandLineArgs from 'command-line-args';
34

5+
/**
6+
* Supported output style. Mirrors `style_option_strings[]` in sassc
7+
* (https://github.com/sass/sassc/blob/6a64d0569205bfcf0794a473f97affcb60b22fcc/sassc.c#L184-L189)
8+
*/
49
enum StyleOption {
510
SASS_STYLE_COMPRESSED = 'compressed',
611
SASS_STYLE_COMPACT = 'compact',
712
SASS_STYLE_EXPANDED = 'expanded',
813
SASS_STYLE_NESTED = 'nested'
914
}
1015

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

37+
/**
38+
* Get usage definition for library version.
39+
*
40+
*/
41+
const buildDisplayVersion = async () => {
42+
const { loadModule } = await import('./loadModule');
43+
const sassFactory = await loadModule();
44+
const { libsassAsm, libsass, sassLang, sass2scss } = await sassFactory.getVersion();
45+
46+
return {
47+
header: chalk.reset('Version'),
48+
content: [
49+
{ name: 'libsass-asm', summary: libsassAsm },
50+
{ name: 'libsass', summary: libsass },
51+
{ name: 'sass', summary: sassLang },
52+
{ name: 'sass2scss', summary: sass2scss }
53+
]
54+
};
55+
};
56+
2957
(async () => {
3058
const options = commandLineArgs(optionDefinitions, { camelCase: true });
3159

32-
if (options.help || Object.keys(options).length === 0) {
33-
const commandLineUsage = await import('command-line-usage');
34-
const usage = commandLineUsage([{ header: 'Options', optionList: optionDefinitions }]);
60+
const displayHelp = options.help || Object.keys(options).length === 0;
61+
const displayVersion = options.version;
62+
63+
if (displayHelp || displayVersion) {
64+
const cmdUsage = await import('command-line-usage');
65+
const usageDefinition = displayHelp
66+
? { header: chalk.reset('Options'), optionList: optionDefinitions }
67+
: await buildDisplayVersion();
68+
const usage = cmdUsage([usageDefinition]);
3569
console.log(usage);
36-
} else if (options.version) {
3770
return;
3871
}
3972
})();

0 commit comments

Comments
 (0)