Skip to content

Commit ced85a2

Browse files
gkocak-scottlogicgokberkkocakMatthew Smith
authored
fix: file encoding of binary files in template (#155)
* Fix: file encoding of binary files in template * Fix: tests writeFileSync > copyFileSync * fix: code style with prettier * Add a comment for write to copy change Co-authored-by: Matthew Smith <ms14981.2014@my.bristol.ac.uk> --------- Co-authored-by: Gokberk Kocak <gokberkkocak@gmail.com> Co-authored-by: Matthew Smith <ms14981.2014@my.bristol.ac.uk>
1 parent f784354 commit ced85a2

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

src/generate.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ function processTemplateFactory(
131131
log.verbose("Copying to output location");
132132
// for other files, simply copy them to the output folder
133133
createNestedDirectories(schema, file, outputFolder);
134-
fs.writeFileSync(`${outputFolder}/${file}`, source);
134+
// Copy instead of read/write to support copying of binary files.
135+
fs.copyFileSync(
136+
`${generatorTemplatesPath}/${file}`,
137+
`${outputFolder}/${file}`
138+
);
135139
}
136140
};
137141
}

test/generate.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ describe("generate", () => {
5050
skipValidation: true,
5151
output: outDir,
5252
});
53-
console.error(fs.writeFileSync.mock.calls);
54-
expect(fs.writeFileSync).toHaveBeenCalledWith(
55-
`${outDir}/${fileName}.${fileExtension}`,
56-
fakeSchema
53+
console.error(fs.copyFileSync.mock.calls);
54+
expect(fs.copyFileSync).toHaveBeenCalledWith(
55+
`${generatorPath}/template/${fileName}.${fileExtension}`,
56+
`${outDir}/${fileName}.${fileExtension}`
5757
);
5858
});
5959

@@ -75,7 +75,7 @@ describe("generate", () => {
7575
);
7676
});
7777

78-
it("should copy files in directories", async () => {
78+
it("should copy non-handlebars files in directories", async () => {
7979
const fileShortName = "ExampleFile";
8080
const extension = "java";
8181
const basePath = `somewhere/else`;
@@ -89,9 +89,9 @@ describe("generate", () => {
8989
skipValidation: true,
9090
output: outDir,
9191
});
92-
expect(fs.writeFileSync).toHaveBeenCalledWith(
93-
`${outDir}/${basePath}/${fileShortName}.${extension}`,
94-
fakeSchema
92+
expect(fs.copyFileSync).toHaveBeenCalledWith(
93+
`${generatorPath}/template/${basePath}/${fileShortName}.${extension}`,
94+
`${outDir}/${basePath}/${fileShortName}.${extension}`
9595
);
9696
});
9797
it("should change the filename of Handlebars files in directories", async () => {

0 commit comments

Comments
 (0)