2 * Jake JavaScript build tool
3 * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 let fs = require('fs');
20 let path = require('path');
21 let exec = require('child_process').execSync;
22 let FileList = require('filelist').FileList;
24 let PublishTask = function () {
25 let args = Array.prototype.slice.call(arguments).filter(function (item) {
26 return typeof item != 'undefined';
32 let createDef = function (arg) {
34 this.packageFiles.include(arg);
38 this.name = args.shift();
40 // Old API, just name + list of files
41 if (args.length == 1 && (Array.isArray(args[0]) || typeof args[0] == 'string')) {
42 definition = createDef(args.pop());
44 // Current API, name + [prereqs] + [opts] + definition
46 while ((arg = args.pop())) {
48 if (typeof arg == 'function') {
52 else if (Array.isArray(arg) || typeof arg == 'string') {
62 this.prereqs = prereqs;
63 this.packageFiles = new FileList();
64 this.publishCmd = opts.publishCmd || 'npm publish %filename';
65 this.publishMessage = opts.publishMessage || 'BOOM! Published.';
66 this.gitCmd = opts.gitCmd || 'git';
67 this.versionFiles = opts.versionFiles || ['package.json'];
68 this.scheduleDelay = 5000;
70 // Override utility funcs for testing
71 this._ensureRepoClean = function (stdout) {
73 fail(new Error('Git repository is not clean.'));
76 this._getCurrentBranch = function (stdout) {
77 return String(stdout).trim();
80 if (typeof definition == 'function') {
81 definition.call(this);
87 PublishTask.prototype = new (function () {
89 let _currentBranch = null;
91 let getPackage = function () {
92 let pkg = JSON.parse(fs.readFileSync(path.join(process.cwd(),
93 '/package.json')).toString());
96 let getPackageVersionNumber = function () {
97 return getPackage().version;
100 this.define = function () {
103 namespace('publish', function () {
104 task('fetchTags', function () {
105 // Make sure local tags are up to date
106 exec(self.gitCmd + ' fetch --tags');
107 console.log('Fetched remote tags.');
110 task('getCurrentBranch', function () {
111 // Figure out what branch to push to
112 let stdout = exec(self.gitCmd + ' symbolic-ref --short HEAD').toString();
114 throw new Error('No current Git branch found');
116 _currentBranch = self._getCurrentBranch(stdout);
117 console.log('On branch ' + _currentBranch);
120 task('ensureClean', function () {
121 // Only bump, push, and tag if the Git repo is clean
122 let stdout = exec(self.gitCmd + ' status --porcelain --untracked-files=no').toString();
123 // Throw if there's output
124 self._ensureRepoClean(stdout);
127 task('updateVersionFiles', function () {
133 // Grab the current version-string
135 version = pkg.version;
136 // Increment the patch-number for the version
137 arr = version.split('.');
138 patch = parseInt(arr.pop(), 10) + 1;
140 version = arr.join('.');
142 // Update package.json or other files with the new version-info
143 self.versionFiles.forEach(function (file) {
144 let p = path.join(process.cwd(), file);
145 let data = JSON.parse(fs.readFileSync(p).toString());
146 data.version = version;
147 fs.writeFileSync(p, JSON.stringify(data, true, 2) + '\n');
149 // Return the version string so that listeners for the 'complete' event
150 // for this task can use it (e.g., to update other files before pushing
155 task('pushVersion', ['ensureClean', 'updateVersionFiles'], function () {
156 let version = getPackageVersionNumber();
157 let message = 'Version ' + version;
159 self.gitCmd + ' commit -a -m "' + message + '"',
160 self.gitCmd + ' push origin ' + _currentBranch,
161 self.gitCmd + ' tag -a v' + version + ' -m "' + message + '"',
162 self.gitCmd + ' push --tags'
164 cmds.forEach((cmd) => {
167 version = getPackageVersionNumber();
168 console.log('Bumped version number to v' + version + '.');
171 let defineTask = task('definePackage', function () {
172 let version = getPackageVersionNumber();
173 new jake.PackageTask(self.name, 'v' + version, self.prereqs, function () {
174 // Replace the PackageTask's FileList with the PublishTask's FileList
175 this.packageFiles = self.packageFiles;
176 this.needTarGz = true; // Default to tar.gz
177 // If any of the need<CompressionFormat> or archive opts are set
178 // proxy them to the PackageTask
179 for (let p in this) {
180 if (p.indexOf('need') === 0 || p.indexOf('archive') === 0) {
181 if (typeof self[p] != 'undefined') {
188 defineTask._internal = true;
190 task('package', function () {
191 let definePack = jake.Task['publish:definePackage'];
192 let pack = jake.Task['package'];
193 let version = getPackageVersionNumber();
195 // May have already been run
196 if (definePack.taskStatus == jake.Task.runStatuses.DONE) {
197 definePack.reenable(true);
200 // Set manually, completion happens in next tick, creating deadlock
201 definePack.taskStatus = jake.Task.runStatuses.DONE;
203 console.log('Created package for ' + self.name + ' v' + version);
206 task('publish', function () {
207 return new Promise((resolve) => {
208 let version = getPackageVersionNumber();
212 console.log('Publishing ' + self.name + ' v' + version);
214 if (typeof self.createPublishCommand == 'function') {
215 cmd = self.createPublishCommand(version);
218 filename = './pkg/' + self.name + '-v' + version + '.tar.gz';
219 cmd = self.publishCmd.replace(/%filename/gi, filename);
222 if (typeof cmd == 'function') {
227 console.log(self.publishMessage);
232 // Hackity hack -- NPM publish sometimes returns errror like:
233 // Error sending version data\nnpm ERR!
234 // Error: forbidden 0.2.4 is modified, should match modified time
235 setTimeout(function () {
236 let stdout = exec(cmd).toString() || '';
237 stdout = stdout.trim();
241 console.log(self.publishMessage);
243 }, self.scheduleDelay);
248 task('cleanup', function () {
249 return new Promise((resolve) => {
250 let clobber = jake.Task.clobber;
251 clobber.reenable(true);
252 clobber.on('complete', function () {
253 console.log('Cleaned up package');
262 let prefixNs = function (item) {
263 return 'publish:' + item;
266 // Create aliases in the default namespace
267 desc('Create a new version and release.');
268 task('publish', self.prereqs.concat(['version', 'release']
271 desc('Release the existing version.');
272 task('publishExisting', self.prereqs.concat(['release']
275 task('version', ['fetchTags', 'getCurrentBranch', 'pushVersion']
278 task('release', ['package', 'publish', 'cleanup']
281 // Invoke proactively so there will be a callable 'package' task
282 // which can be used apart from 'publish'
283 jake.Task['publish:definePackage'].invoke();
288 jake.PublishTask = PublishTask;
289 exports.PublishTask = PublishTask;