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 draft
authorMark Banner <standard8@mozilla.com>
Wed, 12 Apr 2017 18:02:53 +0100
changeset 562393 e1452316c4c12ccd509d782cf8a1ab3670275fab
parent 562392 bd2ba568a475c8a8afa8b35a073ced6f03bbf14d
child 562394 40254d9fa080dec158b346ef0775d153a5d89746
child 563208 e4685bc31ad1f7d2e1e26ee856b56ceabde9283e
push id54019
push userbmo:standard8@mozilla.com
push dateThu, 13 Apr 2017 21:14:24 +0000
reviewersMossop
bugs1347709
milestone55.0a1
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
tools/lint/eslint/eslint-plugin-mozilla/.npmignore
tools/lint/eslint/eslint-plugin-mozilla/lib/environments/browser-window.js
tools/lint/eslint/eslint-plugin-mozilla/lib/environments/places-overlay.js
tools/lint/eslint/eslint-plugin-mozilla/lib/environments/simpletest.js
tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
tools/lint/eslint/eslint-plugin-mozilla/package.json
tools/lint/eslint/eslint-plugin-mozilla/scripts/createExports.js
--- 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");
+});