|
1 | | -import * as yeoman from 'yeoman-generator'; |
2 | 1 | import * as glob from 'glob'; |
3 | 2 | import * as gitignore from 'gitignore-parser'; |
4 | 3 | import * as fs from 'fs'; |
5 | 4 | import * as path from 'path'; |
6 | 5 | import * as _ from 'lodash'; |
7 | | -import * as diff from 'diff'; |
8 | 6 | import * as mkdirp from 'mkdirp'; |
9 | 7 | import * as rimraf from 'rimraf'; |
10 | 8 |
|
@@ -33,63 +31,18 @@ function listFilesExcludingGitignored(root: string): string[] { |
33 | 31 | .filter(fn => gitignoreEvaluator.accepts(fn)); |
34 | 32 | } |
35 | 33 |
|
36 | | -function writeCommonFiles(outDir: string) { |
37 | | - let filesByTemplate = _.mapValues(templates, listFilesExcludingGitignored); |
38 | | - let commonFiles = _.intersection.apply(_, _.values(filesByTemplate)); |
39 | | - |
40 | | - commonFiles.forEach(fn => { |
41 | | - let templateRoots = _.values(templates); |
42 | | - let origContent = fs.readFileSync(path.join(templateRoots[0], fn)); |
43 | | - |
44 | | - if (isTextFile(fn)) { |
45 | | - // For text files, we copy the portion that's common to all the templates |
46 | | - let commonText = origContent.toString('utf8'); |
47 | | - templateRoots.slice(1).forEach(otherTemplateRoot => { |
48 | | - let otherTemplateContent = fs.readFileSync(path.join(otherTemplateRoot, fn), 'utf8'); |
49 | | - commonText = diff.diffLines(commonText, otherTemplateContent) |
50 | | - .filter(c => !(c.added || c.removed)) |
51 | | - .map(c => c.value) |
52 | | - .join(''); |
53 | | - }); |
54 | | - |
55 | | - writeFileEnsuringDirExists(outDir, fn, commonText); |
56 | | - } else { |
57 | | - // For binary (or maybe-binary) files, we only consider them common if they are identical across all templates |
58 | | - let isIdenticalEverywhere = !templateRoots.slice(1).some(otherTemplateRoot => { |
59 | | - return !fs.readFileSync(path.join(otherTemplateRoot, fn)).equals(origContent); |
60 | | - }); |
61 | | - if (isIdenticalEverywhere) { |
62 | | - writeFileEnsuringDirExists(outDir, fn, origContent); |
63 | | - } |
64 | | - } |
65 | | - }); |
66 | | -} |
67 | | - |
68 | | -function writeDiffsForTemplate(sourceRoot: string, destRoot: string, commonRoot: string) { |
| 34 | +function writeTemplate(sourceRoot: string, destRoot: string) { |
69 | 35 | listFilesExcludingGitignored(sourceRoot).forEach(fn => { |
70 | | - const commonFn = path.join(commonRoot, fn); |
71 | | - const sourceContent = fs.readFileSync(path.join(sourceRoot, fn)); |
72 | | - |
73 | | - if (!fs.existsSync(commonFn)) { |
74 | | - // This file is unique to this template - just copy as-is |
75 | | - writeFileEnsuringDirExists(destRoot, fn, sourceContent); |
76 | | - } else { |
77 | | - let commonText = fs.readFileSync(commonFn, 'utf8'); |
78 | | - let sourceText = sourceContent.toString('utf8'); |
79 | | - if (commonText !== sourceText) { |
80 | | - // Write a diff vs the common version of this file |
81 | | - let fileDiff = diff.createPatch(fn, commonText, sourceText, null, null); |
82 | | - writeFileEnsuringDirExists(destRoot, fn + '.patch', fileDiff); |
83 | | - } |
84 | | - } |
85 | | - }); |
| 36 | + const sourceContent = fs.readFileSync(path.join(sourceRoot, fn)); |
| 37 | + writeFileEnsuringDirExists(destRoot, fn, sourceContent); |
| 38 | + }); |
86 | 39 | } |
87 | 40 |
|
88 | 41 | const outputRoot = './generator-aspnet-spa'; |
89 | 42 | const commonRoot = path.join(outputRoot, 'templates/common'); |
90 | 43 | rimraf.sync(outputRoot); |
91 | | -writeCommonFiles(commonRoot); |
92 | 44 |
|
93 | 45 | _.forEach(templates, (templateRootDir, templateName) => { |
94 | | - writeDiffsForTemplate(templateRootDir, path.join(outputRoot, 'templates', templateName), commonRoot); |
| 46 | + const outputDir = path.join(outputRoot, 'templates', templateName); |
| 47 | + writeTemplate(templateRootDir, outputDir); |
95 | 48 | }); |
0 commit comments