From 02652d6f7bb90f2933a43818c54a5a944d5d7a26 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 22 Nov 2016 07:45:37 +0100 Subject: [PATCH] feat(replacePathVars): support interpolation of objects and arrays --- src/spec/config.spec.ts | 43 ++++++++++++++++++++++++++++++++++++++++- src/util/config.ts | 15 ++++++++++++-- src/webpack.ts | 2 +- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/spec/config.spec.ts b/src/spec/config.spec.ts index a934c313..a9086a3c 100644 --- a/src/spec/config.spec.ts +++ b/src/spec/config.spec.ts @@ -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'; @@ -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', () => { diff --git a/src/util/config.ts b/src/util/config.ts index e8e6366a..7090dc30 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -253,7 +253,19 @@ 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) @@ -261,7 +273,6 @@ export function replacePathVars(context: BuildContext, filePath: string) { .replace('{{BUILD}}', context.buildDir); } - export function getNodeBinExecutable(context: BuildContext, cmd: string) { let cmdPath = join(context.rootDir, 'node_modules', '.bin', cmd); diff --git a/src/webpack.ts b/src/webpack.ts index 4e6e18c2..a1b021cd 100644 --- a/src/webpack.ts +++ b/src/webpack.ts @@ -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; }