forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
worker.ts
110 lines (93 loc) 路 3.11 KB
/
worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {createHash} from 'crypto';
import * as path from 'path';
import * as fs from 'graceful-fs';
import blacklist from './blacklist';
import H from './constants';
import * as dependencyExtractor from './lib/dependencyExtractor';
import type {HasteImpl, WorkerMessage, WorkerMetadata} from './types';
const PACKAGE_JSON = path.sep + 'package.json';
let hasteImpl: HasteImpl | null = null;
let hasteImplModulePath: string | null = null;
function sha1hex(content: string | Buffer): string {
return createHash('sha1').update(content).digest('hex');
}
export async function worker(data: WorkerMessage): Promise<WorkerMetadata> {
if (
data.hasteImplModulePath &&
data.hasteImplModulePath !== hasteImplModulePath
) {
if (hasteImpl) {
throw new Error('jest-haste-map: hasteImplModulePath changed');
}
hasteImplModulePath = data.hasteImplModulePath;
hasteImpl = require(hasteImplModulePath);
}
let content: string | undefined;
let dependencies: WorkerMetadata['dependencies'];
let id: WorkerMetadata['id'];
let module: WorkerMetadata['module'];
let sha1: WorkerMetadata['sha1'];
const {computeDependencies, computeSha1, rootDir, filePath} = data;
const getContent = (): string => {
if (content === undefined) {
content = fs.readFileSync(filePath, 'utf8');
}
return content;
};
if (filePath.endsWith(PACKAGE_JSON)) {
// Process a package.json that is returned as a PACKAGE type with its name.
try {
const fileData = JSON.parse(getContent());
if (fileData.name) {
const relativeFilePath = path.relative(rootDir, filePath);
id = fileData.name;
module = [relativeFilePath, H.PACKAGE];
}
} catch (err: any) {
throw new Error(`Cannot parse ${filePath} as JSON: ${err.message}`);
}
} else if (!blacklist.has(filePath.substr(filePath.lastIndexOf('.')))) {
// Process a random file that is returned as a MODULE.
if (hasteImpl) {
id = hasteImpl.getHasteName(filePath);
}
if (computeDependencies) {
const content = getContent();
dependencies = Array.from(
data.dependencyExtractor
? require(data.dependencyExtractor).extract(
content,
filePath,
dependencyExtractor.extract,
)
: dependencyExtractor.extract(content),
);
}
if (id) {
const relativeFilePath = path.relative(rootDir, filePath);
module = [relativeFilePath, H.MODULE];
}
}
// If a SHA-1 is requested on update, compute it.
if (computeSha1) {
sha1 = sha1hex(getContent() || fs.readFileSync(filePath));
}
return {dependencies, id, module, sha1};
}
export async function getSha1(data: WorkerMessage): Promise<WorkerMetadata> {
const sha1 = data.computeSha1
? sha1hex(fs.readFileSync(data.filePath))
: null;
return {
dependencies: undefined,
id: undefined,
module: undefined,
sha1,
};
}