cannibalizr 0.4.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/.eslintignore ADDED
@@ -0,0 +1,4 @@
1
+ /reports
2
+ /node_modules
3
+ /tmp
4
+ /package
package/.eslintrc.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "env": {
3
+ "node": true
4
+ },
5
+ "parser": "@babel/eslint-parser",
6
+ "extends": "eslint:recommended",
7
+ "rules": {
8
+ "indent": [2, 2, {"SwitchCase": 1}],
9
+ "quotes": [2, "single"],
10
+ "dot-notation": [2, {"allowKeywords": true}]
11
+ }
12
+ }
@@ -0,0 +1,32 @@
1
+ name: Verify
2
+ on:
3
+ push:
4
+ branches: [ master ]
5
+ pull_request:
6
+ branches: [ master ]
7
+
8
+ jobs:
9
+ verify:
10
+
11
+ runs-on: ubuntu-latest
12
+
13
+ strategy:
14
+ matrix:
15
+ node-version: [10.x, 12.x, 14.x]
16
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
17
+
18
+ steps:
19
+ - uses: actions/checkout@v2
20
+ - name: Use Node.js ${{ matrix.node-version }}
21
+ uses: actions/setup-node@v2.1.4
22
+ with:
23
+ node-version: ${{ matrix.node-version }}
24
+ - run: npm ci
25
+ - name: Lint, Test, And Coverage
26
+ run: npm run lint && npm run cover
27
+ - name: Coverage Upload
28
+ if: ${{ success() }}
29
+ uses: coverallsapp/github-action@master
30
+ with:
31
+ github-token: ${{ secrets.GITHUB_TOKEN }}
32
+ path-to-lcov: ./reports/coverage/lcov.info
package/LICENSE.md ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2016 - 2021 Alex Grant (@localnerve), LocalNerve, LLC
3
+
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
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Cannibalizr
2
+
3
+ [![npm version](https://badge.fury.io/js/cannibalizr.svg)](http://badge.fury.io/js/cannibalizr)
4
+ ![Verify](https://github.com/localnerve/cannibalizr/workflows/Verify/badge.svg)
5
+ [![Coverage Status](https://coveralls.io/repos/localnerve/cannibalizr/badge.svg?branch=master)](https://coveralls.io/r/localnerve/cannibalizr?branch=master)
6
+ [![Dependency Status](https://david-dm.org/localnerve/cannibalizr.svg)](https://david-dm.org/localnerve/cannibalizr)
7
+ [![devDependency Status](https://david-dm.org/localnerve/cannibalizr/dev-status.svg)](https://david-dm.org/localnerve/cannibalizr#info=devDependencies)
8
+
9
+ > A tool to pull strings from multiple files into a json file using regexes.
10
+
11
+ ## What?
12
+ > "Turns an old pile of rusty files into a shiny, new json data source!"
13
+
14
+ This tool pulls select strings from multiple files and writes them to a
15
+ structured json file.
16
+ So, it allows you to use those files as input to something else, making them the
17
+ single source of truth.
18
+
19
+ *This is not the tool you are looking for.*
20
+
21
+ **Use this as a last resort.**
22
+
23
+ By nature, this is a brittle solution as the input files will (presumably) change
24
+ over time, and inevitably evade your capturing regexes at some point.
25
+ The usefulness of this tool depends on your regexes and the nature of the input
26
+ they capture against (and other viable options available to you in your timeframe).
27
+
28
+ Ideally, you should never have to do this, but sometimes, things are not ideal.
29
+ However, if there are strings in your code that are NOT easy available in any
30
+ other data form (**I'm looking at you, asset references in CSS**), this could be
31
+ a useful way to keep DRY without overcomplicating things.
32
+
33
+ ## Example using all options
34
+
35
+ ```javascript
36
+ import cannibalizr from 'cannibalizr';
37
+
38
+ cannibalizr({
39
+ output: {
40
+ // manifest is an optional object with arbitrary data to store in the output
41
+ manifest: {
42
+ id: 123,
43
+ version: '0.1.0',
44
+ whatever: 'data I want here'
45
+ }
46
+ file: 'data.json' // the json output file path, always overwritten.
47
+ },
48
+ input: {
49
+ someIdentifier: [{
50
+ file: 'somefile.css',
51
+ captures: [{
52
+ global: true, // true if the regex is global (/g)
53
+ matchIndex: 1, // the match index to pull data from
54
+ re: /url\(([^\)]+)\)/ig // the actual regex
55
+ }]
56
+ }],
57
+ anotherIdentifier: [{
58
+ file: 'somefile.js',
59
+ captures: [{
60
+ global: false,
61
+ matchIndex: 1,
62
+ re: /xhrPath\s*\:\s*(?:'|")([^'"]+)/
63
+ }]
64
+ }]
65
+ },
66
+ // A logging function. If omitted, nothing is logged.
67
+ logger: console.log
68
+ });
69
+
70
+ // data.json:
71
+ {
72
+ "manifest": {
73
+ "id": 123,
74
+ "version": "0.1.0",
75
+ "whatever": "data I want here"
76
+ },
77
+ "someIdentifier": [
78
+ "//fonts.gstatic.com/s/sourcesanspro/v9/guid.woff2",
79
+ "//fonts.gstatic.com/s/sourcesanspro/v9/guid.woff",
80
+ "//fonts.gstatic.com/s/sourcesanspro/v9/guid.ttf"
81
+ ]
82
+ "anotherIdentifier": [
83
+ "/api"
84
+ ]
85
+ }
86
+ ```
@@ -0,0 +1,12 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@babel/preset-env",
5
+ {
6
+ "targets": {
7
+ "node": "12"
8
+ }
9
+ }
10
+ ]
11
+ ]
12
+ }
package/index.js ADDED
@@ -0,0 +1,108 @@
1
+ /***
2
+ * Copyright (c) 2016 - 2021 Alex Grant (@localnerve), LocalNerve LLC
3
+ * Copyrights licensed under the MIT License. See the accompanying LICENSE file
4
+ * for terms.
5
+ */
6
+ const fs = require('fs');
7
+
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
+ module.exports = 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 ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "cannibalizr",
3
+ "version": "0.4.0",
4
+ "description": "Turns an old pile of rusty files into a new json data source",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "mocha ./tests/unit --recursive --reporter spec",
8
+ "cover": "istanbul cover --dir ${COVERAGE_DIR:-reports/coverage} -- _mocha tests/unit/* --recursive --reporter spec",
9
+ "lint": "eslint .",
10
+ "validate": "npm ls"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/localnerve/cannibalizr.git"
15
+ },
16
+ "keywords": [
17
+ "data",
18
+ "capture",
19
+ "regex",
20
+ "strings",
21
+ "multiple",
22
+ "files",
23
+ "json"
24
+ ],
25
+ "author": "Alex Grant (@localnerve)",
26
+ "license": "MIT",
27
+ "bugs": {
28
+ "url": "https://github.com/localnerve/cannibalizr/issues"
29
+ },
30
+ "homepage": "https://github.com/localnerve/cannibalizr#readme",
31
+ "engines": {
32
+ "node": ">= 12"
33
+ },
34
+ "dependencies": {},
35
+ "devDependencies": {
36
+ "@babel/core": "^7.15.5",
37
+ "@babel/preset-env": "^7.15.6",
38
+ "@babel/eslint-parser": "^7.15.4",
39
+ "chai": "^4.3.4",
40
+ "coveralls": "^3.1.1",
41
+ "eslint": "^7.32.0",
42
+ "istanbul": "^0.4.5",
43
+ "mkdirp": "^1.0.4",
44
+ "mocha": "^9.1.1",
45
+ "precommit-hook": "^3.0.0"
46
+ },
47
+ "pre-commit": [
48
+ "lint",
49
+ "test"
50
+ ]
51
+ }
@@ -0,0 +1,21 @@
1
+ //
2
+ // Copyright (c) 2016 - 2021 Alex Grant (@localnerve), LocalNerve LLC
3
+ // Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
4
+ //
5
+ // font styles
6
+ // -------------------------
7
+
8
+ @font-face {
9
+ font-family: "Source Sans Pro";
10
+ src: local("Source Sans Pro"), local("SourceSansPro-Regular"),
11
+ url("//fonts.gstatic.com/s/sourcesanspro/v9/ODelI1aHBYDBqgeIAH2zlNV_2ngZ8dMf8fLgjYEouxg.woff2") format("woff2"),
12
+ url("//fonts.gstatic.com/s/sourcesanspro/v9/ODelI1aHBYDBqgeIAH2zlBM0YzuT7MdOe03otPbuUS0.woff") format("woff"),
13
+ url("//fonts.gstatic.com/s/sourcesanspro/v9/ODelI1aHBYDBqgeIAH2zlEY6Fu39Tt9XkmtSosaMoEA.ttf") format("truetype");
14
+ /* unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000; */
15
+ font-weight: 400;
16
+ font-style: normal;
17
+ }
18
+
19
+ .fonts-loaded body {
20
+ font-family: "Source Sans Pro", sans-serif;
21
+ }
@@ -0,0 +1,35 @@
1
+ /***
2
+ * Copyright (c) 2016 - 2021 Alex Grant (@localnerve), LocalNerve LLC
3
+ * Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
4
+ *
5
+ * Assemble the Fluxible app.
6
+ */
7
+ 'use strict';
8
+
9
+ var debug = require('debug')('Example:App');
10
+ var FluxibleApp = require('fluxible');
11
+ var fetchrPlugin = require('fluxible-plugin-fetchr');
12
+ var ApplicationStore = require('./stores/ApplicationStore');
13
+ var ContentStore = require('./stores/ContentStore');
14
+ var ContactStore = require('./stores/ContactStore');
15
+ var BackgroundStore = require('./stores/BackgroundStore');
16
+ var RouteStore = require('./stores/RouteStore');
17
+ var ModalStore = require('./stores/ModalStore');
18
+
19
+ debug('Creating FluxibleApp');
20
+ var app = new FluxibleApp({
21
+ component: require('./components/Application.jsx')
22
+ });
23
+
24
+ debug('Adding Plugins');
25
+ app.plug(fetchrPlugin({ xhrPath: '/_api' }));
26
+
27
+ debug('Registering Stores');
28
+ app.registerStore(ApplicationStore);
29
+ app.registerStore(ContentStore);
30
+ app.registerStore(ContactStore);
31
+ app.registerStore(BackgroundStore);
32
+ app.registerStore(RouteStore);
33
+ app.registerStore(ModalStore);
34
+
35
+ module.exports = app;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Copyright (c) 2016 - 2021 Alex Grant (@localnerve), LocalNerve LLC
3
+ * Copyrights licensed under the MIT License. See the accompanying LICENSE file
4
+ * for terms.
5
+ */
6
+ /*eslint no-console:0 */
7
+ const path = require('path');
8
+
9
+ module.exports = {
10
+ output: {
11
+ manifest: {
12
+ id: 123,
13
+ version: '0.1.0',
14
+ whatever: 'string I want'
15
+ },
16
+ file: path.join(__dirname, './tmp/test_out.json')
17
+ },
18
+ input: {
19
+ assets: [{
20
+ file: path.join(__dirname, './files/_fonts.scss'),
21
+ captures: [{
22
+ global: true,
23
+ matchIndex: 1,
24
+ re: /url\(([^)]+)\)/ig
25
+ }]
26
+ }],
27
+ apis: [{
28
+ file: path.join(__dirname, './files/app.js'),
29
+ captures: [{
30
+ global: false,
31
+ matchIndex: 1,
32
+ re: /xhrPath\s*:\s*(?:'|")([^'"]+)/
33
+ }]
34
+ }]
35
+ },
36
+ logger: console.log
37
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "manifest": {
3
+ "id": 123,
4
+ "version": "0.1.0",
5
+ "whatever": "string I want"
6
+ },
7
+ "assets": [
8
+ "//fonts.gstatic.com/s/sourcesanspro/v9/ODelI1aHBYDBqgeIAH2zlNV_2ngZ8dMf8fLgjYEouxg.woff2",
9
+ "//fonts.gstatic.com/s/sourcesanspro/v9/ODelI1aHBYDBqgeIAH2zlBM0YzuT7MdOe03otPbuUS0.woff",
10
+ "//fonts.gstatic.com/s/sourcesanspro/v9/ODelI1aHBYDBqgeIAH2zlEY6Fu39Tt9XkmtSosaMoEA.ttf"
11
+ ],
12
+ "apis": [
13
+ "/_api"
14
+ ]
15
+ }
@@ -0,0 +1,50 @@
1
+ /***
2
+ * Copyright (c) 2016 - 2021 Alex Grant (@localnerve), LocalNerve LLC
3
+ * Copyrights licensed under the MIT License. See the accompanying LICENSE file
4
+ * for terms.
5
+ */
6
+ /* global before, beforeEach, describe, it */
7
+
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+ const expect = require('chai').expect;
11
+ const mkdirp = require('mkdirp').sync;
12
+
13
+ describe('cannibalizr', () => {
14
+ const inputFixture = require('../fixtures/options');
15
+ const outputFixture = require('../fixtures/output.json');
16
+ const cannibalizr = require('../../index.js');
17
+
18
+ before('cannibalizr', () => {
19
+ const outputDir = path.dirname(inputFixture.output.file);
20
+ mkdirp(outputDir);
21
+ });
22
+
23
+ beforeEach(() => {
24
+ if (fs.existsSync(inputFixture.output.file)) {
25
+ fs.unlinkSync(inputFixture.output.file);
26
+ }
27
+ });
28
+
29
+ it('should produce the expected output', (done) => {
30
+ cannibalizr(inputFixture);
31
+
32
+ fs.readFile(inputFixture.output.file, {
33
+ encoding: 'utf8'
34
+ }, (err, data) => {
35
+ expect(data).to.eql(JSON.stringify(outputFixture));
36
+ done(err);
37
+ });
38
+ });
39
+
40
+ it('should not run if bad input', () => {
41
+ const badOptions = JSON.parse(JSON.stringify(inputFixture));
42
+ delete badOptions.output.file;
43
+
44
+ expect(function () {
45
+ cannibalizr(badOptions);
46
+ }).to.throw(Error);
47
+
48
+ expect(fs.existsSync(inputFixture.output.file)).to.be.false;
49
+ });
50
+ });