Skip to content

Commit

Permalink
lower copy's zero check level when from is glob pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
江珊 committed Apr 28, 2018
1 parent 052403e commit e1a516b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
16 changes: 7 additions & 9 deletions lib/actions/copy-tpl.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
'use strict';

var path = require('path');
var extend = require('deep-extend');
var ejs = require('ejs');
var isBinaryFile = require("isbinaryfile");
var isBinaryFile = require('isbinaryfile');

function render(contents, filename, context, tplSettings) {
if (isBinaryFile.sync(filename)) {
return contents.toString();
} else {
return ejs.render(
contents.toString(),
context,
// Setting filename by default allow including partials.
extend({filename: filename}, tplSettings)
);
}
return ejs.render(
contents.toString(),
context,
// Setting filename by default allow including partials.
extend({filename: filename}, tplSettings)
);
}

module.exports = function (from, to, context, tplSettings, options) {
Expand Down
16 changes: 14 additions & 2 deletions lib/actions/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ var util = require('../util');

function applyProcessingFunc(process, contents, filename) {
var output = process(contents, filename);
return output instanceof Buffer ? output : new Buffer(output);
return output instanceof Buffer ? output : Buffer.from(output);
}

function arrayHasMagic(pattern) {
if (Array.isArray(pattern)) {
return pattern.some(arrayHasMagic);
}
return glob.hasMagic(pattern);
}

exports.copy = function (from, to, options) {
Expand Down Expand Up @@ -44,7 +51,12 @@ exports.copy = function (from, to, options) {
}

// Sanity checks: Makes sure we copy at least one file.
assert(files.length > 0, 'Trying to copy from a source that does not exist: ' + from);
// Glob pattern sometimes match no files, lower check level
if (!arrayHasMagic(from)) {
assert(files.length > 0, 'Trying to copy from a source that does not exist: ' + from);
} else if (files.length === 0) {
console.warn('Trying to copy from a source that does not exist: ' + from);
}

files.forEach(file => {
this._copySingle(file, generateDestination(file), options);
Expand Down
2 changes: 1 addition & 1 deletion lib/actions/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function (filepath, contents, stat) {
var file = this.store.get(filepath);
file.isNew = file.contents === null;
file.state = 'modified';
file.contents = typeof contents === 'string' ? new Buffer(contents) : contents;
file.contents = typeof contents === 'string' ? Buffer.from(contents) : contents;
file.stat = stat;
this.store.add(file);

Expand Down

0 comments on commit e1a516b

Please sign in to comment.