Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Refactor cordova preBuild hook script
Browse files Browse the repository at this point in the history
Add npm scripts to package.json
  • Loading branch information
lepus2589 committed Jan 23, 2017
1 parent b77b8b8 commit ab8645b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"type": "git",
"url": "git+ssh://[email protected]:b0r3as/pocket-combine.git"
},
"scripts": {
"build": "grunt build --stack",
"build:dev": "grunt build-dev --stack"
},
"dependencies": {},
"devDependencies": {
"babel-core": "^6.4.5",
Expand Down
50 changes: 27 additions & 23 deletions src/cordova-hooks/app-before-build.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
module.exports = function(context) {
var Q = context.requireCordovaModule('q');
var deferred = new Q.defer();
var spawn = require('child_process').spawn;
var childProcess = require('child_process');
var isRelease = context.opts.options.release;
var taskName = isRelease ? 'build' : 'build-dev';
var taskName = isRelease ? 'build' : 'build:dev';

console.info('\n> Running "' + taskName + '" Grunt task\n');
console.info('\n> Running "' + taskName + '" Npm task\n');

var grunt = spawn('grunt', [taskName, '--stack', '--release', isRelease]);
var preBuild = childProcess.spawn(
'npm',
[
'run-script',
taskName,
'--',
'--release',
isRelease
],
{stdio: 'inherit'}
);

grunt.stdout.on('data', function(data) {
process.stdout.write(data.toString());
});
grunt.stderr.on('data', function(data) {
process.stderr.write(data.toString());
});
return new Promise(function (aResolve, aReject) {
preBuild.on('error', function (aError) {
aReject(new Error(aError.message));
});

grunt.on('error', function (aError) {
deferred.reject(new Error(aError.message));
preBuild.on('close', function (aCode, aSignal) {
if (aSignal) {
aReject(new Error('Npm task "' + taskName + '" was terminated with signal ' + aSignal));
} else if (aCode) {
aReject(new Error('Npm task "' + taskName + '" exited with code ' + aCode));
} else {
aResolve();
}
});
});
grunt.on('exit', function (aCode) {
if (aCode !== 0) {
deferred.reject(new Error('Grunt task "' + taskName + '" exited with code ' + aCode));
} else {
deferred.resolve();
}
});

return deferred.promise;
}

0 comments on commit ab8645b

Please sign in to comment.