1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.worker = worker;
|
7 | exports.getSha1 = getSha1;
|
8 |
|
9 | function _crypto() {
|
10 | const data = require('crypto');
|
11 |
|
12 | _crypto = function () {
|
13 | return data;
|
14 | };
|
15 |
|
16 | return data;
|
17 | }
|
18 |
|
19 | function path() {
|
20 | const data = _interopRequireWildcard(require('path'));
|
21 |
|
22 | path = function () {
|
23 | return data;
|
24 | };
|
25 |
|
26 | return data;
|
27 | }
|
28 |
|
29 | function fs() {
|
30 | const data = _interopRequireWildcard(require('graceful-fs'));
|
31 |
|
32 | fs = function () {
|
33 | return data;
|
34 | };
|
35 |
|
36 | return data;
|
37 | }
|
38 |
|
39 | var _blacklist = _interopRequireDefault(require('./blacklist'));
|
40 |
|
41 | var _constants = _interopRequireDefault(require('./constants'));
|
42 |
|
43 | var dependencyExtractor = _interopRequireWildcard(
|
44 | require('./lib/dependencyExtractor')
|
45 | );
|
46 |
|
47 | function _interopRequireDefault(obj) {
|
48 | return obj && obj.__esModule ? obj : {default: obj};
|
49 | }
|
50 |
|
51 | function _getRequireWildcardCache(nodeInterop) {
|
52 | if (typeof WeakMap !== 'function') return null;
|
53 | var cacheBabelInterop = new WeakMap();
|
54 | var cacheNodeInterop = new WeakMap();
|
55 | return (_getRequireWildcardCache = function (nodeInterop) {
|
56 | return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
57 | })(nodeInterop);
|
58 | }
|
59 |
|
60 | function _interopRequireWildcard(obj, nodeInterop) {
|
61 | if (!nodeInterop && obj && obj.__esModule) {
|
62 | return obj;
|
63 | }
|
64 | if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
65 | return {default: obj};
|
66 | }
|
67 | var cache = _getRequireWildcardCache(nodeInterop);
|
68 | if (cache && cache.has(obj)) {
|
69 | return cache.get(obj);
|
70 | }
|
71 | var newObj = {};
|
72 | var hasPropertyDescriptor =
|
73 | Object.defineProperty && Object.getOwnPropertyDescriptor;
|
74 | for (var key in obj) {
|
75 | if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
|
76 | var desc = hasPropertyDescriptor
|
77 | ? Object.getOwnPropertyDescriptor(obj, key)
|
78 | : null;
|
79 | if (desc && (desc.get || desc.set)) {
|
80 | Object.defineProperty(newObj, key, desc);
|
81 | } else {
|
82 | newObj[key] = obj[key];
|
83 | }
|
84 | }
|
85 | }
|
86 | newObj.default = obj;
|
87 | if (cache) {
|
88 | cache.set(obj, newObj);
|
89 | }
|
90 | return newObj;
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | const PACKAGE_JSON = path().sep + 'package.json';
|
100 | let hasteImpl = null;
|
101 | let hasteImplModulePath = null;
|
102 |
|
103 | function sha1hex(content) {
|
104 | return (0, _crypto().createHash)('sha1').update(content).digest('hex');
|
105 | }
|
106 |
|
107 | async function worker(data) {
|
108 | if (
|
109 | data.hasteImplModulePath &&
|
110 | data.hasteImplModulePath !== hasteImplModulePath
|
111 | ) {
|
112 | if (hasteImpl) {
|
113 | throw new Error('jest-haste-map: hasteImplModulePath changed');
|
114 | }
|
115 |
|
116 | hasteImplModulePath = data.hasteImplModulePath;
|
117 | hasteImpl = require(hasteImplModulePath);
|
118 | }
|
119 |
|
120 | let content;
|
121 | let dependencies;
|
122 | let id;
|
123 | let module;
|
124 | let sha1;
|
125 | const {computeDependencies, computeSha1, rootDir, filePath} = data;
|
126 |
|
127 | const getContent = () => {
|
128 | if (content === undefined) {
|
129 | content = fs().readFileSync(filePath, 'utf8');
|
130 | }
|
131 |
|
132 | return content;
|
133 | };
|
134 |
|
135 | if (filePath.endsWith(PACKAGE_JSON)) {
|
136 |
|
137 | try {
|
138 | const fileData = JSON.parse(getContent());
|
139 |
|
140 | if (fileData.name) {
|
141 | const relativeFilePath = path().relative(rootDir, filePath);
|
142 | id = fileData.name;
|
143 | module = [relativeFilePath, _constants.default.PACKAGE];
|
144 | }
|
145 | } catch (err) {
|
146 | throw new Error(`Cannot parse ${filePath} as JSON: ${err.message}`);
|
147 | }
|
148 | } else if (
|
149 | !_blacklist.default.has(filePath.substring(filePath.lastIndexOf('.')))
|
150 | ) {
|
151 |
|
152 | if (hasteImpl) {
|
153 | id = hasteImpl.getHasteName(filePath);
|
154 | }
|
155 |
|
156 | if (computeDependencies) {
|
157 | const content = getContent();
|
158 | dependencies = Array.from(
|
159 | data.dependencyExtractor
|
160 | ? require(data.dependencyExtractor).extract(
|
161 | content,
|
162 | filePath,
|
163 | dependencyExtractor.extract
|
164 | )
|
165 | : dependencyExtractor.extract(content)
|
166 | );
|
167 | }
|
168 |
|
169 | if (id) {
|
170 | const relativeFilePath = path().relative(rootDir, filePath);
|
171 | module = [relativeFilePath, _constants.default.MODULE];
|
172 | }
|
173 | }
|
174 |
|
175 | if (computeSha1) {
|
176 | sha1 = sha1hex(getContent() || fs().readFileSync(filePath));
|
177 | }
|
178 |
|
179 | return {
|
180 | dependencies,
|
181 | id,
|
182 | module,
|
183 | sha1
|
184 | };
|
185 | }
|
186 |
|
187 | async function getSha1(data) {
|
188 | const sha1 = data.computeSha1
|
189 | ? sha1hex(fs().readFileSync(data.filePath))
|
190 | : null;
|
191 | return {
|
192 | dependencies: undefined,
|
193 | id: undefined,
|
194 | module: undefined,
|
195 | sha1
|
196 | };
|
197 | }
|