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