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

Commit e039d46

Browse files
alan-agius4danbucholtz
authored andcommitted
feat(replacePathVars): support interpolation of objects and arrays (#449)
1 parent 51b7841 commit e039d46

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/spec/config.spec.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BuildContext } from '../util/interfaces';
2-
import { bundlerStrategy, generateContext, getConfigValue, getUserConfigFile, getIsProd } from '../util/config';
2+
import { bundlerStrategy, generateContext, getConfigValue, getUserConfigFile, getIsProd, replacePathVars } from '../util/config';
33
import { addArgv, setAppPackageJsonData, setProcessEnvVar, setProcessArgs, setProcessEnv, setCwd } from '../util/config';
44
import { resolve } from 'path';
55

@@ -95,6 +95,47 @@ describe('config', () => {
9595

9696
});
9797

98+
describe('replacePathVars', () => {
99+
it('should interpolated value when string', () => {
100+
const context = {
101+
srcDir: 'src',
102+
};
103+
104+
const rtn = replacePathVars(context, '{{SRC}}');
105+
expect(rtn).toEqual('src');
106+
});
107+
108+
it('should interpolated values in string array', () => {
109+
const context = {
110+
wwwDir: 'www',
111+
srcDir: 'src',
112+
};
113+
114+
const filePaths = ['{{SRC}}', '{{WWW}}'];
115+
const rtn = replacePathVars(context, filePaths);
116+
expect(rtn).toEqual(['src', 'www']);
117+
});
118+
119+
it('should interpolated values in key value pair', () => {
120+
const context = {
121+
wwwDir: 'www',
122+
srcDir: 'src',
123+
};
124+
125+
const filePaths = {
126+
src: '{{SRC}}',
127+
www: '{{WWW}}'
128+
};
129+
130+
const rtn = replacePathVars(context, filePaths);
131+
expect(rtn).toEqual({
132+
src: 'src',
133+
www: 'www'
134+
});
135+
});
136+
137+
});
138+
98139
describe('getConfigValue', () => {
99140

100141
it('should get arg full value', () => {

src/util/config.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,26 @@ export function hasArg(fullName: string, shortName: string = null): boolean {
253253
}
254254

255255

256-
export function replacePathVars(context: BuildContext, filePath: string) {
256+
export function replacePathVars(context: BuildContext, filePath: string | string[] | { [key: string]: any }): any {
257+
if (Array.isArray(filePath)) {
258+
return filePath.map(f => replacePathVars(context, f));
259+
}
260+
261+
if (typeof filePath === 'object') {
262+
const clonedFilePaths = Object.assign({}, filePath);
263+
for (let key in clonedFilePaths) {
264+
clonedFilePaths[key] = replacePathVars(context, clonedFilePaths[key]);
265+
}
266+
return clonedFilePaths;
267+
}
268+
257269
return filePath.replace('{{SRC}}', context.srcDir)
258270
.replace('{{WWW}}', context.wwwDir)
259271
.replace('{{TMP}}', context.tmpDir)
260272
.replace('{{ROOT}}', context.rootDir)
261273
.replace('{{BUILD}}', context.buildDir);
262274
}
263275

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

src/webpack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ const taskInfo: TaskInfo = {
207207
export interface WebpackConfig {
208208
// https://www.npmjs.com/package/webpack
209209
devtool: string;
210-
entry: string;
210+
entry: string | { [key: string]: any };
211211
output: WebpackOutputObject;
212212
resolve: WebpackResolveObject;
213213
}

0 commit comments

Comments
 (0)