Skip to content

Commit

Permalink
fix(cli): extract migrate as a separate command
Browse files Browse the repository at this point in the history
  • Loading branch information
joneff committed Sep 5, 2022
1 parent c971ca7 commit 374a0de
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from './utils';
import { sassBuild, sassBuildFiles, sassBuildString } from './build';
import { sassCompile } from './compile';
import { migrateCompilerConfig } from './config-migrate';
import { migrate } from './migrate';
import { buildConfig } from './config-build';

const commandMap = {
Expand Down Expand Up @@ -231,12 +231,12 @@ function cli_compile(params) {

// eslint-disable-next-line camelcase
function cli_migrate(params) {
const { file, outFile } = params;
const { file, outFile, transformer } = params;

const src = file || COMPILER_CONFIG;
const dest = outFile || SASS_CONFIG;

migrateCompilerConfig( src, dest );
migrate( src, dest, transformer );
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import fs from 'fs';

import {
exit,
logger,
requireUserFile,
fileExists,
writeFile
} from './utils';
fileExists
} from '../utils';

type CompilerConfigFileEntry = {
type CompilerConfigEntry = {
inputFile: string;
outputFile: string;
minify: {
Expand All @@ -18,24 +17,21 @@ type CompilerConfigFileEntry = {
}
}

export function migrateCompilerConfig(src : string, dest : string) {

if (!fileExists(src)) {
exit(2, 'error', 'migrate', `Cannot find file ${src}`);
export function compilerConfigTransformer(pathToConfig: string) {
if (!fileExists(pathToConfig)) {
exit(2, 'error', 'migrate > transformer', `Cannot find file ${pathToConfig}`);
}

logger.silly('migrate', 'Found %s.', src);
logger.silly('migrate', ' Migrating...');
const compilerConfigJson : CompilerConfigEntry[] = JSON.parse(fs.readFileSync( pathToConfig, 'utf-8' ));

const compilerConfigContent : CompilerConfigFileEntry[] = requireUserFile(src);
let sassConfigContent : any = {
extends: [
'sass-build:recommended'
],
files: []
};

compilerConfigContent.forEach(oldEntry => {
compilerConfigJson.forEach(oldEntry => {
const newEntry = {
file: oldEntry.inputFile,
outFile: oldEntry.outputFile,
Expand All @@ -52,8 +48,5 @@ export function migrateCompilerConfig(src : string, dest : string) {
return space + word.replaceAll('"', '');
});

writeFile( dest, sassConfigContent );

logger.info('migrate', 'Successfully migrated %s to %s', src, dest );

return sassConfigContent;
}
1 change: 1 addition & 0 deletions src/migrate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './migrate';
37 changes: 37 additions & 0 deletions src/migrate/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
exit,
logger,
requireUserFile,
fileExists,
writeFile
} from '../utils';
import { compilerConfigTransformer } from './complier-config-transformer';


export function migrate(src: string, dest: string, transformer: string | Function = compilerConfigTransformer) {

if (!fileExists(src)) {
exit(2, 'error', 'migrate', `Cannot find file ${src}`);
}

if (typeof transformer === 'string') {
if (!fileExists(transformer)) {
exit(2, 'error', 'migrate', `Cannot find file ${src}`);
}
// eslint-disable-next-line no-param-reassign
transformer = <Function> requireUserFile(transformer);
}

if (typeof transformer !== 'function') {
exit(22, 'error', 'migrate', `Transformer is not a function.`);
}

logger.silly('migrate', 'Found %s.', src);
logger.silly('migrate', ' Migrating...');

const sassConfigContent = transformer( src );

writeFile( dest, sassConfigContent );

logger.info('migrate', 'Successfully migrated %s to %s', src, dest );
}

0 comments on commit 374a0de

Please sign in to comment.