Skip to content

Commit 1ec1737

Browse files
committed
test/style: refactor remaining grunt tasks to use promises instead of callbacks
1 parent 1ebce2b commit 1ec1737

File tree

4 files changed

+211
-259
lines changed

4 files changed

+211
-259
lines changed

tasks/metrics.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
const _ = require('underscore'),
2-
async = require('neo-async'),
3-
metrics = require('../bench');
1+
const metrics = require('../bench');
2+
const { createRegisterAsyncTaskFn } = require('./util/async-grunt-task');
43

54
module.exports = function(grunt) {
6-
grunt.registerTask('metrics', function() {
7-
const done = this.async(),
8-
execName = grunt.option('name'),
9-
events = {};
5+
const registerAsyncTask = createRegisterAsyncTaskFn(grunt);
106

11-
async.each(
12-
_.keys(metrics),
13-
function(name, complete) {
14-
if (/^_/.test(name) || (execName && name !== execName)) {
15-
return complete();
16-
}
7+
registerAsyncTask('metrics', function() {
8+
const onlyExecuteName = grunt.option('name');
9+
const events = {};
1710

11+
const promises = Object.keys(metrics).map(async name => {
12+
if (/^_/.test(name)) {
13+
return;
14+
}
15+
if (onlyExecuteName != null && name !== onlyExecuteName) {
16+
return;
17+
}
18+
19+
return new Promise(resolve => {
1820
metrics[name](grunt, function(data) {
1921
events[name] = data;
20-
complete();
22+
resolve();
2123
});
22-
},
23-
done
24-
);
24+
});
25+
});
26+
27+
return Promise.all(promises);
2528
});
2629
};

tasks/publish.js

+83-87
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,38 @@
1-
const _ = require('underscore'),
2-
async = require('neo-async'),
3-
AWS = require('aws-sdk'),
4-
git = require('./util/git'),
5-
semver = require('semver');
1+
const AWS = require('aws-sdk');
2+
const git = require('./util/git');
3+
const { createRegisterAsyncTaskFn } = require('./util/async-grunt-task');
4+
const semver = require('semver');
65

76
module.exports = function(grunt) {
8-
grunt.registerTask('publish:latest', function() {
9-
const done = this.async();
10-
11-
git.debug(function(remotes, branches) {
12-
grunt.log.writeln('remotes: ' + remotes);
13-
grunt.log.writeln('branches: ' + branches);
14-
15-
git.commitInfo(function(err, info) {
16-
grunt.log.writeln('tag: ' + info.tagName);
17-
18-
const files = [];
19-
20-
// Publish the master as "latest" and with the commit-id
21-
if (info.isMaster) {
22-
files.push('-latest');
23-
files.push('-' + info.head);
24-
}
25-
26-
// Publish tags by their tag-name
27-
if (info.tagName && semver.valid(info.tagName)) {
28-
files.push('-' + info.tagName);
29-
}
30-
31-
if (files.length > 0) {
32-
initSDK();
33-
grunt.log.writeln('publishing files: ' + JSON.stringify(files));
34-
publish(fileMap(files), done);
35-
} else {
36-
// Silently ignore for branches
37-
done();
38-
}
39-
});
40-
});
41-
});
42-
grunt.registerTask('publish:version', function() {
43-
const done = this.async();
44-
initSDK();
7+
const registerAsyncTask = createRegisterAsyncTaskFn(grunt);
458

46-
git.commitInfo(function(err, info) {
47-
if (!info.tagName) {
48-
throw new Error('The current commit must be tagged');
49-
}
50-
publish(fileMap(['-' + info.tagName]), done);
51-
});
9+
registerAsyncTask('publish:latest', async () => {
10+
grunt.log.writeln('remotes: ' + (await git.remotes()));
11+
grunt.log.writeln('branches: ' + (await git.branches()));
12+
13+
const commitInfo = await git.commitInfo();
14+
grunt.log.writeln('tag: ' + commitInfo.tagName);
15+
16+
const suffixes = [];
17+
18+
// Publish the master as "latest" and with the commit-id
19+
if (commitInfo.isMaster) {
20+
suffixes.push('-latest');
21+
suffixes.push('-' + commitInfo.headSha);
22+
}
23+
24+
// Publish tags by their tag-name
25+
if (commitInfo.tagName && semver.valid(commitInfo.tagName)) {
26+
suffixes.push('-' + commitInfo.tagName);
27+
}
28+
29+
if (suffixes.length > 0) {
30+
initSDK();
31+
grunt.log.writeln(
32+
'publishing file-suffixes: ' + JSON.stringify(suffixes)
33+
);
34+
await publish(suffixes);
35+
}
5236
});
5337

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

6347
AWS.config.update({ accessKeyId: key, secretAccessKey: secret });
6448
}
65-
function publish(files, callback) {
66-
const s3 = new AWS.S3(),
67-
bucket = process.env.S3_BUCKET_NAME;
68-
69-
async.each(
70-
_.keys(files),
71-
function(file, callback) {
72-
const params = {
73-
Bucket: bucket,
74-
Key: file,
75-
Body: grunt.file.read(files[file])
76-
};
77-
s3.putObject(params, function(err) {
78-
if (err) {
79-
throw err;
80-
} else {
81-
grunt.log.writeln('Published ' + file + ' to build server.');
82-
callback();
83-
}
84-
});
85-
},
86-
callback
87-
);
49+
50+
async function publish(suffixes) {
51+
const publishPromises = suffixes.map(suffix => publishSuffix(suffix));
52+
return Promise.all(publishPromises);
8853
}
89-
function fileMap(suffixes) {
90-
const map = {};
91-
_.each(
92-
[
93-
'handlebars.js',
94-
'handlebars.min.js',
95-
'handlebars.runtime.js',
96-
'handlebars.runtime.min.js'
97-
],
98-
function(file) {
99-
_.each(suffixes, function(suffix) {
100-
map[file.replace(/\.js$/, suffix + '.js')] = 'dist/' + file;
101-
});
102-
}
103-
);
104-
return map;
54+
55+
async function publishSuffix(suffix) {
56+
const filenames = [
57+
'handlebars.js',
58+
'handlebars.min.js',
59+
'handlebars.runtime.js',
60+
'handlebars.runtime.min.js'
61+
];
62+
const publishPromises = filenames.map(filename => {
63+
const nameInBucket = getNameInBucket(filename, suffix);
64+
const localFile = getLocalFile(filename);
65+
uploadToBucket(localFile, nameInBucket);
66+
grunt.log.writeln(
67+
`Published ${localFile} to build server (${nameInBucket})`
68+
);
69+
});
70+
return Promise.all(publishPromises);
71+
}
72+
73+
async function uploadToBucket(localFile, nameInBucket) {
74+
const bucket = process.env.S3_BUCKET_NAME;
75+
const uploadParams = {
76+
Bucket: bucket,
77+
Key: nameInBucket,
78+
Body: grunt.file.read(localFile)
79+
};
80+
return s3PutObject(uploadParams);
10581
}
10682
};
83+
84+
function s3PutObject(uploadParams) {
85+
const s3 = new AWS.S3();
86+
return new Promise((resolve, reject) => {
87+
s3.putObject(uploadParams, err => {
88+
if (err != null) {
89+
return reject(err);
90+
}
91+
resolve();
92+
});
93+
});
94+
}
95+
96+
function getNameInBucket(filename, suffix) {
97+
return filename.replace(/\.js$/, suffix + '.js');
98+
}
99+
100+
function getLocalFile(filename) {
101+
return 'dist/' + filename;
102+
}

0 commit comments

Comments
 (0)