bio-dts 0.11.0 → 0.12.0
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/README.md +3 -1
- package/bin/cmd.js +38 -4
- package/lib/generate-types.js +28 -12
- package/lib/post-transform.js +2 -2
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -41,6 +41,8 @@ import {
|
|
|
41
41
|
generateTypes
|
|
42
42
|
} from 'bio-dts';
|
|
43
43
|
|
|
44
|
+
import * as typescript from 'typescript';
|
|
45
|
+
|
|
44
46
|
// transform JS so it keeps the shape,
|
|
45
47
|
// but is properly digestable by the typescript
|
|
46
48
|
// compiler
|
|
@@ -54,5 +56,5 @@ const transformedCode = postTransform(tsCode);
|
|
|
54
56
|
// typescript compiler
|
|
55
57
|
generateTypes(files, {
|
|
56
58
|
outDir: 'dist'
|
|
57
|
-
});
|
|
59
|
+
}, typescript);
|
|
58
60
|
```
|
package/bin/cmd.js
CHANGED
|
@@ -5,6 +5,7 @@ import Module from 'node:module';
|
|
|
5
5
|
import glob from 'tiny-glob';
|
|
6
6
|
|
|
7
7
|
import path from 'node:path';
|
|
8
|
+
import os from 'node:os';
|
|
8
9
|
import fs from 'node:fs';
|
|
9
10
|
|
|
10
11
|
import generateTypes from '../lib/generate-types.js';
|
|
@@ -21,9 +22,9 @@ async function run() {
|
|
|
21
22
|
} = ts.parseCommandLine(args);
|
|
22
23
|
|
|
23
24
|
const help = args.includes('--help') || args.includes('-h');
|
|
24
|
-
const
|
|
25
|
+
const [ _verbose, _verboseGrep ] = args.flatMap(arg => /^--verbose(?:=(.*))?$/.exec(arg) || []);
|
|
25
26
|
|
|
26
|
-
const verbose =
|
|
27
|
+
const verbose = _verbose ? _verboseGrep || true : false;
|
|
27
28
|
const recursive = args.includes('--recursive') || args.includes('-r');
|
|
28
29
|
|
|
29
30
|
if (help) {
|
|
@@ -70,9 +71,40 @@ async function run() {
|
|
|
70
71
|
|
|
71
72
|
verbose && console.log('Using options ', generateOptions);
|
|
72
73
|
|
|
73
|
-
|
|
74
|
+
const {
|
|
75
|
+
diagnostics
|
|
76
|
+
} = generateTypes(files, generateOptions, ts);
|
|
77
|
+
|
|
78
|
+
const errors = diagnostics.filter(d => d.category === ts.DiagnosticCategory.Error);
|
|
79
|
+
|
|
80
|
+
if (errors.length) {
|
|
81
|
+
|
|
82
|
+
const formatHost = {
|
|
83
|
+
getNewLine() {
|
|
84
|
+
return os.EOL;
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
getCurrentDirectory() {
|
|
88
|
+
return process.cwd();
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
getCanonicalFileName(fileName) {
|
|
92
|
+
return fileName;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
console.log('Done with errors:');
|
|
97
|
+
console.log();
|
|
98
|
+
console.log(
|
|
99
|
+
ts.formatDiagnosticsWithColorAndContext(errors, formatHost)
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
return 5;
|
|
103
|
+
}
|
|
74
104
|
|
|
75
105
|
console.log('Done.');
|
|
106
|
+
|
|
107
|
+
return 0;
|
|
76
108
|
}
|
|
77
109
|
|
|
78
110
|
function isDir(fileName) {
|
|
@@ -103,7 +135,9 @@ function getTypescript() {
|
|
|
103
135
|
}
|
|
104
136
|
}
|
|
105
137
|
|
|
106
|
-
run().
|
|
138
|
+
run().then(errorCode => {
|
|
139
|
+
process.exit(errorCode);
|
|
140
|
+
}, err => {
|
|
107
141
|
console.error(err);
|
|
108
142
|
|
|
109
143
|
process.exit(1);
|
package/lib/generate-types.js
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
|
|
3
|
-
import ts from 'typescript';
|
|
4
|
-
|
|
5
3
|
import preTransform from './pre-transform.js';
|
|
6
4
|
|
|
7
5
|
import postTransform from './post-transform.js';
|
|
8
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @typedef { import('typescript').CompilerOptions } CompilerOptions
|
|
9
|
+
* @typedef { import('typescript').createCompilerHost } createCompilerHost
|
|
10
|
+
* @typedef { import('typescript').createProgram } createProgram
|
|
11
|
+
* @typedef { {
|
|
12
|
+
* createCompilerHost: createCompilerHost,
|
|
13
|
+
* createProgram: createProgram
|
|
14
|
+
* } } TypeScript
|
|
15
|
+
*/
|
|
9
16
|
|
|
10
17
|
/**
|
|
11
|
-
* @param {string[]} fileNames
|
|
12
|
-
* @param {
|
|
18
|
+
* @param { string[] } fileNames
|
|
19
|
+
* @param { CompilerOptions } options
|
|
20
|
+
* @param { TypeScript } ts
|
|
13
21
|
*/
|
|
14
|
-
export default function generateTypes(fileNames, options) {
|
|
22
|
+
export default function generateTypes(fileNames, options, ts) {
|
|
23
|
+
|
|
24
|
+
if (!ts) {
|
|
25
|
+
throw new Error('must provide <ts=TypeScript>');
|
|
26
|
+
}
|
|
15
27
|
|
|
16
28
|
const names = new Set(fileNames.map((p) => path.resolve(p)));
|
|
17
29
|
|
|
@@ -30,18 +42,18 @@ export default function generateTypes(fileNames, options) {
|
|
|
30
42
|
// @ts-expect-error
|
|
31
43
|
let src = host._readFile(fileName);
|
|
32
44
|
|
|
33
|
-
isVerbose(options, fileName) && console.debug('[generate-types] [pre]', fileName, src);
|
|
34
|
-
|
|
35
45
|
if (names.has(path.resolve(fileName))) {
|
|
46
|
+
isVerbose(options, fileName) && console.debug('[generate-types] [pre]', fileName, src);
|
|
47
|
+
|
|
36
48
|
try {
|
|
37
49
|
src = preTransform(src);
|
|
38
50
|
} catch (err) {
|
|
39
|
-
console.error(`failed to parse ${fileName}
|
|
51
|
+
console.error(`[generate-types] [pre] failed to parse ${fileName} with contents ${src}`, err);
|
|
40
52
|
throw err;
|
|
41
53
|
}
|
|
42
|
-
}
|
|
43
54
|
|
|
44
|
-
|
|
55
|
+
isVerbose(options, fileName) && console.debug('[generate-types] [pre] [generated]', fileName, src);
|
|
56
|
+
}
|
|
45
57
|
|
|
46
58
|
return src;
|
|
47
59
|
};
|
|
@@ -55,7 +67,7 @@ export default function generateTypes(fileNames, options) {
|
|
|
55
67
|
try {
|
|
56
68
|
text = postTransform(text);
|
|
57
69
|
} catch (err) {
|
|
58
|
-
console.error(`failed to parse ${fileName}
|
|
70
|
+
console.error(`[generate-types] [post] failed to parse ${fileName} with contents ${text}`, err);
|
|
59
71
|
throw err;
|
|
60
72
|
}
|
|
61
73
|
|
|
@@ -67,7 +79,7 @@ export default function generateTypes(fileNames, options) {
|
|
|
67
79
|
|
|
68
80
|
const program = ts.createProgram(fileNames, options, host);
|
|
69
81
|
|
|
70
|
-
program.emit();
|
|
82
|
+
return program.emit();
|
|
71
83
|
}
|
|
72
84
|
|
|
73
85
|
function isVerbose(options, fileName) {
|
|
@@ -76,5 +88,9 @@ function isVerbose(options, fileName) {
|
|
|
76
88
|
return options.verbose;
|
|
77
89
|
}
|
|
78
90
|
|
|
91
|
+
if (typeof options.verbose === 'undefined') {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
79
95
|
return fileName.includes(options.verbose);
|
|
80
96
|
}
|
package/lib/post-transform.js
CHANGED
|
@@ -416,7 +416,7 @@ export default function transform(src) {
|
|
|
416
416
|
return;
|
|
417
417
|
}
|
|
418
418
|
|
|
419
|
-
const replacements = overloads.slice().
|
|
419
|
+
const replacements = overloads.slice().map(overload => {
|
|
420
420
|
|
|
421
421
|
const {
|
|
422
422
|
comment,
|
|
@@ -502,7 +502,7 @@ declare function p${
|
|
|
502
502
|
|
|
503
503
|
// remove full line including the non-TS tag
|
|
504
504
|
if (
|
|
505
|
-
/class|constructor|template|method|typedef|property|this|overlord/.test(tag.name) ||
|
|
505
|
+
/class|constructor|template|method|typedef|property|this|overlord|overload/.test(tag.name) ||
|
|
506
506
|
tag.param?.name?.includes('.')
|
|
507
507
|
) {
|
|
508
508
|
replacements.push([ { start: tag.start - 4, end: tag.end } ]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bio-dts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Generate sane and clean types from JavaScript sources",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": "bin/cmd.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"lint": "eslint .",
|
|
15
15
|
"check-types": "tsc --noEmit",
|
|
16
16
|
"test": "mocha test",
|
|
17
|
-
"test:dts": "node bin/cmd.js --outDir
|
|
17
|
+
"test:dts": "node bin/cmd.js --outDir test/fixtures/snapshots -r test/fixtures"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
@@ -23,20 +23,20 @@
|
|
|
23
23
|
"author": "Nico Rehwaldt",
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@babel/parser": "^7.
|
|
27
|
-
"recast": "^0.23.
|
|
26
|
+
"@babel/parser": "^7.26.3",
|
|
27
|
+
"recast": "^0.23.9",
|
|
28
28
|
"tiny-glob": "^0.2.9"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@babel/types": "^7.23.9",
|
|
32
|
-
"@types/mocha": "^10.0.
|
|
33
|
-
"@types/node": "^20.
|
|
34
|
-
"chai": "^5.1.
|
|
32
|
+
"@types/mocha": "^10.0.10",
|
|
33
|
+
"@types/node": "^20.17.12",
|
|
34
|
+
"chai": "^5.1.2",
|
|
35
35
|
"eslint": "^8.56.0",
|
|
36
36
|
"eslint-plugin-bpmn-io": "^1.0.0",
|
|
37
|
-
"mocha": "^10.2
|
|
38
|
-
"npm-run-all2": "^
|
|
39
|
-
"typescript": "^5.
|
|
37
|
+
"mocha": "^10.8.2",
|
|
38
|
+
"npm-run-all2": "^7.0.2",
|
|
39
|
+
"typescript": "^5.7.3"
|
|
40
40
|
},
|
|
41
41
|
"files": [
|
|
42
42
|
"bin",
|