From 17f976b49b8ecb974595a4290852705c9e15667f Mon Sep 17 00:00:00 2001 From: Matt Hernandez Date: Mon, 9 Feb 2015 12:24:05 -0500 Subject: [PATCH 1/3] Updated to use node version from meteor Meteor specifies the minimum node version to use in boot.js, this change adds some logic to parse that file and use the version in the generated package.json. --- lib/cli.js | 25 +++++++++---------------- lib/demeteorizer.js | 21 ++++++++++++++++++--- spec/demeteorizer-spec.js | 8 +++++++- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 367d7db..f6682f1 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -11,28 +11,21 @@ program .option('-d, --debug', 'Bundle in debug mode (don\'t minify, etc).', false) .parse(process.argv); -var appName = program.app_name; -var debug = program.debug; -var input = process.cwd(); -var output = program.output; -var release = program.release; -var tarball = program.tarball; +program.output = program.output || path.join(process.cwd(), '.demeteorized'); -output = output || path.join(process.cwd(), '.demeteorized'); - -if (release) { - console.log('Release:', release); +if (program.release) { + console.log('Release:', program.release); } demeteorizer.on('progress', console.log.bind(console)); var options = { - appName : appName, - debug : debug, - input : input, - output : output, - release : release, - tarball : tarball + appName : program.app_name, + debug : program.debug, + input : process.cwd(), + output : program.output, + release : program.release, + tarball : program.tarball }; demeteorizer.convert( diff --git a/lib/demeteorizer.js b/lib/demeteorizer.js index b17c6f4..275c467 100644 --- a/lib/demeteorizer.js +++ b/lib/demeteorizer.js @@ -370,9 +370,24 @@ Demeteorizer.prototype.createPackageJSON = function (context, callback) { packageJSON.scripts = { start: 'node main.js' }; - // FIXME: this is a hack to avoid defaulting to node@0.10.34 which breaks some - // timer code. - packageJSON.engines = { node: '0.10.33' }; + + // Read boot.js to find the MIN_NODE_VERSION; use that version as the node + // version of the project. + fs.readFileSync(path.resolve( + context.options.output, + 'programs', + 'server', + 'boot.js')).toString().split('\n').some( + function (line) { + if (line.indexOf('MIN_NODE_VERSION') >= 0) { + packageJSON.engines = { + node: line.split(' ')[3].replace(/[v;']/g, '') + }; + + return true; + } + }); + packageJSON.dependencies = context.dependencies; fs.writeFileSync(context.paths.package_json, JSON.stringify(packageJSON, null, 2)); diff --git a/spec/demeteorizer-spec.js b/spec/demeteorizer-spec.js index 61af8f4..737d3ec 100644 --- a/spec/demeteorizer-spec.js +++ b/spec/demeteorizer-spec.js @@ -151,13 +151,19 @@ describe('demeteorizer lib', function () { }); describe('#createPackageJSON', function () { - it('should create package.json with the correct fields', function () { + beforeEach(function () { + fsStub.readFileSync = + sinon.stub().returns('var MIN_NODE_VERSION = \'v0.10.33\';'); + }); + + it('should create package.json with the correct node version', function () { context.paths = {}; context.paths.package_json = './package.json'; fsStub.writeFileSync = function (path, data) { path.should.equal('./package.json'); JSON.parse(data).engines.node.should.exist; + JSON.parse(data).engines.node.should.equal('0.10.33'); }; demeteorizer.createPackageJSON(context, new Function()); From 6c9a784970f9be9c17d796ef01c1f4468b092265 Mon Sep 17 00:00:00 2001 From: Matt Hernandez Date: Mon, 9 Feb 2015 12:37:41 -0500 Subject: [PATCH 2/3] Cleanup and error handling Cleaned up the parse functionality to be more readable. Added handling for if reading the file throws an error and if the version just isn't present in the file. Includes tests for each case. --- lib/demeteorizer.js | 24 ++++++++++++++++-------- spec/demeteorizer-spec.js | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/lib/demeteorizer.js b/lib/demeteorizer.js index 275c467..53e96a6 100644 --- a/lib/demeteorizer.js +++ b/lib/demeteorizer.js @@ -352,6 +352,8 @@ Demeteorizer.prototype.createPackageJSON = function (context, callback) { var input = context.options.input; var appName = context.options.appName; + var bootPath = path.resolve( + context.options.output, 'programs', 'server', 'boot.js'); // Set the app name to the name of the input folder. var name = path.basename(input); @@ -371,14 +373,10 @@ Demeteorizer.prototype.createPackageJSON = function (context, callback) { start: 'node main.js' }; - // Read boot.js to find the MIN_NODE_VERSION; use that version as the node - // version of the project. - fs.readFileSync(path.resolve( - context.options.output, - 'programs', - 'server', - 'boot.js')).toString().split('\n').some( - function (line) { + try { + // Read boot.js to find the MIN_NODE_VERSION; use that version as the node + // version of the project. + fs.readFileSync(bootPath).toString().split('\n').some(function (line) { if (line.indexOf('MIN_NODE_VERSION') >= 0) { packageJSON.engines = { node: line.split(' ')[3].replace(/[v;']/g, '') @@ -387,6 +385,16 @@ Demeteorizer.prototype.createPackageJSON = function (context, callback) { return true; } }); + } catch (err) { + this.emit('progress', 'failed to parse node version, defaulting to 0.10.33'); + packageJSON.engines = { node: '0.10.33' }; + } + + // This happens if boot.js is parsed successfully, but the version wasn't + // found in the file. + if (!packageJSON.engines) { + packageJSON.engines = { node: '0.10.33' }; + } packageJSON.dependencies = context.dependencies; diff --git a/spec/demeteorizer-spec.js b/spec/demeteorizer-spec.js index 737d3ec..19953e2 100644 --- a/spec/demeteorizer-spec.js +++ b/spec/demeteorizer-spec.js @@ -151,12 +151,26 @@ describe('demeteorizer lib', function () { }); describe('#createPackageJSON', function () { - beforeEach(function () { + it('should create package.json with the correct node version', function () { fsStub.readFileSync = - sinon.stub().returns('var MIN_NODE_VERSION = \'v0.10.33\';'); + sinon.stub().returns('var MIN_NODE_VERSION = \'v0.12.0\';'); + + context.paths = {}; + context.paths.package_json = './package.json'; + + fsStub.writeFileSync = function (path, data) { + path.should.equal('./package.json'); + JSON.parse(data).engines.node.should.exist; + JSON.parse(data).engines.node.should.equal('0.12.0'); + }; + + demeteorizer.createPackageJSON(context, new Function()); }); - it('should create package.json with the correct node version', function () { + it('should default the node version if version not found boot.js', function () { + fsStub.readFileSync = + sinon.stub().returns(''); + context.paths = {}; context.paths.package_json = './package.json'; @@ -170,4 +184,18 @@ describe('demeteorizer lib', function () { }); }); + it('should default the node version if boot.js parse fails', function () { + fsStub.readFileSync = function () { throw new Error('ENOENT'); }; + + context.paths = {}; + context.paths.package_json = './package.json'; + + fsStub.writeFileSync = function (path, data) { + path.should.equal('./package.json'); + JSON.parse(data).engines.node.should.exist; + JSON.parse(data).engines.node.should.equal('0.10.33'); + }; + + demeteorizer.createPackageJSON(context, new Function()); + }); }); From f83c6609a0571a2beb3b43ec97e1a2dbe1502d6a Mon Sep 17 00:00:00 2001 From: Matt Hernandez Date: Tue, 10 Feb 2015 14:17:39 -0500 Subject: [PATCH 3/3] Cleaned up some duplicated test code The overriding of package path was duplicated in the package.json tests. This has been moved to the before block. --- spec/demeteorizer-spec.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/spec/demeteorizer-spec.js b/spec/demeteorizer-spec.js index 19953e2..ee898fd 100644 --- a/spec/demeteorizer-spec.js +++ b/spec/demeteorizer-spec.js @@ -151,13 +151,15 @@ describe('demeteorizer lib', function () { }); describe('#createPackageJSON', function () { + before(function () { + context.paths = {}; + context.paths.package_json = './package.json'; + }); + it('should create package.json with the correct node version', function () { fsStub.readFileSync = sinon.stub().returns('var MIN_NODE_VERSION = \'v0.12.0\';'); - context.paths = {}; - context.paths.package_json = './package.json'; - fsStub.writeFileSync = function (path, data) { path.should.equal('./package.json'); JSON.parse(data).engines.node.should.exist; @@ -171,9 +173,6 @@ describe('demeteorizer lib', function () { fsStub.readFileSync = sinon.stub().returns(''); - context.paths = {}; - context.paths.package_json = './package.json'; - fsStub.writeFileSync = function (path, data) { path.should.equal('./package.json'); JSON.parse(data).engines.node.should.exist; @@ -182,14 +181,10 @@ describe('demeteorizer lib', function () { demeteorizer.createPackageJSON(context, new Function()); }); - }); it('should default the node version if boot.js parse fails', function () { fsStub.readFileSync = function () { throw new Error('ENOENT'); }; - context.paths = {}; - context.paths.package_json = './package.json'; - fsStub.writeFileSync = function (path, data) { path.should.equal('./package.json'); JSON.parse(data).engines.node.should.exist; @@ -198,4 +193,5 @@ describe('demeteorizer lib', function () { demeteorizer.createPackageJSON(context, new Function()); }); + }); });