Skip to content

Commit

Permalink
fix(cli): streamline cli based on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joneff committed Jan 7, 2023
1 parent 81b0622 commit 13abdc2
Show file tree
Hide file tree
Showing 43 changed files with 231 additions and 191 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"main": "dist/index.js",
"bin": "bin/sass-build.js",
"types": "lib/types/index.d.ts",
"types": "types/index.d.ts",
"files": [
"bin/",
"dist/",
Expand Down
2 changes: 1 addition & 1 deletion src/build/build-files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { wrapCompiler } from '../compiler';

export function sassBuildFiles( file: string | string[], output: OutputOptions = <OutputOptions>{}, options?: CliBuildOptions ) : void {
export function sassBuildFiles( file: string | string[], output: OutputOptions = <OutputOptions> {}, options?: SassCompilerOptions ) : void {
const compiler = wrapCompiler( options );

compiler.buildFiles( file, output, options?.sassOptions );
Expand Down
2 changes: 1 addition & 1 deletion src/build/build-string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { wrapCompiler } from '../compiler';

export function sassBuildString( source: string, outFile: string, options?: CliBuildOptions ) : void {
export function sassBuildString( source: string, outFile: string, options?: SassCompilerOptions ) : void {
const compiler = wrapCompiler( options );

compiler.buildString( source, outFile, options?.sassOptions );
Expand Down
2 changes: 1 addition & 1 deletion src/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { wrapCompiler } from '../compiler';

export function sassBuild( file: string, outFile: string, options?: CliBuildOptions ) : void {
export function sassBuild( file: string, outFile: string, options?: SassCompilerOptions ) : void {
const compiler = wrapCompiler( options );

compiler.build( file, outFile, options?.sassOptions );
Expand Down
15 changes: 10 additions & 5 deletions src/cli/commands/build/validate-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,27 @@ export function validateParams( params ) {
}
}

if ( source !== undefined ) {
if ( typeof source !== 'string' ) {
throw new Error( '--source must be string.' );
}
if (outFile === undefined) {
throw new Error( 'Supply --outFile.' );
}
}

if ( glob !== undefined ) {
if (typeof glob !== 'string' && !Array.isArray(glob)) {
throw new Error( '--glob must be either string or array.' );
}
if (glob === '' || (Array.isArray( glob ) && glob.length === 0 ) ) {
throw new Error( '--glob must not be empty.' );
}
if (Array.isArray(glob) && outFile) {
if (outFile) {
throw new Error( 'Supply --outDir instead of --outFile.' );
}
}

if ( source !== undefined && typeof source !== 'string' ) {
throw new Error( '--source must be string.' );
}

if ( outFile !== undefined && typeof outFile !== 'string' ) {
throw new Error( '--outFile must be string.' );
}
Expand Down
20 changes: 9 additions & 11 deletions src/cli/commands/compile/validate-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ import { logger } from "../../../utils";
export function validateParams( params ) {
logger.silly('cli > compile', 'Validating params...');

const { file, source, glob, outFile, outDir } = params;
const { file, source, outFile, outDir } = params;

if (file && source) {
throw new Error('Supply either --file or --source, but not both.');
}
if ((!file && !source)) {
if ( file === undefined && source === undefined ) {
throw new Error('Supply either --file or --source.');
}

if (glob || outFile || outDir) {
throw new Error('Do not supply --glob, --outFile or --outDir.');
}

if (file && typeof file !== 'string') {
if ( file && typeof file !== 'string' ) {
throw new Error('--file must be string.');
}
if (source && typeof source !== 'string') {

if ( source && typeof source !== 'string' ) {
throw new Error('--source must be string.');
}

if ( outFile !== undefined || outDir !== undefined ) {
throw new Error('Do not supply --outFile or --outDir.');
}

logger.silly('cli > compile', 'Params validated.');
}
7 changes: 6 additions & 1 deletion src/cli/commands/info/validate-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
export function validateParams( params ) {
const { file, source, glob, outFile, outDir } = params;

if ( file || source || glob || outFile || outDir ) {
if ( file !== undefined
|| source !== undefined
|| glob !== undefined
|| outFile !== undefined
|| outDir !== undefined
) {
throw new Error( 'Command "info" does not accept additional parameters.' );
}

Expand Down
51 changes: 36 additions & 15 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference path="../../types/index.d.ts" />

import fs from 'fs';
import path from 'path';
import argsParser from 'yargs-parser';
Expand Down Expand Up @@ -30,6 +32,24 @@ export function cli() {

const argv = argsParser( process.argv.slice(2), ARGS_PARSER_OPTIONS );

const { command, params, config } = parseArgs( argv );

if ( typeof command === 'function' ) {
command( params );
exitSuccess();
}

if ( config !== undefined ) {
processConfigFile( config );
exitSuccess();
}

process.stdout.write( 'No command passed and no default config found!\n' );
process.exit(0);
}

function parseArgs( argv : argsParser.Arguments ) : { command?: Function, params?: any, config?: string } {

try {
validateParams( argv, ARGS_PARSER_OPTIONS );
} catch ( err ) {
Expand All @@ -53,37 +73,38 @@ export function cli() {
if ( debug ) {
logger.level = 'debug';
logger.silly( 'cli', 'Starting in debug mode.' );
logger.silly( 'cli', 'Arguments parsed.' );
}

if ( commandName ) {
logger.silly( 'cli', 'Command passed' );
logger.silly( 'cli', 'Command is: %s', commandName );

command( params );
exitSuccess();
return { command, params };
}
logger.silly( 'cli', 'No command passed.' );

if ( config ) {
logger.silly( 'cli', 'Config passed' );
logger.silly( 'cli', 'Config passed: %s', config );

processConfigFile( config );
exitSuccess();
return { config };
}

logger.silly( 'cli', 'No config passed.' );

if ( Object.keys(params).length > 0 ) {
logger.silly( 'cli', 'Parameters passed.' );
logger.silly( 'cli', 'Falling back to build command' );
const hasParams = Object.values(params).filter( item => item !== undefined).length > 0;
const hasConfig = fs.existsSync( 'sass.config.js' );

commands.get( 'build' )( params );
exitSuccess();
if ( hasParams ) {
logger.silly( 'cli', 'Attempting build command with cli parameters' );

return { command: commands.get( 'build' ), params };
}

if ( hasConfig ) {
logger.silly( 'cli', 'Attempting to process default config' );

return { config: null };
}

processConfigFile( null );
exitSuccess();
return {};
}

function exitSuccess() {
Expand Down
38 changes: 22 additions & 16 deletions src/cli/validate-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ export function validateParams( argv: argsParser.Arguments, argsParserOpts: args
throw new Error( 'Unknown parameters passed.' );
}

// Throw error for extra parameters for version command
if ( version
// Throw error for command with --version parameter
if ( version && commandName !== undefined ) {
throw new Error( '--version parameter cannot be passed to commands.' );
}

// Throw error for --version parameter with other parameters
if (
version
&& (
commandName
params.length > 0
|| restParams.length > 0
|| config
|| debug
)
) {
throw new Error( 'Version parameter cannot be passed with other parameters.' );
throw new Error( '--version parameter cannot be used with other parameters.' );
}

// Throw error for empty command name
Expand All @@ -45,37 +51,37 @@ export function validateParams( argv: argsParser.Arguments, argsParserOpts: args

// Throw error for unknown command
if ( commandName !== undefined && typeof command !== 'function' ) {
throw new Error( `Command not found: ${commandName}` );
}

// Throw error for command and config passed together
if ( commandName !== undefined && config !== undefined) {
throw new Error( `Command ${commandName} and config param cannot be passed together.` );
throw new Error( `Command not found: ${commandName}.` );
}

// Throw error for debug parameter for info
if ( commandName === 'info' && debug ) {
throw new Error( 'Debug parameter cannot be passed with info command.' );
throw new Error( '--debug parameter cannot be passed to "info" command.' );
}

// Throw error for command with config parameter
if ( commandName !== undefined && config !== undefined) {
throw new Error( '--config parameter cannot be passed to commands.' );
}

// Throw error for extra parameters for config
if ( config !== undefined && params.length > 0 ) {
throw new Error( 'Config parameter cannot be passed with other parameters.' );
throw new Error( '--config parameter cannot be used with other parameters.' );
}

if ( file !== undefined && source !== undefined ) {
throw new Error( 'File and source parameters cannot be passed together.' );
throw new Error( '--file and --source parameters cannot be used together.' );
}

if ( file !== undefined && glob !== undefined ) {
throw new Error( 'File and glob parameters cannot be passed together.' );
throw new Error( '--file and --glob parameters cannot be used together.' );
}

if ( source !== undefined && glob !== undefined ) {
throw new Error( 'Source and glob parameters cannot be passed together.' );
throw new Error( '--source and --glob parameters cannot be used together.' );
}

if ( outFile !== undefined && outDir !== undefined ) {
throw new Error( 'Out file and out dir parameters cannot be passed together.' );
throw new Error( '--outFile and --outDir parameters cannot be used together.' );
}
}
2 changes: 1 addition & 1 deletion src/compile/compile-string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { wrapCompiler } from '../compiler';

export function sassCompileString( source: string, options?: CliBuildOptions ) : string {
export function sassCompileString( source: string, options?: SassCompilerOptions ) : string {
const compiler = wrapCompiler( options );

return compiler.compileString( source, options?.sassOptions );
Expand Down
2 changes: 1 addition & 1 deletion src/compile/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { wrapCompiler } from '../compiler';

export function sassCompile( file: string, options?: CliBuildOptions ) : string {
export function sassCompile( file: string, options?: SassCompilerOptions ) : string {
const compiler = wrapCompiler( options );

return compiler.compile( file, options?.sassOptions );
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/base-sass-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
export abstract class BaseSassCompiler implements SassCompiler {
protected compiler: NativeSassCompiler;
protected postcss: PostcssProcessor;
protected options: CliBuildOptions;
protected options: SassCompilerOptions;

constructor(options: CliBuildOptions) {
constructor(options: SassCompilerOptions) {
this.options = options;
this.compiler = <NativeSassCompiler> this.options.compiler;
this.postcss = <PostcssProcessor> this.options.postcss;
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { ModernSassCompiler } from './modern-sass-compiler';
import { LegacySassCompiler } from './legacy-sass-compiler';


export function wrapCompiler( options?: CliBuildOptions ) : SassCompiler {
const defaults = <Partial<CliBuildOptions>>{
export function wrapCompiler( options?: SassCompilerOptions ) : SassCompiler {
const defaults = <Partial<SassCompilerOptions>> {
cwd: CWD
};
const opts = _.defaultsDeep( {}, options, defaults );
const opts = <SassCompilerOptions> _.defaultsDeep( {}, options, defaults );

opts.compiler = getSassCompiler( opts.compiler );
opts.postcss = getPostcss( opts.postcss );
Expand Down
Empty file added types/build/build-options.d.ts
Empty file.
1 change: 1 addition & 0 deletions types/build/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference path="build-options.d.ts" />
9 changes: 1 addition & 8 deletions types/cli/cli-options.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
declare type ApiType = 'modern' | 'legacy';

declare type CommonCliOptions = {
cwd: string;
}


declare type CliBuildOptions = CommonCliOptions & {
compiler: string | NativeSassCompiler;
api: ApiType;
postcss: false | 'auto' | [] | PostcssProcessor;
sassOptions: SassOptions;
}
declare type CliBuildOptions = CommonCliOptions & SassCompilerOptions;
declare type CliBuilder = string | Function | {
buildFile: Function;
buildGlob: Function;
Expand Down
Empty file.
1 change: 1 addition & 0 deletions types/compile/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference path="compile-options.d.ts" />
3 changes: 3 additions & 0 deletions types/compiler/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// <reference path="sass-compile-result.d.ts" />
/// <reference path="sass-compiler.d.ts" />
/// <reference path="sass-options.d.ts" />
File renamed without changes.
34 changes: 34 additions & 0 deletions types/compiler/sass-compiler.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
declare type ApiType = 'legacy' | 'modern';

declare type SassCompilerOptions = {
compiler: string | NativeSassCompiler;
api: ApiType;
postcss: false | 'auto' | [] | PostcssProcessor;
sassOptions: SassOptions;
cwd: string;
};

declare type SassCompiler = {
build(file: string, outFile: string, sassOptions?: SassOptions) : void;
buildFiles(files: string | string[], output: OutputOptions, sassOptions?: SassOptions) : void;
buildString(source: string, outFile: string, sassOptions?: SassOptions) : void;
compile(file: string, sassOptions?: SassOptions) : string;
compileString(source: string, sassOptions?: SassOptions) : string;
info: string;
}

declare type LegacySassImplementation = {
renderSync(options: LegacySassOptions): LegacyCompileResult;
renderSync(options: Omit<LegacySassOptions, 'data'>): LegacyCompileResult;
logger: SassLogger;
info: string;
}

declare type ModernSassImplementation = {
compile(file: string, options: ModernSassOptions) : ModernCompileResult;
compileString(source: string, options: ModernSassOptions): ModernCompileResult;
logger: SassLogger;
info: string;
}

declare type NativeSassCompiler = LegacySassImplementation | ModernSassImplementation;
File renamed without changes.
Loading

0 comments on commit 13abdc2

Please sign in to comment.