@rushstack/typings-generator 0.8.11 → 0.8.12
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.
|
@@ -92,10 +92,10 @@ export declare class TypingsGenerator {
|
|
|
92
92
|
/**
|
|
93
93
|
* Generate typings for the provided input files.
|
|
94
94
|
*
|
|
95
|
-
* @param
|
|
95
|
+
* @param relativeFilePaths - The input files to process, relative to the source folder. If not provided,
|
|
96
96
|
* all input files will be processed.
|
|
97
97
|
*/
|
|
98
|
-
generateTypingsAsync(
|
|
98
|
+
generateTypingsAsync(relativeFilePaths?: string[]): Promise<void>;
|
|
99
99
|
runWatcherAsync(): Promise<void>;
|
|
100
100
|
/**
|
|
101
101
|
* Register file dependencies that may effect the typings of a consumer file.
|
|
@@ -105,6 +105,7 @@ export declare class TypingsGenerator {
|
|
|
105
105
|
*/
|
|
106
106
|
registerDependency(consumer: string, rawDependency: string): void;
|
|
107
107
|
getOutputFilePaths(relativePath: string): string[];
|
|
108
|
+
private _getOutputFilePathsWithoutCheck;
|
|
108
109
|
private _reprocessFiles;
|
|
109
110
|
private _parseFileAndGenerateTypingsAsync;
|
|
110
111
|
/**
|
|
@@ -49,10 +49,10 @@ export declare class TypingsGenerator {
|
|
|
49
49
|
/**
|
|
50
50
|
* Generate typings for the provided input files.
|
|
51
51
|
*
|
|
52
|
-
* @param
|
|
52
|
+
* @param relativeFilePaths - The input files to process, relative to the source folder. If not provided,
|
|
53
53
|
* all input files will be processed.
|
|
54
54
|
*/
|
|
55
|
-
generateTypingsAsync(
|
|
55
|
+
generateTypingsAsync(relativeFilePaths?: string[]): Promise<void>;
|
|
56
56
|
runWatcherAsync(): Promise<void>;
|
|
57
57
|
/**
|
|
58
58
|
* Register file dependencies that may effect the typings of a consumer file.
|
|
@@ -62,6 +62,7 @@ export declare class TypingsGenerator {
|
|
|
62
62
|
*/
|
|
63
63
|
registerDependency(consumer: string, rawDependency: string): void;
|
|
64
64
|
getOutputFilePaths(relativePath: string): string[];
|
|
65
|
+
private _getOutputFilePathsWithoutCheck;
|
|
65
66
|
private _reprocessFiles;
|
|
66
67
|
private _parseFileAndGenerateTypingsAsync;
|
|
67
68
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TypingsGenerator.d.ts","sourceRoot":"","sources":["../src/TypingsGenerator.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,SAAS,EAOV,MAAM,8BAA8B,CAAC;AAMtC;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC3C,SAAS,EAAE,MAAM,CAAC;IAClB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,2BAA2B,CAAC,EAAE,MAAM,EAAE,CAAC;IACvC,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB,CAAC,cAAc,GAAG,MAAM,GAAG,SAAS,CAC3E,SAAQ,4BAA4B;IACpC,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,uBAAuB,EAAE,CACvB,YAAY,EAAE,MAAM,EACpB,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,MAAM,KACjB,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IAC9C,wBAAwB,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,MAAM,EAAE,CAAC;IAC9D;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B;AAED;;;;GAIG;AACH,qBAAa,gBAAgB;IAE3B,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA2B;IAG/D,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA2B;IAG5D,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAsB;IAErD,SAAS,CAAC,QAAQ,EAAE,wBAAwB,CAAC;IAE7C;;OAEG;IACH,SAAgB,gBAAgB,EAAE,MAAM,CAAC;IAEzC;;OAEG;IAC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IAEzC;;OAEG;IACH,SAAgB,aAAa,EAAE,MAAM,CAAC;IAEtC;;OAEG;IACH,SAAgB,gBAAgB,EAAE,SAAS,MAAM,EAAE,CAAC;gBAEjC,OAAO,EAAE,wBAAwB;IA6CpD;;;;;OAKG;IACU,oBAAoB,CAAC,iBAAiB,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAejE,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAiE7C;;;;;OAKG;IACI,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,IAAI;IAmBjE,kBAAkB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE;IAQzD,OAAO,CAAC,+BAA+B;YAMzB,eAAe;YAsCf,iCAAiC;IAqC/C;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAU1B,OAAO,CAAE,oBAAoB;IAW7B,OAAO,CAAC,wBAAwB;CAYjC"}
|
package/lib/TypingsGenerator.js
CHANGED
|
@@ -74,19 +74,21 @@ class TypingsGenerator {
|
|
|
74
74
|
/**
|
|
75
75
|
* Generate typings for the provided input files.
|
|
76
76
|
*
|
|
77
|
-
* @param
|
|
77
|
+
* @param relativeFilePaths - The input files to process, relative to the source folder. If not provided,
|
|
78
78
|
* all input files will be processed.
|
|
79
79
|
*/
|
|
80
|
-
async generateTypingsAsync(
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
async generateTypingsAsync(relativeFilePaths) {
|
|
81
|
+
let checkFilePaths = true;
|
|
82
|
+
if (!(relativeFilePaths === null || relativeFilePaths === void 0 ? void 0 : relativeFilePaths.length)) {
|
|
83
|
+
checkFilePaths = false; // Don't check file paths if we generate them
|
|
84
|
+
relativeFilePaths = await node_core_library_1.LegacyAdapters.convertCallbackToPromise(glob_1.default, this.inputFileGlob, {
|
|
83
85
|
cwd: this.sourceFolderPath,
|
|
84
86
|
ignore: this.ignoredFileGlobs,
|
|
85
87
|
nosort: true,
|
|
86
88
|
nodir: true
|
|
87
89
|
});
|
|
88
90
|
}
|
|
89
|
-
await this._reprocessFiles(
|
|
91
|
+
await this._reprocessFiles(relativeFilePaths, checkFilePaths);
|
|
90
92
|
}
|
|
91
93
|
async runWatcherAsync() {
|
|
92
94
|
await node_core_library_1.FileSystem.ensureFolderAsync(this._options.generatedTsFolder);
|
|
@@ -103,7 +105,7 @@ class TypingsGenerator {
|
|
|
103
105
|
processing = true;
|
|
104
106
|
const toProcess = Array.from(queue);
|
|
105
107
|
queue.clear();
|
|
106
|
-
this._reprocessFiles(toProcess)
|
|
108
|
+
this._reprocessFiles(toProcess, false)
|
|
107
109
|
.then(() => {
|
|
108
110
|
processing = false;
|
|
109
111
|
// If the timeout was invoked again, immediately reexecute with the changed files.
|
|
@@ -134,7 +136,7 @@ class TypingsGenerator {
|
|
|
134
136
|
watcher.on('add', onChange);
|
|
135
137
|
watcher.on('change', onChange);
|
|
136
138
|
watcher.on('unlink', async (relativePath) => {
|
|
137
|
-
await Promise.all(this.
|
|
139
|
+
await Promise.all(this._getOutputFilePathsWithoutCheck(relativePath).map(async (outputFile) => {
|
|
138
140
|
await node_core_library_1.FileSystem.deleteFileAsync(outputFile);
|
|
139
141
|
}));
|
|
140
142
|
});
|
|
@@ -164,15 +166,24 @@ class TypingsGenerator {
|
|
|
164
166
|
consumers.add(consumer);
|
|
165
167
|
}
|
|
166
168
|
getOutputFilePaths(relativePath) {
|
|
169
|
+
if (path.isAbsolute(relativePath)) {
|
|
170
|
+
throw new Error(`"${relativePath}" must be relative`);
|
|
171
|
+
}
|
|
172
|
+
return this._getOutputFilePathsWithoutCheck(relativePath);
|
|
173
|
+
}
|
|
174
|
+
_getOutputFilePathsWithoutCheck(relativePath) {
|
|
167
175
|
var _a, _b;
|
|
168
176
|
const typingsFilePaths = this._getTypingsFilePaths(relativePath);
|
|
169
177
|
const additionalPaths = (_b = (_a = this._options).getAdditionalOutputFiles) === null || _b === void 0 ? void 0 : _b.call(_a, relativePath);
|
|
170
178
|
return additionalPaths ? [...typingsFilePaths, ...additionalPaths] : Array.from(typingsFilePaths);
|
|
171
179
|
}
|
|
172
|
-
async _reprocessFiles(relativePaths) {
|
|
180
|
+
async _reprocessFiles(relativePaths, checkFilePaths) {
|
|
173
181
|
// Build a queue of resolved paths
|
|
174
182
|
const toProcess = new Set();
|
|
175
183
|
for (const rawPath of relativePaths) {
|
|
184
|
+
if (checkFilePaths && path.isAbsolute(rawPath)) {
|
|
185
|
+
throw new Error(`"${rawPath}" must be relative`);
|
|
186
|
+
}
|
|
176
187
|
const relativePath = node_core_library_1.Path.convertToSlashes(rawPath);
|
|
177
188
|
const resolvedPath = path.resolve(this._options.srcFolder, rawPath);
|
|
178
189
|
this._relativePaths.set(resolvedPath, relativePath);
|
|
@@ -1 +1 @@
|
|
|
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;AAiCrC;;;;GAIG;AACH,MAAa,gBAAgB;IA2B3B,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;QACD,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC;QAEhD,IAAI,wBAA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A2B,EAAE;gBACpE,MAAM,IAAI,CAAC,OAAO,CAAC,0BAA0B,EAAE,WAAW,CAAC,CAAC;aAC7D;SACF;IACH,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;AArSD,4CAqSC","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 ITypingsGeneratorBaseOptions {\n srcFolder: string;\n generatedTsFolder: string;\n secondaryGeneratedTsFolders?: string[];\n globsToIgnore?: string[];\n terminal?: ITerminal;\n}\n\n/**\n * @public\n */\nexport interface ITypingsGeneratorOptions<TTypingsResult = string | undefined>\n extends ITypingsGeneratorBaseOptions {\n fileExtensions: string[];\n parseAndGenerateTypings: (\n fileContents: string,\n filePath: string,\n relativePath: string\n ) => TTypingsResult | Promise<TTypingsResult>;\n getAdditionalOutputFiles?: (relativePath: string) => 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 /**\n * The folder path that contains all input source files.\n */\n public readonly sourceFolderPath: string;\n\n /**\n * The glob pattern used to find input files to process.\n */\n public readonly inputFileGlob: string;\n\n /**\n * The glob patterns that should be ignored when finding input files to process.\n */\n public readonly ignoredFileGlobs: readonly 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 this.sourceFolderPath = this._options.srcFolder;\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 this.ignoredFileGlobs = this._options.globsToIgnore || [];\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.inputFileGlob = `**/*+(${this._options.fileExtensions.join('|')})`;\n }\n\n /**\n * Generate typings for the provided input files.\n *\n * @param filePaths - The input files to process, relative to the source folder. If not provided,\n * all input files will be processed.\n */\n public async generateTypingsAsync(filePaths?: string[]): Promise<void> {\n if (!filePaths?.length) {\n filePaths = await LegacyAdapters.convertCallbackToPromise(glob, this.inputFileGlob, {\n cwd: this.sourceFolderPath,\n ignore: this.ignoredFileGlobs,\n nosort: true,\n nodir: true\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(this.inputFileGlob, {\n cwd: this.sourceFolderPath,\n ignored: this.ignoredFileGlobs\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 typingsFilePaths: Iterable<string> = this._getTypingsFilePaths(relativePath);\n const additionalPaths: string[] | undefined = this._options.getAdditionalOutputFiles?.(relativePath);\n return additionalPaths ? [...typingsFilePaths, ...additionalPaths] : Array.from(typingsFilePaths);\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 generatedTsFilePaths: Iterable<string> = this._getTypingsFilePaths(relativePath);\n for (const generatedTsFilePath of generatedTsFilePaths) {\n await FileSystem.writeFileAsync(generatedTsFilePath, prefixedTypingsData, {\n ensureFolderExists: true,\n convertLineEndings: NewlineKind.OsDefault\n });\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 *_getTypingsFilePaths(relativePath: string): Iterable<string> {\n const { generatedTsFolder, secondaryGeneratedTsFolders } = this._options;\n const dtsFilename: string = `${relativePath}.d.ts`;\n yield path.resolve(generatedTsFolder, dtsFilename);\n if (secondaryGeneratedTsFolders) {\n for (const secondaryGeneratedTsFolder of secondaryGeneratedTsFolders) {\n yield path.resolve(secondaryGeneratedTsFolder, dtsFilename);\n }\n }\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"]}
|
|
1
|
+
{"version":3,"file":"TypingsGenerator.js","sourceRoot":"","sources":["../src/TypingsGenerator.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,oEASsC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,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;AApTD,4CAoTC","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 ITypingsGeneratorBaseOptions {\n srcFolder: string;\n generatedTsFolder: string;\n secondaryGeneratedTsFolders?: string[];\n globsToIgnore?: string[];\n terminal?: ITerminal;\n}\n\n/**\n * @public\n */\nexport interface ITypingsGeneratorOptions<TTypingsResult = string | undefined>\n extends ITypingsGeneratorBaseOptions {\n fileExtensions: string[];\n parseAndGenerateTypings: (\n fileContents: string,\n filePath: string,\n relativePath: string\n ) => TTypingsResult | Promise<TTypingsResult>;\n getAdditionalOutputFiles?: (relativePath: string) => 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 /**\n * The folder path that contains all input source files.\n */\n public readonly sourceFolderPath: string;\n\n /**\n * The glob pattern used to find input files to process.\n */\n public readonly inputFileGlob: string;\n\n /**\n * The glob patterns that should be ignored when finding input files to process.\n */\n public readonly ignoredFileGlobs: readonly 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 this.sourceFolderPath = this._options.srcFolder;\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 this.ignoredFileGlobs = this._options.globsToIgnore || [];\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.inputFileGlob = `**/*+(${this._options.fileExtensions.join('|')})`;\n }\n\n /**\n * Generate typings for the provided input files.\n *\n * @param relativeFilePaths - The input files to process, relative to the source folder. If not provided,\n * all input files will be processed.\n */\n public async generateTypingsAsync(relativeFilePaths?: string[]): Promise<void> {\n let checkFilePaths: boolean = true;\n if (!relativeFilePaths?.length) {\n checkFilePaths = false; // Don't check file paths if we generate them\n relativeFilePaths = await LegacyAdapters.convertCallbackToPromise(glob, this.inputFileGlob, {\n cwd: this.sourceFolderPath,\n ignore: this.ignoredFileGlobs,\n nosort: true,\n nodir: true\n });\n }\n\n await this._reprocessFiles(relativeFilePaths, checkFilePaths);\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(this.inputFileGlob, {\n cwd: this.sourceFolderPath,\n ignored: this.ignoredFileGlobs\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, false)\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._getOutputFilePathsWithoutCheck(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 if (path.isAbsolute(relativePath)) {\n throw new Error(`\"${relativePath}\" must be relative`);\n }\n\n return this._getOutputFilePathsWithoutCheck(relativePath);\n }\n\n private _getOutputFilePathsWithoutCheck(relativePath: string): string[] {\n const typingsFilePaths: Iterable<string> = this._getTypingsFilePaths(relativePath);\n const additionalPaths: string[] | undefined = this._options.getAdditionalOutputFiles?.(relativePath);\n return additionalPaths ? [...typingsFilePaths, ...additionalPaths] : Array.from(typingsFilePaths);\n }\n\n private async _reprocessFiles(relativePaths: Iterable<string>, checkFilePaths: boolean): Promise<void> {\n // Build a queue of resolved paths\n const toProcess: Set<string> = new Set();\n for (const rawPath of relativePaths) {\n if (checkFilePaths && path.isAbsolute(rawPath)) {\n throw new Error(`\"${rawPath}\" must be relative`);\n }\n\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 generatedTsFilePaths: Iterable<string> = this._getTypingsFilePaths(relativePath);\n for (const generatedTsFilePath of generatedTsFilePaths) {\n await FileSystem.writeFileAsync(generatedTsFilePath, prefixedTypingsData, {\n ensureFolderExists: true,\n convertLineEndings: NewlineKind.OsDefault\n });\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 *_getTypingsFilePaths(relativePath: string): Iterable<string> {\n const { generatedTsFolder, secondaryGeneratedTsFolders } = this._options;\n const dtsFilename: string = `${relativePath}.d.ts`;\n yield path.resolve(generatedTsFolder, dtsFilename);\n if (secondaryGeneratedTsFolders) {\n for (const secondaryGeneratedTsFolder of secondaryGeneratedTsFolders) {\n yield path.resolve(secondaryGeneratedTsFolder, dtsFilename);\n }\n }\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"]}
|