@definitelytyped/dts-critic 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/develop.ts DELETED
@@ -1,445 +0,0 @@
1
- import fs = require("fs");
2
- import yargs = require("yargs");
3
- import headerParser = require("@definitelytyped/header-parser");
4
- import path = require("path");
5
- import cp = require("child_process");
6
- import {
7
- dtsCritic,
8
- dtToNpmName,
9
- getNpmInfo,
10
- parseExportErrorKind,
11
- CriticError,
12
- ExportErrorKind,
13
- Mode,
14
- checkSource,
15
- findDtsName,
16
- CheckOptions,
17
- parseMode,
18
- } from "./index";
19
-
20
- const sourcesDir = "sources";
21
- const downloadsPath = path.join(sourcesDir, "dts-critic-internal/downloads.json");
22
- const isNpmPath = path.join(sourcesDir, "dts-critic-internal/npm.json");
23
-
24
- function getPackageDownloads(dtName: string): number {
25
- const npmName = dtToNpmName(dtName);
26
- const url = `https://api.npmjs.org/downloads/point/last-month/${npmName}`;
27
- const result = JSON.parse(cp.execFileSync("curl", ["--silent", "-L", url], { encoding: "utf8" })) as {
28
- downloads?: number;
29
- };
30
- return result.downloads || 0;
31
- }
32
-
33
- interface DownloadsJson {
34
- [key: string]: number | undefined;
35
- }
36
-
37
- function getAllPackageDownloads(dtPath: string): DownloadsJson {
38
- if (fs.existsSync(downloadsPath)) {
39
- return JSON.parse(fs.readFileSync(downloadsPath, { encoding: "utf8" })) as DownloadsJson;
40
- }
41
-
42
- initDir(path.dirname(downloadsPath));
43
- const downloads: DownloadsJson = {};
44
- const dtTypesPath = getDtTypesPath(dtPath);
45
- for (const item of fs.readdirSync(dtTypesPath)) {
46
- const d = getPackageDownloads(item);
47
- downloads[item] = d;
48
- }
49
- fs.writeFileSync(downloadsPath, JSON.stringify(downloads), { encoding: "utf8" });
50
-
51
- return downloads;
52
- }
53
-
54
- function initDir(path: string): void {
55
- if (!fs.existsSync(path)) {
56
- fs.mkdirSync(path);
57
- }
58
- }
59
-
60
- function getDtTypesPath(dtBasePath: string): string {
61
- return path.join(dtBasePath, "types");
62
- }
63
-
64
- function compareDownloads(downloads: DownloadsJson, package1: string, package2: string): number {
65
- const count1 = downloads[package1] || 0;
66
- const count2 = downloads[package2] || 0;
67
- return count1 - count2;
68
- }
69
-
70
- interface IsNpmJson {
71
- [key: string]: boolean | undefined;
72
- }
73
-
74
- function getAllIsNpm(dtPath: string): IsNpmJson {
75
- if (fs.existsSync(isNpmPath)) {
76
- return JSON.parse(fs.readFileSync(isNpmPath, { encoding: "utf8" })) as IsNpmJson;
77
- }
78
- initDir(path.dirname(isNpmPath));
79
- const isNpm: IsNpmJson = {};
80
- const dtTypesPath = getDtTypesPath(dtPath);
81
- for (const item of fs.readdirSync(dtTypesPath)) {
82
- isNpm[item] = getNpmInfo(item).isNpm;
83
- }
84
- fs.writeFileSync(isNpmPath, JSON.stringify(isNpm), { encoding: "utf8" });
85
- return isNpm;
86
- }
87
-
88
- function getPopularNpmPackages(count: number, dtPath: string): string[] {
89
- const dtPackages = getDtNpmPackages(dtPath);
90
- const downloads = getAllPackageDownloads(dtPath);
91
- dtPackages.sort((a, b) => compareDownloads(downloads, a, b));
92
- return dtPackages.slice(dtPackages.length - count);
93
- }
94
-
95
- function getUnpopularNpmPackages(count: number, dtPath: string): string[] {
96
- const dtPackages = getDtNpmPackages(dtPath);
97
- const downloads = getAllPackageDownloads(dtPath);
98
- dtPackages.sort((a, b) => compareDownloads(downloads, a, b));
99
- return dtPackages.slice(0, count);
100
- }
101
-
102
- function getDtNpmPackages(dtPath: string): string[] {
103
- const dtPackages = fs.readdirSync(getDtTypesPath(dtPath));
104
- const isNpmJson = getAllIsNpm(dtPath);
105
- return dtPackages.filter((pkg) => isNpmPackage(pkg, /* header */ undefined, isNpmJson));
106
- }
107
-
108
- function getNonNpm(args: { dtPath: string }): void {
109
- const nonNpm: string[] = [];
110
- const dtTypesPath = getDtTypesPath(args.dtPath);
111
- const isNpmJson = getAllIsNpm(args.dtPath);
112
- for (const item of fs.readdirSync(dtTypesPath)) {
113
- const packageJsonPath = path.join(dtTypesPath, item, "package.json");
114
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
115
- const header = headerParser.validatePackageJson(
116
- item,
117
- packageJson,
118
- headerParser.getTypesVersions(path.join(dtTypesPath, item)),
119
- );
120
- if (!isNpmPackage(item, Array.isArray(header) ? undefined : header, isNpmJson)) {
121
- nonNpm.push(item);
122
- }
123
- }
124
- console.log(`List of non-npm packages on DT:\n${nonNpm.map((name) => `DT name: ${name}\n`).join("")}`);
125
- }
126
-
127
- interface CommonArgs {
128
- dtPath: string;
129
- mode: string;
130
- enableError: string[] | undefined;
131
- debug: boolean;
132
- json: boolean;
133
- }
134
-
135
- function checkAll(args: CommonArgs): void {
136
- const dtPackages = fs.readdirSync(getDtTypesPath(args.dtPath));
137
- checkPackages({ packages: dtPackages, ...args });
138
- }
139
-
140
- function checkPopular(args: { count: number } & CommonArgs): void {
141
- checkPackages({ packages: getPopularNpmPackages(args.count, args.dtPath), ...args });
142
- }
143
-
144
- function checkUnpopular(args: { count: number } & CommonArgs): void {
145
- checkPackages({ packages: getUnpopularNpmPackages(args.count, args.dtPath), ...args });
146
- }
147
-
148
- function checkPackages(args: { packages: string[] } & CommonArgs): void {
149
- const results = args.packages.map((pkg) => doCheck({ package: pkg, ...args }));
150
- printResults(results, args.json);
151
- }
152
-
153
- function checkPackage(args: { package: string } & CommonArgs): void {
154
- printResults([doCheck(args)], args.json);
155
- }
156
-
157
- function doCheck(args: {
158
- package: string;
159
- dtPath: string;
160
- mode: string;
161
- enableError: string[] | undefined;
162
- debug: boolean;
163
- }): Result {
164
- const dtPackage = args.package;
165
- const opts = getOptions(args.mode, args.enableError || []);
166
- try {
167
- const dtsPath = path.join(getDtTypesPath(args.dtPath), dtPackage, "index.d.ts");
168
- const errors = dtsCritic(dtsPath, /* sourcePath */ undefined, opts, args.debug);
169
- return { package: args.package, output: errors };
170
- } catch (e) {
171
- return { package: args.package, output: (e as Error).toString() };
172
- }
173
- }
174
-
175
- function getOptions(modeArg: string, enabledErrors: string[]): CheckOptions {
176
- const mode = parseMode(modeArg);
177
- if (!mode) {
178
- throw new Error(`Could not find mode named '${modeArg}'.`);
179
- }
180
- switch (mode) {
181
- case Mode.NameOnly:
182
- return { mode };
183
- case Mode.Code:
184
- const errors = getEnabledErrors(enabledErrors);
185
- return { mode, errors };
186
- }
187
- }
188
-
189
- function getEnabledErrors(errorNames: string[]): Map<ExportErrorKind, boolean> {
190
- const errors: ExportErrorKind[] = [];
191
- for (const name of errorNames) {
192
- const error = parseExportErrorKind(name);
193
- if (error === undefined) {
194
- throw new Error(`Could not find error named '${name}'.`);
195
- }
196
- errors.push(error);
197
- }
198
- return new Map(errors.map((err) => [err, true]));
199
- }
200
-
201
- function checkFile(args: { jsFile: string; dtsFile: string; debug: boolean }): void {
202
- console.log(`\tChecking JS file ${args.jsFile} and declaration file ${args.dtsFile}`);
203
- try {
204
- const errors = checkSource(findDtsName(args.dtsFile), args.dtsFile, args.jsFile, new Map(), args.debug);
205
- console.log(formatErrors(errors));
206
- } catch (e) {
207
- console.log(e);
208
- }
209
- }
210
-
211
- interface Result {
212
- package: string;
213
- output: CriticError[] | string;
214
- }
215
-
216
- function printResults(results: Result[], json: boolean): void {
217
- if (json) {
218
- console.log(JSON.stringify(results));
219
- return;
220
- }
221
-
222
- for (const result of results) {
223
- console.log(`\tChecking package ${result.package} ...`);
224
- if (typeof result.output === "string") {
225
- console.log(`Exception:\n${result.output}`);
226
- } else {
227
- console.log(formatErrors(result.output));
228
- }
229
- }
230
- }
231
-
232
- function formatErrors(errors: CriticError[]): string {
233
- const lines: string[] = [];
234
- for (const error of errors) {
235
- lines.push("Error: " + error.message);
236
- }
237
- if (errors.length === 0) {
238
- lines.push("No errors found! :)");
239
- }
240
- return lines.join("\n");
241
- }
242
-
243
- function isNpmPackage(name: string, header?: headerParser.Header, isNpmJson: IsNpmJson = {}): boolean {
244
- if (header && header.nonNpm) return false;
245
- const isNpm = isNpmJson[name];
246
- if (isNpm !== undefined) {
247
- return isNpm;
248
- }
249
- return getNpmInfo(name).isNpm;
250
- }
251
-
252
- function main() {
253
- yargs
254
- .usage("$0 <command>")
255
- .command(
256
- "check-all",
257
- "Check source and declaration of all DT packages that are on NPM.",
258
- {
259
- dtPath: {
260
- type: "string",
261
- default: "../DefinitelyTyped",
262
- describe: "Path of DT repository cloned locally.",
263
- },
264
- mode: {
265
- type: "string",
266
- required: true,
267
- choices: [Mode.NameOnly, Mode.Code],
268
- describe: "Mode that defines which group of checks will be made.",
269
- },
270
- enableError: {
271
- type: "array",
272
- string: true,
273
- describe: "Enable checking for a specific export error.",
274
- },
275
- debug: {
276
- type: "boolean",
277
- default: false,
278
- describe: "Turn debug logging on.",
279
- },
280
- json: {
281
- type: "boolean",
282
- default: false,
283
- describe: "Format output result as json.",
284
- },
285
- },
286
- checkAll,
287
- )
288
- .command(
289
- "check-popular",
290
- "Check source and declaration of most popular DT packages that are on NPM.",
291
- {
292
- count: {
293
- alias: "c",
294
- type: "number",
295
- required: true,
296
- describe: "Number of packages to be checked.",
297
- },
298
- dtPath: {
299
- type: "string",
300
- default: "../DefinitelyTyped",
301
- describe: "Path of DT repository cloned locally.",
302
- },
303
- mode: {
304
- type: "string",
305
- required: true,
306
- choices: [Mode.NameOnly, Mode.Code],
307
- describe: "Mode that defines which group of checks will be made.",
308
- },
309
- enableError: {
310
- type: "array",
311
- string: true,
312
- describe: "Enable checking for a specific export error.",
313
- },
314
- debug: {
315
- type: "boolean",
316
- default: false,
317
- describe: "Turn debug logging on.",
318
- },
319
- json: {
320
- type: "boolean",
321
- default: false,
322
- describe: "Format output result as json.",
323
- },
324
- },
325
- checkPopular,
326
- )
327
- .command(
328
- "check-unpopular",
329
- "Check source and declaration of least popular DT packages that are on NPM.",
330
- {
331
- count: {
332
- alias: "c",
333
- type: "number",
334
- required: true,
335
- describe: "Number of packages to be checked.",
336
- },
337
- dtPath: {
338
- type: "string",
339
- default: "../DefinitelyTyped",
340
- describe: "Path of DT repository cloned locally.",
341
- },
342
- mode: {
343
- type: "string",
344
- required: true,
345
- choices: [Mode.NameOnly, Mode.Code],
346
- describe: "Mode that defines which group of checks will be made.",
347
- },
348
- enableError: {
349
- type: "array",
350
- string: true,
351
- describe: "Enable checking for a specific export error.",
352
- },
353
- debug: {
354
- type: "boolean",
355
- default: false,
356
- describe: "Turn debug logging on.",
357
- },
358
- json: {
359
- type: "boolean",
360
- default: false,
361
- describe: "Format output result as json.",
362
- },
363
- },
364
- checkUnpopular,
365
- )
366
- .command(
367
- "check-package",
368
- "Check source and declaration of a DT package.",
369
- {
370
- package: {
371
- alias: "p",
372
- type: "string",
373
- required: true,
374
- describe: "DT name of a package.",
375
- },
376
- dtPath: {
377
- type: "string",
378
- default: "../DefinitelyTyped",
379
- describe: "Path of DT repository cloned locally.",
380
- },
381
- mode: {
382
- type: "string",
383
- required: true,
384
- choices: [Mode.NameOnly, Mode.Code],
385
- describe: "Mode that defines which group of checks will be made.",
386
- },
387
- enableError: {
388
- type: "array",
389
- string: true,
390
- describe: "Enable checking for a specific export error.",
391
- },
392
- debug: {
393
- type: "boolean",
394
- default: false,
395
- describe: "Turn debug logging on.",
396
- },
397
- json: {
398
- type: "boolean",
399
- default: false,
400
- describe: "Format output result as json.",
401
- },
402
- },
403
- checkPackage,
404
- )
405
- .command(
406
- "check-file",
407
- "Check a JavaScript file and its matching declaration file.",
408
- {
409
- jsFile: {
410
- alias: "j",
411
- type: "string",
412
- required: true,
413
- describe: "Path of JavaScript file.",
414
- },
415
- dtsFile: {
416
- alias: "d",
417
- type: "string",
418
- required: true,
419
- describe: "Path of declaration file.",
420
- },
421
- debug: {
422
- type: "boolean",
423
- default: false,
424
- describe: "Turn debug logging on.",
425
- },
426
- },
427
- checkFile,
428
- )
429
- .command(
430
- "get-non-npm",
431
- "Get list of DT packages whose source package is not on NPM",
432
- {
433
- dtPath: {
434
- type: "string",
435
- default: "../DefinitelyTyped",
436
- describe: "Path of DT repository cloned locally.",
437
- },
438
- },
439
- getNonNpm,
440
- )
441
- .demandCommand(1)
442
- .help()
443
- .parseSync();
444
- }
445
- main();
package/dist/develop.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};