ua-parser-js 2.0.3 → 2.0.6

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/script/cli.js CHANGED
@@ -1,4 +1,94 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const UAParser = require('ua-parser-js');
4
- console.log(JSON.stringify(process.argv.slice(2).map(ua => UAParser(ua)), null, 4));
3
+ try {
4
+ const fs = require('node:fs');
5
+ const path = require('node:path');
6
+ const { performance } = require('node:perf_hooks');
7
+ const readline = require('node:readline');
8
+ const { parseArgs } = require('node:util');
9
+ const UAParser = require('../src/main/ua-parser');
10
+ const { Bots, Emails, ExtraDevices, InApps, Vehicles } = require('../src/extensions/ua-parser-extensions');
11
+
12
+ if (!process.argv[2].startsWith('-')) {
13
+
14
+ const results = process.argv.slice(2).map(ua => UAParser(ua));
15
+ console.log(JSON.stringify(results, null, 4));
16
+ process.exit(0);
17
+
18
+ } else if (['-h', '--help'].includes(process.argv[2])) {
19
+
20
+ console.log('Usage: npx ua-parser-js <string>');
21
+ console.log(' or npx ua-parser-js --input-file <filepath> [--output-file <filepath>]');
22
+ console.log('-i, --input-file');
23
+ console.log('-o, --output-file');
24
+ process.exit(0);
25
+
26
+ } else {
27
+
28
+ const startPerf = performance.now();
29
+ const {
30
+ values: {
31
+ 'input-file': inputFile,
32
+ 'output-file': outputFile
33
+ },
34
+ } = parseArgs({
35
+ options: {
36
+ 'input-file': { type: 'string', short: 'i' },
37
+ 'output-file': { type: 'string', short: 'o' }
38
+ }
39
+ });
40
+
41
+ if (!inputFile) {
42
+ console.error('Input file must be present');
43
+ process.exit(1);
44
+ }
45
+
46
+ const inputPath = path.resolve(__dirname, inputFile);
47
+ const outputPath = outputFile ? path.resolve(__dirname, outputFile) : null;
48
+
49
+ if (!fs.existsSync(inputPath)) {
50
+ console.error(`Input file not found: ${inputPath}`);
51
+ process.exit(1);
52
+ }
53
+
54
+ const inputStream = fs.createReadStream(inputPath, 'utf8');
55
+ const rl = readline.createInterface({
56
+ input: inputStream,
57
+ crlfDelay: Infinity
58
+ });
59
+
60
+ const outputStream = outputPath ? fs.createWriteStream(outputPath, { encoding : 'utf8' }) : process.stdout;
61
+
62
+ const uap = new UAParser([Bots, Emails, ExtraDevices, InApps, Vehicles]);
63
+ let lineNumber = 0;
64
+
65
+ outputStream.write('[\n');
66
+
67
+ rl.on('line', line => {
68
+ const result = uap.setUA(line).getResult();
69
+ const json = JSON.stringify(result, null, 4);
70
+ if (lineNumber > 0) outputStream.write(',\n');
71
+ outputStream.write(json);
72
+ lineNumber++;
73
+ });
74
+
75
+ rl.on('close', () => {
76
+ outputStream.write('\n]');
77
+ if (outputPath) {
78
+ outputStream.end(() => {
79
+ const finishPerf = performance.now();
80
+ console.log(`Done!`);
81
+ console.log(`Number of lines found: ${lineNumber}`);
82
+ console.log(`Task finished in: ${(finishPerf - startPerf).toFixed(3)}ms`);
83
+ console.log(`Output written to: ${outputPath}`);
84
+ process.exit(0);
85
+ });
86
+ } else {
87
+ process.exit(0);
88
+ }
89
+ });
90
+ }
91
+ } catch (err) {
92
+ console.error(err);
93
+ process.exit(1);
94
+ }