Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | import * as fs from 'node:fs'; |
| 2 | import * as path from 'node:path'; |
| 3 | // TS7 (tsgo) ships no JS compiler API yet |
| 4 | // (see: https://devblogs.microsoft.com/typescript/announcing-typescript-7-0/#the-road-forward). |
| 5 | // Use the TS6 alias for transpileModule for now. |
| 6 | import ts from 'typescript6'; |
| 7 | |
| 8 | const repoRoot = process.cwd(); |
| 9 | const examplesRoot = path.join(repoRoot, 'ts', 'examples'); |
| 10 | const allowedSourceExtensions = new Set(['.js', '.jsx', '.mjs', '.mts', '.ts', '.tsx', '.json']); |
| 11 | |
| 12 | type ValidationIssue = { |
| 13 | example: string; |
| 14 | file: string; |
| 15 | message: string; |
| 16 | }; |
| 17 | |
| 18 | const issues: ValidationIssue[] = []; |
| 19 | |
| 20 | const addIssue = (example: string, file: string, message: string) => { |
| 21 | issues.push({ example, file, message }); |
| 22 | }; |
| 23 | |
| 24 | const collectFiles = (dir: string): string[] => { |
| 25 | const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 26 | return entries.flatMap(entry => { |
| 27 | const fullPath = path.join(dir, entry.name); |
| 28 | if (entry.isDirectory()) { |
| 29 | return collectFiles(fullPath); |
| 30 | } |
| 31 | return [fullPath]; |
| 32 | }); |
| 33 | }; |
| 34 | |
| 35 | const validatePackageJson = (exampleName: string, exampleDir: string) => { |
| 36 | const packageJsonPath = path.join(exampleDir, 'package.json'); |
| 37 | |
| 38 | if (!fs.existsSync(packageJsonPath)) { |
| 39 | addIssue(exampleName, packageJsonPath, 'Missing package.json'); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | try { |
| 44 | const raw = fs.readFileSync(packageJsonPath, 'utf8'); |
| 45 | const pkg = JSON.parse(raw) as { |
| 46 | dependencies?: Record<string, string>; |
| 47 | devDependencies?: Record<string, string>; |
| 48 | name?: string; |
| 49 | private?: boolean; |
| 50 | scripts?: Record<string, string>; |
| 51 | }; |
| 52 | |
| 53 | if (!pkg.name?.endsWith('-example')) { |
| 54 | addIssue(exampleName, packageJsonPath, 'Package name should end with "-example"'); |
| 55 | } |
| 56 | |
| 57 | if (pkg.private !== true) { |
| 58 | addIssue(exampleName, packageJsonPath, 'Examples must be marked private'); |
| 59 | } |
| 60 | |
| 61 | if (!pkg.scripts || (!pkg.scripts.start && !pkg.scripts.dev)) { |
| 62 | addIssue(exampleName, packageJsonPath, 'Examples must expose a start or dev script'); |
| 63 | } |
| 64 | |
| 65 | const allDeps = { |
| 66 | ...(pkg.dependencies ?? {}), |
| 67 | ...(pkg.devDependencies ?? {}), |
| 68 | }; |
| 69 | |
| 70 | if (!Object.keys(allDeps).some(dep => dep.startsWith('@composio/'))) { |
| 71 | addIssue( |
| 72 | exampleName, |
| 73 | packageJsonPath, |
| 74 | 'Examples must depend on at least one @composio package' |
| 75 | ); |
| 76 | } |
| 77 | } catch (error) { |
| 78 | addIssue( |
| 79 | exampleName, |
| 80 | packageJsonPath, |
| 81 | `Invalid package.json: ${error instanceof Error ? error.message : String(error)}` |
| 82 | ); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | const validateTsconfig = (exampleName: string, exampleDir: string) => { |
| 87 | const tsconfigPath = path.join(exampleDir, 'tsconfig.json'); |
| 88 | |
| 89 | if (!fs.existsSync(tsconfigPath)) { |
| 90 | addIssue(exampleName, tsconfigPath, 'Missing tsconfig.json'); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | try { |
| 95 | const raw = fs.readFileSync(tsconfigPath, 'utf8'); |
| 96 | const parsed = ts.parseConfigFileTextToJson(tsconfigPath, raw); |
| 97 | |
| 98 | if (parsed.error) { |
| 99 | const message = ts.flattenDiagnosticMessageText(parsed.error.messageText, '\n'); |
| 100 | addIssue(exampleName, tsconfigPath, `Invalid tsconfig.json: ${message}`); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | const config = parsed.config as { include?: string[] } | undefined; |
| 105 | if (!Array.isArray(config?.include) || config.include.length === 0) { |
| 106 | addIssue(exampleName, tsconfigPath, 'tsconfig.json should define a non-empty include array'); |
| 107 | } |
| 108 | } catch (error) { |
| 109 | addIssue( |
| 110 | exampleName, |
| 111 | tsconfigPath, |
| 112 | `Invalid tsconfig.json: ${error instanceof Error ? error.message : String(error)}` |
| 113 | ); |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | const validateSourceFile = (exampleName: string, filePath: string) => { |
| 118 | const extension = path.extname(filePath); |
| 119 | const relativePath = path.relative(repoRoot, filePath); |
| 120 | |
| 121 | if (extension === '.json') { |
| 122 | try { |
| 123 | JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 124 | } catch (error) { |
| 125 | addIssue( |
| 126 | exampleName, |
| 127 | relativePath, |
| 128 | `Invalid JSON: ${error instanceof Error ? error.message : String(error)}` |
| 129 | ); |
| 130 | } |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | const sourceText = fs.readFileSync(filePath, 'utf8'); |
| 135 | const isTsFile = ['.ts', '.tsx', '.mts'].includes(extension); |
| 136 | const scriptKind = |
| 137 | extension === '.tsx' |
| 138 | ? ts.ScriptKind.TSX |
| 139 | : extension === '.jsx' |
| 140 | ? ts.ScriptKind.JSX |
| 141 | : extension === '.js' |
| 142 | ? ts.ScriptKind.JS |
| 143 | : extension === '.mjs' |
| 144 | ? ts.ScriptKind.JS |
| 145 | : ts.ScriptKind.TS; |
| 146 | |
| 147 | const transpiled = ts.transpileModule(sourceText, { |
| 148 | compilerOptions: { |
| 149 | allowJs: true, |
| 150 | module: ts.ModuleKind.ESNext, |
| 151 | target: ts.ScriptTarget.ES2022, |
| 152 | }, |
| 153 | fileName: filePath, |
| 154 | reportDiagnostics: true, |
| 155 | }); |
| 156 | |
| 157 | for (const diagnostic of transpiled.diagnostics ?? []) { |
| 158 | if (diagnostic.category !== ts.DiagnosticCategory.Error) { |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); |
| 163 | const location = |
| 164 | diagnostic.start !== undefined |
| 165 | ? ts.getLineAndCharacterOfPosition( |
| 166 | ts.createSourceFile(filePath, sourceText, ts.ScriptTarget.ES2022, true, scriptKind), |
| 167 | diagnostic.start |
| 168 | ) |
| 169 | : undefined; |
| 170 | |
| 171 | const formattedLocation = location ? `:${location.line + 1}:${location.character + 1}` : ''; |
| 172 | |
| 173 | addIssue(exampleName, `${relativePath}${formattedLocation}`, `Syntax error: ${message}`); |
| 174 | } |
| 175 | |
| 176 | if (isTsFile && sourceText.trim().length === 0) { |
| 177 | addIssue(exampleName, relativePath, 'Source file must not be empty'); |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | const validateExample = (exampleDirName: string) => { |
| 182 | const exampleDir = path.join(examplesRoot, exampleDirName); |
| 183 | const srcDir = path.join(exampleDir, 'src'); |
| 184 | |
| 185 | validatePackageJson(exampleDirName, exampleDir); |
| 186 | validateTsconfig(exampleDirName, exampleDir); |
| 187 | |
| 188 | if (!fs.existsSync(srcDir) || !fs.statSync(srcDir).isDirectory()) { |
| 189 | addIssue(exampleDirName, path.relative(repoRoot, srcDir), 'Missing src directory'); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | const sourceFiles = collectFiles(srcDir).filter(file => |
| 194 | allowedSourceExtensions.has(path.extname(file)) |
| 195 | ); |
| 196 | |
| 197 | if (sourceFiles.length === 0) { |
| 198 | addIssue(exampleDirName, path.relative(repoRoot, srcDir), 'No source files found'); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | for (const filePath of sourceFiles) { |
| 203 | validateSourceFile(exampleDirName, filePath); |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | const exampleDirs = fs |
| 208 | .readdirSync(examplesRoot, { withFileTypes: true }) |
| 209 | .filter(entry => entry.isDirectory()) |
| 210 | .map(entry => entry.name) |
| 211 | .sort(); |
| 212 | |
| 213 | for (const exampleDirName of exampleDirs) { |
| 214 | validateExample(exampleDirName); |
| 215 | } |
| 216 | |
| 217 | if (issues.length > 0) { |
| 218 | for (const issue of issues) { |
| 219 | console.error(`[${issue.example}] ${issue.file}: ${issue.message}`); |
| 220 | } |
| 221 | console.error(`\nExample validation failed with ${issues.length} issue(s).`); |
| 222 | process.exit(1); |
| 223 | } |
| 224 | |
| 225 | console.log(`Validated ${exampleDirs.length} example packages.`); |