CB-11657 Add bom to www after plugin operations Since cordova is not calling `prepare` immediately after plugin has been installed/removed, we need to take care of adding BOM ourselves. Otherwise user can run into situation when project, built from Visual Studio immediately after installation/removal of plugin, will not pass WACK tests due to missing BOM in some files.
Project: http://git-wip-us.apache.org/repos/asf/cordova-windows/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-windows/commit/df242ce8 Tree: http://git-wip-us.apache.org/repos/asf/cordova-windows/tree/df242ce8 Diff: http://git-wip-us.apache.org/repos/asf/cordova-windows/diff/df242ce8 Branch: refs/heads/4.4.x Commit: df242ce83afc781508609787defb089fc7649574 Parents: 5e45d70 Author: Vladimir Kotikov <[email protected]> Authored: Wed Aug 10 13:04:24 2016 +0300 Committer: daserge <[email protected]> Committed: Thu Oct 20 20:54:53 2016 +0300 ---------------------------------------------------------------------- template/cordova/Api.js | 13 ++++++++++++ template/cordova/lib/prepare.js | 41 ++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/df242ce8/template/cordova/Api.js ---------------------------------------------------------------------- diff --git a/template/cordova/Api.js b/template/cordova/Api.js index 00cf29a..1fb62ce 100644 --- a/template/cordova/Api.js +++ b/template/cordova/Api.js @@ -179,6 +179,8 @@ Api.prototype.prepare = function (cordovaProject, prepareOptions) { */ Api.prototype.addPlugin = function (plugin, installOptions) { + var self = this; + var jsProject = JsprojManager.getProject(this.root); installOptions = installOptions || {}; installOptions.variables = installOptions.variables || {}; @@ -189,6 +191,11 @@ Api.prototype.addPlugin = function (plugin, installOptions) { return PluginManager.get(this.platform, this.locations, jsProject) .addPlugin(plugin, installOptions) + .then(function () { + // CB-11657 Add BOM to www files here because files added by plugin + // probably don't have it. Prepare would add BOM but it might not be called + return require('./lib/prepare').addBOMSignature(self.locations.www); + }) // CB-11022 return non-falsy value to indicate // that there is no need to run prepare after .thenResolve(true); @@ -208,9 +215,15 @@ Api.prototype.addPlugin = function (plugin, installOptions) { * CordovaError instance. */ Api.prototype.removePlugin = function (plugin, uninstallOptions) { + var self = this; var jsProject = JsprojManager.getProject(this.root); return PluginManager.get(this.platform, this.locations, jsProject) .removePlugin(plugin, uninstallOptions) + .then(function () { + // CB-11657 Add BOM to cordova_plugins, since it is was + // regenerated after plugin uninstallation and does not have BOM + return require('./lib/prepare').addBOMToFile(path.resolve(self.locations.www, 'cordova_plugins.js')); + }) // CB-11022 return non-falsy value to indicate // that there is no need to run prepare after .thenResolve(true); http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/df242ce8/template/cordova/lib/prepare.js ---------------------------------------------------------------------- diff --git a/template/cordova/lib/prepare.js b/template/cordova/lib/prepare.js index 0067803..53838d5 100644 --- a/template/cordova/lib/prepare.js +++ b/template/cordova/lib/prepare.js @@ -497,24 +497,37 @@ module.exports.clean = function (options) { */ function addBOMSignature(directory) { shell.ls('-R', directory) - .forEach(function (file) { - if (!file.match(/\.(js|htm|html|css|json)$/i)) { - return; - } + .map(function (file) { + return path.join(directory, file); + }) + .forEach(addBOMToFile); +} - var filePath = path.join(directory, file); - // skip if this is a folder - if (!fs.lstatSync(filePath).isFile()) { - return; - } +/** + * Adds BOM signature at the beginning of file, specified by absolute + * path. Ignores directories and non-js, -html or -css files. + * + * @param {String} file Absolute path to file to add BOM to + */ +function addBOMToFile(file) { + if (!file.match(/\.(js|htm|html|css|json)$/i)) { + return; + } - var content = fs.readFileSync(filePath); - if (content[0] !== 0xEF && content[1] !== 0xBE && content[2] !== 0xBB) { - fs.writeFileSync(filePath, '\ufeff' + content); - } - }); + // skip if this is a folder + if (!fs.lstatSync(file).isFile()) { + return; + } + + var content = fs.readFileSync(file); + if (content[0] !== 0xEF && content[1] !== 0xBE && content[2] !== 0xBB) { + fs.writeFileSync(file, '\ufeff' + content); + } } +module.exports.addBOMSignature = addBOMSignature; +module.exports.addBOMToFile = addBOMToFile; + /** * Updates config files in project based on app's config.xml and config munge, * generated by plugins. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
