|
| 1 | +/** |
| 2 | + * Copyright 2024 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 | +import fs from "node:fs"; |
| 18 | +import prettier from "prettier"; |
| 19 | +import ts, { factory } from "typescript"; |
| 20 | + |
| 21 | +import type { |
| 22 | + CustomElementDeclaration, |
| 23 | + Declaration, |
| 24 | + Package, |
| 25 | +} from "custom-elements-manifest/schema"; |
| 26 | + |
| 27 | +const manifest = JSON.parse( |
| 28 | + fs.readFileSync("custom-elements.json", "utf-8"), |
| 29 | +) as Package; |
| 30 | + |
| 31 | +function isCustomElementDeclaration( |
| 32 | + declaration?: Declaration, |
| 33 | +): declaration is CustomElementDeclaration { |
| 34 | + return (declaration as CustomElementDeclaration)?.customElement === true; |
| 35 | +} |
| 36 | + |
| 37 | +const customElements = manifest.modules |
| 38 | + .flatMap((module) => module.declarations) |
| 39 | + .filter(isCustomElementDeclaration) |
| 40 | + .sort((a, b) => a.tagName?.localeCompare(b.tagName ?? "") ?? 0); |
| 41 | + |
| 42 | +const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); |
| 43 | +const resultFile = ts.createSourceFile( |
| 44 | + "props.ts", |
| 45 | + "", |
| 46 | + ts.ScriptTarget.Latest, |
| 47 | + false, |
| 48 | + ts.ScriptKind.TS, |
| 49 | +); |
| 50 | + |
| 51 | +const statements: ts.Statement[] = []; |
| 52 | + |
| 53 | +for (const element of customElements) { |
| 54 | + const properties: ts.PropertySignature[] = []; |
| 55 | + |
| 56 | + for (const attribute of element.attributes ?? []) { |
| 57 | + // Create the property signature |
| 58 | + const prop = factory.createPropertySignature( |
| 59 | + undefined, // No modifiers |
| 60 | + attribute.name.includes("-") |
| 61 | + ? factory.createStringLiteral(`${attribute.name}`) |
| 62 | + : factory.createIdentifier(attribute.name), |
| 63 | + factory.createToken(ts.SyntaxKind.QuestionToken), // Make props optional |
| 64 | + factory.createTypeReferenceNode( |
| 65 | + attribute.type?.text ?? "unknown", |
| 66 | + undefined, |
| 67 | + ), |
| 68 | + ); |
| 69 | + |
| 70 | + if (attribute.description) { |
| 71 | + const jsDocText = `* ${attribute.description.replace(/\n/g, "\n * ")}`; |
| 72 | + ts.addSyntheticLeadingComment( |
| 73 | + prop, |
| 74 | + ts.SyntaxKind.MultiLineCommentTrivia, |
| 75 | + jsDocText, |
| 76 | + true, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + properties.push(prop); |
| 81 | + } |
| 82 | + |
| 83 | + const propsInterface = factory.createInterfaceDeclaration( |
| 84 | + [factory.createModifier(ts.SyntaxKind.ExportKeyword)], |
| 85 | + factory.createIdentifier(`${element.name}Props`), |
| 86 | + undefined, |
| 87 | + undefined, |
| 88 | + properties, |
| 89 | + ); |
| 90 | + |
| 91 | + statements.push(propsInterface); |
| 92 | +} |
| 93 | + |
| 94 | +const outputFile = "src/drive-picker/props.ts"; |
| 95 | +const resultText = await prettier.format( |
| 96 | + printer.printList( |
| 97 | + ts.ListFormat.MultiLine, |
| 98 | + factory.createNodeArray(statements), |
| 99 | + resultFile, |
| 100 | + ), |
| 101 | + { |
| 102 | + parser: "typescript", |
| 103 | + useTabs: true, |
| 104 | + }, |
| 105 | +); |
| 106 | + |
| 107 | +const licenseHeader = `/** |
| 108 | + * Copyright 2025 Google LLC |
| 109 | + * |
| 110 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 111 | + * you may not use this file except in compliance with the License. |
| 112 | + * You may obtain a copy of the License at |
| 113 | + * |
| 114 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 115 | + * |
| 116 | + * Unless required by applicable law or agreed to in writing, software |
| 117 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 118 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 119 | + * See the License for the specific language governing permissions and |
| 120 | + * limitations under the License. |
| 121 | + */`; |
| 122 | + |
| 123 | +fs.writeFileSync(outputFile, `${licenseHeader}\n\n${resultText}`, "utf8"); |
| 124 | +console.log(`Generated Drive Picker prop type definitions at ${outputFile}`); |
0 commit comments