|
| 1 | +import { CaseData } from "../../data.js"; |
| 2 | +import { Structure } from "../../writing/writeStructure.js"; |
| 3 | +import { createESLintConfigFile } from "../files/createESLintConfigFile.js"; |
| 4 | +import { createStandardTSConfigFile } from "../files/createStandardTSConfigFile.js"; |
| 5 | +import { range } from "../utils.js"; |
| 6 | + |
| 7 | +function createExampleFile(index: number) { |
| 8 | + return [ |
| 9 | + index > 2 && |
| 10 | + range(1, index) |
| 11 | + .map((i) => `export * as nested${i} from "./nested${i}/index.js";`) |
| 12 | + .join("\n\t\t"), |
| 13 | + ` |
| 14 | + export async function example${index}(prefix: string) { |
| 15 | + await Promise.resolve(); |
| 16 | + return prefix + "" + ${index}; |
| 17 | + } |
| 18 | + `, |
| 19 | + ] |
| 20 | + .filter(Boolean) |
| 21 | + .join("\n\n"); |
| 22 | +} |
| 23 | + |
| 24 | +function createIndexFile(count: number) { |
| 25 | + const indices = count > 1 ? range(0, count - 1) : []; |
| 26 | + |
| 27 | + return ` |
| 28 | + import { example0 } from "./nested0/index.js"; |
| 29 | + |
| 30 | + export async function root() { |
| 31 | + // Lint report: no-floating-promises |
| 32 | + example0(""); |
| 33 | +
|
| 34 | + // No lint report |
| 35 | + await example0(""); |
| 36 | + } |
| 37 | +
|
| 38 | + ${indices.map((index) => `export * as nested${index} from "./nested${index}/index.js";`).join("\n\t\t")} |
| 39 | + `; |
| 40 | +} |
| 41 | + |
| 42 | +function createNestedDirectory(index: number): Structure { |
| 43 | + return { |
| 44 | + "index.ts": [createExampleFile(index), "typescript"], |
| 45 | + ...(index > 2 && |
| 46 | + Object.fromEntries( |
| 47 | + range(1, index).map((i) => [ |
| 48 | + `nested${i}`, |
| 49 | + createNestedDirectory(i - 1), |
| 50 | + ]), |
| 51 | + )), |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +function createProjectDirectory(index: number): Structure { |
| 56 | + return { |
| 57 | + src: { |
| 58 | + "index.ts": [createIndexFile(index), "typescript"], |
| 59 | + ...(index > 2 && |
| 60 | + Object.fromEntries( |
| 61 | + range(0, index - 1).map((i) => [ |
| 62 | + `nested${i}`, |
| 63 | + createNestedDirectory(i), |
| 64 | + ]), |
| 65 | + )), |
| 66 | + }, |
| 67 | + "tsconfig.json": [ |
| 68 | + { |
| 69 | + extends: "../../tsconfig.build.json", |
| 70 | + include: ["src"], |
| 71 | + }, |
| 72 | + "json", |
| 73 | + ], |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +export function createReferencesCaseFiles(data: CaseData): Structure { |
| 78 | + const topLevelWidth = Math.ceil( |
| 79 | + Math.log(data.files) * (data.files > 1000 ? 1.6 : 1.7), |
| 80 | + ); |
| 81 | + const projectNames = range(0, topLevelWidth).map((i) => `project-${i}`); |
| 82 | + |
| 83 | + return { |
| 84 | + "eslint.config.js": [ |
| 85 | + createESLintConfigFile({ |
| 86 | + singleRun: data.singleRun, |
| 87 | + types: |
| 88 | + data.types === "service" |
| 89 | + ? "projectService" |
| 90 | + : data.layout === "references" |
| 91 | + ? "tsconfig.eslint.json" |
| 92 | + : true, |
| 93 | + }), |
| 94 | + "typescript", |
| 95 | + ], |
| 96 | + "tsconfig.build.json": [ |
| 97 | + { ...createStandardTSConfigFile(), include: undefined }, |
| 98 | + "json", |
| 99 | + ], |
| 100 | + ...(data.types === "service" |
| 101 | + ? { |
| 102 | + "tsconfig.json": [ |
| 103 | + { |
| 104 | + include: [], |
| 105 | + references: projectNames.map((projectName) => ({ |
| 106 | + path: `./src/${projectName}`, |
| 107 | + })), |
| 108 | + }, |
| 109 | + "json", |
| 110 | + ], |
| 111 | + } |
| 112 | + : { |
| 113 | + "tsconfig.eslint.json": [createStandardTSConfigFile(), "json"], |
| 114 | + "tsconfig.json": [createStandardTSConfigFile(), "json"], |
| 115 | + }), |
| 116 | + src: { |
| 117 | + ...Object.fromEntries( |
| 118 | + projectNames.map((projectName, index) => [ |
| 119 | + projectName, |
| 120 | + createProjectDirectory(index), |
| 121 | + ]), |
| 122 | + ), |
| 123 | + }, |
| 124 | + }; |
| 125 | +} |
0 commit comments