|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
2 | 2 | // See LICENSE in the project root for license information. |
3 | 3 |
|
4 | | -import * as path from 'path'; |
| 4 | +import path from 'node:path'; |
| 5 | +import type { IRunScriptOptions } from '@rushstack/heft'; |
5 | 6 |
|
6 | | -import { FileSystem, Import, Path } from '@rushstack/node-core-library'; |
| 7 | +import { Async, AsyncQueue, FileSystem, type FolderItem, Import, Path } from '@rushstack/node-core-library'; |
7 | 8 |
|
8 | | -function generateLibFilesRecursively(options: { |
9 | | - parentSourcePath: string; |
10 | | - parentTargetPath: string; |
11 | | - parentSrcImportPathWithSlash: string; |
12 | | - libShimIndexPath: string; |
13 | | -}): void { |
14 | | - for (const folderItem of FileSystem.readFolderItems(options.parentSourcePath)) { |
15 | | - const sourcePath: string = path.join(options.parentSourcePath, folderItem.name); |
16 | | - const targetPath: string = path.join(options.parentTargetPath, folderItem.name); |
| 9 | +const JS_FILE_EXTENSION: '.js' = '.js'; |
17 | 10 |
|
18 | | - if (folderItem.isDirectory()) { |
19 | | - // create destination folder |
20 | | - FileSystem.ensureEmptyFolder(targetPath); |
21 | | - generateLibFilesRecursively({ |
22 | | - parentSourcePath: sourcePath, |
23 | | - parentTargetPath: targetPath, |
24 | | - parentSrcImportPathWithSlash: options.parentSrcImportPathWithSlash + folderItem.name + '/', |
25 | | - libShimIndexPath: options.libShimIndexPath |
26 | | - }); |
27 | | - } else { |
28 | | - if (folderItem.name.endsWith('.d.ts')) { |
29 | | - FileSystem.copyFile({ |
30 | | - sourcePath: sourcePath, |
31 | | - destinationPath: targetPath |
32 | | - }); |
33 | | - } else if (folderItem.name.endsWith('.js')) { |
34 | | - const srcImportPath: string = options.parentSrcImportPathWithSlash + path.parse(folderItem.name).name; |
35 | | - const shimPath: string = path.relative(options.parentTargetPath, options.libShimIndexPath); |
36 | | - const shimPathLiteral: string = JSON.stringify(Path.convertToSlashes(shimPath)); |
37 | | - const srcImportPathLiteral: string = JSON.stringify(srcImportPath); |
38 | | - |
39 | | - FileSystem.writeFile( |
40 | | - targetPath, |
41 | | - // Example: |
42 | | - // module.exports = require("../../../lib-shim/index")._rushSdk_loadInternalModule("logic/policy/GitEmailPolicy"); |
43 | | - `module.exports = require(${shimPathLiteral})._rushSdk_loadInternalModule(${srcImportPathLiteral});` |
44 | | - ); |
45 | | - } |
| 11 | +// Entry point invoked by "runScript" action from config/heft.json |
| 12 | +export async function runAsync(options: IRunScriptOptions): Promise<void> { |
| 13 | + const { |
| 14 | + heftConfiguration: { buildFolderPath }, |
| 15 | + heftTaskSession: { |
| 16 | + logger: { terminal } |
46 | 17 | } |
47 | | - } |
48 | | -} |
| 18 | + } = options; |
49 | 19 |
|
50 | | -// Entry point invoked by "runScript" action from config/heft.json |
51 | | -export async function runAsync(): Promise<void> { |
52 | 20 | const rushLibFolder: string = Import.resolvePackage({ |
53 | | - baseFolderPath: __dirname, |
| 21 | + baseFolderPath: buildFolderPath, |
54 | 22 | packageName: '@microsoft/rush-lib', |
55 | 23 | useNodeJSResolver: true |
56 | 24 | }); |
57 | 25 |
|
58 | | - const stubsTargetPath: string = path.resolve(__dirname, '../lib'); |
59 | | - // eslint-disable-next-line no-console |
60 | | - console.log('generate-stubs: Generating stub files under: ' + stubsTargetPath); |
61 | | - generateLibFilesRecursively({ |
62 | | - parentSourcePath: path.join(rushLibFolder, 'lib'), |
63 | | - parentTargetPath: stubsTargetPath, |
64 | | - parentSrcImportPathWithSlash: '', |
65 | | - libShimIndexPath: path.join(__dirname, '../lib-shim/index') |
66 | | - }); |
67 | | - // eslint-disable-next-line no-console |
68 | | - console.log('generate-stubs: Completed successfully.'); |
| 26 | + const inFolderPath: string = `${rushLibFolder}/lib-esm`; |
| 27 | + const stubsTargetPath: string = `${buildFolderPath}/lib`; |
| 28 | + const libShimsIndexPath: string = `${buildFolderPath}/lib-shim/index`; |
| 29 | + |
| 30 | + terminal.writeLine(' Generating stub files under: ' + stubsTargetPath); |
| 31 | + |
| 32 | + const folderPathQueue: AsyncQueue<string | undefined> = new AsyncQueue([undefined]); |
| 33 | + await Async.forEachAsync( |
| 34 | + folderPathQueue, |
| 35 | + async ([relativeFolderPath, callback]) => { |
| 36 | + let folderPath: string; |
| 37 | + let targetFolderPath: string; |
| 38 | + if (relativeFolderPath) { |
| 39 | + folderPath = `${inFolderPath}/${relativeFolderPath}`; |
| 40 | + targetFolderPath = `${stubsTargetPath}/${relativeFolderPath}`; |
| 41 | + } else { |
| 42 | + folderPath = inFolderPath; |
| 43 | + targetFolderPath = stubsTargetPath; |
| 44 | + } |
| 45 | + |
| 46 | + const folderItems: FolderItem[] = await FileSystem.readFolderItemsAsync(folderPath); |
| 47 | + for (const folderItem of folderItems) { |
| 48 | + const itemName: string = folderItem.name; |
| 49 | + |
| 50 | + if (folderItem.isDirectory()) { |
| 51 | + const relativeItemPath: string = relativeFolderPath |
| 52 | + ? `${relativeFolderPath}/${itemName}` |
| 53 | + : itemName; |
| 54 | + folderPathQueue.push(relativeItemPath); |
| 55 | + } else if (folderItem.isFile() && itemName.endsWith(JS_FILE_EXTENSION)) { |
| 56 | + const fileBaseName: string = path.parse(itemName).name; |
| 57 | + const srcImportPath: string = relativeFolderPath |
| 58 | + ? `${relativeFolderPath}/${fileBaseName}` |
| 59 | + : fileBaseName; |
| 60 | + const shimImportPath: string = path.relative(folderPath, libShimsIndexPath); |
| 61 | + |
| 62 | + const shimPathLiteral: string = JSON.stringify(Path.convertToSlashes(shimImportPath)); |
| 63 | + const srcImportPathLiteral: string = JSON.stringify(srcImportPath); |
| 64 | + |
| 65 | + FileSystem.writeFile( |
| 66 | + `${targetFolderPath}/${itemName}`, |
| 67 | + // Example: |
| 68 | + // module.exports = require("../../../lib-shim/index")._rushSdk_loadInternalModule("logic/policy/GitEmailPolicy"); |
| 69 | + `module.exports = require(${shimPathLiteral})._rushSdk_loadInternalModule(${srcImportPathLiteral});` |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + callback(); |
| 74 | + }, |
| 75 | + { concurrency: 10 } |
| 76 | + ); |
| 77 | + |
| 78 | + terminal.writeLine('Completed successfully.'); |
69 | 79 | } |
0 commit comments