Skip to content

Commit

Permalink
fix: file encoding of binary files in template (#155)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: Gokberk Kocak <[email protected]>
Co-authored-by: Matthew Smith <[email protected]>
  • Loading branch information
3 people authored Feb 1, 2023
1 parent f784354 commit ced85a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ function processTemplateFactory(
log.verbose("Copying to output location");
// for other files, simply copy them to the output folder
createNestedDirectories(schema, file, outputFolder);
fs.writeFileSync(`${outputFolder}/${file}`, source);
// Copy instead of read/write to support copying of binary files.
fs.copyFileSync(
`${generatorTemplatesPath}/${file}`,
`${outputFolder}/${file}`
);
}
};
}
Expand Down
16 changes: 8 additions & 8 deletions test/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ describe("generate", () => {
skipValidation: true,
output: outDir,
});
console.error(fs.writeFileSync.mock.calls);
expect(fs.writeFileSync).toHaveBeenCalledWith(
`${outDir}/${fileName}.${fileExtension}`,
fakeSchema
console.error(fs.copyFileSync.mock.calls);
expect(fs.copyFileSync).toHaveBeenCalledWith(
`${generatorPath}/template/${fileName}.${fileExtension}`,
`${outDir}/${fileName}.${fileExtension}`
);
});

Expand All @@ -75,7 +75,7 @@ describe("generate", () => {
);
});

it("should copy files in directories", async () => {
it("should copy non-handlebars files in directories", async () => {
const fileShortName = "ExampleFile";
const extension = "java";
const basePath = `somewhere/else`;
Expand All @@ -89,9 +89,9 @@ describe("generate", () => {
skipValidation: true,
output: outDir,
});
expect(fs.writeFileSync).toHaveBeenCalledWith(
`${outDir}/${basePath}/${fileShortName}.${extension}`,
fakeSchema
expect(fs.copyFileSync).toHaveBeenCalledWith(
`${generatorPath}/template/${basePath}/${fileShortName}.${extension}`,
`${outDir}/${basePath}/${fileShortName}.${extension}`
);
});
it("should change the filename of Handlebars files in directories", async () => {
Expand Down

0 comments on commit ced85a2

Please sign in to comment.