Bug 1347709 - Add a prepublish script to save the current globals for the published version of eslint-plugin-mozilla, and use that when not in mozilla-central. r?Mossop
MozReview-Commit-ID: GHD7GQ0Pjnp
--- a/tools/lint/eslint/eslint-plugin-mozilla/.npmignore
+++ b/tools/lint/eslint/eslint-plugin-mozilla/.npmignore
@@ -1,4 +1,5 @@
.eslintrc.js
.npmignore
node_modules
+scripts
tests
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/browser-window.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/browser-window.js
@@ -47,21 +47,19 @@ const MAPPINGS = {
"viewSourceUtils.js":
"toolkit/components/viewsource/content/viewSourceUtils.js"
};
const globalScriptsRegExp =
/<script type=\"application\/javascript\" src=\"(.*)\"\/>/;
function getGlobalScriptsIncludes() {
- let globalScriptsPath = path.join(rootDir, "browser", "base", "content",
- "global-scripts.inc");
let fileData;
try {
- fileData = fs.readFileSync(globalScriptsPath, {encoding: "utf8"});
+ fileData = fs.readFileSync(helpers.globalScriptsPath, {encoding: "utf8"});
} catch (ex) {
// The file isn't present, so this isn't an m-c repository.
return null;
}
fileData = fileData.split("\n");
let result = [];
@@ -106,19 +104,25 @@ function getScriptGlobals() {
}
return fileGlobals;
}
function mapGlobals(fileGlobals) {
// placesOverlay.xul is also included in the browser scope, so include
// those globals here.
- var globalObjects = Object.assign({}, placesGlobals);
+ let globalObjects = Object.assign({}, placesGlobals);
for (let global of fileGlobals) {
globalObjects[global.name] = global.writable;
}
return globalObjects;
}
-module.exports = {
- globals: mapGlobals(getScriptGlobals()),
- browserjsScripts: getGlobalScriptsIncludes().concat(EXTRA_SCRIPTS)
-};
+function getMozillaCentralItems() {
+ return {
+ globals: mapGlobals(getScriptGlobals()),
+ browserjsScripts: getGlobalScriptsIncludes().concat(EXTRA_SCRIPTS)
+ };
+}
+
+module.exports = helpers.isMozillaCentralBased() ?
+ getMozillaCentralItems() :
+ helpers.getSavedEnvironmentItems("browser-window");
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/places-overlay.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/places-overlay.js
@@ -71,10 +71,12 @@ function mapGlobals(fileGlobals) {
var globalObjects = {};
for (let global of fileGlobals) {
globalObjects[global.name] = global.writable;
}
return globalObjects;
}
module.exports = {
- globals: mapGlobals(getScriptGlobals())
+ globals: helpers.isMozillaCentralBased() ?
+ mapGlobals(getScriptGlobals()) :
+ helpers.getSavedEnvironmentItems("places-overlay")
};
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/simpletest.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/simpletest.js
@@ -45,10 +45,12 @@ function mapGlobals(fileGlobals) {
var globalObjects = {};
for (let global of fileGlobals) {
globalObjects[global.name] = global.writable;
}
return globalObjects;
}
module.exports = {
- globals: mapGlobals(getScriptGlobals())
+ globals: helpers.isMozillaCentralBased() ?
+ mapGlobals(getScriptGlobals()) :
+ helpers.getSavedEnvironmentItems("simpletest")
};
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
@@ -568,10 +568,23 @@ module.exports = {
/**
* When ESLint is run from SublimeText, paths retrieved from
* context.getFileName contain leading and trailing double-quote characters.
* These characters need to be removed.
*/
cleanUpPath(pathName) {
return pathName.replace(/^"/, "").replace(/"$/, "");
+ },
+
+ get globalScriptsPath() {
+ return path.join(this.getRootDir(module.filename), "browser",
+ "base", "content", "global-scripts.inc");
+ },
+
+ isMozillaCentralBased() {
+ return fs.existsSync(this.globalScriptsPath);
+ },
+
+ getSavedEnvironmentItems(environment) {
+ return require("./environments/saved-globals.json").environments[environment];
}
};
--- a/tools/lint/eslint/eslint-plugin-mozilla/package.json
+++ b/tools/lint/eslint/eslint-plugin-mozilla/package.json
@@ -26,12 +26,13 @@
"globals": "^9.14.0",
"ini-parser": "^0.0.2",
"sax": "^1.2.2"
},
"engines": {
"node": ">=6.9.1"
},
"scripts": {
+ "prepublishOnly": "node scripts/createExports.js",
"test": "node tests/test-run-all.js"
},
"license": "MPL-2.0"
}
new file mode 100644
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/scripts/createExports.js
@@ -0,0 +1,29 @@
+/**
+ * @fileoverview A script to export the known globals to a file.
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+"use strict";
+
+var fs = require("fs");
+var path = require("path");
+var helpers = require("../lib/helpers");
+
+const globalsFile = path.join(helpers.getRootDir(module.filename),
+ "tools", "lint", "eslint", "eslint-plugin-mozilla",
+ "lib", "environments", "saved-globals.json");
+
+console.log("Generating globals file");
+
+// We only export the configs section.
+let environmentGlobals = require("../lib/index.js").environments;
+
+return fs.writeFile(globalsFile, JSON.stringify({environments: environmentGlobals}), err => {
+ if (err) {
+ console.error(err);
+ process.exit(1);
+ }
+
+ console.log("Globals file generation complete");
+});