Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
feat(replacePathVars): support interpolation of objects and arrays (#449
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alan-agius4 authored and danbucholtz committed Nov 22, 2016
1 parent 51b7841 commit e039d46
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
43 changes: 42 additions & 1 deletion src/spec/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BuildContext } from '../util/interfaces';
import { bundlerStrategy, generateContext, getConfigValue, getUserConfigFile, getIsProd } from '../util/config';
import { bundlerStrategy, generateContext, getConfigValue, getUserConfigFile, getIsProd, replacePathVars } from '../util/config';
import { addArgv, setAppPackageJsonData, setProcessEnvVar, setProcessArgs, setProcessEnv, setCwd } from '../util/config';
import { resolve } from 'path';

Expand Down Expand Up @@ -95,6 +95,47 @@ describe('config', () => {

});

describe('replacePathVars', () => {
it('should interpolated value when string', () => {
const context = {
srcDir: 'src',
};

const rtn = replacePathVars(context, '{{SRC}}');
expect(rtn).toEqual('src');
});

it('should interpolated values in string array', () => {
const context = {
wwwDir: 'www',
srcDir: 'src',
};

const filePaths = ['{{SRC}}', '{{WWW}}'];
const rtn = replacePathVars(context, filePaths);
expect(rtn).toEqual(['src', 'www']);
});

it('should interpolated values in key value pair', () => {
const context = {
wwwDir: 'www',
srcDir: 'src',
};

const filePaths = {
src: '{{SRC}}',
www: '{{WWW}}'
};

const rtn = replacePathVars(context, filePaths);
expect(rtn).toEqual({
src: 'src',
www: 'www'
});
});

});

describe('getConfigValue', () => {

it('should get arg full value', () => {
Expand Down
15 changes: 13 additions & 2 deletions src/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,26 @@ export function hasArg(fullName: string, shortName: string = null): boolean {
}


export function replacePathVars(context: BuildContext, filePath: string) {
export function replacePathVars(context: BuildContext, filePath: string | string[] | { [key: string]: any }): any {
if (Array.isArray(filePath)) {
return filePath.map(f => replacePathVars(context, f));
}

if (typeof filePath === 'object') {
const clonedFilePaths = Object.assign({}, filePath);
for (let key in clonedFilePaths) {
clonedFilePaths[key] = replacePathVars(context, clonedFilePaths[key]);
}
return clonedFilePaths;
}

return filePath.replace('{{SRC}}', context.srcDir)
.replace('{{WWW}}', context.wwwDir)
.replace('{{TMP}}', context.tmpDir)
.replace('{{ROOT}}', context.rootDir)
.replace('{{BUILD}}', context.buildDir);
}


export function getNodeBinExecutable(context: BuildContext, cmd: string) {
let cmdPath = join(context.rootDir, 'node_modules', '.bin', cmd);

Expand Down
2 changes: 1 addition & 1 deletion src/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ const taskInfo: TaskInfo = {
export interface WebpackConfig {
// https://www.npmjs.com/package/webpack
devtool: string;
entry: string;
entry: string | { [key: string]: any };
output: WebpackOutputObject;
resolve: WebpackResolveObject;
}
Expand Down

0 comments on commit e039d46

Please sign in to comment.