@iebh/reflib 2.8.0 → 2.8.2

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/app.js CHANGED
@@ -1,85 +1,87 @@
1
- #!/usr/bin/node
2
-
3
- import parseArgs from './shared/parseArgs.js';
4
- import packageInfo from './package.json' with {type: 'json'};
5
- import reflib from './lib/default.js';
6
-
7
- let help = `
8
-
9
- Usage: reflib -i INPUT_FILE [-f FORMAT] [-o -|OUTPUT_FILE]
10
-
11
-
12
- -i, --input <file> Input file to process
13
-
14
- -o, --output <file> Output file to save. Use '-' for STDOUT
15
-
16
- -f, --format <reflib-format> Override or set the file output type (if omitted the outfile filename
17
- is used to determine the format)
18
-
19
- -v, --verbose Be verbose when processing
20
-
21
- --version Print CLI version and exit
22
-
23
- -h, --help This help screen
24
- `;
25
-
26
- // Parse args {{{
27
- let args = parseArgs.parse();
28
- args = parseArgs.expand(args, {
29
- 'h': 'help',
30
- 'i': 'input',
31
- 'o': 'output',
32
- 'f': 'format',
33
- 'v': 'verbose',
34
- });
35
- // }}}
36
-
37
- // Action: Version {{{
38
- if (args.version) {
39
- console.log(`Version: ${packageInfo.version}`);
40
- process.exit(0);
41
- }
42
- // }}}
43
- // Action: Convert (if --input) {{{
44
- if (args.input) {
45
- if (args.verbose) console.log('Reading', args.input);
46
- let refs = await reflib.readFile(args.input);
47
- if (args.verbose) console.log('Read', refs.length, 'refs');
48
-
49
- if (!args.output) {
50
- if (args.verbose) console.log('No output file specified - assuming STDOUT JSON');
51
- console.log(JSON.stringify(refs, null, 2));
52
- } else if (args.output == '-' || args.output === true) {
53
- if (!args.format) {
54
- if (args.verbose) console.log('No STDOUT format specified - assuming JSON');
55
- console.log(JSON.stringify(refs, null, 2));
56
- } else {
57
- if (args.verbose) console.log(`Raw output to STDOUT using "${args.format}" format`);
58
-
59
- let stream = reflib.writeStream(args.format, process.stdout);
60
- await stream.start();
61
- await Array.fromAsync(refs, ref =>
62
- stream.write(ref)
63
- );
64
- await stream.end();
65
- }
66
- } else if (args.output) {
67
- if (args.verbose) console.log('Writing', args.output);
68
- await reflib.writeFile(args.output, refs);
69
- }
70
-
71
- if (args.verbose) console.log('All done');
72
- process.exit(0);
73
- }
74
- // }}}
75
- // Action: Help {{{
76
- if (args.help) {
77
- console.log(help);
78
- process.exit(0);
79
- }
80
- // }}}
81
- // Action: Unknown {{{
82
- console.warn('Nothing to do');
83
- console.log(help);
84
- process.exit(1);
85
- // }}}
1
+ #!/usr/bin/node
2
+
3
+ import parseArgs from './shared/parseArgs.js';
4
+ import packageInfo from './package.json' with {type: 'json'};
5
+ import reflib from './lib/default.js';
6
+
7
+ let help = `
8
+
9
+ Usage: reflib -i INPUT_FILE [-f FORMAT] [-o -|OUTPUT_FILE]
10
+
11
+
12
+ -i, --input <file> Input file to process
13
+
14
+ -o, --output <file> Output file to save. Use '-' for STDOUT
15
+
16
+ -f, --format <reflib-format> Override or set the file output type (if omitted the outfile filename
17
+ is used to determine the format)
18
+
19
+ -v, --verbose Be verbose when processing
20
+
21
+ --version Print CLI version and exit
22
+
23
+ -h, --help This help screen
24
+ `;
25
+
26
+ // Parse args {{{
27
+ let args = parseArgs.parse();
28
+ args = parseArgs.expand(args, {
29
+ 'h': 'help',
30
+ 'i': 'input',
31
+ 'o': 'output',
32
+ 'f': 'format',
33
+ 'v': 'verbose',
34
+ });
35
+ args.format ||= 'json';
36
+ // }}}
37
+
38
+ // Action: Version {{{
39
+ if (args.version) {
40
+ console.log(`Version: ${packageInfo.version}`);
41
+ process.exit(0);
42
+ }
43
+ // }}}
44
+ // Action: Convert (if --input) {{{
45
+ if (args.input) {
46
+ if (args.verbose) console.log('Reading', args.input);
47
+ let refs = await reflib.readFile(args.input);
48
+ if (args.verbose) console.log('Read', refs.length, 'refs');
49
+
50
+ if (
51
+ !args.output // No output file specified
52
+ || args.output == '-' // OR use STDOUT
53
+ || args.output === true // OR output is just specified as a flag with no rider
54
+ ) {
55
+ if (args.verbose) console.log(`Raw output to STDOUT using "${args.format}" format`);
56
+
57
+ let stream = reflib.writeStream(
58
+ args.format,
59
+ process.stdout,
60
+ args.format == 'json' ? {indent:'\t'} : {},
61
+ );
62
+ await stream.start();
63
+ await Array.fromAsync(refs, (ref, refIndex) => Promise.resolve()
64
+ .then(()=> stream.write(ref))
65
+ .then(()=> refIndex < refs.length && stream.middle && stream.middle(ref))
66
+ );
67
+ await stream.end();
68
+ } else if (args.output) {
69
+ if (args.verbose) console.log('Writing', args.output);
70
+ await reflib.writeFile(args.output, refs);
71
+ }
72
+
73
+ if (args.verbose) console.log('All done');
74
+ process.exit(0);
75
+ }
76
+ // }}}
77
+ // Action: Help {{{
78
+ if (args.help) {
79
+ console.log(help);
80
+ process.exit(0);
81
+ }
82
+ // }}}
83
+ // Action: Unknown {{{
84
+ console.warn('Nothing to do');
85
+ console.log(help);
86
+ process.exit(1);
87
+ // }}}
package/lib/browser.js CHANGED
@@ -1,30 +1,30 @@
1
- import {downloadFile} from './downloadFile.js';
2
- import {formats} from './formats.js';
3
- import {getModule} from './getModule.js';
4
- import {identifyFormat} from './identifyFormat.js';
5
- import {readStream} from './readStream.js';
6
- import {uploadFile} from './uploadFile.js';
7
- import {writeStream} from './writeStream.js';
8
- import { getRefDoi } from './getRefDoi.js';
9
-
10
- export {
11
- downloadFile,
12
- formats,
13
- getModule,
14
- identifyFormat,
15
- readStream,
16
- uploadFile,
17
- writeStream,
18
- getRefDoi
19
- };
20
-
21
- export default {
22
- downloadFile,
23
- formats,
24
- getModule,
25
- identifyFormat,
26
- readStream,
27
- uploadFile,
28
- writeStream,
29
- getRefDoi
30
- };
1
+ import {downloadFile} from './downloadFile.js';
2
+ import {formats} from './formats.js';
3
+ import {getModule} from './getModule.js';
4
+ import {identifyFormat} from './identifyFormat.js';
5
+ import {readStream} from './readStream.js';
6
+ import {uploadFile} from './uploadFile.js';
7
+ import {writeStream} from './writeStream.js';
8
+ import { getRefDoi } from './getRefDoi.js';
9
+
10
+ export {
11
+ downloadFile,
12
+ formats,
13
+ getModule,
14
+ identifyFormat,
15
+ readStream,
16
+ uploadFile,
17
+ writeStream,
18
+ getRefDoi
19
+ };
20
+
21
+ export default {
22
+ downloadFile,
23
+ formats,
24
+ getModule,
25
+ identifyFormat,
26
+ readStream,
27
+ uploadFile,
28
+ writeStream,
29
+ getRefDoi
30
+ };
package/lib/default.js CHANGED
@@ -1,30 +1,30 @@
1
- import {identifyFormat} from './identifyFormat.js';
2
- import {formats} from './formats.js';
3
- import {getModule} from './getModule.js';
4
- import {readStream} from './readStream.js';
5
- import {readFile} from './readFile.js';
6
- import {writeStream} from './writeStream.js';
7
- import {writeFile} from './writeFile.js';
8
- import { getRefDoi } from './getRefDoi.js';
9
-
10
- export {
11
- formats,
12
- getModule,
13
- identifyFormat,
14
- readFile,
15
- readStream,
16
- writeFile,
17
- writeStream,
18
- getRefDoi
19
- };
20
-
21
- export default {
22
- formats,
23
- getModule,
24
- identifyFormat,
25
- readFile,
26
- readStream,
27
- writeFile,
28
- writeStream,
29
- getRefDoi
30
- };
1
+ import {identifyFormat} from './identifyFormat.js';
2
+ import {formats} from './formats.js';
3
+ import {getModule} from './getModule.js';
4
+ import {readStream} from './readStream.js';
5
+ import {readFile} from './readFile.js';
6
+ import {writeStream} from './writeStream.js';
7
+ import {writeFile} from './writeFile.js';
8
+ import { getRefDoi } from './getRefDoi.js';
9
+
10
+ export {
11
+ formats,
12
+ getModule,
13
+ identifyFormat,
14
+ readFile,
15
+ readStream,
16
+ writeFile,
17
+ writeStream,
18
+ getRefDoi
19
+ };
20
+
21
+ export default {
22
+ formats,
23
+ getModule,
24
+ identifyFormat,
25
+ readFile,
26
+ readStream,
27
+ writeFile,
28
+ writeStream,
29
+ getRefDoi
30
+ };
@@ -1,94 +1,94 @@
1
- import {identifyFormat} from './identifyFormat.js';
2
- import {writeStream} from './writeStream.js';
3
-
4
- /**
5
- * Prompt the user for a file to save as and Write a given array of citations
6
- *
7
- * @param {Array<ReflibRef>} refs Collection of references to write
8
- *
9
- * @param {Object} [options] Additional options when prompting the user
10
- * @param {String} [options.filename='References.xml'] The filename to download
11
- * @param {Object} [options.writeStreamOptions] Additional options for the subsequent `writeStream` internal call
12
- * @param {Object} [options.promptDownload=true] Prompt the user to save the file, if falsy this returns the Blob file content instead of asking the user where to save it
13
- *
14
- * @returns {Promise|Promise<Blob>} A promise which will resolve when the file download has completed or (if `!promptDownload`) with the blob contents
15
- */
16
- export function downloadFile(refs, options) {
17
- let settings = {
18
- filename: 'References.xml',
19
- writeStreamOptions: {},
20
- promptDownload: true,
21
- ...options,
22
- };
23
-
24
- return Promise.resolve()
25
- // Identify module from filename {{{
26
- .then(()=> {
27
- let module = identifyFormat(settings.filename);
28
- if (!module) throw new Error(`Unsupported Reflib filename "${settings.filename}"`);
29
- return module;
30
- })
31
- // }}}
32
- // Create stream {{{
33
- .then(async (module) => {
34
- let blobData = [];
35
- let writableStream = new WritableStream({
36
- write(chunk) {
37
- blobData.push(chunk);
38
- },
39
- });
40
-
41
- let writer = writableStream.getWriter();
42
-
43
- // Map end->close to keep browser compatibility with Node
44
- writer.end = async (cb) => {
45
- await writer.close();
46
- cb();
47
- };
48
-
49
- let streamer = await writeStream(module.id, writer, settings.writeStreamOptions);
50
-
51
- return {blobData, streamer};
52
- })
53
- // }}}
54
- // Flush all references into the stream {{{
55
- .then(async ({blobData, streamer}) => {
56
- // Start stream
57
- await streamer.start();
58
-
59
- // Write all references as a promise chain
60
- await refs.reduce((promiseChain, ref) =>
61
- promiseChain.then(()=>
62
- streamer.write(ref)
63
- )
64
- , Promise.resolve());
65
-
66
- // End stream
67
- await streamer.end();
68
-
69
- return blobData;
70
- })
71
- // }}}
72
- // Convert blobData array to a Blob {{{
73
- .then(blobData => {
74
- return new Blob(blobData);
75
- })
76
- // }}}
77
- // Download Blob
78
- .then(blob => {
79
- if (!settings.promptDownload) return blob;
80
-
81
- let url = URL.createObjectURL(blob);
82
- let aEl = document.createElement('a');
83
- aEl.href = url;
84
- aEl.download = settings.filename;
85
- document.body.appendChild(aEl);
86
- aEl.click();
87
-
88
- // Clean up DOM
89
- document.body.removeChild(aEl);
90
- URL.revokeObjectURL(url);
91
- // }}}
92
- })
93
- // }}}
94
- }
1
+ import {identifyFormat} from './identifyFormat.js';
2
+ import {writeStream} from './writeStream.js';
3
+
4
+ /**
5
+ * Prompt the user for a file to save as and Write a given array of citations
6
+ *
7
+ * @param {Array<ReflibRef>} refs Collection of references to write
8
+ *
9
+ * @param {Object} [options] Additional options when prompting the user
10
+ * @param {String} [options.filename='References.xml'] The filename to download
11
+ * @param {Object} [options.writeStreamOptions] Additional options for the subsequent `writeStream` internal call
12
+ * @param {Object} [options.promptDownload=true] Prompt the user to save the file, if falsy this returns the Blob file content instead of asking the user where to save it
13
+ *
14
+ * @returns {Promise|Promise<Blob>} A promise which will resolve when the file download has completed or (if `!promptDownload`) with the blob contents
15
+ */
16
+ export function downloadFile(refs, options) {
17
+ let settings = {
18
+ filename: 'References.xml',
19
+ writeStreamOptions: {},
20
+ promptDownload: true,
21
+ ...options,
22
+ };
23
+
24
+ return Promise.resolve()
25
+ // Identify module from filename {{{
26
+ .then(()=> {
27
+ let module = identifyFormat(settings.filename);
28
+ if (!module) throw new Error(`Unsupported Reflib filename "${settings.filename}"`);
29
+ return module;
30
+ })
31
+ // }}}
32
+ // Create stream {{{
33
+ .then(async (module) => {
34
+ let blobData = [];
35
+ let writableStream = new WritableStream({
36
+ write(chunk) {
37
+ blobData.push(chunk);
38
+ },
39
+ });
40
+
41
+ let writer = writableStream.getWriter();
42
+
43
+ // Map end->close to keep browser compatibility with Node
44
+ writer.end = async (cb) => {
45
+ await writer.close();
46
+ cb();
47
+ };
48
+
49
+ let streamer = await writeStream(module.id, writer, settings.writeStreamOptions);
50
+
51
+ return {blobData, streamer};
52
+ })
53
+ // }}}
54
+ // Flush all references into the stream {{{
55
+ .then(async ({blobData, streamer}) => {
56
+ // Start stream
57
+ await streamer.start();
58
+
59
+ // Write all references as a promise chain
60
+ await refs.reduce((promiseChain, ref, refIndex, refs) =>
61
+ promiseChain
62
+ .then(()=> streamer.write(ref))
63
+ .then(()=> refIndex < refs.length && streamer.middle && streamer.middle(ref))
64
+ , Promise.resolve());
65
+
66
+ // End stream
67
+ await streamer.end();
68
+
69
+ return blobData;
70
+ })
71
+ // }}}
72
+ // Convert blobData array to a Blob {{{
73
+ .then(blobData => {
74
+ return new Blob(blobData);
75
+ })
76
+ // }}}
77
+ // Download Blob
78
+ .then(blob => {
79
+ if (!settings.promptDownload) return blob;
80
+
81
+ let url = URL.createObjectURL(blob);
82
+ let aEl = document.createElement('a');
83
+ aEl.href = url;
84
+ aEl.download = settings.filename;
85
+ document.body.appendChild(aEl);
86
+ aEl.click();
87
+
88
+ // Clean up DOM
89
+ document.body.removeChild(aEl);
90
+ URL.revokeObjectURL(url);
91
+ // }}}
92
+ })
93
+ // }}}
94
+ }