alveifbklsiu259 commented on code in PR #32261: URL: https://github.com/apache/superset/pull/32261#discussion_r1961468780
########## scripts/check-type.js: ########## @@ -0,0 +1,244 @@ +#!/usr/bin/env node + +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// @ts-check +const { exit } = require("node:process"); +const { join, dirname, normalize, sep } = require("node:path"); +const { readdir, stat } = require("node:fs/promises"); +const { existsSync } = require("node:fs"); +const { chdir, cwd } = require("node:process"); +const { createRequire } = require("node:module"); + +const SUPERSET_ROOT = dirname(__dirname); +const PACKAGE_ARG_REGEX = /^package=/; +const EXCLUDE_DECLARATION_DIR_REGEX = /^excludeDeclarationDir=/; +const DECLARATION_FILE_REGEX = /\.d\.ts$/; + +void (async () => { + const args = process.argv.slice(2); + const { + matchedArgs: [packageArg, excludeDeclarationDirArg], + remainingArgs, + } = extractArgs(args, [PACKAGE_ARG_REGEX, EXCLUDE_DECLARATION_DIR_REGEX]); + + if (!packageArg) { + console.error("package is not specified"); + exit(1); + } + + const packageRootDir = await getPackage(packageArg); + const updatedArgs = removePackageSegment(remainingArgs, packageRootDir); + const argsStr = updatedArgs.join(" "); + + const excludedDeclarationDirs = getExcludedDeclarationDirs( + excludeDeclarationDirArg + ); + let declarationFiles = await getFilesRecursively( + packageRootDir, + DECLARATION_FILE_REGEX, + excludedDeclarationDirs + ); + declarationFiles = removePackageSegment(declarationFiles, packageRootDir); + const declarationFilesStr = declarationFiles.join(" "); + + const packageRootDirAbsolute = join(SUPERSET_ROOT, packageRootDir); + const tsConfig = getTsConfig(packageRootDirAbsolute); + const command = `--noEmit --allowJs --composite false --project ${tsConfig} ${argsStr} ${declarationFilesStr}`; + + try { + chdir(packageRootDirAbsolute); + // Please ensure that tscw-config is installed in the package being type-checked. + const tscw = packageRequire("tscw-config"); + const child = await tscw`${command}`; + + if (child.stdout) { + console.log(child.stdout); + } + + if (child.stderr) { + console.error(child.stderr); + } + + exit(child.exitCode); + } catch (e) { + console.error("Failed to execute type checking:", e); + console.error("Package:", packageRootDir); + console.error("Command:", `tscw ${command}`); + exit(1); + } +})(); + +/** + * @param {string} dir + * @param {RegExp} regex + * @param {string[]} excludedDirs + * + * @returns {Promise<string[]>} + */ + +async function getFilesRecursively(dir, regex, excludedDirs) { + try { + const files = await readdir(dir, { withFileTypes: true }); + const recursivePromises = []; + const result = []; + + for (const file of files) { + const fullPath = join(dir, file.name); + const shouldExclude = excludedDirs.some((excludedDir) => + normalize(fullPath).includes(normalize(excludedDir)) + ); + + if (file.isDirectory() && !shouldExclude) { + recursivePromises.push( + getFilesRecursively(fullPath, regex, excludedDirs) + ); + } else if (regex.test(file.name)) { + result.push(fullPath); + } + } + + const recursiveResults = await Promise.all(recursivePromises); Review Comment: @korbit-ai After analyzing the memory impact, I found that the increase is not significant enough to warrant the added complexity of introducing a semaphore or concurrency limit. Implementing such a solution would complicate the codebase. I believe the current implementation is sufficient for our use case and does not pose any immediate performance concerns. Maybe we will consider revisiting this approach if we encounter any memory-related issues in the future. ```ts const initial = process.memoryUsage(); let declarationFiles = await getFilesRecursively( packageRootDir, DECLARATION_FILE_REGEX, excludedDeclarationDirs ); const final = process.memoryUsage(); const memoryDiff = { rss: `${Math.round((final.rss - initial.rss) / (1024 * 1024))} MB`, heapTotal: `${Math.round( (final.heapTotal - initial.heapTotal) / (1024 * 1024) )} MB`, heapUsed: `${Math.round( (final.heapUsed - initial.heapUsed) / (1024 * 1024) )} MB`, external: `${Math.round( (final.external - initial.external) / (1024 * 1024) )} MB`, }; console.log("Memory Difference:", memoryDiff); // Memory Difference: { rss: '16 MB', heapTotal: '9 MB', heapUsed: '3 MB', external: '0 MB' } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org