Skip to content

Commit

Permalink
test/style: refactor remaining grunt tasks to use promises instead of…
Browse files Browse the repository at this point in the history
… callbacks
  • Loading branch information
nknapp committed Dec 14, 2019
1 parent 1ebce2b commit 1ec1737
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 259 deletions.
37 changes: 20 additions & 17 deletions tasks/metrics.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
const _ = require('underscore'),
async = require('neo-async'),
metrics = require('../bench');
const metrics = require('../bench');
const { createRegisterAsyncTaskFn } = require('./util/async-grunt-task');

module.exports = function(grunt) {
grunt.registerTask('metrics', function() {
const done = this.async(),
execName = grunt.option('name'),
events = {};
const registerAsyncTask = createRegisterAsyncTaskFn(grunt);

async.each(
_.keys(metrics),
function(name, complete) {
if (/^_/.test(name) || (execName && name !== execName)) {
return complete();
}
registerAsyncTask('metrics', function() {
const onlyExecuteName = grunt.option('name');
const events = {};

const promises = Object.keys(metrics).map(async name => {
if (/^_/.test(name)) {
return;
}
if (onlyExecuteName != null && name !== onlyExecuteName) {
return;
}

return new Promise(resolve => {
metrics[name](grunt, function(data) {
events[name] = data;
complete();
resolve();
});
},
done
);
});
});

return Promise.all(promises);
});
};
170 changes: 83 additions & 87 deletions tasks/publish.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,38 @@
const _ = require('underscore'),
async = require('neo-async'),
AWS = require('aws-sdk'),
git = require('./util/git'),
semver = require('semver');
const AWS = require('aws-sdk');
const git = require('./util/git');
const { createRegisterAsyncTaskFn } = require('./util/async-grunt-task');
const semver = require('semver');

module.exports = function(grunt) {
grunt.registerTask('publish:latest', function() {
const done = this.async();

git.debug(function(remotes, branches) {
grunt.log.writeln('remotes: ' + remotes);
grunt.log.writeln('branches: ' + branches);

git.commitInfo(function(err, info) {
grunt.log.writeln('tag: ' + info.tagName);

const files = [];

// Publish the master as "latest" and with the commit-id
if (info.isMaster) {
files.push('-latest');
files.push('-' + info.head);
}

// Publish tags by their tag-name
if (info.tagName && semver.valid(info.tagName)) {
files.push('-' + info.tagName);
}

if (files.length > 0) {
initSDK();
grunt.log.writeln('publishing files: ' + JSON.stringify(files));
publish(fileMap(files), done);
} else {
// Silently ignore for branches
done();
}
});
});
});
grunt.registerTask('publish:version', function() {
const done = this.async();
initSDK();
const registerAsyncTask = createRegisterAsyncTaskFn(grunt);

git.commitInfo(function(err, info) {
if (!info.tagName) {
throw new Error('The current commit must be tagged');
}
publish(fileMap(['-' + info.tagName]), done);
});
registerAsyncTask('publish:latest', async () => {
grunt.log.writeln('remotes: ' + (await git.remotes()));
grunt.log.writeln('branches: ' + (await git.branches()));

const commitInfo = await git.commitInfo();
grunt.log.writeln('tag: ' + commitInfo.tagName);

const suffixes = [];

// Publish the master as "latest" and with the commit-id
if (commitInfo.isMaster) {
suffixes.push('-latest');
suffixes.push('-' + commitInfo.headSha);
}

// Publish tags by their tag-name
if (commitInfo.tagName && semver.valid(commitInfo.tagName)) {
suffixes.push('-' + commitInfo.tagName);
}

if (suffixes.length > 0) {
initSDK();
grunt.log.writeln(
'publishing file-suffixes: ' + JSON.stringify(suffixes)
);
await publish(suffixes);
}
});

function initSDK() {
Expand All @@ -62,45 +46,57 @@ module.exports = function(grunt) {

AWS.config.update({ accessKeyId: key, secretAccessKey: secret });
}
function publish(files, callback) {
const s3 = new AWS.S3(),
bucket = process.env.S3_BUCKET_NAME;

async.each(
_.keys(files),
function(file, callback) {
const params = {
Bucket: bucket,
Key: file,
Body: grunt.file.read(files[file])
};
s3.putObject(params, function(err) {
if (err) {
throw err;
} else {
grunt.log.writeln('Published ' + file + ' to build server.');
callback();
}
});
},
callback
);

async function publish(suffixes) {
const publishPromises = suffixes.map(suffix => publishSuffix(suffix));
return Promise.all(publishPromises);
}
function fileMap(suffixes) {
const map = {};
_.each(
[
'handlebars.js',
'handlebars.min.js',
'handlebars.runtime.js',
'handlebars.runtime.min.js'
],
function(file) {
_.each(suffixes, function(suffix) {
map[file.replace(/\.js$/, suffix + '.js')] = 'dist/' + file;
});
}
);
return map;

async function publishSuffix(suffix) {
const filenames = [
'handlebars.js',
'handlebars.min.js',
'handlebars.runtime.js',
'handlebars.runtime.min.js'
];
const publishPromises = filenames.map(filename => {
const nameInBucket = getNameInBucket(filename, suffix);
const localFile = getLocalFile(filename);
uploadToBucket(localFile, nameInBucket);
grunt.log.writeln(
`Published ${localFile} to build server (${nameInBucket})`
);
});
return Promise.all(publishPromises);
}

async function uploadToBucket(localFile, nameInBucket) {
const bucket = process.env.S3_BUCKET_NAME;
const uploadParams = {
Bucket: bucket,
Key: nameInBucket,
Body: grunt.file.read(localFile)
};
return s3PutObject(uploadParams);
}
};

function s3PutObject(uploadParams) {
const s3 = new AWS.S3();
return new Promise((resolve, reject) => {
s3.putObject(uploadParams, err => {
if (err != null) {
return reject(err);
}
resolve();
});
});
}

function getNameInBucket(filename, suffix) {
return filename.replace(/\.js$/, suffix + '.js');
}

function getLocalFile(filename) {
return 'dist/' + filename;
}
Loading

0 comments on commit 1ec1737

Please sign in to comment.