paco
Version:
Node package development/distribution utility kit
208 lines (170 loc) • 4.84 kB
JavaScript
;
var Q = require('q');
var comeondo = require('comeondo');
var fs = require('fs');
var path = require('path');
var colors = require('colors');
var bump = require('./helpers/bump');
var configHelpers = require('./helpers/local-configs');
var gitHelpers = require('./helpers/git');
var npmHelpers = require('./helpers/local-npm');
var paths = require('./helpers/paths');
function exitWithError(err) {
console.log('Exiting due to error:'.red);
console.log(err);
console.log(err.stack);
process.exit(err ? 1 : 0);
}
/**
* paco api
*/
var api = {};
/**
* Init
*/
api.init = function () {
var deferred = Q.defer();
var pacorcJson = require('./pacorc-default.js');
var pacorcContents = JSON.stringify(pacorcJson, null, ' ');
fs.writeFile(path.join(process.cwd(), '.pacorc'), pacorcContents, { encoding: 'utf8' }, function (err) {
if (err) {
exitWithError(err);
}
deferred.resolve();
});
return deferred.promise;
};
/**
* Config
*/
api.config = function () {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var key = options.key;
var value = options.value;
if (key && value) {
return configHelpers.setConfig(key, value);
} else if (key) {
var configValue = configHelpers.getConfig(key);
return JSON.stringify(configValue, null, ' ');
} else {
var allConfigs = configHelpers.getMergedPacorc();
return JSON.stringify(allConfigs, null, ' ');
}
};
/**
* Lint
*/
api.lint = function () {
var configs = configHelpers.getMergedPacorc();
var npmConfig = { traverse: configs.traverse };
if (configs.lint === false) {
console.log('The `lint` command is explicitly disabled.');
return Q();
}
if (typeof configs.lint === 'string') {
return comeondo.exec(configs.lint).catch(exitWithError);
} else if (npmHelpers.hasTask('lint', npmConfig)) {
return npmHelpers.runTask('lint', npmConfig).catch(exitWithError);
}
return Q();
};
/**
* Test
*/
api.test = function () {
var configs = configHelpers.getMergedPacorc();
var npmConfig = { traverse: configs.traverse };
if (configs.test === false) {
console.log('The `test` command is explicitly disabled.');
return Q();
}
if (typeof configs.test === 'string') {
return comeondo.exec(configs.test).catch(exitWithError);
} else if (npmHelpers.hasTask('test', npmConfig)) {
return npmHelpers.runTask('test', npmConfig).catch(exitWithError);
}
return Q();
};
/**
* Verify
*/
api.verify = function () {
return Q().then(function () {
return api.lint();
}).then(function () {
return api.test();
});
};
/**
* Build
*/
api.build = function () {
var configs = configHelpers.getMergedPacorc();
if (configs.build === false) {
console.log('The `build` command is explicitly disabled.');
return Q();
}
var npmConfig = {
traverse: configs.traverse
};
if (configs.build) {
var commands = (configs.build || []).map(paths.getInterpolatedPaths);
return comeondo.run(commands, { cwd: process.env.PACO_PACKAGE_PATH }).catch(exitWithError);
} else if (npmHelpers.hasTask('build', npmConfig)) {
return npmHelpers.runTask('build', npmConfig).catch(exitWithError);
}
return Q();
};
/**
* Prepare
*/
api.prepare = function (options) {
return Q().then(function () {
return api.verify();
}).then(function () {
return api.build(options);
});
};
/**
* Bump
*/
api.bump = function (options) {
var pacorc = configHelpers.getMergedPacorc();
return gitHelpers.isCleanState().then(function (isClean) {
if (!isClean) {
console.log('\n' + 'Oh nose!'.red.bold + '\nYou cannot bump the version number without commiting all changes in this package.\n');
process.exit(1);
}
var mergedOptions = {
version: options.version || 'patch',
tag: options.tag || pacorc.bump.tag,
commit: options.commit || pacorc.bump.commit,
message: options.message || pacorc.bump.message
};
if (!mergedOptions.tag && mergedOptions.commit && mergedOptions.message) {
return bump.bumpAndCommit(mergedOptions);
}
return bump.bump(mergedOptions);
});
};
/**
* Release
*/
api.release = function (options) {
return api.prepare(options).then(function () {
return api.bump(options);
}).then(function () {
return comeondo.exec('npm publish', { cwd: process.env.PACO_PACKAGE_PATH }).catch(exitWithError);
}).then(function () {
if (options.gitPush && (options.commit || options.tag)) {
return comeondo.exec('git push').catch(exitWithError);
}
return Q();
}).then(function () {
if (options.gitPushTags && options.tag) {
return comeondo.exec('git push --tags').catch(exitWithError);
}
return Q();
}).catch(exitWithError);
};
module.exports = api;