Skip to content

Commit

Permalink
Add append support to copy operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Mar 26, 2021
1 parent 71f8a13 commit 9083f3c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ Copy a file from the `from` path to the `to` path.

Optionally, pass an `options.process` function (`process(contents)`) returning a string or a buffer who'll become the new file content. The process function will take a single contents argument who is the copied file contents as a `Buffer`.

`option.ignoreNoMatch` can be used to silence the error thrown if no files match the `from` pattern.
`options.ignoreNoMatch` can be used to silence the error thrown if no files match the `from` pattern.

`options.append` can be used to append `from` contents to `to` instead of copying.

`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.

Expand Down
9 changes: 9 additions & 0 deletions __tests__/copy-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ describe('#copyTpl()', () => {
expect(fs.read(newPath)).toBe('partial' + os.EOL + os.EOL);
});

it('allow appending files', function () {
const filepath = path.join(__dirname, 'fixtures/file-tpl.txt');
const newPath = '/new/path/file-append.txt';
fs.copyTpl(filepath, newPath, {name: 'new content'});
expect(fs.read(newPath)).toBe('new content' + os.EOL);
fs.copyTpl(filepath, newPath, {name: 'new content'}, undefined, {append: true});
expect(fs.read(newPath)).toBe('new content' + os.EOL + 'new content' + os.EOL);
});

it('allow including glob options', function () {
const filenames = [
path.join(__dirname, 'fixtures/file-tpl-partial.txt'),
Expand Down
12 changes: 12 additions & 0 deletions __tests__/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ describe('#copy()', () => {
expect(fs.store.get(newPath).state).toBe('modified');
});

it('append file', () => {
const filepath = path.join(__dirname, 'fixtures/file-a.txt');
const initialContents = fs.read(filepath);
const newPath = '/new/path/file.txt';
fs.copy(filepath, newPath);
expect(fs.read(newPath)).toBe(initialContents);
expect(fs.store.get(newPath).state).toBe('modified');

fs.copy(filepath, newPath, {append: true});
expect(fs.read(newPath)).toBe(initialContents + initialContents);
});

it('can copy directory not commited to disk', () => {
let sourceDir = path.join(__dirname, '../test/foo');
let destDir = path.join(__dirname, '../test/bar');
Expand Down
7 changes: 6 additions & 1 deletion lib/actions/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.copy = function (from, to, options, context, tplSettings) {
var storeFiles = [];
this.store.each(file => {
// The store may have a glob path and when we try to copy it will fail because not real file
if (!globby.hasMagic(normalize(file.path)) && multimatch([file.path], fromGlob).length !== 0) {
if (!globby.hasMagic(normalize(file.path)) && multimatch([file.path], fromGlob).length !== 0 && !diskFiles.includes(file.path)) {
storeFiles.push(file.path);
}
});
Expand Down Expand Up @@ -69,5 +69,10 @@ exports._copySingle = function (from, to, options, context, tplSettings) {
to = ejs.render(to, context, tplSettings);
}

if (options.append) {
this.append(to, contents, options);
return;
}

this.write(to, contents, file.stat);
};

0 comments on commit 9083f3c

Please sign in to comment.