@iebh/reflib 2.6.2 → 2.6.4

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/lib/writeFile.js CHANGED
@@ -1,32 +1,32 @@
1
- import {createWriteStream} from 'node:fs';
2
- import {finished as streamFinished} from 'node:stream/promises';
3
- import {identifyFormat} from './identifyFormat.js';
4
- import {writeStream} from './writeStream.js';
5
-
6
- /**
7
- * Write a file to disk via the appropriate module
8
- *
9
- * @param {string} path The file path to write, the module to use will be determined from this
10
- * @param {Array<ReflibRef>} refs Collection of references to write
11
- * @param {Object} [options] Additional options to pass to the file writer
12
- * @param {string} [options.module] The module to use if overriding from the file path
13
- *
14
- * @returns {Promise} A promise which resolves when the operation has completed
15
- */
16
- export function writeFile(path, refs, options) {
17
- let format = options?.module || identifyFormat(path)?.id;
18
- if (!format) throw new Error(`Unable to identify reference library format when saving file "${path}"`);
19
-
20
- let fileStream = createWriteStream(path);
21
- let writer = writeStream(format, fileStream, options);
22
-
23
- return Promise.resolve()
24
- .then(()=> writer.start())
25
- .then(()=> refs.reduce((chain, ref, refIndex, refs) => chain // Write all refs as a series of promises
26
- .then(()=> writer.write(ref))
27
- .then(()=> refIndex < refs.length && writer.middle && writer.middle(ref))
28
- , Promise.resolve()))
29
- .then(()=> writer.end())
30
- .then(()=> fileStream.close())
31
- .then(()=> streamFinished(fileStream))
32
- }
1
+ import {createWriteStream} from 'node:fs';
2
+ import {finished as streamFinished} from 'node:stream/promises';
3
+ import {identifyFormat} from './identifyFormat.js';
4
+ import {writeStream} from './writeStream.js';
5
+
6
+ /**
7
+ * Write a file to disk via the appropriate module
8
+ *
9
+ * @param {string} path The file path to write, the module to use will be determined from this
10
+ * @param {Array<ReflibRef>} refs Collection of references to write
11
+ * @param {Object} [options] Additional options to pass to the file writer
12
+ * @param {string} [options.module] The module to use if overriding from the file path
13
+ *
14
+ * @returns {Promise} A promise which resolves when the operation has completed
15
+ */
16
+ export function writeFile(path, refs, options) {
17
+ let format = options?.module || identifyFormat(path)?.id;
18
+ if (!format) throw new Error(`Unable to identify reference library format when saving file "${path}"`);
19
+
20
+ let fileStream = createWriteStream(path);
21
+ let writer = writeStream(format, fileStream, options);
22
+
23
+ return Promise.resolve()
24
+ .then(()=> writer.start())
25
+ .then(()=> refs.reduce((chain, ref, refIndex, refs) => chain // Write all refs as a series of promises
26
+ .then(()=> writer.write(ref))
27
+ .then(()=> refIndex < refs.length && writer.middle && writer.middle(ref))
28
+ , Promise.resolve()))
29
+ .then(()=> writer.end())
30
+ .then(()=> fileStream.close())
31
+ .then(()=> streamFinished(fileStream))
32
+ }
@@ -1,16 +1,16 @@
1
- import {getModule} from './getModule.js';
2
-
3
- /**
4
- * Write an output stream via a given format ID
5
- * This function is really just a multiplexor around each modules `writeStream` export
6
- * @param {string} module The module ID as per `lib/formats.js`
7
- * @param {Stream.Writable} stream Output stream to write to
8
- * @param {Object} [options] Additional options to pass to the stream writer
9
- * @returns {Object} An object composed of `{start, write, end}` functions
10
- */
11
- export function writeStream(module, stream, options) {
12
- if (!module) throw new Error('No module provided to parse with');
13
- if (!stream) throw new Error('No stream provided to parse');
14
-
15
- return getModule(module).writeStream(stream, options);
16
- }
1
+ import {getModule} from './getModule.js';
2
+
3
+ /**
4
+ * Write an output stream via a given format ID
5
+ * This function is really just a multiplexor around each modules `writeStream` export
6
+ * @param {string} module The module ID as per `lib/formats.js`
7
+ * @param {Stream.Writable} stream Output stream to write to
8
+ * @param {Object} [options] Additional options to pass to the stream writer
9
+ * @returns {Object} An object composed of `{start, write, end}` functions
10
+ */
11
+ export function writeStream(module, stream, options) {
12
+ if (!module) throw new Error('No module provided to parse with');
13
+ if (!stream) throw new Error('No stream provided to parse');
14
+
15
+ return getModule(module).writeStream(stream, options);
16
+ }