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

feat(replacePathVars): support interpolation of objects and arrays #449

Merged
merged 1 commit into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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