|
| 1 | +import path from 'path'; |
| 2 | +import fs from 'fs'; |
| 3 | +import * as ts from 'typescript'; |
| 4 | +import { Project, ProjectNames, Projects } from './api/utils'; |
| 5 | + |
| 6 | +const workspaceRoot = path.resolve(__dirname, '../../'); |
| 7 | + |
| 8 | +interface CreateProgramOptions { |
| 9 | + name: ProjectNames; |
| 10 | + rootPath: string; |
| 11 | + /** |
| 12 | + * Config to use to build this package. |
| 13 | + * The path must be relative to the root path. |
| 14 | + */ |
| 15 | + tsConfigPath: string; |
| 16 | + /** |
| 17 | + * File used as root of the package. |
| 18 | + * The path must be relative to the root path. |
| 19 | + */ |
| 20 | + entryPointPath: string; |
| 21 | + /** |
| 22 | + * Folder containing all the components of this package. |
| 23 | + * The path must be relative to the root path. |
| 24 | + */ |
| 25 | + componentsFolder?: string; |
| 26 | + /** |
| 27 | + * Additional files containing components outside the component's folder. |
| 28 | + * The path must be relative to the root path. |
| 29 | + */ |
| 30 | + otherComponentFiles?: string[]; |
| 31 | +} |
| 32 | + |
| 33 | +const createProject = (options: CreateProgramOptions): Project => { |
| 34 | + const { name, tsConfigPath, rootPath, entryPointPath, componentsFolder, otherComponentFiles } = |
| 35 | + options; |
| 36 | + |
| 37 | + const tsConfigFile = ts.readConfigFile(tsConfigPath, (filePath) => |
| 38 | + fs.readFileSync(filePath).toString(), |
| 39 | + ); |
| 40 | + |
| 41 | + if (tsConfigFile.error) { |
| 42 | + throw tsConfigFile.error; |
| 43 | + } |
| 44 | + |
| 45 | + const tsConfigFileContent = ts.parseJsonConfigFileContent( |
| 46 | + tsConfigFile.config, |
| 47 | + ts.sys, |
| 48 | + path.dirname(tsConfigPath), |
| 49 | + ); |
| 50 | + |
| 51 | + if (tsConfigFileContent.errors.length > 0) { |
| 52 | + throw tsConfigFileContent.errors[0]; |
| 53 | + } |
| 54 | + |
| 55 | + const fullEntryPointPath = path.join(rootPath, entryPointPath); |
| 56 | + |
| 57 | + const program = ts.createProgram({ |
| 58 | + rootNames: [fullEntryPointPath], |
| 59 | + options: tsConfigFileContent.options, |
| 60 | + }); |
| 61 | + |
| 62 | + const checker = program.getTypeChecker(); |
| 63 | + const sourceFile = program.getSourceFile(fullEntryPointPath); |
| 64 | + |
| 65 | + const exports = Object.fromEntries( |
| 66 | + checker.getExportsOfModule(checker.getSymbolAtLocation(sourceFile!)!).map((symbol) => { |
| 67 | + return [symbol.name, symbol]; |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + return { |
| 72 | + name, |
| 73 | + exports, |
| 74 | + program, |
| 75 | + checker, |
| 76 | + workspaceRoot, |
| 77 | + componentsFolder: componentsFolder ? path.join(rootPath, componentsFolder) : undefined, |
| 78 | + otherComponentFiles: otherComponentFiles?.map((file) => path.join(rootPath, file)), |
| 79 | + prettierConfigPath: path.join(workspaceRoot, 'prettier.config.js'), |
| 80 | + }; |
| 81 | +}; |
| 82 | + |
| 83 | +export const getTypeScriptProjects = () => { |
| 84 | + const projects: Projects = new Map(); |
| 85 | + |
| 86 | + projects.set( |
| 87 | + 'x-data-grid-pro', |
| 88 | + createProject({ |
| 89 | + name: 'x-data-grid-pro', |
| 90 | + rootPath: path.join(workspaceRoot, 'packages/grid/x-data-grid-pro'), |
| 91 | + tsConfigPath: 'tsconfig.json', |
| 92 | + entryPointPath: 'src/index.ts', |
| 93 | + componentsFolder: 'src/internals/components', |
| 94 | + otherComponentFiles: ['src/DataGridPro.tsx'], |
| 95 | + }), |
| 96 | + ); |
| 97 | + |
| 98 | + projects.set( |
| 99 | + 'x-data-grid', |
| 100 | + createProject({ |
| 101 | + name: 'x-data-grid', |
| 102 | + rootPath: path.join(workspaceRoot, 'packages/grid/x-data-grid'), |
| 103 | + tsConfigPath: 'tsconfig.json', |
| 104 | + entryPointPath: 'src/index.ts', |
| 105 | + componentsFolder: 'src/internals/components', |
| 106 | + otherComponentFiles: ['src/DataGrid.tsx'], |
| 107 | + }), |
| 108 | + ); |
| 109 | + |
| 110 | + return projects; |
| 111 | +}; |
0 commit comments