@samitouri / QOS-React-2 / commits / 60a6665bbd
Fork 0

The library for web and native user interfaces.

Provide `grunt npm:test` for verifying NPM package functionality.

This basically calls `npm pack`, installs the resulting package in a temporary directory, then requires it and attempts to use the .transform method. Closes #12.

Ben Newman committed May 30, 2013 at 10:42 UTC 60a6665bbdf5fe5e4c526efe38eb375c54e14aa9
4 files changed +109
Gruntfile.js
+5
@@ -5,6 +5,7 @@ var jsxTask = require('./grunt/tasks/jsx');
5 var browserifyTask = require('./grunt/tasks/browserify');
6 var wrapupTask = require('./grunt/tasks/wrapup');
7 var phantomTask = require('./grunt/tasks/phantom');
8 +var npmTask = require('./grunt/tasks/npm');
9 var releaseTasks = require('./grunt/tasks/release');
10
11 module.exports = function(grunt) {
@@ -16,6 +17,7 @@ module.exports = function(grunt) {
17 browserify: require('./grunt/config/browserify'),
18 wrapup: require('./grunt/config/wrapup'),
19 phantom: require('./grunt/config/phantom'),
20 + npm: require('./grunt/config/npm'),
21 clean: ['./build', './*.gem', './docs/_site', './examples/shared/*.js'],
22 jshint: require('./grunt/config/jshint'),
23 compare_size: require('./grunt/config/compare_size')
@@ -44,6 +46,8 @@ module.exports = function(grunt) {
46
47 grunt.registerMultiTask('phantom', phantomTask);
48
49 + grunt.registerMultiTask('npm', npmTask);
50 +
51 grunt.registerTask('build:basic', ['jsx:debug', 'browserify:basic']);
52 grunt.registerTask('build:transformer', ['jsx:debug', 'browserify:transformer']);
53 grunt.registerTask('build:min', ['jsx:release', 'browserify:min']);
@@ -54,6 +58,7 @@ module.exports = function(grunt) {
58 ]);
59
60 grunt.registerTask('test', ['build:test', 'phantom:run']);
61 + grunt.registerTask('npm:test', ['build', 'npm:pack']);
62
63 // Optimized build task that does all of our builds. The subtasks will be run
64 // in order so we can take advantage of that and only run jsx:debug once.
grunt/config/npm.js new
+1
@@ -0,0 +1 @@
1 +exports.pack = {};
grunt/tasks/npm.js new
+102
@@ -0,0 +1,102 @@
1 +'use strict';
2 +
3 +var assert = require("assert");
4 +var path = require("path");
5 +var fs = require("fs");
6 +var tmp = require("tmp");
7 +var grunt = require("grunt");
8 +var spawn = grunt.util.spawn;
9 +
10 +module.exports = function() {
11 + var config = this.data;
12 + var done = this.async();
13 +
14 + function run(cmd, args, opts, callback) {
15 + assert.strictEqual(typeof cmd, "string");
16 + assert.ok(args instanceof Array);
17 +
18 + if (typeof opts === "function" && !callback) {
19 + callback = opts;
20 + opts = {};
21 + }
22 +
23 + assert.strictEqual(typeof opts, "object");
24 + assert.strictEqual(typeof callback, "function");
25 +
26 + grunt.log.writeln("> " + cmd + " " + args.join(" "));
27 +
28 + var proc = spawn({
29 + cmd: cmd,
30 + args: args,
31 + opts: opts
32 + }, function(error, result, code) {
33 + if (error) {
34 + grunt.log.error(error);
35 + done(false);
36 + } else {
37 + callback(result, code);
38 + }
39 + });
40 +
41 + // Uncomment these to see the output of the commands.
42 + // proc.stdout.pipe(process.stdout);
43 + // proc.stderr.pipe(process.stderr);
44 + }
45 +
46 + var pkg = grunt.config.data.pkg;
47 + var tgz = pkg.name + "-" + pkg.version + ".tgz";
48 +
49 + grunt.log.writeln("Packing " + tgz + " (this could take a while)...");
50 +
51 + run("npm", ["pack", "--verbose", "."], function() {
52 + tmp.dir(function(err, dir) {
53 + if (err) {
54 + grunt.log.error(err);
55 + done(false);
56 + return;
57 + }
58 +
59 + run("cp", [tgz, dir], function() {
60 + run("npm", [
61 + "install",
62 + "--production",
63 + tgz
64 + ], { cwd: dir }, function() {
65 + var nodePath = path.join(dir, "node_modules");
66 + var pkgDir = path.join(nodePath, pkg.name);
67 + var doneCount = 2;
68 +
69 + // Make sure that bin/jsx is runnable by echoing main.js.
70 + run("bin/jsx", ["main.js"], {
71 + cwd: pkgDir
72 + }, function(result) {
73 + assert.ok(result.stdout.indexOf("transform") >= 0, result.stdout);
74 +
75 + if (--doneCount === 0) {
76 + done();
77 + }
78 + });
79 +
80 + // Make sure the .transform package method works.
81 + run("node", [
82 + "--print",
83 + 'require("react-tools").transform(' +
84 + JSON.stringify(
85 + "/** @jsx React.DOM */ <div>oyez</div>;"
86 + ) + ')'
87 + ], {
88 + env: { NODE_PATH: nodePath }
89 + }, function(result, code) {
90 + assert.ok(result.stdout.indexOf(
91 + 'React.DOM.div(null, "oyez");'
92 + ) >= 0, result.stdout);
93 +
94 + if (--doneCount === 0) {
95 + done();
96 + }
97 + });
98 + });
99 + });
100 + });
101 + });
102 +};
package.json
+1
@@ -52,6 +52,7 @@
52 "grunt-contrib-clean": "~0.4.1",
53 "grunt-compare-size": "~0.4.0",
54 "gzip-js": "~0.3.2",
55 + "tmp": "~0.0.18",
56 "grunt-contrib-compress": "~0.5.1"
57 },
58 "preferGlobal": true