dts-gen 0.5.7 → 0.6.1
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 +10 -4
- package/SECURITY.md +41 -0
- package/bin/lib/definitely-typed.js +59 -21
- package/bin/lib/index.js +9 -29
- package/bin/lib/names.js +12 -0
- package/bin/lib/run.js +18 -19
- package/package.json +3 -3
- package/templates/module.d.ts +2 -2
- package/tslint.json +3 -2
- package/.npmignore +0 -7
- package/bin/browser/browser.js +0 -23
- package/bin/browser-bundle.js +0 -34
- package/bin/lib/browser.js +0 -19
- package/bin/tests/test.js +0 -60
- package/debug.log +0 -4
package/README.md
CHANGED
|
@@ -120,7 +120,7 @@ When using `--expression`, the `--name` parameter changes the global name given
|
|
|
120
120
|
At most one output option may be specified.
|
|
121
121
|
If no output option is given, the default is `--file`.
|
|
122
122
|
|
|
123
|
-
#### `--file` (`-f`)
|
|
123
|
+
#### `--file` (`-f`)
|
|
124
124
|
|
|
125
125
|
Example: `--file my.d.ts`
|
|
126
126
|
|
|
@@ -139,6 +139,10 @@ Creates a folder suitable for submitting to DefinitelyTyped.
|
|
|
139
139
|
Before submitting, you'll need to write some tests and fill in the header comments in `index.d.ts`.
|
|
140
140
|
You may change the name of the folder by specifing an argument, otherwise the folder name will be inferred.
|
|
141
141
|
|
|
142
|
+
1. Clone [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped).
|
|
143
|
+
2. `cd DefinitelyTyped/types`
|
|
144
|
+
3. Run dts-gen; eg `dts-gen --dt --name yargs`
|
|
145
|
+
|
|
142
146
|
#### `--overwrite` (`-o`)
|
|
143
147
|
|
|
144
148
|
By default, `dts-gen` will not overwrite existing files on disk.
|
|
@@ -146,7 +150,9 @@ Specify `-o` to allow overwriting of files.
|
|
|
146
150
|
|
|
147
151
|
# Browser
|
|
148
152
|
|
|
149
|
-
|
|
153
|
+
<table><td>
|
|
154
|
+
📝 <strong>Note:</strong> Browser support in progress! This is not quite ready yet.
|
|
155
|
+
</td></table>
|
|
150
156
|
|
|
151
157
|
To generate a definition file for a browser-only module, we'll use the in-browser popup window.
|
|
152
158
|
This works by injecting the `dts-gen` script into the current page by adding it as a `script` tag.
|
|
@@ -155,8 +161,8 @@ Run this in your browser's developer console:
|
|
|
155
161
|
```js
|
|
156
162
|
var s = document.createElement('script'); s.src = 'https://unpkg.com/dts-gen/bin/browser-bundle.js'; document.body.appendChild(s);
|
|
157
163
|
```
|
|
158
|
-
If this script is blocked by the same-origin content policy of the page, go to
|
|
159
|
-
and copy the script text shown on the page.
|
|
164
|
+
If this script is blocked by the same-origin content policy of the page, go to
|
|
165
|
+
https://microsoft.github.io/dts-gen/manual.html and copy the script text shown on the page.
|
|
160
166
|
Paste this into the browser console.
|
|
161
167
|
|
|
162
168
|
When the window appears, enter an identifier or expression in the textbox and click "Generate".
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<!-- BEGIN MICROSOFT SECURITY.MD V0.0.7 BLOCK -->
|
|
2
|
+
|
|
3
|
+
## Security
|
|
4
|
+
|
|
5
|
+
Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/).
|
|
6
|
+
|
|
7
|
+
If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below.
|
|
8
|
+
|
|
9
|
+
## Reporting Security Issues
|
|
10
|
+
|
|
11
|
+
**Please do not report security vulnerabilities through public GitHub issues.**
|
|
12
|
+
|
|
13
|
+
Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report).
|
|
14
|
+
|
|
15
|
+
If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey).
|
|
16
|
+
|
|
17
|
+
You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc).
|
|
18
|
+
|
|
19
|
+
Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
|
|
20
|
+
|
|
21
|
+
* Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
|
|
22
|
+
* Full paths of source file(s) related to the manifestation of the issue
|
|
23
|
+
* The location of the affected source code (tag/branch/commit or direct URL)
|
|
24
|
+
* Any special configuration required to reproduce the issue
|
|
25
|
+
* Step-by-step instructions to reproduce the issue
|
|
26
|
+
* Proof-of-concept or exploit code (if possible)
|
|
27
|
+
* Impact of the issue, including how an attacker might exploit the issue
|
|
28
|
+
|
|
29
|
+
This information will help us triage your report more quickly.
|
|
30
|
+
|
|
31
|
+
If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs.
|
|
32
|
+
|
|
33
|
+
## Preferred Languages
|
|
34
|
+
|
|
35
|
+
We prefer all communications to be in English.
|
|
36
|
+
|
|
37
|
+
## Policy
|
|
38
|
+
|
|
39
|
+
Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd).
|
|
40
|
+
|
|
41
|
+
<!-- END MICROSOFT SECURITY.MD BLOCK -->
|
|
@@ -1,59 +1,63 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
5
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
6
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) :
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
9
|
});
|
|
9
10
|
};
|
|
10
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
12
|
const fs_1 = require("fs");
|
|
12
13
|
const http_1 = require("http");
|
|
14
|
+
const https_1 = require("https");
|
|
15
|
+
const os_1 = require("os");
|
|
16
|
+
const parseGitConfig = require("parse-git-config");
|
|
13
17
|
const path_1 = require("path");
|
|
18
|
+
const url_1 = require("url");
|
|
19
|
+
const names_1 = require("./names");
|
|
14
20
|
function writeDefinitelyTypedPackage(indexDtsContent, packageName, overwrite) {
|
|
15
|
-
const
|
|
21
|
+
const dtName = names_1.getDTName(packageName);
|
|
22
|
+
const packageDir = path_1.join("types", dtName);
|
|
16
23
|
// Check for overwrite
|
|
17
24
|
if (!overwrite) {
|
|
18
25
|
if (fs_1.existsSync(packageDir)) {
|
|
19
|
-
console.log(`Directory ${packageDir} already exists and
|
|
26
|
+
console.log(`Directory ${packageDir} already exists and --overwrite was not specified; exiting.`);
|
|
20
27
|
process.exit(2);
|
|
21
28
|
}
|
|
22
29
|
}
|
|
23
30
|
if (!fs_1.existsSync(packageDir)) {
|
|
24
31
|
fs_1.mkdirSync(packageDir);
|
|
25
32
|
}
|
|
26
|
-
run(indexDtsContent, packageName, packageDir).catch(e => {
|
|
33
|
+
run(indexDtsContent, packageName, dtName, packageDir).catch(e => {
|
|
27
34
|
console.error(e);
|
|
28
35
|
process.exit(1);
|
|
29
36
|
});
|
|
30
37
|
}
|
|
31
38
|
exports.default = writeDefinitelyTypedPackage;
|
|
32
|
-
function run(indexDtsContent, packageName, packageDir) {
|
|
39
|
+
function run(indexDtsContent, packageName, dtName, packageDir) {
|
|
33
40
|
return __awaiter(this, void 0, void 0, function* () {
|
|
34
41
|
const files = [
|
|
35
42
|
["index.d.ts", yield getIndex(indexDtsContent, packageName)],
|
|
36
|
-
[`${
|
|
37
|
-
["tsconfig.json", `${JSON.stringify(getTSConfig(
|
|
38
|
-
["tslint.json", '{ "extends": "dtslint/dt.json" }\n'],
|
|
43
|
+
[`${dtName}-tests.ts`, ""],
|
|
44
|
+
["tsconfig.json", `${JSON.stringify(getTSConfig(dtName), undefined, 4)}\n`],
|
|
45
|
+
["tslint.json", '{ "extends": "@definitelytyped/dtslint/dt.json" }\n'],
|
|
39
46
|
];
|
|
40
47
|
for (const [name, text] of files) {
|
|
41
|
-
|
|
48
|
+
fs_1.writeFileSync(path_1.join(packageDir, name), text, "utf-8");
|
|
42
49
|
}
|
|
43
50
|
});
|
|
44
51
|
}
|
|
45
52
|
function getIndex(content, packageName) {
|
|
46
|
-
|
|
47
|
-
return `${yield getHeader(packageName)}\n\n${content}`;
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
function getHeader(packageName) {
|
|
53
|
+
var _a, _b, _c;
|
|
51
54
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
55
|
let version = "x.x";
|
|
53
56
|
let project = "https://github.com/baz/foo " +
|
|
54
|
-
"(Does not have to be to GitHub,
|
|
57
|
+
"(Does not have to be to GitHub, " +
|
|
58
|
+
"but prefer linking to a source code repository rather than to a project website.)";
|
|
55
59
|
try {
|
|
56
|
-
const reg = JSON.parse(yield loadString(`
|
|
60
|
+
const reg = JSON.parse(yield loadString(`https://registry.npmjs.org/${packageName}`));
|
|
57
61
|
const { latest } = reg["dist-tags"];
|
|
58
62
|
const { homepage } = reg.versions[latest];
|
|
59
63
|
version = latest.split(".").slice(0, 2).join("."); // Just major.minor
|
|
@@ -63,19 +67,53 @@ function getHeader(packageName) {
|
|
|
63
67
|
catch (e) {
|
|
64
68
|
console.warn(`Warning: Could not retrieve version/homepage information: ${e.message}`);
|
|
65
69
|
}
|
|
70
|
+
let authorName = 'My Self';
|
|
71
|
+
try {
|
|
72
|
+
const globalGitConfig = parseGitConfig.sync({ cwd: os_1.homedir(), path: '.gitconfig' });
|
|
73
|
+
if (globalGitConfig.user && globalGitConfig.user.name) {
|
|
74
|
+
authorName = globalGitConfig.user.name;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (e) {
|
|
78
|
+
console.warn(`Warning: Could not retrieve author name: ${e.message}`);
|
|
79
|
+
}
|
|
80
|
+
let authorUserName = 'me';
|
|
81
|
+
try {
|
|
82
|
+
const repoGitConfig = parseGitConfig.sync({ path: path_1.join('.git', 'config') });
|
|
83
|
+
if (repoGitConfig['remote "origin"'] && repoGitConfig['remote "origin"'].url) {
|
|
84
|
+
const url = url_1.parse(repoGitConfig['remote "origin"'].url);
|
|
85
|
+
if (url.hostname === 'github.com' && url.pathname) {
|
|
86
|
+
authorUserName = url.pathname.split('/')[1] || authorUserName;
|
|
87
|
+
}
|
|
88
|
+
else if ((_a = url.pathname) === null || _a === void 0 ? void 0 : _a.startsWith('git@github.com')) {
|
|
89
|
+
authorUserName = ((_c = (_b = url.pathname.split(':')) === null || _b === void 0 ? void 0 : _b[1].split('/')) === null || _c === void 0 ? void 0 : _c[0]) || authorUserName;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
console.warn(`Warning: Could not retrieve author's user name: ${e.message}`);
|
|
95
|
+
}
|
|
96
|
+
const authorUrl = url_1.format({
|
|
97
|
+
protocol: 'https',
|
|
98
|
+
hostname: 'github.com',
|
|
99
|
+
pathname: authorUserName,
|
|
100
|
+
});
|
|
66
101
|
return `// Type definitions for ${packageName} ${version}
|
|
67
102
|
// Project: ${project}
|
|
68
|
-
// Definitions by:
|
|
69
|
-
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
103
|
+
// Definitions by: ${authorName} <${authorUrl}>
|
|
104
|
+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
105
|
+
|
|
106
|
+
${content}`;
|
|
70
107
|
});
|
|
71
108
|
}
|
|
72
|
-
function getTSConfig(
|
|
109
|
+
function getTSConfig(dtName) {
|
|
73
110
|
return {
|
|
74
111
|
compilerOptions: {
|
|
75
112
|
module: "commonjs",
|
|
76
113
|
lib: ["es6"],
|
|
77
114
|
noImplicitAny: true,
|
|
78
115
|
noImplicitThis: true,
|
|
116
|
+
strictFunctionTypes: true,
|
|
79
117
|
strictNullChecks: true,
|
|
80
118
|
baseUrl: "../",
|
|
81
119
|
typeRoots: ["../"],
|
|
@@ -85,13 +123,13 @@ function getTSConfig(packageName) {
|
|
|
85
123
|
},
|
|
86
124
|
files: [
|
|
87
125
|
"index.d.ts",
|
|
88
|
-
`${
|
|
126
|
+
`${dtName}-tests.ts`,
|
|
89
127
|
],
|
|
90
128
|
};
|
|
91
129
|
}
|
|
92
130
|
function loadString(url) {
|
|
93
131
|
return new Promise((resolve, reject) => {
|
|
94
|
-
|
|
132
|
+
https_1.get(url, res => {
|
|
95
133
|
if (res.statusCode !== 200) {
|
|
96
134
|
return reject(new Error(`HTTP Error ${res.statusCode}: ${http_1.STATUS_CODES[res.statusCode || 500]} for ${url}`));
|
|
97
135
|
}
|
package/bin/lib/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateIdentifierDeclarationFile = exports.generateModuleDeclarationFile = void 0;
|
|
3
4
|
const dom = require("dts-dom");
|
|
4
5
|
const dts_dom_1 = require("dts-dom");
|
|
5
6
|
const ts = require("typescript");
|
|
7
|
+
const names_1 = require("./names");
|
|
6
8
|
const builtins = {
|
|
7
9
|
Date,
|
|
8
10
|
RegExp,
|
|
@@ -11,7 +13,7 @@ const builtins = {
|
|
|
11
13
|
};
|
|
12
14
|
function forceAsIdentifier(s) {
|
|
13
15
|
// TODO: Make this more comprehensive
|
|
14
|
-
return s.replace(/-/g, '_');
|
|
16
|
+
return names_1.getDTName(s.replace(/-/g, '_'));
|
|
15
17
|
}
|
|
16
18
|
function getValueTypes(value) {
|
|
17
19
|
if (typeof value === 'object') {
|
|
@@ -88,7 +90,7 @@ function getKeysOfObject(obj) {
|
|
|
88
90
|
return keys;
|
|
89
91
|
}
|
|
90
92
|
function isVisitableName(s) {
|
|
91
|
-
return (s[0] !== '_') && (["caller", "arguments", "constructor", "super_"].indexOf(s) < 0);
|
|
93
|
+
return (s[0] !== '_') && (["caller", "arguments", "constructor", "super_", "prototype"].indexOf(s) < 0);
|
|
92
94
|
}
|
|
93
95
|
function isLegalIdentifier(s) {
|
|
94
96
|
if (s.length === 0) {
|
|
@@ -131,7 +133,6 @@ function getTopLevelDeclarations(name, obj) {
|
|
|
131
133
|
let primaryDecl;
|
|
132
134
|
if (isClasslike(obj)) {
|
|
133
135
|
const cls = dom.create.class(name);
|
|
134
|
-
getClassInstanceMembers(obj).forEach(m => cls.members.push(m));
|
|
135
136
|
getClassPrototypeMembers(obj).forEach(m => cls.members.push(m));
|
|
136
137
|
cls.members.push(dom.create.constructor(funcType[0]));
|
|
137
138
|
cls.members.sort(declarationComparer);
|
|
@@ -174,7 +175,8 @@ function getTopLevelDeclarations(name, obj) {
|
|
|
174
175
|
if (typeof simpleType === 'string' || simpleType.kind === 'name' || simpleType.kind === 'array') {
|
|
175
176
|
const result = dom.create.const(name, simpleType);
|
|
176
177
|
if (simpleType === 'string') {
|
|
177
|
-
|
|
178
|
+
const preview = `"${simpleType.substr(0, 100)}${simpleType.length > 100 ? '...' : ''}"`;
|
|
179
|
+
result.comment = "Value of string: " + preview;
|
|
178
180
|
}
|
|
179
181
|
return [result];
|
|
180
182
|
}
|
|
@@ -281,29 +283,6 @@ function getClassPrototypeMembers(ctor) {
|
|
|
281
283
|
return (s === 'constructor') || (s[0] === '_');
|
|
282
284
|
}
|
|
283
285
|
}
|
|
284
|
-
// Parses assignments to 'this.x' in the constructor into class property declarations
|
|
285
|
-
function getClassInstanceMembers(ctor) {
|
|
286
|
-
if (isNativeFunction(ctor)) {
|
|
287
|
-
return [];
|
|
288
|
-
}
|
|
289
|
-
const members = [];
|
|
290
|
-
function visit(node) {
|
|
291
|
-
switch (node.kind) {
|
|
292
|
-
case ts.SyntaxKind.BinaryExpression:
|
|
293
|
-
if (node.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
294
|
-
const lhs = node.left;
|
|
295
|
-
if (lhs.kind === ts.SyntaxKind.PropertyAccessExpression) {
|
|
296
|
-
if (lhs.expression.kind === ts.SyntaxKind.ThisKeyword) {
|
|
297
|
-
members.push(dts_dom_1.create.property(lhs.name.getText(), dom.type.any, dom.DeclarationFlags.None));
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
break;
|
|
302
|
-
}
|
|
303
|
-
ts.forEachChild(node, visit);
|
|
304
|
-
}
|
|
305
|
-
return members;
|
|
306
|
-
}
|
|
307
286
|
function declarationComparer(left, right) {
|
|
308
287
|
if (left.kind === right.kind) {
|
|
309
288
|
return left.name > right.name ? 1 : left.name < right.name ? -1 : 0;
|
|
@@ -363,8 +342,9 @@ function inferParameterType(_fn, _param) {
|
|
|
363
342
|
// TODO: Inspect function body for clues
|
|
364
343
|
return dom.type.any;
|
|
365
344
|
}
|
|
345
|
+
const stringifyFunction = Function.prototype.call.bind(Function.prototype.toString);
|
|
366
346
|
function parseFunctionBody(fn) {
|
|
367
|
-
const setup = `const myFn = ${fn
|
|
347
|
+
const setup = `const myFn = ${stringifyFunction(fn)};`;
|
|
368
348
|
const srcFile = ts.createSourceFile('test.ts', setup, ts.ScriptTarget.Latest, true);
|
|
369
349
|
const statement = srcFile.statements[0];
|
|
370
350
|
const decl = statement.declarationList.declarations[0];
|
|
@@ -372,5 +352,5 @@ function parseFunctionBody(fn) {
|
|
|
372
352
|
return init;
|
|
373
353
|
}
|
|
374
354
|
function isNativeFunction(fn) {
|
|
375
|
-
return fn
|
|
355
|
+
return stringifyFunction(fn).indexOf('{ [native code] }') > 0;
|
|
376
356
|
}
|
package/bin/lib/names.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDTName = void 0;
|
|
4
|
+
function getDTName(s) {
|
|
5
|
+
if (s.indexOf('@') === 0 && s.indexOf('/') !== -1) {
|
|
6
|
+
// we have a scoped module, e.g. @bla/foo
|
|
7
|
+
// which should be converted to bla__foo
|
|
8
|
+
s = s.substr(1).replace('/', '__');
|
|
9
|
+
}
|
|
10
|
+
return s;
|
|
11
|
+
}
|
|
12
|
+
exports.getDTName = getDTName;
|
package/bin/lib/run.js
CHANGED
|
@@ -37,21 +37,22 @@ try {
|
|
|
37
37
|
if (+!!args.dt + +!!args.file + +!!args.stdout > 1) {
|
|
38
38
|
throw new ArgsError('Cannot specify more than one output mode');
|
|
39
39
|
}
|
|
40
|
-
if (+!!args.identifier + +!!args.expression + +!!args.module + +!!args['expression-file'] + +!!args.template
|
|
40
|
+
if (+!!args.identifier + +!!args.expression + +!!args.module + +!!args['expression-file'] + +!!args.template
|
|
41
|
+
!== 1) {
|
|
41
42
|
throw new ArgsError('Must specify exactly one input');
|
|
42
43
|
}
|
|
43
44
|
if (typeof args.name === 'boolean')
|
|
44
|
-
throw new ArgsError('Must specify a value for "
|
|
45
|
+
throw new ArgsError('Must specify a value for "--name"');
|
|
45
46
|
if (typeof args.identifier === 'boolean')
|
|
46
|
-
throw new ArgsError('Must specify a value for "
|
|
47
|
+
throw new ArgsError('Must specify a value for "--identifier"');
|
|
47
48
|
if (typeof args.module === 'boolean')
|
|
48
|
-
throw new ArgsError('Must specify a value for "
|
|
49
|
+
throw new ArgsError('Must specify a value for "--module"');
|
|
49
50
|
if (args.overwrite !== undefined && args.overwrite !== true)
|
|
50
|
-
throw new ArgsError('
|
|
51
|
+
throw new ArgsError('--overwrite does not accept an argument');
|
|
51
52
|
let name;
|
|
52
53
|
if (args.module) {
|
|
53
54
|
if (args.name)
|
|
54
|
-
throw new ArgsError('Cannot use
|
|
55
|
+
throw new ArgsError('Cannot use --name with --module');
|
|
55
56
|
name = args.module;
|
|
56
57
|
module.paths.unshift(process.cwd() + '/node_modules');
|
|
57
58
|
result = guess.generateModuleDeclarationFile(args.module, require(args.module));
|
|
@@ -62,7 +63,7 @@ try {
|
|
|
62
63
|
}
|
|
63
64
|
else if (args['expression-file']) {
|
|
64
65
|
if (args.name)
|
|
65
|
-
throw new ArgsError('Cannot use
|
|
66
|
+
throw new ArgsError('Cannot use --name with --expression-file');
|
|
66
67
|
const filename = args['expression-file'];
|
|
67
68
|
name = path.basename(filename, path.extname(filename)).replace(/[^A-Za-z0-9]/g, '_');
|
|
68
69
|
module.paths.unshift(process.cwd() + '/node_modules');
|
|
@@ -71,7 +72,7 @@ try {
|
|
|
71
72
|
}
|
|
72
73
|
else if (args.identifier) {
|
|
73
74
|
if (args.name)
|
|
74
|
-
throw new ArgsError('Cannot use
|
|
75
|
+
throw new ArgsError('Cannot use --name with --identifier');
|
|
75
76
|
if (args.module || args.expression)
|
|
76
77
|
throw new ArgsError('Cannot specify more than one input');
|
|
77
78
|
name = args.identifier;
|
|
@@ -82,7 +83,7 @@ try {
|
|
|
82
83
|
throw new ArgsError('Needs a name');
|
|
83
84
|
name = args.name;
|
|
84
85
|
if (args.module || args.expression)
|
|
85
|
-
throw new ArgsError('Cannot mix
|
|
86
|
+
throw new ArgsError('Cannot mix --template with --module or --expression');
|
|
86
87
|
result = getTemplate(args.template);
|
|
87
88
|
}
|
|
88
89
|
else {
|
|
@@ -95,18 +96,12 @@ try {
|
|
|
95
96
|
console.log(result);
|
|
96
97
|
}
|
|
97
98
|
else {
|
|
98
|
-
let filename;
|
|
99
|
-
if (typeof args.file === 'boolean' || args.file === undefined) {
|
|
100
|
-
filename = name + '.d.ts';
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
filename = args.file;
|
|
104
|
-
}
|
|
99
|
+
let filename = typeof args.file === 'boolean' || args.file === undefined ? name + '.d.ts' : args.file;
|
|
105
100
|
if (!filename.endsWith('.d.ts')) {
|
|
106
101
|
filename = filename + '.d.ts';
|
|
107
102
|
}
|
|
108
103
|
if (!args.overwrite && fs.existsSync(filename)) {
|
|
109
|
-
console.error(`File ${filename} already exists and
|
|
104
|
+
console.error(`File ${filename} already exists and --overwrite was not specified; exiting.`);
|
|
110
105
|
process.exit(2);
|
|
111
106
|
}
|
|
112
107
|
fs.writeFileSync(filename, prependOurHeader(result), 'utf-8');
|
|
@@ -121,8 +116,9 @@ catch (e) {
|
|
|
121
116
|
process.exit(1);
|
|
122
117
|
}
|
|
123
118
|
else if (e.code === 'MODULE_NOT_FOUND') {
|
|
124
|
-
console.
|
|
125
|
-
|
|
119
|
+
console.error(`Error loading module "${args.module}".\n` +
|
|
120
|
+
getErrorMessageFirstLine(e).replace(/'/g, '"') + '.\n' +
|
|
121
|
+
`Please install missing module and try again.`);
|
|
126
122
|
process.exit(1);
|
|
127
123
|
}
|
|
128
124
|
else {
|
|
@@ -165,3 +161,6 @@ function getTemplate(templateName) {
|
|
|
165
161
|
function allTemplateNames() {
|
|
166
162
|
return fs.readdirSync(templatesDirectory).map(t => t.slice(0, t.length - ".d.ts".length)).join(", ");
|
|
167
163
|
}
|
|
164
|
+
function getErrorMessageFirstLine(error) {
|
|
165
|
+
return error.message.split('\n', 1)[0];
|
|
166
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dts-gen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "TypeScript Definition File Generator",
|
|
5
5
|
"main": "bin/lib/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"dts-dom": "latest",
|
|
11
11
|
"parse-git-config": "^1.1.1",
|
|
12
|
-
"typescript": "^
|
|
12
|
+
"typescript": "^3.5.1",
|
|
13
13
|
"yargs": "^4.8.1"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"test": "mocha bin/tests/test.js",
|
|
34
34
|
"build": "tsc",
|
|
35
35
|
"build-browser": "tsc -p browser && webpack",
|
|
36
|
-
"lint": "tslint --
|
|
36
|
+
"lint": "tslint --project tsconfig.json --format stylish"
|
|
37
37
|
},
|
|
38
38
|
"repository": {
|
|
39
39
|
"type": "git",
|
package/templates/module.d.ts
CHANGED
package/tslint.json
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
"extends": "tslint:latest",
|
|
3
3
|
"rules": {
|
|
4
4
|
"arrow-parens": [true, "ban-single-arg-parens"],
|
|
5
|
-
"indent": [true, "
|
|
5
|
+
"indent": [true, "spaces"],
|
|
6
6
|
"interface-name": [true, "never-prefix"],
|
|
7
7
|
"member-access": [true, "no-public"],
|
|
8
8
|
|
|
9
9
|
"ban-types": false,
|
|
10
10
|
"curly": false,
|
|
11
11
|
"member-ordering": false,
|
|
12
|
+
"no-duplicate-imports": false,
|
|
12
13
|
"no-angle-bracket-type-assertion": false,
|
|
13
14
|
"no-bitwise": false,
|
|
14
15
|
"no-console": false,
|
|
@@ -20,4 +21,4 @@
|
|
|
20
21
|
"quotemark": false,
|
|
21
22
|
"variable-name": false
|
|
22
23
|
}
|
|
23
|
-
}
|
|
24
|
+
}
|
package/.npmignore
DELETED
package/bin/browser/browser.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var guess = require("../lib");
|
|
3
|
-
(function () {
|
|
4
|
-
"use strict";
|
|
5
|
-
var css = "position: fixed;\n\tdisplay: inline-block;\n\tleft: 10%;\n\ttop: 10%;\n\twidth: auto;\n\theight: auto;\n\tpadding: 2em;\n\tmargin-left: auto;\n\tmargin-right: auto;\n\tborder: solid black 2px;\n\tz-index: 100000;\n\tbackground-color: #294E80;\n color: white;\n\ttext-align: center;";
|
|
6
|
-
var displayWindow = document.createElement('div');
|
|
7
|
-
displayWindow.setAttribute('id', 'tsguess-window');
|
|
8
|
-
displayWindow.setAttribute('style', css);
|
|
9
|
-
displayWindow.innerHTML = "\n\t\t<div>tsguess Browser Type Guesser</div>\n\t\t <div style=\"font-family: monospace; text-align: left\">\n <form onsubmit=\"return false;\">\n <input type=\"text\" length=\"40\" id=\"tsguess-input\" placeholder=\"Enter any JavaScript identifier or expression\" />\n <input type=\"submit\" value=\"Generate\" default id=\"tsguess-generate\" />\n </form>\n\t\t </div>\n\t\t <textarea id=\"tsguess-output\" style=\"font-family: monospace; align: center; border: solid black 2px;\" rows=\"40\" cols=\"120\" placeholder=\"Generated .d.ts content will appear here\"></textarea>\n </div>\n\t";
|
|
10
|
-
window.setTimeout(function () {
|
|
11
|
-
var button = document.getElementById('tsguess-generate');
|
|
12
|
-
var input = document.getElementById('tsguess-input');
|
|
13
|
-
var output = document.getElementById('tsguess-output');
|
|
14
|
-
button.addEventListener("click", function () {
|
|
15
|
-
output.value = guess.generateIdentifierDeclarationFile(input.value, eval(input.value));
|
|
16
|
-
});
|
|
17
|
-
window['infer'] = function (name) {
|
|
18
|
-
input.value = name;
|
|
19
|
-
button.click();
|
|
20
|
-
};
|
|
21
|
-
}, 10);
|
|
22
|
-
document.body.appendChild(displayWindow);
|
|
23
|
-
})();
|