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

Commit

Permalink
feat(cli): build sass options
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jul 10, 2018
1 parent e2377f8 commit 318de80
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
#!/usr/bin/env node
import chalk from 'chalk';
import * as commandLineArgs from 'command-line-args';
import * as debug from 'debug';
import { OutputStyle } from './index';
import { buildContext } from './interop/context';
import './verbose';

const d = debug('libsass:cli');
const styleOptions = ['nested', 'expanded', 'compact', 'compressed'];
/**
* Definitions of available command line args.
*/
const optionDefinitions = [
{ name: 'stdin', alias: 's', description: 'Read input from standard input instead of an input file.' },
{ name: 'stdin', alias: 's', description: 'Read input from standard input instead of an input file.', type: Boolean },
{
name: 'style',
alias: 't',
description: `Output style. Can be: ${styleOptions.join(', ')}.`
},
{ name: 'line-numbers', alias: 'l', description: 'Emit comments showing original line numbers.' },
{ name: 'line-numbers', alias: 'l', description: 'Emit comments showing original line numbers.', type: Boolean },
{ name: 'load-path', alias: 'I', description: 'Set Sass import path.' },
{ name: 'plugin-path', alias: 'P', description: 'Set path to autoload plugins.' },
{ name: 'sourcemap', alias: 'm', description: 'Emit source map (auto or inline).' },
{ 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: 'omit-map-comment', alias: 'M', description: 'Omits the source map url comment.', type: Boolean },
{ name: 'precision', alias: 'p', description: 'Set the precision for numbers.', type: Number },
{ name: 'sass', alias: 'a', description: 'Treat input as indented syntax.', type: Boolean },
{ name: 'version', alias: 'v', description: 'Display compiled versions.', type: Boolean },
{ name: 'help', alias: 'h', description: 'Display this help message.', type: Boolean }
];
Expand Down Expand Up @@ -59,11 +62,54 @@ const buildDisplayVersion = async () => {
];
};

/**
* Construct value of sass option per command line options.
*
* @param sassOption
*/
const buildSassOption = (context: ReturnType<typeof buildContext>, options: commandLineArgs.CommandLineOptions) => {
const sassOption = context.options.create();
//Set default values
sassOption.outputStyle = OutputStyle.SASS_STYLE_NESTED;
sassOption.precision = 5;

Object.keys(options)
.map(key => ({ key, value: options[key] }))
.forEach(({ key, value }) => {
switch (key) {
case 'stdin':
break;
case 'style':
break;
case 'lineNumbers':
sassOption.sourceComments = true;
break;
case 'loadPath':
break;
case 'pluginPath':
break;
case 'sourcemap':
break;
case 'omitMapComment':
break;
case 'precision':
sassOption.precision = parseInt(value, 10);
break;
case 'sass':
sassOption.isIndentedSyntaxSrc = true;
break;
}
});

return sassOption;
};

(async () => {
const options = commandLineArgs(optionDefinitions, { camelCase: true });
const displayHelp = options.help || Object.keys(options).length === 0;
const displayVersion = options.version;

d(`Received options`, { options });
if (displayHelp || displayVersion) {
const cmdUsage = await import('command-line-usage');
const usageDefinition = displayHelp ? helpDefinitions : await buildDisplayVersion();
Expand All @@ -74,9 +120,7 @@ const buildDisplayVersion = async () => {

const { loadModule } = await import('./loadModule');
const { context } = await loadModule();
const sassOption = context.options.create();
sassOption.outputStyle = OutputStyle.SASS_STYLE_NESTED;
sassOption.precision = 5;
const sassOption = buildSassOption(context, options);

sassOption.dispose();
})();

0 comments on commit 318de80

Please sign in to comment.