Skip to content

Commit baa1c03

Browse files
committed
feat: output generated code
Includes a few fixes that were needed to make it work.
1 parent 6419ed7 commit baa1c03

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

Diff for: packages/cli/src/commands/import/plan.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { writeFileSync } from 'node:fs'
12
import { Flags, ux } from '@oclif/core'
23
import prompts from 'prompts'
34
import chalk from 'chalk'
@@ -10,6 +11,8 @@ import commonMessages from '../../messages/common-messages'
1011
import { splitConfigFilePath } from '../../services/util'
1112
import { loadChecklyConfig } from '../../services/checkly-config-loader'
1213
import { ImportPlan } from '../../rest/projects'
14+
import { Program, Output } from '../../sourcegen'
15+
import { codegen } from '../../constructs/construct.codegen'
1316

1417
export default class ImportPlanCommand extends AuthCommand {
1518
static coreCommand = true
@@ -50,8 +53,11 @@ export default class ImportPlanCommand extends AuthCommand {
5053
ux.action.start('Creating a new plan', undefined, { stdout: true })
5154
}
5255

56+
let plan: ImportPlan
57+
5358
try {
54-
const { data: plan } = await api.projects.createImportPlan(logicalId)
59+
const { data } = await api.projects.createImportPlan(logicalId)
60+
plan = data
5561

5662
if (this.fancy) {
5763
ux.action.stop('✅ ')
@@ -74,16 +80,35 @@ export default class ImportPlanCommand extends AuthCommand {
7480
throw err
7581
}
7682

83+
this.#generateCode(plan)
84+
}
85+
86+
#generateCode (plan: ImportPlan) {
7787
if (this.fancy) {
7888
ux.action.start('Generating Checkly constructs for imported resources', undefined, { stdout: true })
7989
}
8090

8191
try {
82-
this.log('TODO') // TODO
92+
const program = new Program()
93+
if (plan.changes) {
94+
for (const resource of plan.changes.resources) {
95+
codegen(program, resource as any)
96+
}
97+
}
8398

8499
if (this.fancy) {
85100
ux.action.stop('✅ ')
86101
}
102+
103+
const output = new Output()
104+
program.render(output)
105+
106+
// TODO: file structure
107+
const filename = './generated_resources.ts'
108+
109+
writeFileSync(filename, output.finalize())
110+
111+
this.log(`${logSymbols.success} Generated code can be found in ${chalk.bold(filename)}`)
87112
} catch (err) {
88113
if (this.fancy) {
89114
ux.action.stop('❌')

Diff for: packages/cli/src/constructs/construct.codegen.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Program } from '../sourcegen'
2+
3+
import { codegen as alertChannelCodegen } from './alert-channel.codegen'
4+
import { codegen as apiCheckCodegen } from './api-check.codegen'
5+
import { codegen as browserCheckCodegen } from './browser-check.codegen'
6+
import { codegen as checkGroupCodegen } from './check-group.codegen'
7+
import { codegen as dashboardCodegen } from './dashboard.codegen'
8+
import { codegen as heartbeatCheckCodegen } from './heartbeat-check.codegen'
9+
import { codegen as maintenanceWindowCodegen } from './maintenance-window.codegen'
10+
import { codegen as multiStepCheckCodegen } from './multi-step-check.codegen'
11+
import { codegen as privateLocationCheckCodegen } from './private-location.codegen'
12+
import { codegen as tcpCheckCodegen } from './tcp-check.codegen'
13+
14+
export type ResourceType =
15+
'alert-channel-subscription' |
16+
'alert-channel' |
17+
'check-group' |
18+
'check' |
19+
'dashboard' |
20+
'maintenance-window' |
21+
'private-location-check-assignment' |
22+
'private-location-group-assignment' |
23+
'private-location'
24+
25+
interface Resource {
26+
type: ResourceType
27+
logicalId: string
28+
payload: any
29+
}
30+
31+
export function codegen (program: Program, resource: Resource): void {
32+
switch (resource.type) {
33+
case 'alert-channel-subscription':
34+
return // Skip temporarily
35+
case 'alert-channel':
36+
return alertChannelCodegen(program, resource.logicalId, resource.payload)
37+
case 'check-group':
38+
return checkGroupCodegen(program, resource.logicalId, resource.payload)
39+
case 'check': {
40+
const checkType = resource.payload.checkType
41+
switch (checkType) {
42+
case 'BROWSER':
43+
return browserCheckCodegen(program, resource.logicalId, resource.payload)
44+
case 'API':
45+
return apiCheckCodegen(program, resource.logicalId, resource.payload)
46+
case 'TCP':
47+
return tcpCheckCodegen(program, resource.logicalId, resource.payload)
48+
case 'MULTI_STEP':
49+
return multiStepCheckCodegen(program, resource.logicalId, resource.payload)
50+
case 'HEARTBEAT':
51+
return heartbeatCheckCodegen(program, resource.logicalId, resource.payload)
52+
default:
53+
throw new Error(`Unable to generate for for unsupported check type '${checkType}'.`)
54+
}
55+
}
56+
case 'dashboard':
57+
return dashboardCodegen(program, resource.logicalId, resource.payload)
58+
case 'maintenance-window':
59+
return maintenanceWindowCodegen(program, resource.logicalId, resource.payload)
60+
case 'private-location-check-assignment':
61+
return // Skip temporarily
62+
case 'private-location-group-assignment':
63+
return // Skip temporarily
64+
case 'private-location':
65+
return privateLocationCheckCodegen(program, resource.logicalId, resource.payload)
66+
default:
67+
throw new Error(`Unable to generate for for unsupported resource type '${resource.type}'.`)
68+
}
69+
}

Diff for: packages/cli/src/rest/projects.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface ProjectDeployResponse {
3737
}
3838

3939
export interface ImportPlanChanges {
40-
resources: ResourceSync
40+
resources: ResourceSync[]
4141
}
4242

4343
export interface ImportPlan {

Diff for: packages/cli/src/sourcegen/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export { NullValue } from './null'
2525
export { NumberValue } from './number'
2626
export { ObjectValue } from './object'
2727
export { object, ObjectValueBuilder } from './objectbuilder'
28+
export { Output } from './output'
2829
export { Program } from './program'
2930
export { StringValue } from './string'
3031
export { UndefinedValue } from './undefined'

0 commit comments

Comments
 (0)