Skip to content

Commit 346d725

Browse files
committed
fix: cleanup rendering
1 parent 72bdb61 commit 346d725

File tree

10 files changed

+153
-177
lines changed

10 files changed

+153
-177
lines changed

packages/tester/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"find": "^0.3.0",
1515
"handlebars": "^4.7.7",
1616
"json5": "^2.2.3",
17-
"type-plus": "^5.4.1"
17+
"type-plus": "^5.5.1"
1818
},
1919
"devDependencies": {
2020
"@types/find": "^0.2.1",

packages/tester/ts/app.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import fs from 'node:fs'
55
import path from 'node:path'
66
import { context } from 'type-plus'
77
import { getProjectPath, getTestSubjects, readPackageJson } from './logic/project'
8-
import { getTestResults } from './testResults'
8+
import { genTestResults, getTestResults } from './testResults'
99

1010
export const app = cli({ name: 'tester', version: '0.0.1' })
1111
.default({
@@ -24,18 +24,14 @@ export const app = cli({ name: 'tester', version: '0.0.1' })
2424

2525
fs.writeFileSync(
2626
path.join(ctx.projectPath, 'test-result.md'),
27-
await render(ctx)
27+
[genTestConfiguration(ctx),
28+
genTestSubjects(ctx),
29+
genLegends(),
30+
genTestResults({ ...ctx, results: await ctx.results })].join('\n')
2831
)
2932
}
3033
})
3134

