cannibalizr 2.0.0 → 3.0.0

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/LICENSE.md CHANGED
@@ -1,5 +1,5 @@
1
1
  The MIT License (MIT)
2
- Copyright (c) 2016 - 2022 Alex Grant (@localnerve), LocalNerve, LLC
2
+ Copyright (c) 2016 - 2023 Alex Grant (@localnerve), LocalNerve, LLC
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
5
 
package/dist/index.cjs CHANGED
@@ -8,7 +8,7 @@ exports.default = void 0;
8
8
  var _nodeFs = _interopRequireDefault(require("node:fs"));
9
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
10
  /***
11
- * Copyright (c) 2016 - 2022 Alex Grant (@localnerve), LocalNerve LLC
11
+ * Copyright (c) 2016 - 2023 Alex Grant (@localnerve), LocalNerve LLC
12
12
  * Copyrights licensed under the MIT License. See the accompanying LICENSE file
13
13
  * for terms.
14
14
  */
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /***
2
- * Copyright (c) 2016 - 2022 Alex Grant (@localnerve), LocalNerve LLC
2
+ * Copyright (c) 2016 - 2023 Alex Grant (@localnerve), LocalNerve LLC
3
3
  * Copyrights licensed under the MIT License. See the accompanying LICENSE file
4
4
  * for terms.
5
5
  */
package/lib/index.js ADDED
@@ -0,0 +1,108 @@
1
+ /***
2
+ * Copyright (c) 2016 - 2023 Alex Grant (@localnerve), LocalNerve LLC
3
+ * Copyrights licensed under the MIT License. See the accompanying LICENSE file
4
+ * for terms.
5
+ */
6
+ import fs from 'node:fs';
7
+ export default cannibalizr;
8
+ /**
9
+ * Pull strings from multiple input files, write to structured json.
10
+ *
11
+ * json output file structure:
12
+ * {
13
+ * manifest: {
14
+ * // any output manifest data supplied in options
15
+ * },
16
+ * anyname: [
17
+ * 'some captured string',
18
+ * 'another captured string'
19
+ * ]
20
+ * }
21
+ *
22
+ * @param {Object} options - Cannibalizr options.
23
+ * @param {Object} options.input - input options.
24
+ * @param {Object} options.input.anyname - At least one input is required,
25
+ * use a name that makes sense for what is being captured.
26
+ * @param {String} options.input.anyname.file - The path of the input file.
27
+ * @param {Array} options.input.anyname.captures - The captures to run against
28
+ * the input file.
29
+ * @param {Boolean} options.input.anyname.captures.n.global - true if the regex
30
+ * is global, false otherwise.
31
+ * @param {Number} options.input.anyname.captures.n.matchIndex - The match index
32
+ * to use to capture data for the regex.
33
+ * @param {Regex} options.input.anyname.captures.n.re - The capturing regex to
34
+ * use to get data.
35
+ * @param {Object} options.output - output options.
36
+ * @param {String} options.output.file - The path of the json output file.
37
+ * @param {Object} [options.output.manifest] - An arbitrary manifest to write
38
+ * to the json output file.
39
+ * @param {Function} [options.logger] - A logger to use.
40
+ * @returns {Undefined} Nothing.
41
+ */
42
+ export function cannibalizr (options) {
43
+ const output = {};
44
+
45
+ // used to trim "|' from the beginning and end of a captured result.
46
+ const reClean = /^(?:\s+|"|')|(?:\s+|"|')$/g;
47
+
48
+ // basic input check
49
+ const inputOk = options.input && Object.keys(options.input).length &&
50
+ options.output && options.output.file;
51
+
52
+ if (!inputOk) {
53
+ throw new Error(
54
+ 'Cannibalizr received invalid options. Please review readme'
55
+ );
56
+ }
57
+
58
+ if (options.output.manifest) {
59
+ output.manifest = options.output.manifest;
60
+ }
61
+
62
+ /**
63
+ * Report output results
64
+ *
65
+ * @param {Object} items - An array of output values.
66
+ * @param {String} name - The name assocaited with the output.
67
+ * @returns {Undefined} Nothing.
68
+ */
69
+ const report = function (items, name) {
70
+ if (options.logger) {
71
+ if (items && items.length) {
72
+ items.forEach(str => {
73
+ options.logger(`"${name}": ${str}`);
74
+ });
75
+ } else {
76
+ options.logger(`nothing found for "${name}"`);
77
+ }
78
+ }
79
+ };
80
+
81
+ Object.keys(options.input).forEach(item => {
82
+ options.input[item].forEach(input => {
83
+ const contents = fs.readFileSync(input.file, { encoding: 'utf8' });
84
+
85
+ input.captures.forEach(capSpec => {
86
+ let m;
87
+
88
+ output[item] || (output[item] = []);
89
+
90
+ if (capSpec.global) {
91
+ while ((m = capSpec.re.exec(contents)) !== null) {
92
+ output[item].push(m[capSpec.matchIndex].replace(reClean, ''));
93
+ }
94
+ } else {
95
+ m = capSpec.re.exec(contents);
96
+ if (m) {
97
+ output[item].push(m[capSpec.matchIndex].replace(reClean, ''));
98
+ }
99
+ }
100
+ });
101
+ });
102
+ report(output[item], item);
103
+ });
104
+
105
+ fs.writeFileSync(options.output.file, JSON.stringify(
106
+ output
107
+ ));
108
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cannibalizr",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "Turns an old pile of rusty files into a new json data source",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -36,19 +36,18 @@
36
36
  },
37
37
  "homepage": "https://github.com/localnerve/cannibalizr#readme",
38
38
  "engines": {
39
- "node": ">= 14"
39
+ "node": ">= 16"
40
40
  },
41
41
  "devDependencies": {
42
- "@babel/cli": "^7.19.3",
43
- "@babel/core": "^7.20.2",
42
+ "@babel/cli": "^7.20.7",
43
+ "@babel/core": "^7.20.12",
44
44
  "@babel/eslint-parser": "^7.19.1",
45
45
  "@babel/preset-env": "^7.20.2",
46
46
  "c8": "^7.12.0",
47
47
  "chai": "^4.3.7",
48
- "eslint": "^8.28.0",
49
- "mocha": "^10.1.0",
50
- "precommit-hook": "^3.0.0",
51
- "rimraf": "^3.0.2"
48
+ "eslint": "^8.33.0",
49
+ "mocha": "^10.2.0",
50
+ "precommit-hook": "^3.0.0"
52
51
  },
53
52
  "pre-commit": [
54
53
  "lint",