@rushstack/typings-generator 0.6.0 → 0.6.4

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 CHANGED
@@ -1,132 +1,132 @@
1
- # @rushstack/typings-generator
2
-
3
- ## Installation
4
-
5
- `npm install @rushstack/typings-generator --save-dev`
6
-
7
- ## Overview
8
-
9
- This is a utility for generating typings for non-TS files. It can operate in either a single-run mode or
10
- a watch mode. It is designed to generate `.d.ts` files with a specified generation function for all files matching
11
- specified file extensions, with an option to ignore individual files.
12
-
13
- ## Usage
14
-
15
- ```TypeScript
16
- import { TypingsGenerator } from '@rushstack/typings-generator';
17
-
18
- const typingsGenerator: TypingsGenerator = new TypingsGenerator({
19
- srcFolder: '/repo/package/src',
20
- generatedTsFolder: '/repo/package/temp/generated-typings',
21
- fileExtensions: ['file-extension'],
22
- parseAndGenerateTypings: (fileContents: string, filePath: string) => {
23
- const parsedFile = parseFile(fileContents);
24
- const typings: string = generateTypings(parsedFile);
25
- return typings;
26
- }
27
- });
28
-
29
- // To run once before a compilation:
30
- await typingsGenerator.generateTypings();
31
-
32
- // To start a watcher:
33
- await typingsGenerator.runWatcher();
34
- ```
35
-
36
- ## Options
37
-
38
- ### `srcFolder = '...'`
39
-
40
- This property is used as the source root folder for discovery of files for which typings should be generated.
41
-
42
- ### `generatedTsFolder = '...'`
43
-
44
- This property specifies the folder in which `.d.ts` files should be dropped. It is recommended
45
- that this be a folder parallel to the source folder, specified in addition to the source folder in the
46
- [`rootDirs` `tsconfig.json` option](https://www.typescriptlang.org/docs/handbook/compiler-options.html).
47
- **The folder specified by this option is emptied when the utility is invoked.**
48
-
49
- ### `fileExtensions = [...]`
50
-
51
- This property enumerates the file extensions that should be handled.
52
-
53
- ### `parseAndGenerateTypings = (fileContents: string, filePath: string) => string | Promise<string>`
54
-
55
- This property is used to specify the function that should be called on every file for which typings
56
- are being generated. In watch mode, this is called on every file creation and file update. It should
57
- return TypeScript declarations for the file it is called with.
58
-
59
- ### `terminal`
60
-
61
- Optionally provide a [Terminal](https://github.com/microsoft/rushstack/blob/master/libraries/node-core-library/src/Terminal/Terminal.ts)
62
- object for logging. If one isn't provided, logs will go to the console.
63
-
64
- ### `globsToIgnore`
65
-
66
- Optionally, provide an array of globs matching files that should be ignored. These globs are evaluated
67
- under [`srcFolder`](#srcFolder--)
68
-
69
- ## `StringValuesTypingsGenerator`
70
-
71
- There is an extension of this utility specifically for file types where typings should be a simple
72
- set of exported string values. This is useful for file types like CSS and RESX. This class takes
73
- the same options as the standard `TypingsGenerator`, with one additional option ([`exportAsDefault`](#exportAsDefault--)) and a different return value for `parseAndGenerateTypings`.
74
-
75
- ### `parseAndGenerateTypings = (fileContents: string, filePath: string) => { typings: ({ exportName: string, comment?: string })[] } | Promise<{ typings: ({ exportName: string, comment?: string })[] }>`
76
-
77
- This function should behave the same as the `parseAndGenerateTypings` function for the standard
78
- `TypingsGenerator`, except that it should return an object with a `typings` property, set to
79
- an array of objects with an `exportName` property and an optional `comment` property.
80
- See the example below.
81
-
82
- #### Example return value:
83
-
84
- ```TypeScript
85
- {
86
- typings: [
87
- {
88
- exportName: 'myExport'
89
- },
90
- {
91
- exportName: 'myOtherExport',
92
- comment: 'This is the other export'
93
- }
94
- ]
95
- }
96
- ```
97
-
98
- #### Example generated declaration file:
99
-
100
- ```TypeScript
101
- // This file was generated by a tool. Modifying it will produce unexpected behavior
102
-
103
- export declare const myExport: string;
104
-
105
- /**
106
- * This is the other export
107
- */
108
- export declare const myOtherExport: string;
109
-
110
- ```
111
-
112
- ### `exportAsDefault = true | false`
113
-
114
- If this option is set to `true`, the typings will be exported wrapped in a `default` property. This
115
- allows the file to be imported by using the `import myFile from './myFile.my-extension';` syntax instead of
116
- the `import { myExport } from './myFile.my-extension';` or the `import * as myFile from './myFile.my-extension';`
117
- syntax. This style of export is not recommended as it can prevent tree-shaking optimization.
118
-
119
- ### `exportAsDefaultInterfaceName = true | false`
120
-
121
- When `exportAsDefault` is true, this optional setting determines the interface name
122
- for the default wrapped export. For example, in the Sass Typings plugin, the interface name
123
- is set to `IExportStyles`. If not specified, the interface name will be `IExport`.
124
- (This setting is ignored when `exportAsDefault` is false).
125
-
126
- ## Links
127
-
128
- - [CHANGELOG.md](https://github.com/microsoft/rushstack/blob/master/libraries/typings-generator/CHANGELOG.md) - Find
129
- out what's new in the latest version
130
- - [API Reference](https://rushstack.io/pages/api/typings-generator/)
131
-
132
- `@rushstack/typings-generator` is part of the [Rush Stack](https://rushstack.io/) family of projects.
1
+ # @rushstack/typings-generator
2
+
3
+ ## Installation
4
+
5
+ `npm install @rushstack/typings-generator --save-dev`
6
+
7
+ ## Overview
8
+
9
+ This is a utility for generating typings for non-TS files. It can operate in either a single-run mode or
10
+ a watch mode. It is designed to generate `.d.ts` files with a specified generation function for all files matching
11
+ specified file extensions, with an option to ignore individual files.
12
+
13
+ ## Usage
14
+
15
+ ```TypeScript
16
+ import { TypingsGenerator } from '@rushstack/typings-generator';
17
+
18
+ const typingsGenerator: TypingsGenerator = new TypingsGenerator({
19
+ srcFolder: '/repo/package/src',
20
+ generatedTsFolder: '/repo/package/temp/generated-typings',
21
+ fileExtensions: ['file-extension'],
22
+ parseAndGenerateTypings: (fileContents: string, filePath: string) => {
23
+ const parsedFile = parseFile(fileContents);
24
+ const typings: string = generateTypings(parsedFile);
25
+ return typings;
26
+ }
27
+ });
28
+
29
+ // To run once before a compilation:
30
+ await typingsGenerator.generateTypings();
31
+
32
+ // To start a watcher:
33
+ await typingsGenerator.runWatcher();
34
+ ```
35
+
36
+ ## Options
37
+
38
+ ### `srcFolder = '...'`
39
+
40
+ This property is used as the source root folder for discovery of files for which typings should be generated.
41
+
42
+ ### `generatedTsFolder = '...'`
43
+
44
+ This property specifies the folder in which `.d.ts` files should be dropped. It is recommended
45
+ that this be a folder parallel to the source folder, specified in addition to the source folder in the
46
+ [`rootDirs` `tsconfig.json` option](https://www.typescriptlang.org/docs/handbook/compiler-options.html).
47
+ **The folder specified by this option is emptied when the utility is invoked.**
48
+
49
+ ### `fileExtensions = [...]`
50
+
51
+ This property enumerates the file extensions that should be handled.
52
+
53
+ ### `parseAndGenerateTypings = (fileContents: string, filePath: string) => string | Promise<string>`
54
+
55
+ This property is used to specify the function that should be called on every file for which typings
56
+ are being generated. In watch mode, this is called on every file creation and file update. It should
57
+ return TypeScript declarations for the file it is called with.
58
+
59
+ ### `terminal`
60
+
61
+ Optionally provide a [Terminal](https://github.com/microsoft/rushstack/blob/master/libraries/node-core-library/src/Terminal/Terminal.ts)
62
+ object for logging. If one isn't provided, logs will go to the console.
63
+
64
+ ### `globsToIgnore`
65
+
66
+ Optionally, provide an array of globs matching files that should be ignored. These globs are evaluated
67
+ under [`srcFolder`](#srcFolder--)
68
+
69
+ ## `StringValuesTypingsGenerator`
70
+
71
+ There is an extension of this utility specifically for file types where typings should be a simple
72
+ set of exported string values. This is useful for file types like CSS and RESX. This class takes
73
+ the same options as the standard `TypingsGenerator`, with one additional option ([`exportAsDefault`](#exportAsDefault--)) and a different return value for `parseAndGenerateTypings`.
74
+
75
+ ### `parseAndGenerateTypings = (fileContents: string, filePath: string) => { typings: ({ exportName: string, comment?: string })[] } | Promise<{ typings: ({ exportName: string, comment?: string })[] }>`
76
+
77
+ This function should behave the same as the `parseAndGenerateTypings` function for the standard
78
+ `TypingsGenerator`, except that it should return an object with a `typings` property, set to
79
+ an array of objects with an `exportName` property and an optional `comment` property.
80
+ See the example below.
81
+
82
+ #### Example return value:
83
+
84
+ ```TypeScript
85
+ {
86
+ typings: [
87
+ {
88
+ exportName: 'myExport'
89
+ },
90
+ {
91
+ exportName: 'myOtherExport',
92
+ comment: 'This is the other export'
93
+ }
94
+ ]
95
+ }
96
+ ```
97
+
98
+ #### Example generated declaration file:
99
+
100
+ ```TypeScript
101
+ // This file was generated by a tool. Modifying it will produce unexpected behavior
102
+
103
+ export declare const myExport: string;
104
+
105
+ /**
106
+ * This is the other export
107
+ */
108
+ export declare const myOtherExport: string;
109
+
110
+ ```
111
+
112
+ ### `exportAsDefault = true | false`
113
+
114
+ If this option is set to `true`, the typings will be exported wrapped in a `default` property. This
115
+ allows the file to be imported by using the `import myFile from './myFile.my-extension';` syntax instead of
116
+ the `import { myExport } from './myFile.my-extension';` or the `import * as myFile from './myFile.my-extension';`
117
+ syntax. This style of export is not recommended as it can prevent tree-shaking optimization.
118
+
119
+ ### `exportAsDefaultInterfaceName = true | false`
120
+
121
+ When `exportAsDefault` is true, this optional setting determines the interface name
122
+ for the default wrapped export. For example, in the Sass Typings plugin, the interface name
123
+ is set to `IExportStyles`. If not specified, the interface name will be `IExport`.
124
+ (This setting is ignored when `exportAsDefault` is false).
125
+
126
+ ## Links
127
+
128
+ - [CHANGELOG.md](https://github.com/microsoft/rushstack/blob/master/libraries/typings-generator/CHANGELOG.md) - Find
129
+ out what's new in the latest version
130
+ - [API Reference](https://rushstack.io/pages/api/typings-generator/)
131
+
132
+ `@rushstack/typings-generator` is part of the [Rush Stack](https://rushstack.io/) family of projects.
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.19.2"
8
+ "packageVersion": "7.19.4"
9
9
  }
10
10
  ]
11
11
  }
@@ -1 +1 @@
1
- {"version":3,"file":"StringValuesTypingsGenerator.js","sourceRoot":"","sources":["../src/StringValuesTypingsGenerator.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,2BAAyB;AAEzB,yDAAgF;AAkChF,MAAM,gCAAgC,GAAW,SAAS,CAAC;AAE3D;;;;;GAKG;AACH,MAAa,4BAA6B,SAAQ,mCAAgB;IAChE,YAAmB,OAA6C;QAC9D,KAAK,iCACA,OAAO,KACV,uBAAuB,EAAE,KAAK,EAAE,YAAoB,EAAE,QAAgB,EAAE,YAAoB,EAAE,EAAE;gBAC9F,MAAM,kBAAkB,GAAoC,MAAM,OAAO,CAAC,uBAAuB,CAC/F,YAAY,EACZ,QAAQ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ACL,CAAC;CACF;AAzDD,oEAyDC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\r\n// See LICENSE in the project root for license information.\r\n\r\nimport { EOL } from 'os';\r\n\r\nimport { ITypingsGeneratorOptions, TypingsGenerator } from './TypingsGenerator';\r\n\r\n/**\r\n * @public\r\n */\r\nexport interface IStringValueTyping {\r\n exportName: string;\r\n comment?: string;\r\n}\r\n\r\n/**\r\n * @public\r\n */\r\nexport interface IStringValueTypings {\r\n typings: IStringValueTyping[];\r\n}\r\n\r\n/**\r\n * @public\r\n */\r\nexport interface IStringValuesTypingsGeneratorOptions\r\n extends ITypingsGeneratorOptions<IStringValueTypings | undefined> {\r\n /**\r\n * Setting this option wraps the typings export in a default property.\r\n */\r\n exportAsDefault?: boolean;\r\n\r\n /**\r\n * When `exportAsDefault` is true, this optional setting determines the interface name\r\n * for the default wrapped export. Ignored when `exportAsDefault` is false.\r\n */\r\n exportAsDefaultInterfaceName?: string;\r\n}\r\n\r\nconst EXPORT_AS_DEFAULT_INTERFACE_NAME: string = 'IExport';\r\n\r\n/**\r\n * This is a simple tool that generates .d.ts files for non-TS files that can be represented as\r\n * a simple set of named string exports.\r\n *\r\n * @public\r\n */\r\nexport class StringValuesTypingsGenerator extends TypingsGenerator {\r\n public constructor(options: IStringValuesTypingsGeneratorOptions) {\r\n super({\r\n ...options,\r\n parseAndGenerateTypings: async (fileContents: string, filePath: string, relativePath: string) => {\r\n const stringValueTypings: IStringValueTypings | undefined = await options.parseAndGenerateTypings(\r\n fileContents,\r\n filePath,\r\n relativePath\r\n );\r\n\r\n if (stringValueTypings === undefined) {\r\n return;\r\n }\r\n\r\n const outputLines: string[] = [];\r\n const interfaceName: string = options.exportAsDefaultInterfaceName\r\n ? options.exportAsDefaultInterfaceName\r\n : EXPORT_AS_DEFAULT_INTERFACE_NAME;\r\n let indent: string = '';\r\n if (options.exportAsDefault) {\r\n outputLines.push(`export interface ${interfaceName} {`);\r\n indent = ' ';\r\n }\r\n\r\n for (const stringValueTyping of stringValueTypings.typings) {\r\n const { exportName, comment } = stringValueTyping;\r\n\r\n if (comment && comment.trim() !== '') {\r\n outputLines.push(\r\n `${indent}/**`,\r\n `${indent} * ${comment.replace(/\\*\\//g, '*\\\\/')}`,\r\n `${indent} */`\r\n );\r\n }\r\n\r\n if (options.exportAsDefault) {\r\n outputLines.push(`${indent}'${exportName}': string;`, '');\r\n } else {\r\n outputLines.push(`export declare const ${exportName}: string;`, '');\r\n }\r\n }\r\n\r\n if (options.exportAsDefault) {\r\n outputLines.push(\r\n '}',\r\n '',\r\n `declare const strings: ${interfaceName};`,\r\n '',\r\n 'export default strings;'\r\n );\r\n }\r\n\r\n return outputLines.join(EOL);\r\n }\r\n });\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"StringValuesTypingsGenerator.js","sourceRoot":"","sources":["../src/StringValuesTypingsGenerator.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,2BAAyB;AAEzB,yDAAgF;AAkChF,MAAM,gCAAgC,GAAW,SAAS,CAAC;AAE3D;;;;;GAKG;AACH,MAAa,4BAA6B,SAAQ,mCAAgB;IAChE,YAAmB,OAA6C;QAC9D,KAAK,iCACA,OAAO,KACV,uBAAuB,EAAE,KAAK,EAAE,YAAoB,EAAE,QAAgB,EAAE,YAAoB,EAAE,EAAE;gBAC9F,MAAM,kBAAkB,GAAoC,MAAM,OAAO,CAAC,uBAAuB,CAC/F,YAAY,EACZ,QAAQ,EACR,YAAY,CACb,CAAC;gBAEF,IAAI,kBAAkB,KAAK,SAAS,EAAE;oBACpC,OAAO;iBACR;gBAED,MAAM,WAAW,GAAa,EAAE,CAAC;gBACjC,MAAM,aAAa,GAAW,OAAO,CAAC,4BAA4B;oBAChE,CAAC,CAAC,OAAO,CAAC,4BAA4B;oBACtC,CAAC,CAAC,gCAAgC,CAAC;gBACrC,IAAI,MAAM,GAAW,EAAE,CAAC;gBACxB,IAAI,OAAO,CAAC,eAAe,EAAE;oBAC3B,WAAW,CAAC,IAAI,CAAC,oBAAoB,aAAa,IAAI,CAAC,CAAC;oBACxD,MAAM,GAAG,IAAI,CAAC;iBACf;gBAED,KAAK,MAAM,iBAAiB,IAAI,kBAAkB,CAAC,OAAO,EAAE;oBAC1D,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,iBAAiB,CAAC;oBAElD,IAAI,OAAO,IAAI,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;wBACpC,WAAW,CAAC,IAAI,CACd,GAAG,MAAM,KAAK,EACd,GAAG,MAAM,MAAM,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,EACjD,GAAG,MAAM,KAAK,CACf,CAAC;qBACH;oBAED,IAAI,OAAO,CAAC,eAAe,EAAE;wBAC3B,WAAW,CAAC,IAAI,CAAC,GAAG,MAAM,IAAI,UAAU,YAAY,EAAE,EAAE,CAAC,CAAC;qBAC3D;yBAAM;wBACL,WAAW,CAAC,IAAI,CAAC,wBAAwB,UAAU,WAAW,EAAE,EAAE,CAAC,CAAC;qBACrE;iBACF;gBAED,IAAI,OAAO,CAAC,eAAe,EAAE;oBAC3B,WAAW,CAAC,IAAI,CACd,GAAG,EACH,EAAE,EACF,0BAA0B,aAAa,GAAG,EAC1C,EAAE,EACF,yBAAyB,CAC1B,CAAC;iBACH;gBAED,OAAO,WAAW,CAAC,IAAI,CAAC,QAAG,CAAC,CAAC;YAC/B,CAAC,IACD,CAAC;IACL,CAAC;CACF;AAzDD,oEAyDC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { EOL } from 'os';\n\nimport { ITypingsGeneratorOptions, TypingsGenerator } from './TypingsGenerator';\n\n/**\n * @public\n */\nexport interface IStringValueTyping {\n exportName: string;\n comment?: string;\n}\n\n/**\n * @public\n */\nexport interface IStringValueTypings {\n typings: IStringValueTyping[];\n}\n\n/**\n * @public\n */\nexport interface IStringValuesTypingsGeneratorOptions\n extends ITypingsGeneratorOptions<IStringValueTypings | undefined> {\n /**\n * Setting this option wraps the typings export in a default property.\n */\n exportAsDefault?: boolean;\n\n /**\n * When `exportAsDefault` is true, this optional setting determines the interface name\n * for the default wrapped export. Ignored when `exportAsDefault` is false.\n */\n exportAsDefaultInterfaceName?: string;\n}\n\nconst EXPORT_AS_DEFAULT_INTERFACE_NAME: string = 'IExport';\n\n/**\n * This is a simple tool that generates .d.ts files for non-TS files that can be represented as\n * a simple set of named string exports.\n *\n * @public\n */\nexport class StringValuesTypingsGenerator extends TypingsGenerator {\n public constructor(options: IStringValuesTypingsGeneratorOptions) {\n super({\n ...options,\n parseAndGenerateTypings: async (fileContents: string, filePath: string, relativePath: string) => {\n const stringValueTypings: IStringValueTypings | undefined = await options.parseAndGenerateTypings(\n fileContents,\n filePath,\n relativePath\n );\n\n if (stringValueTypings === undefined) {\n return;\n }\n\n const outputLines: string[] = [];\n const interfaceName: string = options.exportAsDefaultInterfaceName\n ? options.exportAsDefaultInterfaceName\n : EXPORT_AS_DEFAULT_INTERFACE_NAME;\n let indent: string = '';\n if (options.exportAsDefault) {\n outputLines.push(`export interface ${interfaceName} {`);\n indent = ' ';\n }\n\n for (const stringValueTyping of stringValueTypings.typings) {\n const { exportName, comment } = stringValueTyping;\n\n if (comment && comment.trim() !== '') {\n outputLines.push(\n `${indent}/**`,\n `${indent} * ${comment.replace(/\\*\\//g, '*\\\\/')}`,\n `${indent} */`\n );\n }\n\n if (options.exportAsDefault) {\n outputLines.push(`${indent}'${exportName}': string;`, '');\n } else {\n outputLines.push(`export declare const ${exportName}: string;`, '');\n }\n }\n\n if (options.exportAsDefault) {\n outputLines.push(\n '}',\n '',\n `declare const strings: ${interfaceName};`,\n '',\n 'export default strings;'\n );\n }\n\n return outputLines.join(EOL);\n }\n });\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"TypingsGenerator.js","sourceRoot":"","sources":["../src/TypingsG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GAA4B,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACrF,IAAI,YAAY,EAAE;YAChB,KAAK,MAAM,UAAU,IAAI,YAAY,EAAE;gBACrC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,UAAU,CAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;aACzD;YACD,YAAY,CAAC,KAAK,EAAE,CAAC;SACtB;IACH,CAAC;IAEO,mBAAmB,CAAC,YAAoB;QAC9C,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,iBAAiB,EAAE,GAAG,YAAY,OAAO,CAAC,CAAC;IAC/E,CAAC;IAEO,wBAAwB,CAAC,cAAwB;QACvD,MAAM,MAAM,GAAgB,IAAI,GAAG,EAAE,CAAC;QACtC,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE;YAC1C,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;gBAClC,MAAM,CAAC,GAAG,CAAC,IAAI,aAAa,EAAE,CAAC,CAAC;aACjC;iBAAM;gBACL,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;aAC3B;SACF;QAED,OAAO,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5B,CAAC;CACF;AApRD,4CAoRC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\r\n// See LICENSE in the project root for license information.\r\n\r\nimport {\r\n FileSystem,\r\n ITerminal,\r\n Terminal,\r\n ConsoleTerminalProvider,\r\n Path,\r\n NewlineKind,\r\n LegacyAdapters,\r\n Async\r\n} from '@rushstack/node-core-library';\r\nimport glob from 'glob';\r\nimport * as path from 'path';\r\nimport { EOL } from 'os';\r\nimport * as chokidar from 'chokidar';\r\n\r\n/**\r\n * @public\r\n */\r\nexport interface ITypingsGeneratorOptions<TTypingsResult = string | undefined> {\r\n srcFolder: string;\r\n generatedTsFolder: string;\r\n fileExtensions: string[];\r\n parseAndGenerateTypings: (\r\n fileContents: string,\r\n filePath: string,\r\n relativePath: string\r\n ) => TTypingsResult | Promise<TTypingsResult>;\r\n getAdditionalOutputFiles?: (relativePath: string) => string[];\r\n terminal?: ITerminal;\r\n globsToIgnore?: string[];\r\n /**\r\n * @deprecated\r\n *\r\n * TODO: Remove when version 1.0.0 is released.\r\n */\r\n filesToIgnore?: string[];\r\n}\r\n\r\n/**\r\n * This is a simple tool that generates .d.ts files for non-TS files.\r\n *\r\n * @public\r\n */\r\nexport class TypingsGenerator {\r\n // Map of resolved consumer file path -> Set<resolved dependency file path>\r\n private readonly _dependenciesOfFile: Map<string, Set<string>>;\r\n\r\n // Map of resolved dependency file path -> Set<resolved consumer file path>\r\n private readonly _consumersOfFile: Map<string, Set<string>>;\r\n\r\n // Map of resolved file path -> relative file path\r\n private readonly _relativePaths: Map<string, string>;\r\n\r\n protected _options: ITypingsGeneratorOptions;\r\n\r\n private readonly _fileGlob: string;\r\n\r\n public constructor(options: ITypingsGeneratorOptions) {\r\n this._options = {\r\n ...options\r\n };\r\n\r\n if (options.filesToIgnore) {\r\n throw new Error('The filesToIgnore option is no longer supported. Please use globsToIgnore instead.');\r\n }\r\n\r\n if (!this._options.generatedTsFolder) {\r\n throw new Error('generatedTsFolder must be provided');\r\n }\r\n\r\n if (!this._options.srcFolder) {\r\n throw new Error('srcFolder must be provided');\r\n }\r\n\r\n if (Path.isUnder(this._options.srcFolder, this._options.generatedTsFolder)) {\r\n throw new Error('srcFolder must not be under generatedTsFolder');\r\n }\r\n\r\n if (Path.isUnder(this._options.generatedTsFolder, this._options.srcFolder)) {\r\n throw new Error('generatedTsFolder must not be under srcFolder');\r\n }\r\n\r\n if (!this._options.fileExtensions || this._options.fileExtensions.length === 0) {\r\n throw new Error('At least one file extension must be provided.');\r\n }\r\n\r\n if (!this._options.globsToIgnore) {\r\n this._options.globsToIgnore = [];\r\n }\r\n\r\n if (!this._options.terminal) {\r\n this._options.terminal = new Terminal(new ConsoleTerminalProvider({ verboseEnabled: true }));\r\n }\r\n\r\n this._options.fileExtensions = this._normalizeFileExtensions(this._options.fileExtensions);\r\n\r\n this._dependenciesOfFile = new Map();\r\n this._consumersOfFile = new Map();\r\n this._relativePaths = new Map();\r\n\r\n this._fileGlob = `**/*+(${this._options.fileExtensions.join('|')})`;\r\n }\r\n\r\n public async generateTypingsAsync(): Promise<void> {\r\n await FileSystem.ensureEmptyFolderAsync(this._options.generatedTsFolder);\r\n\r\n const filePaths: string[] = await LegacyAdapters.convertCallbackToPromise(\r\n glob,\r\n this._fileGlob,\r\n {\r\n cwd: this._options.srcFolder,\r\n absolute: true,\r\n nosort: true,\r\n nodir: true,\r\n ignore: this._options.globsToIgnore\r\n }\r\n );\r\n\r\n await this._reprocessFiles(filePaths);\r\n }\r\n\r\n public async runWatcherAsync(): Promise<void> {\r\n await FileSystem.ensureFolderAsync(this._options.generatedTsFolder);\r\n\r\n await new Promise((resolve, reject): void => {\r\n const watcher: chokidar.FSWatcher = chokidar.watch(\r\n this._fileGlob,\r\n {\r\n cwd: this._options.srcFolder,\r\n ignored: this._options.globsToIgnore\r\n }\r\n );\r\n\r\n const queue: Set<string> = new Set();\r\n let timeout: NodeJS.Timeout | undefined;\r\n let processing: boolean = false;\r\n let flushAfterCompletion: boolean = false;\r\n\r\n const flushInternal: () => void = () => {\r\n processing = true;\r\n\r\n const toProcess: string[] = Array.from(queue);\r\n queue.clear();\r\n this._reprocessFiles(toProcess)\r\n .then(() => {\r\n processing = false;\r\n // If the timeout was invoked again, immediately reexecute with the changed files.\r\n if (flushAfterCompletion) {\r\n flushAfterCompletion = false;\r\n flushInternal();\r\n }\r\n })\r\n .catch(reject);\r\n };\r\n\r\n const debouncedFlush: () => void = () => {\r\n timeout = undefined;\r\n if (processing) {\r\n // If the callback was invoked while processing is ongoing, indicate that we should flush immediately\r\n // upon completion of the current change batch.\r\n flushAfterCompletion = true;\r\n return;\r\n }\r\n\r\n flushInternal();\r\n };\r\n\r\n const onChange: (relativePath: string) => void = (relativePath: string) => {\r\n queue.add(relativePath);\r\n if (timeout) {\r\n clearTimeout(timeout);\r\n }\r\n\r\n setTimeout(debouncedFlush, 100);\r\n };\r\n\r\n watcher.on('add', onChange);\r\n watcher.on('change', onChange);\r\n watcher.on('unlink', async (relativePath) => {\r\n await Promise.all(\r\n this.getOutputFilePaths(relativePath).map(async (outputFile: string) => {\r\n await FileSystem.deleteFileAsync(outputFile);\r\n })\r\n );\r\n });\r\n watcher.on('error', reject);\r\n });\r\n }\r\n\r\n /**\r\n * Register file dependencies that may effect the typings of a consumer file.\r\n * Note: This feature is only useful in watch mode.\r\n * The registerDependency method must be called in the body of parseAndGenerateTypings every\r\n * time because the registry for a file is cleared at the beginning of processing.\r\n */\r\n public registerDependency(consumer: string, rawDependency: string): void {\r\n // Need to normalize slashes in the dependency path\r\n const dependency: string = path.resolve(this._options.srcFolder, rawDependency);\r\n\r\n let dependencies: Set<string> | undefined = this._dependenciesOfFile.get(consumer);\r\n if (!dependencies) {\r\n dependencies = new Set();\r\n this._dependenciesOfFile.set(consumer, dependencies);\r\n }\r\n dependencies.add(dependency);\r\n\r\n let consumers: Set<string> | undefined = this._consumersOfFile.get(dependency);\r\n if (!consumers) {\r\n consumers = new Set();\r\n this._consumersOfFile.set(dependency, consumers);\r\n }\r\n consumers.add(consumer);\r\n }\r\n\r\n public getOutputFilePaths(relativePath: string): string[] {\r\n const typingsFile: string = this._getTypingsFilePath(relativePath);\r\n const additionalPaths: string[] | undefined = this._options.getAdditionalOutputFiles?.(relativePath);\r\n return additionalPaths ? [typingsFile, ...additionalPaths] : [typingsFile];\r\n }\r\n\r\n private async _reprocessFiles(relativePaths: Iterable<string>): Promise<void> {\r\n // Build a queue of resolved paths\r\n const toProcess: Set<string> = new Set();\r\n for (const rawPath of relativePaths) {\r\n const relativePath: string = Path.convertToSlashes(rawPath);\r\n const resolvedPath: string = path.resolve(this._options.srcFolder, rawPath);\r\n this._relativePaths.set(resolvedPath, relativePath);\r\n toProcess.add(resolvedPath);\r\n }\r\n\r\n // Expand out all registered consumers, according to the current dependency graph\r\n for (const file of toProcess) {\r\n const consumers: Set<string> | undefined = this._consumersOfFile.get(file);\r\n if (consumers) {\r\n for (const consumer of consumers) {\r\n toProcess.add(consumer);\r\n }\r\n }\r\n }\r\n\r\n // Map back to the relative paths so that the information is available\r\n await Async.forEachAsync(\r\n toProcess,\r\n async (resolvedPath: string) => {\r\n const relativePath: string | undefined = this._relativePaths.get(resolvedPath);\r\n if (!relativePath) {\r\n throw new Error(`Missing relative path for file ${resolvedPath}`);\r\n }\r\n await this._parseFileAndGenerateTypingsAsync(relativePath, resolvedPath);\r\n },\r\n { concurrency: 20 }\r\n );\r\n }\r\n\r\n private async _parseFileAndGenerateTypingsAsync(relativePath: string, resolvedPath: string): Promise<void> {\r\n // Clear registered dependencies prior to reprocessing.\r\n this._clearDependencies(resolvedPath);\r\n\r\n try {\r\n const fileContents: string = await FileSystem.readFileAsync(resolvedPath);\r\n const typingsData: string | undefined = await this._options.parseAndGenerateTypings(\r\n fileContents,\r\n resolvedPath,\r\n relativePath\r\n );\r\n\r\n // Typings data will be undefined when no types should be generated for the parsed file.\r\n if (typingsData === undefined) {\r\n return;\r\n }\r\n\r\n const prefixedTypingsData: string = [\r\n '// This file was generated by a tool. Modifying it will produce unexpected behavior',\r\n '',\r\n typingsData\r\n ].join(EOL);\r\n\r\n const generatedTsFilePath: string = this._getTypingsFilePath(relativePath);\r\n\r\n await FileSystem.writeFileAsync(generatedTsFilePath, prefixedTypingsData, {\r\n ensureFolderExists: true,\r\n convertLineEndings: NewlineKind.OsDefault\r\n });\r\n } catch (e) {\r\n this._options.terminal!.writeError(\r\n `Error occurred parsing and generating typings for file \"${resolvedPath}\": ${e}`\r\n );\r\n }\r\n }\r\n\r\n /**\r\n * Removes the consumer from all extant dependencies\r\n */\r\n private _clearDependencies(consumer: string): void {\r\n const dependencies: Set<string> | undefined = this._dependenciesOfFile.get(consumer);\r\n if (dependencies) {\r\n for (const dependency of dependencies) {\r\n this._consumersOfFile.get(dependency)!.delete(consumer);\r\n }\r\n dependencies.clear();\r\n }\r\n }\r\n\r\n private _getTypingsFilePath(relativePath: string): string {\r\n return path.resolve(this._options.generatedTsFolder, `${relativePath}.d.ts`);\r\n }\r\n\r\n private _normalizeFileExtensions(fileExtensions: string[]): string[] {\r\n const result: Set<string> = new Set();\r\n for (const fileExtension of fileExtensions) {\r\n if (!fileExtension.startsWith('.')) {\r\n result.add(`.${fileExtension}`);\r\n } else {\r\n result.add(fileExtension);\r\n }\r\n }\r\n\r\n return Array.from(result);\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"TypingsGenerator.js","sourceRoot":"","sources":["../src/TypingsGenerator.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,oEASsC;AACtC,gDAAwB;AACxB,2CAA6B;AAC7B,2BAAyB;AACzB,mDAAqC;AAyBrC;;;;GAIG;AACH,MAAa,gBAAgB;IAc3B,YAAmB,OAAiC;QAClD,IAAI,CAAC,QAAQ,qBACR,OAAO,CACX,CAAC;QAEF,IAAI,OAAO,CAAC,aAAa,EAAE;YACzB,MAAM,IAAI,KAAK,CAAC,oFAAoF,CAAC,CAAC;SACvG;QAED,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,iBAAiB,EAAE;YACpC,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;SACvD;QAED,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE;YAC5B,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;SAC/C;QAED,IAAI,wBAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE;YAC1E,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;SAClE;QAED,IAAI,wBAAI,CAAC,O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AAM,CAAC,CAAC;IAC5B,CAAC;CACF;AApRD,4CAoRC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport {\n FileSystem,\n ITerminal,\n Terminal,\n ConsoleTerminalProvider,\n Path,\n NewlineKind,\n LegacyAdapters,\n Async\n} from '@rushstack/node-core-library';\nimport glob from 'glob';\nimport * as path from 'path';\nimport { EOL } from 'os';\nimport * as chokidar from 'chokidar';\n\n/**\n * @public\n */\nexport interface ITypingsGeneratorOptions<TTypingsResult = string | undefined> {\n srcFolder: string;\n generatedTsFolder: string;\n fileExtensions: string[];\n parseAndGenerateTypings: (\n fileContents: string,\n filePath: string,\n relativePath: string\n ) => TTypingsResult | Promise<TTypingsResult>;\n getAdditionalOutputFiles?: (relativePath: string) => string[];\n terminal?: ITerminal;\n globsToIgnore?: string[];\n /**\n * @deprecated\n *\n * TODO: Remove when version 1.0.0 is released.\n */\n filesToIgnore?: string[];\n}\n\n/**\n * This is a simple tool that generates .d.ts files for non-TS files.\n *\n * @public\n */\nexport class TypingsGenerator {\n // Map of resolved consumer file path -> Set<resolved dependency file path>\n private readonly _dependenciesOfFile: Map<string, Set<string>>;\n\n // Map of resolved dependency file path -> Set<resolved consumer file path>\n private readonly _consumersOfFile: Map<string, Set<string>>;\n\n // Map of resolved file path -> relative file path\n private readonly _relativePaths: Map<string, string>;\n\n protected _options: ITypingsGeneratorOptions;\n\n private readonly _fileGlob: string;\n\n public constructor(options: ITypingsGeneratorOptions) {\n this._options = {\n ...options\n };\n\n if (options.filesToIgnore) {\n throw new Error('The filesToIgnore option is no longer supported. Please use globsToIgnore instead.');\n }\n\n if (!this._options.generatedTsFolder) {\n throw new Error('generatedTsFolder must be provided');\n }\n\n if (!this._options.srcFolder) {\n throw new Error('srcFolder must be provided');\n }\n\n if (Path.isUnder(this._options.srcFolder, this._options.generatedTsFolder)) {\n throw new Error('srcFolder must not be under generatedTsFolder');\n }\n\n if (Path.isUnder(this._options.generatedTsFolder, this._options.srcFolder)) {\n throw new Error('generatedTsFolder must not be under srcFolder');\n }\n\n if (!this._options.fileExtensions || this._options.fileExtensions.length === 0) {\n throw new Error('At least one file extension must be provided.');\n }\n\n if (!this._options.globsToIgnore) {\n this._options.globsToIgnore = [];\n }\n\n if (!this._options.terminal) {\n this._options.terminal = new Terminal(new ConsoleTerminalProvider({ verboseEnabled: true }));\n }\n\n this._options.fileExtensions = this._normalizeFileExtensions(this._options.fileExtensions);\n\n this._dependenciesOfFile = new Map();\n this._consumersOfFile = new Map();\n this._relativePaths = new Map();\n\n this._fileGlob = `**/*+(${this._options.fileExtensions.join('|')})`;\n }\n\n public async generateTypingsAsync(): Promise<void> {\n await FileSystem.ensureEmptyFolderAsync(this._options.generatedTsFolder);\n\n const filePaths: string[] = await LegacyAdapters.convertCallbackToPromise(\n glob,\n this._fileGlob,\n {\n cwd: this._options.srcFolder,\n absolute: true,\n nosort: true,\n nodir: true,\n ignore: this._options.globsToIgnore\n }\n );\n\n await this._reprocessFiles(filePaths);\n }\n\n public async runWatcherAsync(): Promise<void> {\n await FileSystem.ensureFolderAsync(this._options.generatedTsFolder);\n\n await new Promise((resolve, reject): void => {\n const watcher: chokidar.FSWatcher = chokidar.watch(\n this._fileGlob,\n {\n cwd: this._options.srcFolder,\n ignored: this._options.globsToIgnore\n }\n );\n\n const queue: Set<string> = new Set();\n let timeout: NodeJS.Timeout | undefined;\n let processing: boolean = false;\n let flushAfterCompletion: boolean = false;\n\n const flushInternal: () => void = () => {\n processing = true;\n\n const toProcess: string[] = Array.from(queue);\n queue.clear();\n this._reprocessFiles(toProcess)\n .then(() => {\n processing = false;\n // If the timeout was invoked again, immediately reexecute with the changed files.\n if (flushAfterCompletion) {\n flushAfterCompletion = false;\n flushInternal();\n }\n })\n .catch(reject);\n };\n\n const debouncedFlush: () => void = () => {\n timeout = undefined;\n if (processing) {\n // If the callback was invoked while processing is ongoing, indicate that we should flush immediately\n // upon completion of the current change batch.\n flushAfterCompletion = true;\n return;\n }\n\n flushInternal();\n };\n\n const onChange: (relativePath: string) => void = (relativePath: string) => {\n queue.add(relativePath);\n if (timeout) {\n clearTimeout(timeout);\n }\n\n setTimeout(debouncedFlush, 100);\n };\n\n watcher.on('add', onChange);\n watcher.on('change', onChange);\n watcher.on('unlink', async (relativePath) => {\n await Promise.all(\n this.getOutputFilePaths(relativePath).map(async (outputFile: string) => {\n await FileSystem.deleteFileAsync(outputFile);\n })\n );\n });\n watcher.on('error', reject);\n });\n }\n\n /**\n * Register file dependencies that may effect the typings of a consumer file.\n * Note: This feature is only useful in watch mode.\n * The registerDependency method must be called in the body of parseAndGenerateTypings every\n * time because the registry for a file is cleared at the beginning of processing.\n */\n public registerDependency(consumer: string, rawDependency: string): void {\n // Need to normalize slashes in the dependency path\n const dependency: string = path.resolve(this._options.srcFolder, rawDependency);\n\n let dependencies: Set<string> | undefined = this._dependenciesOfFile.get(consumer);\n if (!dependencies) {\n dependencies = new Set();\n this._dependenciesOfFile.set(consumer, dependencies);\n }\n dependencies.add(dependency);\n\n let consumers: Set<string> | undefined = this._consumersOfFile.get(dependency);\n if (!consumers) {\n consumers = new Set();\n this._consumersOfFile.set(dependency, consumers);\n }\n consumers.add(consumer);\n }\n\n public getOutputFilePaths(relativePath: string): string[] {\n const typingsFile: string = this._getTypingsFilePath(relativePath);\n const additionalPaths: string[] | undefined = this._options.getAdditionalOutputFiles?.(relativePath);\n return additionalPaths ? [typingsFile, ...additionalPaths] : [typingsFile];\n }\n\n private async _reprocessFiles(relativePaths: Iterable<string>): Promise<void> {\n // Build a queue of resolved paths\n const toProcess: Set<string> = new Set();\n for (const rawPath of relativePaths) {\n const relativePath: string = Path.convertToSlashes(rawPath);\n const resolvedPath: string = path.resolve(this._options.srcFolder, rawPath);\n this._relativePaths.set(resolvedPath, relativePath);\n toProcess.add(resolvedPath);\n }\n\n // Expand out all registered consumers, according to the current dependency graph\n for (const file of toProcess) {\n const consumers: Set<string> | undefined = this._consumersOfFile.get(file);\n if (consumers) {\n for (const consumer of consumers) {\n toProcess.add(consumer);\n }\n }\n }\n\n // Map back to the relative paths so that the information is available\n await Async.forEachAsync(\n toProcess,\n async (resolvedPath: string) => {\n const relativePath: string | undefined = this._relativePaths.get(resolvedPath);\n if (!relativePath) {\n throw new Error(`Missing relative path for file ${resolvedPath}`);\n }\n await this._parseFileAndGenerateTypingsAsync(relativePath, resolvedPath);\n },\n { concurrency: 20 }\n );\n }\n\n private async _parseFileAndGenerateTypingsAsync(relativePath: string, resolvedPath: string): Promise<void> {\n // Clear registered dependencies prior to reprocessing.\n this._clearDependencies(resolvedPath);\n\n try {\n const fileContents: string = await FileSystem.readFileAsync(resolvedPath);\n const typingsData: string | undefined = await this._options.parseAndGenerateTypings(\n fileContents,\n resolvedPath,\n relativePath\n );\n\n // Typings data will be undefined when no types should be generated for the parsed file.\n if (typingsData === undefined) {\n return;\n }\n\n const prefixedTypingsData: string = [\n '// This file was generated by a tool. Modifying it will produce unexpected behavior',\n '',\n typingsData\n ].join(EOL);\n\n const generatedTsFilePath: string = this._getTypingsFilePath(relativePath);\n\n await FileSystem.writeFileAsync(generatedTsFilePath, prefixedTypingsData, {\n ensureFolderExists: true,\n convertLineEndings: NewlineKind.OsDefault\n });\n } catch (e) {\n this._options.terminal!.writeError(\n `Error occurred parsing and generating typings for file \"${resolvedPath}\": ${e}`\n );\n }\n }\n\n /**\n * Removes the consumer from all extant dependencies\n */\n private _clearDependencies(consumer: string): void {\n const dependencies: Set<string> | undefined = this._dependenciesOfFile.get(consumer);\n if (dependencies) {\n for (const dependency of dependencies) {\n this._consumersOfFile.get(dependency)!.delete(consumer);\n }\n dependencies.clear();\n }\n }\n\n private _getTypingsFilePath(relativePath: string): string {\n return path.resolve(this._options.generatedTsFolder, `${relativePath}.d.ts`);\n }\n\n private _normalizeFileExtensions(fileExtensions: string[]): string[] {\n const result: Set<string> = new Set();\n for (const fileExtension of fileExtensions) {\n if (!fileExtension.startsWith('.')) {\n result.add(`.${fileExtension}`);\n } else {\n result.add(fileExtension);\n }\n }\n\n return Array.from(result);\n }\n}\n"]}
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,uDAAgF;AAA7C,oHAAA,gBAAgB,OAAA;AAEnD,+EAKwC;AADtC,4IAAA,4BAA4B,OAAA","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\r\n// See LICENSE in the project root for license information.\r\n\r\nexport { ITypingsGeneratorOptions, TypingsGenerator } from './TypingsGenerator';\r\n\r\nexport {\r\n IStringValueTyping,\r\n IStringValueTypings,\r\n IStringValuesTypingsGeneratorOptions,\r\n StringValuesTypingsGenerator\r\n} from './StringValuesTypingsGenerator';\r\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,uDAAgF;AAA7C,oHAAA,gBAAgB,OAAA;AAEnD,+EAKwC;AADtC,4IAAA,4BAA4B,OAAA","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nexport { ITypingsGeneratorOptions, TypingsGenerator } from './TypingsGenerator';\n\nexport {\n IStringValueTyping,\n IStringValueTypings,\n IStringValuesTypingsGeneratorOptions,\n StringValuesTypingsGenerator\n} from './StringValuesTypingsGenerator';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rushstack/typings-generator",
3
- "version": "0.6.0",
3
+ "version": "0.6.4",
4
4
  "description": "This library provides functionality for automatically generating typings for non-TS files.",
5
5
  "keywords": [
6
6
  "dts",
@@ -16,15 +16,15 @@
16
16
  "directory": "libraries/typings-generator"
17
17
  },
18
18
  "dependencies": {
19
- "@rushstack/node-core-library": "3.44.2",
19
+ "@rushstack/node-core-library": "3.45.0",
20
20
  "@types/node": "12.20.24",
21
21
  "chokidar": "~3.4.0",
22
22
  "glob": "~7.0.5"
23
23
  },
24
24
  "devDependencies": {
25
- "@rushstack/eslint-config": "2.5.0",
26
- "@rushstack/heft": "0.43.2",
27
- "@rushstack/heft-node-rig": "1.4.3",
25
+ "@rushstack/eslint-config": "2.5.1",
26
+ "@rushstack/heft": "0.44.2",
27
+ "@rushstack/heft-node-rig": "1.6.0",
28
28
  "@types/glob": "7.1.1"
29
29
  },
30
30
  "scripts": {