forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildUtils.mjs
122 lines (108 loc) · 3.57 KB
/
buildUtils.mjs
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
111
112
113
114
115
116
117
118
119
120
121
122
/**
* 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 assert from 'assert';
import {createRequire} from 'module';
import path from 'path';
import {fileURLToPath} from 'url';
import chalk from 'chalk';
import fs from 'graceful-fs';
import {sync as readPkg} from 'read-pkg';
import stringLength from 'string-length';
export const PACKAGES_DIR = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../packages',
);
export const OK = chalk.reset.inverse.bold.green(' DONE ');
// Get absolute paths of all directories under packages/*
export function getPackages() {
const packages = fs
.readdirSync(PACKAGES_DIR)
.map(file => path.resolve(PACKAGES_DIR, file))
.filter(f => fs.lstatSync(path.resolve(f)).isDirectory());
const require = createRequire(import.meta.url);
const rootPackage = require('../package.json');
const nodeEngineRequirement = rootPackage.engines.node;
return packages.map(packageDir => {
const pkg = readPkg({cwd: packageDir});
assert.ok(pkg.engines, `Engine requirement in "${pkg.name}" should exist`);
assert.strictEqual(
pkg.engines.node,
nodeEngineRequirement,
`Engine requirement in "${pkg.name}" should match root`,
);
assert.ok(
pkg.exports,
`Package "${pkg.name}" is missing \`exports\` field`,
);
assert.deepStrictEqual(
pkg.exports,
{
'.':
pkg.types == null
? pkg.main
: {
types: pkg.types,
// eslint-disable-next-line sort-keys
default: pkg.main,
},
'./package.json': './package.json',
...Object.values(pkg.bin || {}).reduce(
(mem, curr) =>
Object.assign(mem, {[curr.replace(/\.js$/, '')]: curr}),
{},
),
...(pkg.name === 'jest-circus' ? {'./runner': './runner.js'} : {}),
...(pkg.name === 'expect'
? {'./build/matchers': './build/matchers.js'}
: {}),
...(pkg.name === 'pretty-format'
? {'./ConvertAnsi': './build/plugins/ConvertAnsi.js'}
: {}),
},
`Package "${pkg.name}" does not export correct files`,
);
if (pkg.types) {
assert.strictEqual(
pkg.main,
'./build/index.js',
`Package "${pkg.name}" should have "./build/index.js" as main`,
);
assert.strictEqual(
pkg.types,
'./build/index.d.ts',
`Package "${pkg.name}" should have "./build/index.d.ts" as types`,
);
} else {
assert.strictEqual(
pkg.main,
'./index.js',
`Package "${pkg.name}" should have "./index.js" as main`,
);
}
if (pkg.bin) {
Object.entries(pkg.bin).forEach(([binName, binPath]) => {
const fullBinPath = path.resolve(packageDir, binPath);
if (!fs.existsSync(fullBinPath)) {
throw new Error(
`Binary in package "${pkg.name}" with name "${binName}" at ${binPath} does not exist`,
);
}
});
}
return {packageDir, pkg};
});
}
export function adjustToTerminalWidth(str) {
const columns = process.stdout.columns || 80;
const WIDTH = columns - stringLength(OK) + 1;
const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g'));
let lastString = strs[strs.length - 1];
if (lastString.length < WIDTH) {
lastString += Array(WIDTH - lastString.length).join(chalk.dim('.'));
}
return strs.slice(0, -1).concat(lastString).join('\n');
}