32-
async function render(ctx: any) {
33-
return [genTestConfiguration(ctx),
34-
genTestSubjects(ctx),
35-
genLegends(),
36-
genTestResults(await ctx.results)].join('\n')
37-
}
38-
3935
function getTSConfig(ctx: { projectPath: string }) {
4036
const tsconfigPath = path.join(ctx.projectPath, 'tsconfig.base.json')
4137
return {
@@ -48,7 +44,7 @@ function genTestConfiguration({ tsconfig }: { tsconfig: any }) {
4844
return template(tsconfig.compilerOptions)
4945
}
5046

51-
function genTestSubjects({ subjects }: { subjects: ReturnType<typeof getTestSubjects> }) {
47+
function genTestSubjects({ subjects }: ReturnType<typeof getTestSubjects>) {
5248
const template = compile(fs.readFileSync(path.join(__dirname, '../templates/test-subjects.hbs'), 'utf8'), { noEscape: true })
5349
return template({ subjects })
5450
}
@@ -57,8 +53,3 @@ function genLegends() {
5753
const template = compile(fs.readFileSync(path.join(__dirname, '../templates/legends.hbs'), 'utf8'))
5854
return template({})
5955
}
60-
61-
function genTestResults(ctx: { results: Awaited<ReturnType<typeof getTestResults>> }) {
62-
const template = compile(fs.readFileSync(path.join(__dirname, '../templates/test-results.hbs'), 'utf8'))
63-
return template(ctx)
64-
}

packages/tester/ts/logic/project.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export function getTestSubjects({
2323
const dependencies = getDependencies(packageJson)
2424
return {
2525
subjects: dependencies.map(name => ({
26-
packageJson,
2726
name,
2827
files: getTestFiles(projectPath, name)
2928
}))
@@ -39,7 +38,8 @@ export function readPackageJson(ctx: { projectPath: string }) {
3938

4039
function getTestFiles(projectPath: string, dependencyName: string) {
4140
const base = path.join(projectPath, 'ts')
42-
const filePrefix = path.join(base, `${dependencyName}`)
41+
// the `.` at the end makes sure it match the exact package name.
42+
const filePrefix = path.join(base, `${dependencyName}.`)
4343
return fileSync(base)
4444
.filter(f => f.startsWith(filePrefix))
4545
.map(filepath => ({
@@ -82,17 +82,32 @@ function getPackageNameFromTransient(filename: string) {
8282
return undefined
8383
}
8484

85-
export function toImportMap(results: Array<{ importType: string, value: string, transient: boolean }> | undefined) {
85+
export function toImportMap(results: Array<{
86+
importType: string,
87+
notApply?: boolean,
88+
transient?: boolean
89+
value?: string,
90+
}> | undefined) {
8691
return {
8792
'importDefault': toImportMapEntry(results, 'default'),
8893
'importDefaultAs': toImportMapEntry(results, 'default-as'),
8994
'importStarAs': toImportMapEntry(results, 'star'),
9095
}
9196
}
9297

93-
function toImportMapEntry(results: Array<{ importType: string, value: string, transient: boolean }> | undefined, importType: string) {
98+
function toImportMapEntry(results: Array<{
99+
importType: string,
100+
notApply?: boolean,
101+
value?: string,
102+
transient?: boolean
103+
}> | undefined, importType: string) {
94104
const entry = results?.find(r => r.importType === importType)
95105
if (entry) {
106+
if (entry.notApply) {
107+
return {
108+
icon: '➖'
109+
}
110+
}
96111
return {
97112
icon: entry.value ? entry.transient ? '🟡' : '🔴' : '🟢',
98113
value: entry.value

packages/tester/ts/logic/runtime.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'node:path'
44
import { getTestSubjects } from './project'
55

66
export type RuntimeResult = {
7-
package: string
7+
name: string
88
results: {
99
filename: string
1010
importType: string
@@ -20,7 +20,7 @@ export async function runProject(ctx: {
2020
const base = path.join(ctx.projectPath, moduleType)
2121
const files = fileSync(base)
2222
return Promise.all(ctx.subjects.map(async testSubject => {
23-
const filePrefix = path.join(base, `${testSubject.name}`)
23+
const filePrefix = path.join(base, `${testSubject.name}.`)
2424
const results = await Promise.all(files.filter(f => f.startsWith(filePrefix) && f.endsWith('.js'))
2525
.map(file => file.slice(base.length + 1))
2626
.map(filename => new Promise<{
@@ -35,11 +35,10 @@ export async function runProject(ctx: {
3535
error: error ? error : undefined
3636
})
3737
})
38-
})
39-
))
38+
})))
4039
return {
41-
package: testSubject.name,
40+
name: testSubject.name,
4241
results
4342
}
44-
}))
43+
})).then(r => r.filter(r => r.results.length > 0))
4544
}

packages/tester/ts/testResults.ts

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { record } from 'type-plus'
1+
import { compile } from 'handlebars'
2+
import fs from 'node:fs'
3+
import path from 'node:path'
4+
import { AwaitedProp, record } from 'type-plus'
25
import { CompileResult, PackageCompileResults, compileProject } from './logic/compile'
36
import { getTestSubjects, toImportMap } from './logic/project'
47
import { RuntimeResult, runProject } from './logic/runtime'
@@ -19,45 +22,94 @@ export function getTestResults(ctx: {
1922
compileResults: compileResults[moduleType],
2023
runtimeResults
2124
}))))
22-
).then(collectTestResults)
25+
)
2326
}
2427
}
2528

26-
function collectTestResults(results: Array<{
27-
moduleType: string
28-
compileResults: PackageCompileResults,
29-
runtimeResults: Array<RuntimeResult>
30-
}>) {
29+
type TestSubjectsContext = ReturnType<typeof getTestSubjects>
30+
type TestResultsContext = AwaitedProp<ReturnType<typeof getTestResults>, 'results'>
31+
32+
export function genTestResults(
33+
ctx: TestSubjectsContext & TestResultsContext) {
34+
const results = collectTestResults(ctx)
35+
const template = compile(fs.readFileSync(path.join(__dirname, '../templates/test-results.hbs'), 'utf8'))
36+
return template(results)
37+
}
38+
39+
function collectTestResults({ subjects, results }: TestSubjectsContext & TestResultsContext) {
3140
const errors = toErrorRecord(results)
3241
return {
3342
results: results.flatMap(({ moduleType, compileResults, runtimeResults }) => {
34-
return runtimeResults.flatMap((r, i) => {
43+
return subjects.flatMap((s, i) => {
44+
const compileResult = compileResults[s.name]
45+
const runtimeResult = runtimeResults.find(r => r.name === s.name)
3546
return [{
3647
moduleType: i === 0 ? moduleType : undefined,
37-
package: r.package,
48+
package: s.name,
3849
type: '💻 compile',
39-
...toImportMap(compileResults[r.package]?.map(c => {
50+
...toImportMap(compileResult ? compileResult.map(c => {
4051
const error = errors.compile.find(e => e.message === c.messageText)
4152
return {
4253
importType: c.importType,
4354
transient: c.transient,
4455
value: error?.key ?? ''
4556
}
46-
}))
57+
}) : (s.files.length === 0 ? [
58+
{ importType: 'default', notApply: true },
59+
{ importType: 'default-as', notApply: true },
60+
{ importType: 'star', notApply: true },
61+
] : [
62+
{ importType: 'default', },
63+
{ importType: 'default-as' },
64+
{ importType: 'star' },
65+
]))
4766
}, {
48-
moduleType: undefined,
49-
package: r.package,
5067
type: '🏃 runtime',
51-
...toImportMap(r.results.map(r => {
68+
...toImportMap(runtimeResult ? runtimeResult.results.map(r => {
5269
const error = errors.runtime.find(e => e.message === extractRuntimeErrorMessage(r.error))
5370
return {
5471
importType: r.importType,
5572
transient: false,
5673
value: error?.key ?? ''
5774
}
58-
}))
75+
}) : (s.files.length === 0 ? [
76+
{ importType: 'default', notApply: true },
77+
{ importType: 'default-as', notApply: true },
78+
{ importType: 'star', notApply: true },
79+
] : [
80+
{ importType: 'default', },
81+
{ importType: 'default-as' },
82+
{ importType: 'star' },
83+
]))
5984
}]
6085
})
86+
// return runtimeResults.flatMap((r, i) => {
87+
// return [{
88+
// moduleType: i === 0 ? moduleType : undefined,
89+
// package: r.name,
90+
// type: '💻 compile',
91+
// ...toImportMap(compileResults[r.name]?.map(c => {
92+
// const error = errors.compile.find(e => e.message === c.messageText)
93+
// return {
94+
// importType: c.importType,
95+
// transient: c.transient,
96+
// value: error?.key ?? ''
97+
// }
98+
// }))
99+
// }, {
100+
// moduleType: undefined,
101+
// package: r.name,
102+
// type: '🏃 runtime',
103+
// ...toImportMap(r.results.map(r => {
104+
// const error = errors.runtime.find(e => e.message === extractRuntimeErrorMessage(r.error))
105+
// return {
106+
// importType: r.importType,
107+
// transient: false,
108+
// value: error?.key ?? ''
109+
// }
110+
// }))
111+
// }]
112+
// })
61113
}),
62114
errors: [...errors.compile, ...errors.runtime]
63115
}

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)