|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/// <reference types="node" /> |
| 18 | + |
| 19 | +import { |
| 20 | + readdirSync, |
| 21 | + statSync, |
| 22 | + existsSync, |
| 23 | + rmSync, |
| 24 | + mkdirSync, |
| 25 | + copyFileSync, |
| 26 | + writeFileSync |
| 27 | +} from 'fs'; |
| 28 | +import {join, relative, dirname, resolve, sep} from 'path'; |
| 29 | +import {exec} from 'child_process'; |
| 30 | +import {promisify} from 'util'; |
| 31 | + |
| 32 | +const execAsync = promisify(exec); |
| 33 | +const TEMP_ROOT = '.tsc_check'; |
| 34 | + |
| 35 | +interface Project { |
| 36 | + files: string[]; |
| 37 | + name: string; |
| 38 | + path: string; |
| 39 | +} |
| 40 | + |
| 41 | +interface CheckResult { |
| 42 | + name: string; |
| 43 | + success: boolean; |
| 44 | + output: string; |
| 45 | +} |
| 46 | + |
| 47 | +// Helper to recursively find all files with a specific extension |
| 48 | +function findFiles(dir: string, extension: string, fileList: string[] = []): string[] { |
| 49 | + const files = readdirSync(dir); |
| 50 | + for (const file of files) { |
| 51 | + if (file.endsWith('.js')) continue; |
| 52 | + const filePath = join(dir, file); |
| 53 | + const stat = statSync(filePath); |
| 54 | + if (stat.isDirectory()) { |
| 55 | + if (file !== 'node_modules' && file !== '.git' && file !== TEMP_ROOT) { |
| 56 | + findFiles(filePath, extension, fileList); |
| 57 | + } |
| 58 | + } else if (file.endsWith(extension)) { |
| 59 | + fileList.push(filePath); |
| 60 | + } |
| 61 | + } |
| 62 | + return fileList; |
| 63 | +} |
| 64 | + |
| 65 | +// Find all directories containing appsscript.json |
| 66 | +function findProjectRoots(rootDir: string): string[] { |
| 67 | + return findFiles(rootDir, 'appsscript.json').map((f) => dirname(f)); |
| 68 | +} |
| 69 | + |
| 70 | +function createProjects(rootDir: string, projectRoots: string[], allGsFiles: string[]): Project[] { |
| 71 | + // Holds files that belong to a formal Apps Script project (defined by the presence of appsscript.json). |
| 72 | + const projectGroups = new Map<string, string[]>(); |
| 73 | + |
| 74 | + // Holds "orphan" files that do not belong to any defined Apps Script project (no appsscript.json found). |
| 75 | + const looseGroups = new Map<string, string[]>(); |
| 76 | + |
| 77 | + // Initialize project groups |
| 78 | + for (const p of projectRoots) { |
| 79 | + projectGroups.set(p, []); |
| 80 | + } |
| 81 | + |
| 82 | + for (const file of allGsFiles) { |
| 83 | + let assigned = false; |
| 84 | + let currentDir = dirname(file); |
| 85 | + |
| 86 | + while (currentDir.startsWith(rootDir) && currentDir !== rootDir) { |
| 87 | + if (projectGroups.has(currentDir)) { |
| 88 | + projectGroups.get(currentDir)?.push(file); |
| 89 | + assigned = true; |
| 90 | + break; |
| 91 | + } |
| 92 | + currentDir = dirname(currentDir); |
| 93 | + } |
| 94 | + |
| 95 | + if (!assigned) { |
| 96 | + const dir = dirname(file); |
| 97 | + if (!looseGroups.has(dir)) { |
| 98 | + looseGroups.set(dir, []); |
| 99 | + } |
| 100 | + looseGroups.get(dir)?.push(file); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + const projects: Project[] = []; |
| 105 | + projectGroups.forEach((files, dir) => { |
| 106 | + if (files.length > 0) { |
| 107 | + projects.push({ |
| 108 | + files, |
| 109 | + name: `Project: ${relative(rootDir, dir)}`, |
| 110 | + path: relative(rootDir, dir) |
| 111 | + }); |
| 112 | + } |
| 113 | + }); |
| 114 | + looseGroups.forEach((files, dir) => { |
| 115 | + if (files.length > 0) { |
| 116 | + projects.push({ |
| 117 | + files, |
| 118 | + name: `Loose Project: ${relative(rootDir, dir)}`, |
| 119 | + path: relative(rootDir, dir) |
| 120 | + }); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + return projects; |
| 125 | +} |
| 126 | + |
| 127 | +async function checkProject(project: Project, rootDir: string): Promise<CheckResult> { |
| 128 | + const projectNameSafe = project.name.replace(/[^a-zA-Z0-9]/g, '_'); |
| 129 | + const projectTempDir = join(TEMP_ROOT, projectNameSafe); |
| 130 | + |
| 131 | + // Synchronous setup is fine as it's fast and avoids race conditions on mkdir if we were sharing dirs (we aren't) |
| 132 | + mkdirSync(projectTempDir, {recursive: true}); |
| 133 | + |
| 134 | + for (const file of project.files) { |
| 135 | + const fileRelPath = relative(rootDir, file); |
| 136 | + const destPath = join(projectTempDir, fileRelPath.replace(/\.gs$/, '.js')); |
| 137 | + const destDir = dirname(destPath); |
| 138 | + mkdirSync(destDir, {recursive: true}); |
| 139 | + copyFileSync(file, destPath); |
| 140 | + } |
| 141 | + |
| 142 | + const tsConfig = { |
| 143 | + extends: '../../tsconfig.json', |
| 144 | + compilerOptions: { |
| 145 | + noEmit: true, |
| 146 | + allowJs: true, |
| 147 | + checkJs: true, |
| 148 | + typeRoots: [resolve(rootDir, 'node_modules/@types')] |
| 149 | + }, |
| 150 | + include: ['**/*.js'] |
| 151 | + }; |
| 152 | + |
| 153 | + writeFileSync( |
| 154 | + join(projectTempDir, 'tsconfig.json'), |
| 155 | + JSON.stringify(tsConfig, null, 2) |
| 156 | + ); |
| 157 | + |
| 158 | + try { |
| 159 | + await execAsync(`tsc -p "${projectTempDir}"`, {cwd: rootDir}); |
| 160 | + return {name: project.name, success: true, output: ''}; |
| 161 | + } catch (e: any) { |
| 162 | + const rawOutput = (e.stdout || '') + (e.stderr || ''); |
| 163 | + |
| 164 | + const rewritten = rawOutput.split('\n').map((line: string) => { |
| 165 | + if (line.includes(projectTempDir)) { |
| 166 | + let newLine = line.split(projectTempDir + sep).pop(); |
| 167 | + if (!newLine) { |
| 168 | + return line; |
| 169 | + } |
| 170 | + newLine = newLine.replace(/\.js(:|\()/g, '.gs$1'); |
| 171 | + return newLine; |
| 172 | + } |
| 173 | + return line; |
| 174 | + }).join('\n'); |
| 175 | + |
| 176 | + return {name: project.name, success: false, output: rewritten}; |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +async function main() { |
| 181 | + try { |
| 182 | + const rootDir = resolve('.'); |
| 183 | + const searchArg = process.argv[2]; |
| 184 | + |
| 185 | + // 1. Discovery |
| 186 | + const projectRoots = findProjectRoots(rootDir); |
| 187 | + const allGsFiles = findFiles(rootDir, '.gs'); |
| 188 | + |
| 189 | + // 2. Grouping |
| 190 | + const projects = createProjects(rootDir, projectRoots, allGsFiles); |
| 191 | + |
| 192 | + // 3. Filtering |
| 193 | + const projectsToCheck = projects.filter(p => { |
| 194 | + return !searchArg || p.path.startsWith(searchArg); |
| 195 | + }); |
| 196 | + |
| 197 | + if (projectsToCheck.length === 0) { |
| 198 | + console.log('No projects found matching the search path.'); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + // 4. Setup |
| 203 | + if (existsSync(TEMP_ROOT)) { |
| 204 | + rmSync(TEMP_ROOT, {recursive: true, force: true}); |
| 205 | + } |
| 206 | + mkdirSync(TEMP_ROOT); |
| 207 | + |
| 208 | + console.log(`Checking ${projectsToCheck.length} projects in parallel...`); |
| 209 | + |
| 210 | + // 5. Parallel Execution |
| 211 | + const results = await Promise.all(projectsToCheck.map(p => checkProject(p, rootDir))); |
| 212 | + |
| 213 | + // 6. Reporting |
| 214 | + let hasError = false; |
| 215 | + for (const result of results) { |
| 216 | + if (!result.success) { |
| 217 | + hasError = true; |
| 218 | + console.log(`\n--- Failed: ${result.name} ---`); |
| 219 | + console.log(result.output); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + if (hasError) { |
| 224 | + console.error('\nOne or more checks failed.'); |
| 225 | + process.exit(1); |
| 226 | + } else { |
| 227 | + console.log('\nAll checks passed.'); |
| 228 | + } |
| 229 | + |
| 230 | + } catch (err) { |
| 231 | + console.error('Unexpected error:', err); |
| 232 | + process.exit(1); |
| 233 | + } finally { |
| 234 | + if (existsSync(TEMP_ROOT)) { |
| 235 | + rmSync(TEMP_ROOT, {recursive: true, force: true}); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +main(); |
0 commit comments