Skip to content

Commit cb32e91

Browse files
authoredJun 1, 2020
Misc improvements and fixes. (#147)
1 parent a37c36c commit cb32e91

File tree

10 files changed

+65
-12
lines changed

10 files changed

+65
-12
lines changed
 

‎.github/workflows/test.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Node.js CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
name: Node ${{ matrix.node-version }}
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
matrix:
12+
node-version: [14.x, 12.x]
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
- run: npm ci
20+
- run: npm test

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Optionally, pass an `options.process` function (`process(contents)`) returning a
7171

7272
`from` can be a glob pattern that'll be match against the file system. If that's the case, then `to` must be an output directory. For a globified `from`, you can optionally pass in an `options.globOptions` object to change its pattern matching behavior. The full list of options are being described [here](https://github.com/mrmlnc/fast-glob#options-1). The `nodir` flag is forced to be `true` in `globOptions` to ensure a vinyl object representing each matching directory is marked as `deleted` in the `mem-fs` store.
7373

74+
Optionally, when `from` is a glob pattern, pass an `options.processDestinationPath` function (`processDestinationPath(destinationFile)`) returning a string who'll become the new file name.
75+
7476
### `#copyTpl(from, to, context[, templateOptions [, copyOptions]])`
7577

7678
Copy the `from` file and, if it is not a binary file, parse its content as an [ejs](http://ejs.co/) template where `context` is the template context (the variable names available inside the template).

‎__tests__/copy-tpl.js

+26
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,30 @@ describe('#copyTpl()', () => {
6868
fs.copyTpl(pathCopied, newPath);
6969
expect(fs.read(newPath)).toBe(fs.read(filepath));
7070
});
71+
72+
it('allow passing circular function context', function () {
73+
const b = {};
74+
const a = {name: 'new content', b};
75+
b.a = a;
76+
const filepath = path.join(__dirname, 'fixtures/file-circular.txt');
77+
const newPath = '/new/path/file.txt';
78+
fs.copyTpl(filepath, newPath, {}, {
79+
context: {a}
80+
});
81+
expect(fs.read(newPath)).toBe('new content new content' + os.EOL);
82+
});
83+
84+
it('removes ejs extension when globbing', function () {
85+
const filepath = path.join(__dirname, 'fixtures/ejs');
86+
const newPath = '/new/path/';
87+
fs.copyTpl(filepath, newPath);
88+
expect(fs.exists(path.join(newPath, 'file-ejs-extension.txt'))).toBeTruthy();
89+
});
90+
91+
it('doens\'t removes ejs extension when not globbing', function () {
92+
const filepath = path.join(__dirname, 'fixtures/ejs/file-ejs-extension.txt.ejs');
93+
const newPath = '/new/path/file-ejs-extension.txt.ejs';
94+
fs.copyTpl(filepath, newPath);
95+
expect(fs.exists(newPath)).toBeTruthy();
96+
});
7197
});

‎__tests__/copy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('#copy()', () => {
8484
let outputDir = path.join(__dirname, '../test/output');
8585
const process = sinon.stub().returnsArg(0);
8686
fs.copy(path.join(__dirname, '/fixtures/**'), outputDir, {process});
87-
sinon.assert.callCount(process, 8); // 7 total files under 'fixtures', not counting folders
87+
sinon.assert.callCount(process, 10); // 8 total files under 'fixtures', not counting folders
8888
expect(fs.read(path.join(outputDir, 'file-a.txt'))).toBe('foo' + os.EOL);
8989
expect(fs.read(path.join(outputDir, '/nested/file.txt'))).toBe('nested' + os.EOL);
9090
});

‎__tests__/fixtures/ejs/file-ejs-extension.txt.ejs

Whitespace-only changes.

‎__tests__/fixtures/file-circular.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= this.a.name %> <%= this.a.b.a.name %>

‎lib/actions/append.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22

3-
var extend = require('deep-extend');
43
var EOL = require('os').EOL;
54

65
module.exports = function (to, contents, options) {
7-
options = extend({
6+
options = {
87
trimEnd: true,
9-
separator: EOL
10-
}, options || {});
8+
separator: EOL,
9+
...options
10+
};
1111

1212
var currentContents = this.read(to);
1313
if (options.trimEnd) {

‎lib/actions/copy-tpl.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
var extend = require('deep-extend');
43
var ejs = require('ejs');
54
var isBinaryFileSync = require('isbinaryfile').isBinaryFileSync;
65

@@ -15,7 +14,7 @@ function render(contents, filename, context, tplSettings) {
1514
contents.toString(),
1615
context,
1716
// Setting filename by default allow including partials.
18-
extend({filename: filename}, tplSettings)
17+
{filename: filename, ...tplSettings}
1918
);
2019
}
2120

@@ -29,11 +28,13 @@ module.exports = function (from, to, context, tplSettings, options) {
2928
this.copy(
3029
from,
3130
to,
32-
extend(options || {}, {
31+
{
32+
processDestinationPath: path => path.replace(/.ejs$/, ''),
33+
...options,
3334
process: function (contents, filename) {
3435
return render(contents, filename, context, tplSettings);
3536
}
36-
}),
37+
},
3738
context,
3839
tplSettings
3940
);

‎lib/actions/copy.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var fs = require('fs');
55
var path = require('path');
66
var glob = require('glob');
77
var globby = require('globby');
8-
var extend = require('deep-extend');
98
var multimatch = require('multimatch');
109
var ejs = require('ejs');
1110
var util = require('../util');
@@ -20,7 +19,7 @@ exports.copy = function (from, to, options, context, tplSettings) {
2019
options = options || {};
2120
var fromGlob = util.globify(from);
2221

23-
var globOptions = extend(options.globOptions || {}, {nodir: true});
22+
var globOptions = {...options.globOptions, nodir: true};
2423
var diskFiles = globby.sync(fromGlob, globOptions);
2524
var storeFiles = [];
2625
this.store.each(file => {
@@ -38,10 +37,11 @@ exports.copy = function (from, to, options, context, tplSettings) {
3837
'When copying multiple files, provide a directory as destination'
3938
);
4039

40+
const processDestinationPath = options.processDestinationPath || (path => path);
4141
var root = util.getCommonPath(from);
4242
generateDestination = filepath => {
4343
var toFile = path.relative(root, filepath);
44-
return path.join(to, toFile);
44+
return processDestinationPath(path.join(to, toFile));
4545
};
4646
}
4747

‎package.json

+3
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@
3939
"collectCoverage": true,
4040
"coverageDirectory": "coverage",
4141
"testEnvironment": "node"
42+
},
43+
"engines": {
44+
"node": ">=10.0.0"
4245
}
4346
}

0 commit comments

Comments
 (0)
Please sign in to comment.