Skip to content

Commit d2cea70

Browse files
committed
Move logic to ensure directory into util function
1 parent b058ae2 commit d2cea70

5 files changed

Lines changed: 33 additions & 24 deletions

File tree

src/Consumer.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const stream = require('stream');
22
const fs = require('fs');
33
const path = require('path');
4-
const mkdirpSync = require('mkdirp').sync;
5-
const { assert } = require('./util');
4+
const { assert, ensureDirectory } = require('./util');
65

76
const Storage = require('./Storage/ReadableStorage');
87
const MAX_CATCHUP_BATCH = 10;
@@ -59,9 +58,7 @@ class Consumer extends stream.Readable {
5958
this.indexName = indexName;
6059
const consumerDirectory = path.join(this.storage.indexDirectory, 'consumers');
6160
this.fileName = path.join(consumerDirectory, this.storage.storageFile + '.' + indexName + '.' + identifier);
62-
if (!fs.existsSync(consumerDirectory)) {
63-
mkdirpSync(consumerDirectory);
64-
} else {
61+
if (ensureDirectory(consumerDirectory)) {
6562
this.cleanUpFailedWrites();
6663
}
6764
}

src/Index/WritableIndex.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const fs = require('fs');
2-
const mkdirpSync = require('mkdirp').sync;
32
const ReadableIndex = require('./ReadableIndex');
4-
const { assertEqual, buildMetadataHeader } = require('../util');
3+
const { assertEqual, buildMetadataHeader, ensureDirectory } = require('../util');
54

65
/**
76
* An index is a simple append-only file that stores an ordered list of entry elements pointing to the actual file position
@@ -45,9 +44,7 @@ class WritableIndex extends ReadableIndex {
4544
*/
4645
initialize(options) {
4746
super.initialize(options);
48-
if (!fs.existsSync(options.dataDirectory)) {
49-
mkdirpSync(options.dataDirectory);
50-
}
47+
ensureDirectory(options.dataDirectory);
5148

5249
this.fileMode = 'a+';
5350
this.writeBuffer = Buffer.allocUnsafe(options.writeBufferSize >>> 0); // jshint ignore:line

src/Partition/WritablePartition.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const fs = require('fs');
2-
const mkdirpSync = require('mkdirp').sync;
32
const ReadablePartition = require('./ReadablePartition');
4-
const { assert, buildMetadataHeader, alignTo } = require('../util');
3+
const { assert, buildMetadataHeader, alignTo, ensureDirectory } = require('../util');
54
const Clock = require('../Clock');
65

76
const DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
@@ -40,9 +39,7 @@ class WritablePartition extends ReadablePartition {
4039
config.metadata = Object.assign(defaults.metadata, config.metadata);
4140
config = Object.assign(defaults, config);
4241
super(name, config);
43-
if (!fs.existsSync(this.dataDirectory)) {
44-
mkdirpSync(this.dataDirectory);
45-
}
42+
ensureDirectory(this.dataDirectory);
4643
this.fileMode = 'a+';
4744
this.writeBufferSize = config.writeBufferSize >>> 0; // jshint ignore:line
4845
this.maxWriteBufferDocuments = config.maxWriteBufferDocuments >>> 0; // jshint ignore:line

src/Storage/WritableStorage.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const fs = require('fs');
2-
const mkdirpSync = require('mkdirp').sync;
32
const path = require('path');
43
const WritablePartition = require('../Partition/WritablePartition');
54
const WritableIndex = require('../Index/WritableIndex');
65
const ReadableStorage = require('./ReadableStorage');
7-
const { assert, matches, buildMetadataForMatcher, buildMatcherFromMetadata } = require('../util');
6+
const { assert, matches, buildMetadataForMatcher, buildMatcherFromMetadata, ensureDirectory } = require('../util');
87

98
const DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
109

@@ -57,12 +56,7 @@ class WritableStorage extends ReadableStorage {
5756
};
5857
config = Object.assign(defaults, config);
5958
config.indexOptions = Object.assign({ syncOnFlush: config.syncOnFlush }, config.indexOptions);
60-
if (!fs.existsSync(config.dataDirectory)) {
61-
try {
62-
mkdirpSync(config.dataDirectory);
63-
} catch (e) {
64-
}
65-
}
59+
ensureDirectory(config.dataDirectory);
6660
super(storageName, config);
6761

6862
this.lockFile = path.resolve(this.dataDirectory, this.storageFile + '.lock');

src/util.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const crypto = require('crypto');
2+
const fs = require('fs');
3+
const mkdirpSync = require('mkdirp').sync;
24

35
/**
46
* Assert that actual and expected match or throw an Error with the given message appended by information about expected and actual value.
@@ -183,6 +185,27 @@ function wrapAndCheck(index, length) {
183185
return index;
184186
}
185187

188+
/**
189+
* Ensure that the given directory exists.
190+
* @param {string} dirName
191+
* @return {boolean} true if the directory existed already
192+
*/
193+
function ensureDirectory(dirName) {
194+
if (!fs.existsSync(dirName)) {
195+
try {
196+
mkdirpSync(dirName);
197+
} catch (e) {
198+
if (e.code !== 'EEXIST') {
199+
throw e;
200+
}
201+
return true;
202+
}
203+
return false;
204+
}
205+
return true;
206+
}
207+
208+
186209
module.exports = {
187210
assert,
188211
assertEqual,
@@ -193,5 +216,6 @@ module.exports = {
193216
buildMetadataForMatcher,
194217
buildMatcherFromMetadata,
195218
buildMetadataHeader,
196-
alignTo
219+
alignTo,
220+
ensureDirectory
197221
};

0 commit comments

Comments
 (0)