cannibalizr 1.0.3 → 2.0.1
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/dist/index.cjs +107 -0
- package/{index.js → dist/index.js} +4 -4
- package/lib/index.js +108 -0
- package/package.json +18 -11
- package/.c8rc.json +0 -5
- package/.eslintignore +0 -4
- package/.eslintrc.json +0 -12
- package/.github/workflows/verify.yml +0 -32
- package/babel.config.json +0 -12
- package/tests/fixtures/files/_fonts.scss +0 -21
- package/tests/fixtures/files/app.js +0 -35
- package/tests/fixtures/options.js +0 -37
- package/tests/fixtures/output.json +0 -15
- package/tests/unit/test.js +0 -50
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.cannibalizr = cannibalizr;
|
|
7
|
+
exports.default = void 0;
|
|
8
|
+
var _nodeFs = _interopRequireDefault(require("node:fs"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
/***
|
|
11
|
+
* Copyright (c) 2016 - 2022 Alex Grant (@localnerve), LocalNerve LLC
|
|
12
|
+
* Copyrights licensed under the MIT License. See the accompanying LICENSE file
|
|
13
|
+
* for terms.
|
|
14
|
+
*/
|
|
15
|
+
var _default = cannibalizr;
|
|
16
|
+
/**
|
|
17
|
+
* Pull strings from multiple input files, write to structured json.
|
|
18
|
+
*
|
|
19
|
+
* json output file structure:
|
|
20
|
+
* {
|
|
21
|
+
* manifest: {
|
|
22
|
+
* // any output manifest data supplied in options
|
|
23
|
+
* },
|
|
24
|
+
* anyname: [
|
|
25
|
+
* 'some captured string',
|
|
26
|
+
* 'another captured string'
|
|
27
|
+
* ]
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* @param {Object} options - Cannibalizr options.
|
|
31
|
+
* @param {Object} options.input - input options.
|
|
32
|
+
* @param {Object} options.input.anyname - At least one input is required,
|
|
33
|
+
* use a name that makes sense for what is being captured.
|
|
34
|
+
* @param {String} options.input.anyname.file - The path of the input file.
|
|
35
|
+
* @param {Array} options.input.anyname.captures - The captures to run against
|
|
36
|
+
* the input file.
|
|
37
|
+
* @param {Boolean} options.input.anyname.captures.n.global - true if the regex
|
|
38
|
+
* is global, false otherwise.
|
|
39
|
+
* @param {Number} options.input.anyname.captures.n.matchIndex - The match index
|
|
40
|
+
* to use to capture data for the regex.
|
|
41
|
+
* @param {Regex} options.input.anyname.captures.n.re - The capturing regex to
|
|
42
|
+
* use to get data.
|
|
43
|
+
* @param {Object} options.output - output options.
|
|
44
|
+
* @param {String} options.output.file - The path of the json output file.
|
|
45
|
+
* @param {Object} [options.output.manifest] - An arbitrary manifest to write
|
|
46
|
+
* to the json output file.
|
|
47
|
+
* @param {Function} [options.logger] - A logger to use.
|
|
48
|
+
* @returns {Undefined} Nothing.
|
|
49
|
+
*/
|
|
50
|
+
exports.default = _default;
|
|
51
|
+
function cannibalizr(options) {
|
|
52
|
+
const output = {};
|
|
53
|
+
|
|
54
|
+
// used to trim "|' from the beginning and end of a captured result.
|
|
55
|
+
const reClean = /^(?:\s+|"|')|(?:\s+|"|')$/g;
|
|
56
|
+
|
|
57
|
+
// basic input check
|
|
58
|
+
const inputOk = options.input && Object.keys(options.input).length && options.output && options.output.file;
|
|
59
|
+
if (!inputOk) {
|
|
60
|
+
throw new Error('Cannibalizr received invalid options. Please review readme');
|
|
61
|
+
}
|
|
62
|
+
if (options.output.manifest) {
|
|
63
|
+
output.manifest = options.output.manifest;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Report output results
|
|
68
|
+
*
|
|
69
|
+
* @param {Object} items - An array of output values.
|
|
70
|
+
* @param {String} name - The name assocaited with the output.
|
|
71
|
+
* @returns {Undefined} Nothing.
|
|
72
|
+
*/
|
|
73
|
+
const report = function (items, name) {
|
|
74
|
+
if (options.logger) {
|
|
75
|
+
if (items && items.length) {
|
|
76
|
+
items.forEach(str => {
|
|
77
|
+
options.logger(`"${name}": ${str}`);
|
|
78
|
+
});
|
|
79
|
+
} else {
|
|
80
|
+
options.logger(`nothing found for "${name}"`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
Object.keys(options.input).forEach(item => {
|
|
85
|
+
options.input[item].forEach(input => {
|
|
86
|
+
const contents = _nodeFs.default.readFileSync(input.file, {
|
|
87
|
+
encoding: 'utf8'
|
|
88
|
+
});
|
|
89
|
+
input.captures.forEach(capSpec => {
|
|
90
|
+
let m;
|
|
91
|
+
output[item] || (output[item] = []);
|
|
92
|
+
if (capSpec.global) {
|
|
93
|
+
while ((m = capSpec.re.exec(contents)) !== null) {
|
|
94
|
+
output[item].push(m[capSpec.matchIndex].replace(reClean, ''));
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
m = capSpec.re.exec(contents);
|
|
98
|
+
if (m) {
|
|
99
|
+
output[item].push(m[capSpec.matchIndex].replace(reClean, ''));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
report(output[item], item);
|
|
105
|
+
});
|
|
106
|
+
_nodeFs.default.writeFileSync(options.output.file, JSON.stringify(output));
|
|
107
|
+
}
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* Copyrights licensed under the MIT License. See the accompanying LICENSE file
|
|
4
4
|
* for terms.
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
export default cannibalizr;
|
|
8
8
|
/**
|
|
9
9
|
* Pull strings from multiple input files, write to structured json.
|
|
10
10
|
*
|
|
@@ -39,7 +39,7 @@ const fs = require('fs');
|
|
|
39
39
|
* @param {Function} [options.logger] - A logger to use.
|
|
40
40
|
* @returns {Undefined} Nothing.
|
|
41
41
|
*/
|
|
42
|
-
|
|
42
|
+
export function cannibalizr (options) {
|
|
43
43
|
const output = {};
|
|
44
44
|
|
|
45
45
|
// used to trim "|' from the beginning and end of a captured result.
|
|
@@ -105,4 +105,4 @@ module.exports = function cannibalizr (options) {
|
|
|
105
105
|
fs.writeFileSync(options.output.file, JSON.stringify(
|
|
106
106
|
output
|
|
107
107
|
));
|
|
108
|
-
}
|
|
108
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/***
|
|
2
|
+
* Copyright (c) 2016 - 2022 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,9 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cannibalizr",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Turns an old pile of rusty files into a new json data source",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"require": "./dist/index.cjs",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
},
|
|
6
12
|
"scripts": {
|
|
13
|
+
"prepublishOnly": "rimraf ./dist && babel ./lib/index.js -o ./dist/index.cjs && node -e 'require(\"fs\").copyFileSync(\"./lib/index.js\", \"./dist/index.js\");'",
|
|
7
14
|
"test": "mocha ./tests/unit --recursive --reporter spec",
|
|
8
15
|
"cover": "c8 -- npm test",
|
|
9
16
|
"lint": "eslint .",
|
|
@@ -31,17 +38,17 @@
|
|
|
31
38
|
"engines": {
|
|
32
39
|
"node": ">= 14"
|
|
33
40
|
},
|
|
34
|
-
"dependencies": {},
|
|
35
41
|
"devDependencies": {
|
|
36
|
-
"@babel/
|
|
37
|
-
"@babel/
|
|
38
|
-
"@babel/eslint-parser": "^7.
|
|
42
|
+
"@babel/cli": "^7.19.3",
|
|
43
|
+
"@babel/core": "^7.20.2",
|
|
44
|
+
"@babel/eslint-parser": "^7.19.1",
|
|
45
|
+
"@babel/preset-env": "^7.20.2",
|
|
39
46
|
"c8": "^7.12.0",
|
|
40
|
-
"chai": "^4.3.
|
|
41
|
-
"eslint": "^8.
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
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"
|
|
45
52
|
},
|
|
46
53
|
"pre-commit": [
|
|
47
54
|
"lint",
|
package/.c8rc.json
DELETED
package/.eslintignore
DELETED
package/.eslintrc.json
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
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: [14, 16]
|
|
16
|
-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
|
17
|
-
|
|
18
|
-
steps:
|
|
19
|
-
- uses: actions/checkout@v3
|
|
20
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
21
|
-
uses: actions/setup-node@v3
|
|
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/babel.config.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright (c) 2016 - 2022 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
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/***
|
|
2
|
-
* Copyright (c) 2016 - 2022 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;
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2016 - 2022 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
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
}
|
package/tests/unit/test.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/***
|
|
2
|
-
* Copyright (c) 2016 - 2022 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
|
-
});
|