Skip to content

Commit 6ef2d67

Browse files
committed
chore: migrate to typescript
1 parent 77683ba commit 6ef2d67

63 files changed

Lines changed: 11529 additions & 789 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.eslintignore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Third party
2+
**/node_modules
3+
4+
# Build products
5+
dist/
6+
template/

‎.eslintrc.json‎

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
11
{
2-
"extends": "airbnb",
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"extends": [
5+
"plugin:@typescript-eslint/recommended",
6+
"prettier",
7+
"plugin:prettier/recommended"
8+
],
9+
"plugins": [
10+
"import"
11+
],
12+
"overrides": [
13+
{
14+
"files": [
15+
"src/**/*.ts"
16+
],
17+
"parserOptions": {
18+
"ecmaVersion": 2018,
19+
"sourceType": "module"
20+
}
21+
},
22+
{
23+
"files": [
24+
"__tests__/**/*.ts"
25+
],
26+
"env": {
27+
"jest": true
28+
}
29+
}
30+
],
331
"rules": {
4-
"func-names": 0,
5-
"no-param-reassign": 0,
6-
"one-var-declaration-per-line": [
32+
"prefer-const": [
733
"error",
8-
"initializations"
34+
{
35+
"destructuring": "all"
36+
}
937
],
10-
"no-underscore-dangle": 0,
11-
// es6
12-
"object-shorthand": 0,
13-
"one-var": [
14-
"error",
15-
"consecutive"
16-
],
17-
"no-plusplus": "off",
18-
"operator-linebreak" : "off",
19-
"comma-dangle": "off"
20-
},
21-
"env": {
22-
"mocha": true
23-
},
24-
"globals": {
25-
"expect": true
38+
"import/no-default-export": "error",
39+
"@typescript-eslint/ban-ts-ignore": "off",
40+
"@typescript-eslint/no-explicit-any": "off",
41+
"@typescript-eslint/no-empty-function": "off",
42+
"@typescript-eslint/no-empty-interface": "off",
43+
"@typescript-eslint/no-inferrable-types": "off",
44+
"@typescript-eslint/no-use-before-define": "off",
45+
"@typescript-eslint/interface-name-prefix": "off",
46+
"@typescript-eslint/triple-slash-reference": "off",
47+
"@typescript-eslint/explicit-module-boundary-types": "off",
48+
"@typescript-eslint/no-unused-vars": "off"
2649
}
2750
}

‎bin/textract‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env -S node --experimental-modules
2-
const path = require('path')
3-
, fs = require('fs');
2+
const path = require('path');
3+
const fs = require('fs');
44
const meow = (...args) => import('meow').then(({default: meow}) => meow(...args));
55
const help = 'Usage:\n' +
66
' textract pathToFile\n\n' +
@@ -20,11 +20,12 @@ const mewoCli = meow({
2020
help: help,
2121
importMeta: {url: `file://${path.resolve('../package.json')}`},
2222
});
23+
2324
mewoCli.then(cli => {
2425
if (!cli.input || cli.input.length === 0) {
2526
console.log('\n' + help);
2627
} else {
27-
const lib = path.join(path.dirname(fs.realpathSync(__filename)), '..', 'src', 'cli.js');
28+
const lib = path.join(path.dirname(fs.realpathSync(__filename)), '..', 'dist', 'cli');
2829
const textract = require(lib);
2930
textract(cli.input[0], cli.flags);
3031
}

‎dist/cli.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
var path_1 = __importDefault(require("path"));
7+
var index_1 = require("./index");
8+
module.exports = function (filePath, flags) {
9+
filePath = path_1.default.resolve(process.cwd(), filePath);
10+
flags.preserveLineBreaks = flags.preserveLineBreaks !== 'false';
11+
(0, index_1.fromFileWithPath)(filePath, flags, function (error, text) {
12+
if (error) {
13+
// eslint-disable-next-line no-console
14+
console.error(error);
15+
}
16+
else {
17+
// eslint-disable-next-line no-console
18+
console.log(text);
19+
}
20+
});
21+
};

‎dist/extract.js‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.extract = void 0;
7+
var fs_1 = __importDefault(require("fs"));
8+
var path_1 = __importDefault(require("path"));
9+
var html_entities_1 = require("html-entities");
10+
var util_1 = require("./util");
11+
var extractorPath = path_1.default.join(__dirname, 'extractors');
12+
var typeExtractors = {};
13+
var regexExtractors = [];
14+
var failedExtractorTypes = {};
15+
var STRIP_ONLY_SINGLE_LINEBREAKS = /(^|[^\n])\n(?!\n)/g;
16+
var WHITELIST_PRESERVE_LINEBREAKS = /[^A-Za-z\x80-\xFF\x24\u20AC\xA3\xA5 0-9 \u2015\u2116\u2018\u2019\u201C|\u201D\u2026 \uFF0C \u2013 \u2014 \u00C0-\u1FFF \u2C00-\uD7FF \uFB50–\uFDFF \uFE70–\uFEFF \uFF01-\uFFE6 .,?""!@#$%^&*()-_=+;:<>/\\|}{[\]`~'-\w\n\r]*/g, WHITELIST_STRIP_LINEBREAKS = /[^A-Za-z\x80-\xFF\x24\u20AC\xA3\xA5 0-9 \u2015\u2116\u2018\u2019\u201C|\u201D\u2026 \uFF0C \u2013 \u2014 \u00C0-\u1FFF \u2C00-\uD7FF \uFB50–\uFDFF \uFE70–\uFEFF \uFF01-\uFFE6 .,?""!@#$%^&*()-_=+;:<>/\\|}{[\]`~'-\w]*/g;
17+
var totalExtractors = 0, satisfiedExtractors = 0, hasInitialized = false;
18+
function registerExtractor(extractor) {
19+
if (extractor.types) {
20+
extractor.types.forEach(function (type) {
21+
if (typeof type === 'string') {
22+
type = type.toLowerCase();
23+
typeExtractors[type] = extractor.extract;
24+
}
25+
else if (type instanceof RegExp) {
26+
regexExtractors.push({ reg: type, extractor: extractor.extract });
27+
}
28+
});
29+
}
30+
}
31+
function registerFailedExtractor(extractor, failedMessage) {
32+
if (extractor.types) {
33+
extractor.types.forEach(function (type) {
34+
failedExtractorTypes[type.toLowerCase()] = failedMessage;
35+
});
36+
}
37+
}
38+
function testExtractor(extractor, options) {
39+
extractor.test(options, function (passedTest, failedMessage) {
40+
satisfiedExtractors++;
41+
if (passedTest) {
42+
registerExtractor(extractor);
43+
}
44+
else {
45+
registerFailedExtractor(extractor, failedMessage);
46+
}
47+
});
48+
}
49+
// global, all file type, content cleansing
50+
function cleanseText(options, cb) {
51+
return function (error, text) {
52+
if (!error) {
53+
// clean up text
54+
text = (0, util_1.replaceBadCharacters)(text);
55+
if (options.preserveLineBreaks || options.preserveOnlyMultipleLineBreaks) {
56+
if (options.preserveOnlyMultipleLineBreaks) {
57+
text = text.replace(STRIP_ONLY_SINGLE_LINEBREAKS, '$1 ').trim();
58+
}
59+
text = text.replace(WHITELIST_PRESERVE_LINEBREAKS, ' ');
60+
}
61+
else {
62+
text = text.replace(WHITELIST_STRIP_LINEBREAKS, ' ');
63+
}
64+
// multiple spaces, tabs, vertical tabs, non-breaking space]
65+
text = text.replace(/ (?! )/g, '').replace(/[ \t\v\u00A0]{2,}/g, ' ');
66+
text = (0, html_entities_1.decode)(text);
67+
}
68+
cb(error, text);
69+
};
70+
}
71+
function initializeExtractors(options) {
72+
hasInitialized = true;
73+
// discover available extractors
74+
var extractors = fs_1.default.readdirSync(extractorPath).map(function (item) {
75+
var fullExtractorPath = path_1.default.join(extractorPath, item);
76+
// get the extractor
77+
// eslint-disable-next-line global-require,import/no-dynamic-require
78+
return require(fullExtractorPath);
79+
});
80+
// perform any binary tests to ensure extractor is possible
81+
// given execution environment
82+
extractors.forEach(function (extractor) {
83+
if (extractor.test) {
84+
testExtractor(extractor, options);
85+
}
86+
else {
87+
satisfiedExtractors++;
88+
registerExtractor(extractor);
89+
}
90+
});
91+
// need to keep track of how many extractors we have in total
92+
totalExtractors = extractors.length;
93+
}
94+
function findExtractor(type) {
95+
var iLen = regexExtractors.length;
96+
var extractor, regexExtractor;
97+
type = type.toLowerCase();
98+
if (typeExtractors[type]) {
99+
extractor = typeExtractors[type];
100+
}
101+
else {
102+
for (var i = 0; i < iLen; i++) {
103+
regexExtractor = regexExtractors[i];
104+
if (type.match(regexExtractor.reg)) {
105+
extractor = regexExtractor.extractor;
106+
}
107+
}
108+
}
109+
return extractor;
110+
}
111+
function extract(type, filePath, options, cb) {
112+
var error, msg, theExtractor;
113+
if (!hasInitialized) {
114+
initializeExtractors(options);
115+
}
116+
// registration of extractors complete?
117+
if (totalExtractors === satisfiedExtractors) {
118+
theExtractor = findExtractor(type);
119+
if (theExtractor) {
120+
cb = cleanseText(options, cb);
121+
theExtractor(filePath, options, cb);
122+
}
123+
else {
124+
// cannot extract this file type
125+
msg = "Error for type: [[ ".concat(type, " ]], file: [[ ").concat(filePath, " ]]");
126+
// update error message if type is supported but just not configured/installed properly
127+
if (failedExtractorTypes[type]) {
128+
msg += "".concat(', extractor for type exists, but failed to initialize. Message:').concat(failedExtractorTypes[type]);
129+
}
130+
error = new Error(msg);
131+
cb(error, null);
132+
}
133+
}
134+
else {
135+
// async registration has not wrapped up
136+
// try again later
137+
setTimeout(function () {
138+
extract(type, filePath, options, cb);
139+
}, 100);
140+
}
141+
}
142+
exports.extract = extract;

‎dist/extractors/doc-osx.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.types = exports.test = exports.extract = void 0;
7+
var child_process_1 = require("child_process");
8+
var os_1 = __importDefault(require("os"));
9+
var path_1 = __importDefault(require("path"));
10+
// textutil -convert txt -stdout foo.doc
11+
function extract(filePath, options, cb) {
12+
var result = '';
13+
var error = null;
14+
var textutil = (0, child_process_1.spawn)('textutil', ['-convert', 'txt', '-stdout', filePath]);
15+
textutil.stdout.on('data', function (buffer) {
16+
result += buffer.toString();
17+
});
18+
textutil.stderr.on('error', function (buffer) {
19+
if (!error) {
20+
error = '';
21+
}
22+
error += buffer.toString();
23+
});
24+
textutil.on('close', function ( /* code */) {
25+
if (result === 'not a doc') {
26+
cb(result, null);
27+
return;
28+
}
29+
if (error) {
30+
error = new Error("textutil read of file named [[ ".concat(path_1.default.basename(filePath), " ]] failed: ").concat(error));
31+
cb(error, null);
32+
return;
33+
}
34+
cb(null, result.trim());
35+
});
36+
}
37+
exports.extract = extract;
38+
function test(options, cb) {
39+
// just osx extractor, so don't bother checking on osx
40+
if (os_1.default.platform() !== 'darwin') {
41+
cb(true);
42+
return;
43+
}
44+
(0, child_process_1.exec)("textutil ".concat(__filename), function (error /* , stdout, stderr */) {
45+
var msg;
46+
if (error !== null) {
47+
msg = "INFO: 'textutil' does not appear to be installed,so textract will be unable to extract DOCs.";
48+
}
49+
cb(error === null, msg);
50+
});
51+
}
52+
exports.test = test;
53+
var types = [];
54+
exports.types = types;
55+
if (os_1.default.platform() === 'darwin') {
56+
exports.types = types = ['application/msword', 'application/rtf', 'text/rtf'];
57+
// types = [];
58+
}

‎dist/extractors/doc.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.types = exports.test = exports.extract = void 0;
7+
var child_process_1 = require("child_process");
8+
var os_1 = __importDefault(require("os"));
9+
var path_1 = __importDefault(require("path"));
10+
var util_1 = require("../util");
11+
function extract(filePath, options, cb) {
12+
var execOptions = (0, util_1.createExecOptions)('doc', options);
13+
(0, child_process_1.exec)("antiword -m UTF-8.txt \"".concat(filePath, "\""), execOptions, function (error, stdout) {
14+
var err;
15+
if (error) {
16+
if (error.toString().indexOf('is not a Word Document') > 0) {
17+
err = new Error("file named [[ ".concat(path_1.default.basename(filePath), " ]] does not appear to really be a .doc file"));
18+
}
19+
else {
20+
err = new Error("antiword read of file named [[ ".concat(path_1.default.basename(filePath), " ]] failed: ").concat(error));
21+
}
22+
cb(err, null);
23+
}
24+
else {
25+
cb(null, stdout
26+
.toString()
27+
.trim()
28+
.replace(/\[pic\]/g, ''));
29+
}
30+
});
31+
}
32+
exports.extract = extract;
33+
function test(options, cb) {
34+
// just non-osx extractor
35+
if (os_1.default.platform() === 'darwin') {
36+
cb(true);
37+
return;
38+
}
39+
var execOptions = (0, util_1.createExecOptions)('doc', options);
40+
(0, child_process_1.exec)("antiword -m UTF-8.txt ".concat(__filename), execOptions, function (error /* , stdout, stderr */) {
41+
var msg;
42+
if (error !== null && error.message && error.message.indexOf('not found') !== -1) {
43+
msg = "INFO: 'antiword' does not appear to be installed, so textract will be unable to extract DOCs.";
44+
cb(false, msg);
45+
}
46+
else {
47+
cb(true);
48+
}
49+
});
50+
}
51+
exports.test = test;
52+
var types;
53+
exports.types = types;
54+
if (os_1.default.platform() === 'darwin') {
55+
// types = ['application/msword'];
56+
}
57+
else {
58+
exports.types = types = ['application/msword'];
59+
}

0 commit comments

Comments
 (0)