diff --git a/lib/utils/npmScripts.js b/lib/utils/npmScripts.js index 4315c673fe..e538e5dd7c 100644 --- a/lib/utils/npmScripts.js +++ b/lib/utils/npmScripts.js @@ -6,17 +6,21 @@ var fs = require('fs'); var IonicAppLib = require('ionic-app-lib'); var log = IonicAppLib.logging.logger; +function getScriptName(name) { + return 'ionic:' + name; +} + function hasIonicScript(name) { return getPackageJsonContents().then(function(packageJsonContents) { return packageJsonContents.hasOwnProperty('scripts') && - packageJsonContents.scripts.hasOwnProperty('ionic:' + name); + packageJsonContents.scripts.hasOwnProperty(getScriptName(name)); }); } function runIonicScript(name, argv) { var spawn = require('cross-spawn-async'); - var scriptName = 'ionic:' + name; + var scriptName = getScriptName(name); var q = Q.defer(); var scriptSpawn = spawn('npm', ['run', scriptName].concat(argv || []), { stdio: 'inherit' }) @@ -48,7 +52,7 @@ var getPackageJsonContents = (function() { var q = Q.defer(); if (packageJson) { - Q.resolve(packageJson); + return Q.resolve(packageJson); } try { diff --git a/spec/fixtures/package.json b/spec/fixtures/package.json new file mode 100644 index 0000000000..6ddf931184 --- /dev/null +++ b/spec/fixtures/package.json @@ -0,0 +1,53 @@ +{ + "name": "ionic-hello-world", + "author": "Ionic Framework", + "homepage": "http://ionicframework.com/", + "scripts": { + "ionic:build": "ionic-app-scripts build", + "watch": "ionic-app-scripts watch", + "ionic:test": "dir", + "serve:before": "watch", + "emulate:before": "build", + "deploy:before": "build", + "build:before": "build", + "run:before": "build" + }, + "dependencies": { + "@angular/common": "^2.0.0", + "@angular/compiler": "^2.0.0", + "@angular/compiler-cli": "0.6.2", + "@angular/core": "^2.0.0", + "@angular/forms": "^2.0.0", + "@angular/http": "^2.0.0", + "@angular/platform-browser": "^2.0.0", + "@angular/platform-browser-dynamic": "^2.0.0", + "@angular/platform-server": "^2.0.0", + "@ionic/storage": "^1.0.3", + "ionic-angular": "^2.0.0-rc.1", + "ionic-native": "^2.2.3", + "ionicons": "^3.0.0", + "rxjs": "5.0.0-beta.12", + "zone.js": "^0.6.21" + }, + "devDependencies": { + "@ionic/app-scripts": "^0.0.36", + "typescript": "^2.0.3" + }, + "cordovaPlugins": [ + "cordova-plugin-whitelist", + "cordova-plugin-statusbar", + "cordova-plugin-console", + "cordova-plugin-device", + "cordova-plugin-splashscreen", + "ionic-plugin-keyboard" + ], + "cordovaPlatforms": [ + "ios", + { + "platform": "ios", + "version": "", + "locator": "ios" + } + ], + "description": "hi: An Ionic project" +} \ No newline at end of file diff --git a/spec/utils/npmScripts.spec.js b/spec/utils/npmScripts.spec.js new file mode 100644 index 0000000000..ca1a84fec0 --- /dev/null +++ b/spec/utils/npmScripts.spec.js @@ -0,0 +1,73 @@ +'use strict'; + +var rewire = require('rewire'); +var path = require('path'); +var fs = require('fs'); +var npmScripts = rewire('../../lib/utils/npmScripts'); +var EventEmitter = require('events'); +var Q = require('q'); +var spawn = require('cross-spawn-async'); + +describe('hasIonicScript function', function() { + + it('should return false if script does not exist', function(done) { + var jsonContent = require(path.join(__dirname, '../', 'fixtures/', 'package.json')); + var getPackageJsonContentsSpy = jasmine.createSpy('getPackageJsonContentsSpy').andReturn(Q(jsonContent)); + var revert = npmScripts.__set__('getPackageJsonContents', getPackageJsonContentsSpy); + spyOn(npmScripts, 'getPackageJsonContents').andReturn(Q(jsonContent)); + + npmScripts.hasIonicScript('stuff').then(function(results) { + expect(results).toEqual(false); + done(); + revert(); + }); + }); + it('should return true if script does not exist', function(done) { + var jsonContent = require(path.join(__dirname, '../', 'fixtures/', 'package.json')); + var getPackageJsonContentsSpy = jasmine.createSpy('getPackageJsonContentsSpy').andReturn(Q(jsonContent)); + var revert = npmScripts.__set__('getPackageJsonContents', getPackageJsonContentsSpy); + spyOn(npmScripts, 'getPackageJsonContents').andReturn(Q(jsonContent)); + + npmScripts.hasIonicScript('build').then(function(results) { + expect(results).toEqual(true); + done(); + revert(); + }); + }); +}); +/* +describe('runIonicScript function', function() { + it('should call spawn', function(done) { + //'npm', ['run', scriptName].concat(argv || []), { stdio: 'inherit' } + var emitter = new EventEmitter(); + var error = new Error(); + + spawn = jasmine.createSpy('spawnSpy', spawn).andCallFake(function() { + return emitter; + }); + + npmScripts.runIonicScript('test').catch(function(err) { + expect(err).toEqual(error); + done(); + }); + emitter.emit('error', error); + }); +}); +*/ +describe('getPackageJsonContents method', function() { + it('getPackageJsonContents should return json contents of package.json file and should memoize', function(done) { + var dapath = path.join(__dirname, '../', 'fixtures/package.json'); + spyOn(path, 'resolve').andReturn(dapath); + spyOn(fs, 'readFile').andCallThrough(); + + npmScripts.getPackageJsonContents().then(function(contents) { + expect(contents).toEqual(require(dapath)); + + npmScripts.getPackageJsonContents().then(function(secondContents) { + expect(secondContents).toEqual(require(dapath)); + expect(fs.readFile.calls.length).toEqual(1); + done(); + }); + }); + }); +}); \ No newline at end of file