From f7941f447237d543522f3fa1b8458de83ec51ef8 Mon Sep 17 00:00:00 2001 From: Chunpeng Huo Date: Sat, 10 Mar 2018 23:45:04 +1100 Subject: [PATCH] fix: fix binary file copy on favicon.ico 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. --- lib/commands/new/buildsystems/webpack/index.js | 2 +- lib/commands/new/project-template.js | 4 ++-- lib/file-system.js | 12 ++++++++++-- lib/project-item.js | 7 ++++++- spec/lib/file-system.spec.js | 13 +++++++++++++ 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/commands/new/buildsystems/webpack/index.js b/lib/commands/new/buildsystems/webpack/index.js index 7762b9b80..791f24bd5 100644 --- a/lib/commands/new/buildsystems/webpack/index.js +++ b/lib/commands/new/buildsystems/webpack/index.js @@ -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( diff --git a/lib/commands/new/project-template.js b/lib/commands/new/project-template.js index 677d7857f..2bc050807 100644 --- a/lib/commands/new/project-template.js +++ b/lib/commands/new/project-template.js @@ -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(), @@ -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; diff --git a/lib/file-system.js b/lib/file-system.js index 0ba05ac58..33feb88b1 100644 --- a/lib/file-system.js +++ b/lib/file-system.js @@ -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); }); @@ -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) { diff --git a/lib/project-item.js b/lib/project-item.js index 8e30b1895..c8fade323 100644 --- a/lib/project-item.js +++ b/lib/project-item.js @@ -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.'); @@ -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) { diff --git a/spec/lib/file-system.spec.js b/spec/lib/file-system.spec.js index b2e9a4847..b0999c3c2 100644 --- a/spec/lib/file-system.spec.js +++ b/spec/lib/file-system.spec.js @@ -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'); @@ -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);