* Generates TypeScript definition file (.d.ts) for a package. * * @param {*} workspaceName * @param {string} definitionsPath The path of the .d.ts file to generate. * @param {*} configurationPath * @param {*} processSourceFunc * @param {*} processModulesFunc * @param {*} importModules * @ret
( workspaceName, definitionsPath, configurationPath, processSourceFunc, processModulesFunc, importModules, )
| 1011 | * @returns |
| 1012 | */ |
| 1013 | function generateTypeScriptDefinitions( |
| 1014 | workspaceName, |
| 1015 | definitionsPath, |
| 1016 | configurationPath, |
| 1017 | processSourceFunc, |
| 1018 | processModulesFunc, |
| 1019 | importModules, |
| 1020 | ) { |
| 1021 | // Run JSDoc with tsd-jsdoc to generate an initial definition file. |
| 1022 | execSync(`npx jsdoc --configure ${configurationPath}`, { |
| 1023 | stdio: `inherit`, |
| 1024 | }); |
| 1025 | |
| 1026 | let source = readFileSync(definitionsPath).toString(); |
| 1027 | |
| 1028 | if (processSourceFunc) { |
| 1029 | source = processSourceFunc(definitionsPath, source); |
| 1030 | } |
| 1031 | |
| 1032 | // The next step is to find the list of Cesium modules exported by the Cesium API |
| 1033 | // So that we can map these modules with a link back to their original source file. |
| 1034 | |
| 1035 | const regex = /^declare[ const ]*(function|class|namespace|enum) (.+)/gm; |
| 1036 | let matches; |
| 1037 | let publicModules = new Set(); |
| 1038 | |
| 1039 | while ((matches = regex.exec(source))) { |
| 1040 | const moduleName = matches[2].match(/([^<\s|\(]+)/); |
| 1041 | publicModules.add(moduleName[1]); |
| 1042 | } |
| 1043 | |
| 1044 | if (processModulesFunc) { |
| 1045 | publicModules = processModulesFunc(publicModules); |
| 1046 | } |
| 1047 | |
| 1048 | source = fixTypescriptDefinitionsSource(source); |
| 1049 | |
| 1050 | if (importModules) { |
| 1051 | let imports = ""; |
| 1052 | Object.keys(importModules).forEach((workspace) => { |
| 1053 | const workspaceModules = Array.from(importModules[workspace]).filter( |
| 1054 | (importModule) => source.indexOf(importModule) !== -1, |
| 1055 | ); |
| 1056 | imports += `import { ${workspaceModules.join( |
| 1057 | ",\n", |
| 1058 | )} } from "@${scope}/${workspace}";\n`; |
| 1059 | }); |
| 1060 | source = imports + source; |
| 1061 | } |
| 1062 | |
| 1063 | // Wrap the source to actually be inside of a declared cesium module |
| 1064 | // and add any workaround and private utility types. |
| 1065 | source = `declare module "@${scope}/${workspaceName}" { |
| 1066 | ${source} |
| 1067 | } |
| 1068 | `; |
| 1069 | |
| 1070 | // Write the final source file back out |
no test coverage detected
searching dependent graphs…