Skip to content

Commit 2dcfefb

Browse files
author
Peter Ullrich
committed
-skippedTemplateFiles
-json merge :(
1 parent abce9e6 commit 2dcfefb

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/BaseGenerator.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export abstract class BaseGenerator<TStep extends string> extends generator {
2525

2626
readonly projectInfo: ProjectInfo;
2727
private readonly doNotEjsReplace: string[] = [];
28+
private readonly skippedTemplateFiles: string[] = [];
2829
private readonly generatorName: string;
2930

3031
skipQuestions: boolean = false;
@@ -49,6 +50,12 @@ export abstract class BaseGenerator<TStep extends string> extends generator {
4950
_.merge(options, this.sharedOptions.values);
5051
}
5152

53+
if ((<IProperty>options).skippedTemplateFiles !== undefined) {
54+
(<string[]>(<IProperty>options).skippedTemplateFiles).forEach(f => {
55+
this.addSkippedTemplateFiles(f);
56+
})
57+
}
58+
5259
this.generatorName = this.constructor.name;
5360
this.projectInfo = new ProjectInfo();
5461

@@ -80,6 +87,11 @@ export abstract class BaseGenerator<TStep extends string> extends generator {
8087
this.doNotEjsReplace.push(targetPath);
8188
}
8289

90+
addSkippedTemplateFiles(targetPath: string): void {
91+
const tmp = path.normalize(targetPath);
92+
this.skippedTemplateFiles.push(tmp);
93+
}
94+
8395
isAnswered(): boolean {
8496
const required = this.questions.filter(item => item.isRequired === true);
8597

@@ -371,17 +383,38 @@ export abstract class BaseGenerator<TStep extends string> extends generator {
371383
ext = path.basename(file.filePath);
372384
}
373385

386+
// Skip this file?
387+
if (this.skippedTemplateFiles.includes(file.targetPath)) {
388+
return;
389+
}
390+
374391
if (this.fs.exists(this.destinationPath(file.targetPath))) {
375392

376393
switch (ext) {
377394
case '.ts':
378395
throw new Error(`Resolving conflicted ${ext} files not implemented.`);
379396

380397
case '.json':
381-
const fileContent = fs.readFileSync(file.filePath, 'utf-8');
382-
const addJsonContent = JSON.parse(fileContent);
398+
const newJsonContent = {};
399+
const existingJsonFileContent = this.fs.read(file.targetPath, 'utf-8');
400+
const existingJsonContent = JSON.parse(existingJsonFileContent);
401+
const addJsonFileContent = fs.readFileSync(file.filePath, 'utf-8');
402+
const addJsonContent = JSON.parse(addJsonFileContent);
403+
404+
_.mergeWith(newJsonContent, existingJsonContent, (objValue, srcValue) => {
405+
if (_.isArray(objValue)) {
406+
return objValue.concat(srcValue);
407+
}
408+
});
409+
_.mergeWith(newJsonContent, addJsonContent, (objValue, srcValue) => {
410+
if (_.isArray(objValue)) {
411+
return objValue.concat(srcValue);
412+
}
413+
});
414+
383415
// tslint:disable-next-line: no-unsafe-any
384-
this.fs.extendJSON(this.destinationPath(file.targetPath), addJsonContent);
416+
// this.fs.extendJSON(this.destinationPath(file.targetPath), addJsonContent);
417+
this.fs.write(this.destinationPath(file.targetPath), JSON.stringify(newJsonContent, undefined, 2));
385418
if (!this.doNotEjsReplace.includes(file.targetPath)) {
386419
this.fs.copyTpl(this.destinationPath(file.targetPath), this.destinationPath(file.targetPath), this.answers);
387420
}

0 commit comments

Comments
 (0)