Skip to content

Commit

Permalink
Merge d563ad1 into 3ff18e2
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Mar 26, 2021
2 parents 3ff18e2 + d563ad1 commit a243724
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
Expand Down
40 changes: 40 additions & 0 deletions lib/actions/commit-file-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var fs = require('fs').promises;
var path = require('path');

async function write(file) {
var dir = path.dirname(file.path);
try {
await fs.stat(dir);
} catch (error) {
if (error.code === 'ENOENT') {
await fs.mkdir(dir, {recursive: true});
return;
}

throw error;
}

await fs.writeFile(file.path, file.contents, {
mode: file.stat ? file.stat.mode : null
});
}

async function remove(file) {
await fs.rmdir(file.path, {recursive: true});
}

module.exports = async function (file) {
this.store.add(file);
if (file.state === 'modified') {
file.committed = true;
await write(file);
} else if (file.state === 'deleted') {
file.committed = true;
await remove(file);
}

delete file.state;
delete file.isNew;
};
32 changes: 2 additions & 30 deletions lib/actions/commit.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
'use strict';

var fs = require('fs');
var path = require('path');
var through = require('through2');
var {pipeline} = require('stream');

function write(file) {
var dir = path.dirname(file.path);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true});
}

fs.writeFileSync(file.path, file.contents, {
mode: file.stat ? file.stat.mode : null
});
}

function remove(file) {
fs.rmdirSync(file.path, {recursive: true});
}

module.exports = function (filters, stream, cb) {
var store = this.store;
var self = this;

if (typeof filters === 'function') {
cb = filters;
Expand All @@ -43,18 +26,7 @@ module.exports = function (filters, stream, cb) {
});

var commitFilter = through.obj(function (file, enc, cb) {
store.add(file);
if (file.state === 'modified') {
write(file);
file.committed = true;
} else if (file.state === 'deleted') {
remove(file);
file.committed = true;
}

delete file.state;
delete file.isNew;
cb();
self.commitFileAsync(file).then(() => cb()).catch(error => cb(error));
});

pipeline(
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ EditionInterface.prototype._copySingle = require('./actions/copy.js')._copySingl
EditionInterface.prototype.copyTpl = require('./actions/copy-tpl.js');
EditionInterface.prototype.move = require('./actions/move.js');
EditionInterface.prototype.commit = require('./actions/commit.js');
EditionInterface.prototype.commitFileAsync = require('./actions/commit-file-async.js');

exports.create = function (store) {
return new EditionInterface(store);
Expand Down

0 comments on commit a243724

Please sign in to comment.