|
| 1 | +import { readdirSync } from 'node:fs'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import type { |
| 4 | + InterfaceDeclaration, |
| 5 | + ObjectLiteralExpression, |
| 6 | + ParameterDeclaration, |
| 7 | + ParameterDeclarationStructure, |
| 8 | + TypeParameterDeclaration, |
| 9 | + TypeParameterDeclarationStructure, |
| 10 | +} from 'ts-morph'; |
| 11 | +import { Project, SyntaxKind } from 'ts-morph'; |
| 12 | + |
| 13 | +const RoutesInterfaceName = 'RoutesDeclarations'; |
| 14 | +const CDNRoutesInterfaceName = 'CDNRoutesDeclarations'; |
| 15 | + |
| 16 | +const isInWebsite = __dirname.includes('website'); |
| 17 | + |
| 18 | +const extraPath = isInWebsite ? '../' : ''; |
| 19 | + |
| 20 | +const versions = readdirSync(join(__dirname, extraPath, '../rest')).filter((dir) => /^v\d+$/.exec(dir)); |
| 21 | + |
| 22 | +const generatedRestDirectory = join(__dirname, extraPath, '../_generated_/rest'); |
| 23 | +const globalsFilePath = join(__dirname, extraPath, '../globals.ts'); |
| 24 | + |
| 25 | +function parameterDeclarationToStructure(parameter: ParameterDeclaration): ParameterDeclarationStructure { |
| 26 | + const obj = parameter.getStructure(); |
| 27 | + |
| 28 | + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing |
| 29 | + obj.hasQuestionToken = obj.hasQuestionToken || Boolean(obj.initializer); |
| 30 | + |
| 31 | + delete obj.initializer; |
| 32 | + |
| 33 | + return obj; |
| 34 | +} |
| 35 | + |
| 36 | +function typeParameterToStructure(typeParameter: TypeParameterDeclaration): TypeParameterDeclarationStructure { |
| 37 | + // eslint-disable-next-line sonarjs/prefer-immediate-return |
| 38 | + const obj = typeParameter.getStructure(); |
| 39 | + |
| 40 | + return obj; |
| 41 | +} |
| 42 | + |
| 43 | +function handleTypeParameter(typeParameter: TypeParameterDeclaration, allSeen: Set<string>) { |
| 44 | + const extendsClause = typeParameter.getConstraint(); |
| 45 | + |
| 46 | + if (!extendsClause) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const type = extendsClause.getText(); |
| 51 | + |
| 52 | + if (allSeen.has(type)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + allSeen.add(type); |
| 57 | + |
| 58 | + if (type === 'StorePageAssetFormat') { |
| 59 | + allSeen.add('ImageFormat'); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function handleObject(object: ObjectLiteralExpression, interfaceToAddTo: InterfaceDeclaration) { |
| 64 | + const seenTypeParameters = new Set<string>(); |
| 65 | + |
| 66 | + for (const property of object.getPropertiesWithComments()) { |
| 67 | + const castedMethod = property.asKindOrThrow(SyntaxKind.MethodDeclaration); |
| 68 | + |
| 69 | + const methodName = castedMethod.getName(); |
| 70 | + const methodParameters = castedMethod.getParameters(); |
| 71 | + let methodReturnType = castedMethod.getReturnType().getText(); |
| 72 | + const methodDocs = castedMethod.getJsDocs(); |
| 73 | + |
| 74 | + const returnBody = castedMethod |
| 75 | + .getChildren() |
| 76 | + ?.at(-1) |
| 77 | + ?.getChildren() |
| 78 | + ?.at(1) |
| 79 | + ?.getChildren() |
| 80 | + ?.at(-1) |
| 81 | + ?.asKindOrThrow(SyntaxKind.ReturnStatement); |
| 82 | + |
| 83 | + const asExpression = returnBody?.getChildrenOfKind(SyntaxKind.AsExpression)[0]; |
| 84 | + |
| 85 | + const unionType = asExpression?.getChildrenOfKind(SyntaxKind.UnionType)?.[0]; |
| 86 | + |
| 87 | + // Override with union if it exists in the cast |
| 88 | + if (unionType) { |
| 89 | + methodReturnType = unionType |
| 90 | + .getText() |
| 91 | + .split('\n') |
| 92 | + .map((line) => line.trim()) |
| 93 | + .join(' '); |
| 94 | + } |
| 95 | + |
| 96 | + if (methodReturnType.startsWith('| ')) { |
| 97 | + methodReturnType = methodReturnType.slice(2); |
| 98 | + } |
| 99 | + |
| 100 | + const typeParameters = castedMethod.getTypeParameters(); |
| 101 | + |
| 102 | + for (const typeParameter of typeParameters) { |
| 103 | + handleTypeParameter(typeParameter, seenTypeParameters); |
| 104 | + } |
| 105 | + |
| 106 | + interfaceToAddTo.addMethod({ |
| 107 | + name: methodName, |
| 108 | + parameters: methodParameters.map(parameterDeclarationToStructure), |
| 109 | + typeParameters: typeParameters.map(typeParameterToStructure), |
| 110 | + returnType: methodReturnType, |
| 111 | + leadingTrivia: |
| 112 | + methodDocs |
| 113 | + .map((doc) => doc.getText()) |
| 114 | + .join('\n') |
| 115 | + .replaceAll('\t', '') + '\n', |
| 116 | + }); |
| 117 | + |
| 118 | + for (const overload of castedMethod.getOverloads()) { |
| 119 | + const typeParameters = overload.getTypeParameters(); |
| 120 | + |
| 121 | + for (const typeParameter of typeParameters) { |
| 122 | + handleTypeParameter(typeParameter, seenTypeParameters); |
| 123 | + } |
| 124 | + |
| 125 | + interfaceToAddTo.addMethod({ |
| 126 | + name: overload.getName(), |
| 127 | + parameters: overload.getParameters().map(parameterDeclarationToStructure), |
| 128 | + typeParameters: typeParameters.map(typeParameterToStructure), |
| 129 | + returnType: overload.getReturnType().getText(), |
| 130 | + leadingTrivia: |
| 131 | + overload |
| 132 | + .getJsDocs() |
| 133 | + .map((doc) => doc.getText()) |
| 134 | + .join('\n') |
| 135 | + .replaceAll('\t', '') + '\n', |
| 136 | + }); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return seenTypeParameters; |
| 141 | +} |
| 142 | + |
| 143 | +for (const version of versions) { |
| 144 | + console.log(`Generating interfaces for ${version}...`); |
| 145 | + |
| 146 | + const inputFilePath = join(__dirname, extraPath, `../rest/${version}/index.ts`); |
| 147 | + const generatedRestInterfacesFilePath = join(__dirname, extraPath, `../_generated_/rest/${version}/interfaces.ts`); |
| 148 | + |
| 149 | + const project = new Project({}); |
| 150 | + |
| 151 | + project.addSourceFileAtPathIfExists(inputFilePath); |
| 152 | + project.addSourceFileAtPathIfExists(globalsFilePath); |
| 153 | + project.createDirectory(generatedRestDirectory); |
| 154 | + |
| 155 | + const generatedRestInterfacesFile = project.createSourceFile(generatedRestInterfacesFilePath, undefined, { |
| 156 | + overwrite: true, |
| 157 | + }); |
| 158 | + |
| 159 | + const routesDeclarationInterface = generatedRestInterfacesFile.addInterface({ |
| 160 | + name: RoutesInterfaceName, |
| 161 | + leadingTrivia: '// Automatically generated interface from the Routes object.\n', |
| 162 | + isExported: true, |
| 163 | + }); |
| 164 | + |
| 165 | + const cdnRoutesDeclarationInterface = generatedRestInterfacesFile.addInterface({ |
| 166 | + name: CDNRoutesInterfaceName, |
| 167 | + leadingTrivia: '// Automatically generated interface from the CDN Routes object.\n', |
| 168 | + isExported: true, |
| 169 | + }); |
| 170 | + |
| 171 | + generatedRestInterfacesFile.addImportDeclaration({ |
| 172 | + moduleSpecifier: '../../../globals', |
| 173 | + namedImports: ['Snowflake'], |
| 174 | + isTypeOnly: true, |
| 175 | + }); |
| 176 | + |
| 177 | + const routesObjectFile = project.getSourceFileOrThrow(inputFilePath); |
| 178 | + |
| 179 | + routesObjectFile.addImportDeclaration({ |
| 180 | + moduleSpecifier: `../../_generated_/rest/${version}/interfaces`, |
| 181 | + isTypeOnly: true, |
| 182 | + namedImports: [RoutesInterfaceName, CDNRoutesInterfaceName], |
| 183 | + }); |
| 184 | + |
| 185 | + routesObjectFile.addExportDeclaration({ |
| 186 | + isTypeOnly: true, |
| 187 | + moduleSpecifier: `../../_generated_/rest/${version}/interfaces`, |
| 188 | + leadingTrivia: '// Exports all generated interfaces from the REST API.\n', |
| 189 | + }); |
| 190 | + |
| 191 | + const routesObject = routesObjectFile.getVariableDeclarationOrThrow('Routes'); |
| 192 | + const cdnRoutesObject = routesObjectFile.getVariableDeclaration('CDNRoutes'); |
| 193 | + |
| 194 | + if (!cdnRoutesObject) { |
| 195 | + console.log('Skipping type generation for', version); |
| 196 | + continue; |
| 197 | + } |
| 198 | + |
| 199 | + const routesObjectChildren = routesObject.getChildren(); |
| 200 | + const cdnRoutesObjectChildren = cdnRoutesObject.getChildren(); |
| 201 | + |
| 202 | + const [routesIdentifier] = routesObjectChildren; |
| 203 | + |
| 204 | + const routesObjectDeclaration = routesObject.getInitializerOrThrow(); |
| 205 | + const [cdnRoutesIdentifier] = cdnRoutesObjectChildren; |
| 206 | + const cdnRoutesObjectDeclaration = cdnRoutesObject.getInitializerOrThrow(); |
| 207 | + |
| 208 | + const importsNeededForRoutes = handleObject( |
| 209 | + routesObjectDeclaration.asKindOrThrow(SyntaxKind.ObjectLiteralExpression), |
| 210 | + routesDeclarationInterface, |
| 211 | + ); |
| 212 | + |
| 213 | + const importsNeededForCDNRoutes = handleObject( |
| 214 | + cdnRoutesObjectDeclaration.asKindOrThrow(SyntaxKind.ObjectLiteralExpression), |
| 215 | + cdnRoutesDeclarationInterface, |
| 216 | + ); |
| 217 | + |
| 218 | + const typesToImportFromOriginalFile = new Set<string>([...importsNeededForRoutes, ...importsNeededForCDNRoutes]); |
| 219 | + |
| 220 | + if (typesToImportFromOriginalFile.size > 0) { |
| 221 | + generatedRestInterfacesFile.addImportDeclaration({ |
| 222 | + moduleSpecifier: `../../../rest/${version}/index`, |
| 223 | + isTypeOnly: true, |
| 224 | + namedImports: [...typesToImportFromOriginalFile].sort((a, b) => a.localeCompare(b)), |
| 225 | + }); |
| 226 | + } |
| 227 | + |
| 228 | + routesIdentifier.replaceWithText(`Routes: ${RoutesInterfaceName}`); |
| 229 | + cdnRoutesIdentifier.replaceWithText(`CDNRoutes: ${CDNRoutesInterfaceName}`); |
| 230 | + |
| 231 | + project.saveSync(); |
| 232 | +} |
0 commit comments