cht-conf 3.21.4 → 3.21.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [3.21.5](https://github.com/medic/cht-conf/compare/v3.21.4...v3.21.5) (2024-02-05)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **#26:** add warning if jsonDir contains file when running csv-to-doc command ([#603](https://github.com/medic/cht-conf/issues/603)) ([e445c9b](https://github.com/medic/cht-conf/commit/e445c9b44be94b3a7492a471c5c709857509076e)), closes [#26](https://github.com/medic/cht-conf/issues/26)
7
+
1
8
  ## [3.21.4](https://github.com/medic/cht-conf/compare/v3.21.3...v3.21.4) (2024-01-31)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cht-conf",
3
- "version": "3.21.4",
3
+ "version": "3.21.5",
4
4
  "description": "Configure CHT deployments",
5
5
  "main": "./src/lib/main.js",
6
6
  "engines": {
@@ -29,6 +29,10 @@ const execute = () => {
29
29
  const jsonDir = `${environment.pathToProject}/json_docs`;
30
30
  fs.mkdir(jsonDir);
31
31
 
32
+ const warningMsg = `There are already docs in ${jsonDir}.
33
+ New json files will be created along side these existing docs.`;
34
+ fs.warnIfDirectoryIsNotEmpty(jsonDir, warningMsg);
35
+
32
36
  const saveJsonDoc = doc => fs.write(`${jsonDir}/${doc._id}.doc.json`, safeStringify(doc) + '\n');
33
37
 
34
38
  const model = {
@@ -262,5 +266,5 @@ module.exports = {
262
266
  int,
263
267
  setCol,
264
268
  parseColumn,
265
- removeExcludedField
269
+ removeExcludedField,
266
270
  };
@@ -4,6 +4,7 @@ const mkdirp = require('mkdirp').sync;
4
4
  const os = require('os');
5
5
  const path = require('path');
6
6
  const trace = require('../lib/log').trace;
7
+ const userPrompt = require('../lib/user-prompt');
7
8
  const warn = require('../lib/log').warn;
8
9
 
9
10
  function read(path) {
@@ -80,12 +81,27 @@ function copy(from, to, { overwrite=true }={}) {
80
81
  }
81
82
  }
82
83
 
84
+ function isDirectoryEmpty(dir) {
85
+ return !fs.readdirSync(dir).length;
86
+ }
87
+
88
+ function warnIfDirectoryIsNotEmpty(dir, warningMsg) {
89
+ if (!isDirectoryEmpty(dir)) {
90
+ warn(warningMsg);
91
+
92
+ if (!userPrompt.keyInYN('Are you sure you want to continue?')) {
93
+ throw new Error('User aborted execution.');
94
+ }
95
+ }
96
+ }
97
+
83
98
  module.exports = {
84
99
  copy,
85
100
  dirs,
86
101
  exists: fs.existsSync,
87
102
  extension,
88
103
  fs,
104
+ isDirectoryEmpty,
89
105
  mkdir: path => { try { mkdirp(path); } catch(e) { warn(e); } },
90
106
  mkdtemp: () => fs.mkdtempSync(`${os.tmpdir()}/cht-conf`),
91
107
  path,
@@ -98,6 +114,7 @@ module.exports = {
98
114
  deleteFilesInFolder: folderPath => recurseFiles(folderPath).forEach(filePath => fs.unlinkSync(filePath)),
99
115
  readdir: fs.readdirSync,
100
116
  statSync: fs.statSync,
117
+ warnIfDirectoryIsNotEmpty,
101
118
  withoutExtension,
102
119
  write: (path, data, options = 'utf8') => fs.writeFileSync(path, data, options),
103
120
  writeBinary: (path, content) => fs.writeFileSync(path, content),