Skip to content

Commit

Permalink
fix: fix binary file copy on favicon.ico
Browse files Browse the repository at this point in the history
Set encoding:null on binary file resource.
fs.readFile returns raw buffer.
fs.writeFile ignores our default 'utf8' encoding when
incoming data is a buffer.

Closes #688.
  • Loading branch information
3cp committed Mar 10, 2018
1 parent dcdd0f1 commit f7941f4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/commands/new/buildsystems/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function(project, options) {
ProjectItem.resource('index.ejs', 'content/index-webpack.ejs'),
ProjectItem.resource('package-scripts.js', 'content/package-scripts.template.js')
.asTemplate(normalizeForPreprocess(model)),
ProjectItem.resource('static/favicon.ico', 'content/favicon.ico'),
ProjectItem.resource('static/favicon.ico', 'content/favicon.ico').binaryFile(),
ProjectItem.resource('webpack.config.js', 'content/webpack.config.template.js')
.asTemplate(model)
).addToDevDependencies(
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/new/project-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ exports.ProjectTemplate = class {
this.projectFolder,
this.src,
this.projectOutput.add(
ProjectItem.resource('favicon.ico', 'img/favicon.ico').skipIfExists()
ProjectItem.resource('favicon.ico', 'img/favicon.ico').skipIfExists().binaryFile()
),
ProjectItem.jsonObject('package.json', this.package).mergeIfExists(),
ProjectItem.resource('.editorconfig', 'content/editorconfig').skipIfExists(),
Expand Down Expand Up @@ -170,7 +170,7 @@ exports.ProjectTemplate = class {
ProjectItem.jsonObject('package.json', this.package),
ProjectItem.resource('.editorconfig', 'content/editorconfig'),
ProjectItem.resource('.gitignore', 'content/gitignore'),
ProjectItem.resource('favicon.ico', 'img/favicon.ico')
ProjectItem.resource('favicon.ico', 'img/favicon.ico').binaryFile()
);

return this;
Expand Down
12 changes: 10 additions & 2 deletions lib/file-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ exports.readdirSync = function(path) {
};

exports.readFile = function(path, encoding) {
if (encoding !== null) {
encoding = encoding || 'utf8';
}

return new Promise((resolve, reject) => {
fs.readFile(path, encoding || 'utf8', (error, data) => {
fs.readFile(path, encoding, (error, data) => {
if (error) reject(error);
else resolve(data);
});
Expand All @@ -75,7 +79,11 @@ exports.readFile = function(path, encoding) {
exports.readFileSync = fs.readFileSync;

exports.readFileSync = function(path, encoding) {
return fs.readFileSync(path, encoding || 'utf8');
if (encoding !== null) {
encoding = encoding || 'utf8';
}

return fs.readFileSync(path, encoding);
};

exports.copySync = function(sourceFile, targetFile) {
Expand Down
7 changes: 6 additions & 1 deletion lib/project-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ exports.ProjectItem = class {
return this;
}

binaryFile() {
this._isBinaryFile = true;
return this;
}

add() {
if (!this.isDirectory) {
throw new Error('You cannot add items to a non-directory.');
Expand Down Expand Up @@ -142,7 +147,7 @@ exports.ProjectItem = class {
.catch(() => fs.mkdir(fullPath))
.then(() => Utils.runSequentially(this.children, child => child.create(ui, fullPath)));
} else if (this.sourcePath) {
return fs.readFile(this.sourcePath).then(data => {
return fs.readFile(this.sourcePath, this._isBinaryFile ? null : 'utf8').then(data => {
return this._write(fullPath, data, ui);
});
} else if (this.jsonObject) {
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/file-system.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ describe('The file-system module', () => {
}).catch(fail).then(done);
});

it('returns a promise resolving to raw buffer of the files content when encoding is null', done => {
fs.readFile(readFile.path, null).then(buf => {
expect(Buffer.isBuffer(buf)).toBe(true);
expect(buf.toString('utf8')).toBe(readFile.content);
}).catch(fail).then(done);
});

it('rejects with ENOENT error', done => {
fs.readFile(writeFile.path).then(() => {
fail('expected promise to be rejected');
Expand All @@ -138,6 +145,12 @@ describe('The file-system module', () => {
.toBe(readFile.content);
});

it('returns raw buffer of files content when encoding is null', () => {
let buf = fs.readFileSync(readFile.path, null);
expect(Buffer.isBuffer(buf)).toBe(true);
expect(buf.toString('utf8')).toBe(readFile.content);
});

it('throws an ENOENT error', () => {
try {
fs.readFileSync(writeFile.path);
Expand Down

0 comments on commit f7941f4

Please sign in to comment.