Permalink
Browse files

Bug 745741 (coverage): Add 'unamd' to remove AMD module wrapping

- Copy modules without AMD headers into node_modules
- Use fs.readFileSync to handle text! modules
- Add an unamd command to show off
- Now we can use 'node-cover' to do coverage analysis
  • Loading branch information...
1 parent 5fa085c commit cf24edc70e9698d7e3ef7fa53241f85a8ae58ae5 @joewalker joewalker committed Apr 16, 2012
Showing with 150 additions and 18 deletions.
  1. +15 −0 .coverignore
  2. +8 −0 .coverrc
  3. +44 −18 gcli.js
  4. +45 −0 lib/server/commands/unamd.js
  5. +38 −0 lib/server/unamd.js
View
@@ -0,0 +1,15 @@
+
+lib/server
+
+node_modules/connect
+node_modules/dryice
+node_modules/express
+node_modules/formidable
+node_modules/gclitest
+node_modules/jsdom
+node_modules/socket.io
+node_modules/test
+
+node_modules/gcli/promise.js
+
+node_modules/gcli/index.js
View
@@ -0,0 +1,8 @@
+{
+ "dataDirectory": "built/coverage_data",
+ "modules": true,
+ "html": {
+ "directory": "built/coverage_html",
+ "generateIndex": true
+ }
+}
View
@@ -7,24 +7,50 @@
exports.gcliHome = __dirname;
-// It's tempting to use RequireJS from npm, however that would break
-// running GCLI in Firefox just by opening index.html
-var requirejs = require('./scripts/r.js');
-requirejs.config({
- nodeRequire: require,
- paths: { 'text': 'scripts/text', 'i18n': 'scripts/i18n' },
- packagePaths: {
- 'lib': [
- { name: 'gcli', main: 'index', lib: '.' },
- { name: 'test', main: 'index', lib: '.' },
- { name: 'gclitest', main: 'index', lib: '.' },
- { name: 'demo', main: 'index', lib: '.' },
- { name: 'server', main: 'index', lib: '.' }
- ]
- }
-});
-
-exports.require = requirejs;
+/**
+ * There are 2 options for loading GCLI CommonJS modules:
+ * 1. Use Require's r.js
+ * 2. Convert the modules to CommonJS format on the fly.
+ *
+ * The former feels less hacky, the latter allows us to use 'cover' test
+ * coverage. Neither are complex so we've left them both in so they can fight
+ * it out.
+ */
+exports.useUnamd = false;
+
+// Setup the exports.require function to use either:
+// - requirejs (through r.js) or
+// - node's require (via unamd)
+if (exports.useUnamd) {
+ var unamd = require('./lib/server/unamd');
+ [ 'gcli', 'gclitest', 'test' ].forEach(function(packageName) {
+ var srcDir = exports.gcliHome + '/lib/' + packageName;
+ var destDir = exports.gcliHome + '/node_modules/' + packageName;
+ unamd.unamdize(srcDir, destDir);
+ });
+
+ exports.require = require;
+}
+else {
+ // It's tempting to use RequireJS from npm, however that would break
+ // running GCLI in Firefox just by opening index.html
+ var requirejs = require('./scripts/r.js');
+ requirejs.config({
+ nodeRequire: require,
+ paths: { 'text': 'scripts/text', 'i18n': 'scripts/i18n' },
+ packagePaths: {
+ 'lib': [
+ { name: 'gcli', main: 'index', lib: '.' },
+ { name: 'test', main: 'index', lib: '.' },
+ { name: 'gclitest', main: 'index', lib: '.' },
+ { name: 'demo', main: 'index', lib: '.' },
+ { name: 'server', main: 'index', lib: '.' }
+ ]
+ }
+ });
+
+ exports.require = requirejs;
+}
exports.require('gcli/index');
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2009-2011 Mozilla Foundation and contributors
+ * Licensed under the New BSD license. See LICENSE.txt or:
+ * http://opensource.org/licenses/BSD-3-Clause
+ */
+
+var copy = require('dryice').copy;
+var unamd = require('../unamd');
+var main = require('../../../gcli');
+var gcli = main.require('gcli/index');
+
+/**
+ * Registration and de-registration.
+ */
+exports.startup = function() {
+ gcli.addCommand(unamdCmdSpec);
+};
+
+exports.shutdown = function() {
+ gcli.removeCommand(unamdCmdSpec);
+};
+
+/**
+ * 'unamd' build command
+ */
+var unamdCmdSpec = {
+ name: 'unamd',
+ description: 'Convert CommonJS module format to NodeJS module format',
+ params: [
+ {
+ name: 'source',
+ type: 'string',
+ description: 'The source directory'
+ },
+ {
+ name: 'dest',
+ type: 'string',
+ description: 'The destination directory'
+ }
+ ],
+ returnType: 'terminal',
+ exec: function(args, context) {
+ return unamd.unamdize(args.source, args.dest);
+ }
+};
View
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2009-2011 Mozilla Foundation and contributors
+ * Licensed under the New BSD license. See LICENSE.txt or:
+ * http://opensource.org/licenses/BSD-3-Clause
+ */
+
+var copy = require('dryice').copy;
+
+/**
+ * There are 2 important ways to build GCLI.
+ * The first is for use within a normal web page.
+ * It has compressed and uncompressed versions of the output script file.
+ */
+exports.unamdize = function(srcDir, destDir) {
+ count = 0;
+
+ copy.mkdirSync(destDir, 0755);
+ copy({
+ source: srcDir,
+ filter: unamdModule,
+ dest: destDir
+ });
+
+ return 'Converted ' + count + ' modules.';
+};
+
+var count;
+
+function unamdModule(input) {
+ if (typeof input !== 'string') {
+ input = input.toString();
+ }
+ count++;
+ return input
+ .replace(/define\(function\(require, exports, module\) \{/, '')
+ .replace(/}\);\s*$/, '')
+ .replace(/require\('text!([^']*)'\)/g, 'require(\'fs\').readFileSync(\'node_modules/$1\')');
+}

0 comments on commit cf24edc

Please sign in to comment.