-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathpkg-check.js
executable file
Β·218 lines (191 loc) Β· 4.48 KB
/
pkg-check.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const zlib = require('zlib');
const execa = require('execa');
const meow = require('meow');
const readPkg = require('read-pkg');
const requireFromString = require('require-from-string');
const tar = require('tar-fs');
const {values} = require('lodash');
const {fix} = require('@commitlint/test');
const builtin = require.resolve('is-builtin-module');
const PRELUDE = `
var _require = require;
require = function(id) {
var dummy = new Proxy({}, {
get() {
return dummy;
}
});
var _isBuiltIn = _require('${builtin}');
if (id[0] === '.' || _isBuiltIn(id)) {
return _require(id);
} else {
return dummy;
}
};
`;
function main(cli) {
if (!Proxy) {
console
.warn('Skipping pkg-check, detected missing Proxy support')
.process.exit(0);
}
const cwd = cli.flags.cwd || process.cwd();
const skipImport =
typeof cli.flags.skipImport === 'boolean' ? cli.flags.skipImport : false;
return readPkg({cwd}).then(pkg => {
return getTarballFiles(cwd, {write: !skipImport}).then(tarball => {
return getPackageFiles(cwd).then(pkgFiles => {
let problems = [];
if (!cli.flags.skipBin) {
problems = problems.concat(
pkgFiles.bin
.filter(binFile => tarball.files.indexOf(binFile) === -1)
.map(binFile => ({
type: 'bin',
file: binFile,
message: `Required bin file ${binFile} not found for ${
pkg.name
}`
}))
);
}
if (
!cli.flags.skipMain &&
tarball.files.indexOf(pkgFiles.main) === -1
) {
problems.push({
type: 'main',
file: pkgFiles.main,
message: `Required main file ${pkgFiles.main} not found for ${
pkg.name
}`
});
}
if (!cli.flags.skipImport && !cli.flags.skipMain) {
const importable = fileImportable(
path.join(tarball.dirname, pkgFiles.main)
);
if (!importable[1]) {
problems.push({
type: 'import',
file: pkgFiles.main,
message: `Error while importing ${pkgFiles.main}: ${
importable[0].message
}`
});
}
}
return {
pkg: pkg, // eslint-disable-line object-shorthand
pkgFiles: pkgFiles, // eslint-disable-line object-shorthand
files: tarball.files,
problems: problems // eslint-disable-line object-shorthand
};
});
});
});
}
main(
meow(`
pkg-check
Check if a package creates valid tarballs
Options
--skip-main Skip main checks
--skip-bin Skip bin checks
--skip-import Skip import smoke test
Examples
$ pkg-check
`)
)
.then(report => {
if (report.problems.length > 0) {
console.log(
`Found ${report.problems.length} problems while checking tarball for ${
report.pkg.name
}:`
);
report.problems.forEach(problem => {
console.log(problem.message);
});
process.exit(1);
}
})
.catch(err => {
setTimeout(() => {
throw err;
});
});
function getTarballFiles(source, options) {
return fix
.bootstrap(source)
.then(cwd =>
execa('npm', ['pack'], {cwd}).then(cp => path.join(cwd, cp.stdout))
)
.then(tarball => getArchiveFiles(tarball, options));
}
function getArchiveFiles(filePath, options) {
const write = typeof options.write === 'boolean' ? options.write : true;
return new Promise((resolve, reject) => {
const files = [];
fs.createReadStream(filePath)
.pipe(zlib.createGunzip())
.pipe(
tar.extract(path.dirname(filePath), {
ignore(_, header) {
files.push(path.relative('package', header.name));
return !write;
}
})
)
.once('error', err => reject(err))
.once('finish', () =>
resolve({
dirname: path.join(path.dirname(filePath), 'package'),
files: files // eslint-disable-line object-shorthand
})
);
});
}
function getPackageFiles(source) {
return readPkg(source).then(pkg => {
return {
main: normalizeMainPath(pkg.main || './index.js'),
bin: getPkgBinFiles(pkg.bin)
};
});
}
function normalizeMainPath(mainPath) {
const norm = path.normalize(mainPath);
if (norm[norm.length - 1] === '/') {
return `${norm}index.js`;
}
return norm;
}
function getPkgBinFiles(bin) {
if (!bin) {
return [];
}
if (typeof bin === 'string') {
return [path.normalize(bin)];
}
if (typeof bin === 'object') {
return values(bin).map(b => path.normalize(b));
}
}
function fileImportable(file) {
try {
requireFromString(
`
${PRELUDE}
${fs.readFileSync(file)}
`,
file
);
return [null, true];
} catch (err) {
return [err, false];
}
}