Skip to content

Commit

Permalink
Drop node 4 support and modernize the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadKillingsworth committed Apr 7, 2018
1 parent a2f5768 commit 53f06e2
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 2,801 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -2,7 +2,6 @@ language: node_js
node_js:
- '8'
- '6'
- '4'
branches:
only:
- master
Expand Down
48 changes: 23 additions & 25 deletions lib/grunt/index.js
Expand Up @@ -26,14 +26,12 @@

'use strict';

module.exports = function(grunt, extraArguments) {
var chalk = require('chalk');
var VinylStream = require('./vinyl-stream');
var fs = require('fs');
var path = require('path');
var gulpCompiler = require('../gulp')({extraArguments: extraArguments});
var Transform = require('stream').Transform;
var gulpCompilerOptions = {
module.exports = (grunt, extraArguments) => {
const chalk = require('chalk');
const VinylStream = require('./vinyl-stream');
const gulpCompiler = require('../gulp')({extraArguments: extraArguments});
const Transform = require('stream').Transform;
const gulpCompilerOptions = {
streamMode: 'IN',
logger: grunt.log,
pluginName: 'grunt-google-closure-compiler',
Expand All @@ -46,7 +44,7 @@ module.exports = function(grunt, extraArguments) {
* @return {Promise}
*/
function compilationPromise(files, options) {
var hadError = false;
let hadError = false;
function logFile(cb) {
// If an error was encoutered, it will have already been logged
if (!hadError) {
Expand All @@ -59,14 +57,14 @@ module.exports = function(grunt, extraArguments) {
cb();
}

var loggingStream = new Transform({
const loggingStream = new Transform({
objectMode: true,
transform: function() {},
flush: logFile
});

return new Promise(function(resolve, reject) {
var stream;
let stream;
if (files) {
// Source files were provided by grunt. Read these
// in to a stream of vinyl files and pipe them through
Expand Down Expand Up @@ -95,16 +93,16 @@ module.exports = function(grunt, extraArguments) {
}

function closureCompilerGruntTask() {
var taskObject = this;
var asyncDone = this.async();
var compileTasks = [];
const taskObject = this;
const asyncDone = this.async();
const compileTasks = [];

function getCompilerOptions() {
var opts = taskObject.options({
const opts = taskObject.options({
args: undefined
});

var args = opts.args;
const args = opts.args;

delete opts.args;

Expand All @@ -116,9 +114,9 @@ module.exports = function(grunt, extraArguments) {

// Invoke the compiler once for each set of source files
taskObject.files.forEach(function (f) {
var options = getCompilerOptions();
const options = getCompilerOptions();

var src = f.src.filter(function (filepath) {
const src = f.src.filter(filepath => {
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found');
return false;
Expand All @@ -144,18 +142,18 @@ module.exports = function(grunt, extraArguments) {
// If the task was invoked without any files provided by grunt, assume that
// --js flags are present and we want to run the compiler anyway.
if (taskObject.files.length === 0) {
var options = getCompilerOptions();
const options = getCompilerOptions();
compileTasks.push(compilationPromise(null, options.args || options.compilerOpts));
}

// Multiple invocations of the compiler can occur for a single task target. Wait until
// they are all completed before calling the "done" method.
Promise.all(compileTasks).then(function () {
asyncDone();
}, function () {
grunt.fail.warn('Compilation error');
asyncDone();
});
Promise.all(compileTasks)
.then(() => asyncDone())
.catch(() => {
grunt.fail.warn('Compilation error');
asyncDone();
});
}

grunt.registerMultiTask('closure-compiler',
Expand Down
77 changes: 39 additions & 38 deletions lib/grunt/vinyl-stream.js
Expand Up @@ -22,51 +22,52 @@
*/
'use strict';

var fs = require('fs');
var path = require('path');
var Readable = require('stream').Readable;
var File = require('vinyl');
const fs = require('fs');
const path = require('path');
const Readable = require('stream').Readable;
const File = require('vinyl');

function VinylStream(files, opts) {
Readable.call(this, {objectMode: true});
this._base = path.resolve(opts.base || process.cwd());
this._files = files.slice();
this.resume();
}
VinylStream.prototype = Object.create(Readable.prototype);

VinylStream.prototype._read = function() {
if (this._files.length === 0) {
this.emit('end');
return;
class VinylStream extends Readable {
constructor(files, opts) {
super({objectMode: true});
this._base = path.resolve(opts.base || process.cwd());
this._files = files.slice();
this.resume();
}
this.readFile();
};

VinylStream.prototype.readFile = function() {
if (this._files.length === 0) {
return;
}
var filepath = this._files.shift();
var fullpath = path.resolve(this._base, filepath);
fs.readFile(fullpath, (function (fullpath, err, data) {
if (err) {
this.emit('error', err);
_read() {
if (this._files.length === 0) {
this.emit('end');
return;
}
this.readFile();
}

var file = new File({
base: this._base,
path: fullpath,
contents: data
});

if (!this.push(file)) {
readFile() {
if (this._files.length === 0) {
return;
} else {
this.readFile();
}
}).bind(this, fullpath));
};
const filepath = this._files.shift();
const fullpath = path.resolve(this._base, filepath);
fs.readFile(fullpath, (err, data) => {
if (err) {
this.emit('error', err);
return;
}

const file = new File({
base: this._base,
path: fullpath,
contents: data
});

if (!this.push(file)) {
return;
} else {
this.readFile();
}
});
}
}

module.exports = VinylStream;
10 changes: 5 additions & 5 deletions lib/gulp/concat-to-json.js
Expand Up @@ -24,7 +24,7 @@
'use strict';

function json_file(src, path, source_map) {
var filejson = {
const filejson = {
src: src
};

Expand All @@ -43,10 +43,10 @@ function json_file(src, path, source_map) {
* @param {Array<Object>} files
* @return {string}
*/
module.exports = function(files) {
var path = require('path');
var jsonFiles = [];
for (var i = 0; i < files.length; i++) {
module.exports = files => {
const path = require('path');
const jsonFiles = [];
for (let i = 0; i < files.length; i++) {
jsonFiles.push(
json_file(files[i].contents.toString(), files[i].relative || path.relative(process.cwd(), files[i].path),
files[i].sourceMap ? JSON.stringify(files[i].sourceMap) : undefined));
Expand Down

0 comments on commit 53f06e2

Please sign in to comment.