Skip to content

Commit

Permalink
fix(#26): add warning if jsonDir contains file when running csv-to-do…
Browse files Browse the repository at this point in the history
…c command
  • Loading branch information
sugat009 committed Jan 24, 2024
1 parent d168379 commit f7440c6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/fn/csv-to-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fs = require('../lib/sync-fs');
const { info, warn } = require('../lib/log');
const generateCsv = require('../lib/generate-users-csv');
const safeStringify = require('../lib/safe-stringify');
const userPrompt = require('../lib/user-prompt');

const pretty = o => JSON.stringify(o, null, 2);

Expand All @@ -29,6 +30,14 @@ const execute = () => {
const jsonDir = `${environment.pathToProject}/json_docs`;
fs.mkdir(jsonDir);

if (!fs.isDirectoryEmpty(jsonDir)) {
warn(`There are already docs in ${jsonDir}.
New json files will be created along side these existing docs.`);
if (!userPrompt.keyInYN('Are you sure you want to continue?')) {
throw new Error('User aborted execution.');
}
}

const saveJsonDoc = doc => fs.write(`${jsonDir}/${doc._id}.doc.json`, safeStringify(doc) + '\n');

const model = {
Expand Down Expand Up @@ -252,6 +261,7 @@ function setNOW(t) {
NOW = t;
}


module.exports = {
requiresInstance: false,
execute,
Expand Down
5 changes: 5 additions & 0 deletions src/lib/sync-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,17 @@ function copy(from, to, { overwrite=true }={}) {
}
}

function isDirectoryEmpty(dir) {
return fs.readdirSync(dir).length > 0 ? false : true;
}

module.exports = {
copy,
dirs,
exists: fs.existsSync,
extension,
fs,
isDirectoryEmpty,
mkdir: path => { try { mkdirp(path); } catch(e) { warn(e); } },
mkdtemp: () => fs.mkdtempSync(`${os.tmpdir()}/cht-conf`),
path,
Expand Down
31 changes: 31 additions & 0 deletions test/lib/sync-fs.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const assert = require('chai').assert;

const chai = require('chai');
const fs = require('../../src/lib/sync-fs');
// NOTE: be careful with the names, this one is the original fs module
// from nodeJS where the upper one is from our sync-fs module
const fs_ = require('fs');
const sinon = require('sinon');

const { expect } = chai;

describe('sync-fs', () => {

Expand All @@ -16,4 +23,28 @@ describe('sync-fs', () => {
});
});
});

describe('#isDirectoryEmpty()', () => {
afterEach(() => {
sinon.restore();
});

it('should return true for an empty directory', () => {
const readdirSyncStub = sinon.stub(fs_, 'readdirSync').returns([]);

const emptyDir = 'path/to/empty/directory';
expect(fs.isDirectoryEmpty(emptyDir)).to.be.true;

expect(readdirSyncStub.calledWith(emptyDir)).to.be.true;
});

it('should return false for a non-empty directory', () => {
const readdirSyncStub = sinon.stub(fs_, 'readdirSync').returns(['file1', 'file2']);

const nonEmptyDir = 'path/to/non-empty/directory';
expect(fs.isDirectoryEmpty(nonEmptyDir)).to.be.false;

expect(readdirSyncStub.calledWith(nonEmptyDir)).to.be.true;
});
});
});

0 comments on commit f7440c6

Please sign in to comment.