-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectBuilder.ts
More file actions
227 lines (204 loc) · 6.14 KB
/
Copy pathprojectBuilder.ts
File metadata and controls
227 lines (204 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import dedent from 'dedent';
import { join } from 'path';
import { writeFile } from '../../utils/writeFile.js';
import type { GeneratorOptions } from '../interface.js';
import { generateApp } from './app.js';
import { generateAppExtensions } from './appExtensions.js';
import { generateDatabase } from './database.js';
import { generateOauth } from './oauth.js';
import { generatePipedriveClient } from './pipedriveClient.js';
import { generateWebhooks } from './webhooks.js';
import { envVarAccess } from '../../utils/templates.js';
export interface BuildStep {
execute(outputDir: string, options: GeneratorOptions): Promise<void>;
}
class OAuthStep implements BuildStep {
async execute(outputDir: string, options: GeneratorOptions): Promise<void> {
await generateOauth(outputDir, options);
}
}
class DatabaseStep implements BuildStep {
async execute(outputDir: string, options: GeneratorOptions): Promise<void> {
await generateDatabase(outputDir, options);
}
}
class AppStep implements BuildStep {
async execute(outputDir: string, options: GeneratorOptions): Promise<void> {
await generateApp(outputDir, options);
}
}
class WebhooksStep implements BuildStep {
async execute(outputDir: string, options: GeneratorOptions): Promise<void> {
await generateWebhooks(outputDir, options);
}
}
class AppExtensionsStep implements BuildStep {
async execute(outputDir: string, options: GeneratorOptions): Promise<void> {
await generateAppExtensions(outputDir, options);
}
}
class PipedriveClientStep implements BuildStep {
async execute(outputDir: string, options: GeneratorOptions): Promise<void> {
await generatePipedriveClient(outputDir, options);
}
}
class ServerEntryStep implements BuildStep {
async execute(outputDir: string, _options: GeneratorOptions): Promise<void> {
await writeFile(
join(outputDir, 'src/index.ts'),
dedent`
import { runMigrations } from './database/migrate.js';
import app from './app.js';
const PORT = ${envVarAccess('PORT', '3000')};
const STARTUP_RETRY_ATTEMPTS = 60;
const STARTUP_RETRY_DELAY_MS = 1000;
async function waitForDatabase(): Promise<void> {
for (let attempt = 1; attempt <= STARTUP_RETRY_ATTEMPTS; attempt++) {
try {
await runMigrations();
return;
} catch (error) {
if (attempt === STARTUP_RETRY_ATTEMPTS) throw error;
const message = error instanceof Error ? error.message : String(error);
console.warn(
\`Database is not ready yet (\${attempt}/\${STARTUP_RETRY_ATTEMPTS}): \${message}\`,
);
await new Promise<void>((resolve) => setTimeout(resolve, STARTUP_RETRY_DELAY_MS));
}
}
}
await waitForDatabase();
app.listen(PORT, () => {
console.log(\`Server running on port \${PORT}\`);
});
`,
);
}
}
class PackageJsonStep implements BuildStep {
async execute(outputDir: string, options: GeneratorOptions): Promise<void> {
const dbDrivers: Record<GeneratorOptions['database'], Record<string, string>> = {
postgres: { postgres: '^3.4.0' },
mysql: { mysql2: '^3.9.0' },
sqlite: { '@libsql/client': '^0.14.0' },
};
const dbDevDrivers: Record<GeneratorOptions['database'], Record<string, string>> = {
postgres: {},
mysql: {},
sqlite: {},
};
const pkg = {
name: options.projectName,
version: '0.1.0',
type: 'module',
scripts: {
'dev': 'tsx watch --env-file=.env src/index.ts',
'build': 'tsc',
'typecheck': 'tsc --noEmit',
'db:migrate': 'drizzle-kit migrate',
},
dependencies: {
'express': '^4.19.0',
'drizzle-orm': '^0.30.0',
'pipedrive': '^32.0.0',
...dbDrivers[options.database],
},
devDependencies: {
'typescript': '^5.4.0',
'@types/express': '^4.17.0',
'@types/node': '^20.0.0',
'tsx': '^4.7.0',
'drizzle-kit': '^0.21.0',
...dbDevDrivers[options.database],
},
};
await writeFile(join(outputDir, 'package.json'), JSON.stringify(pkg, null, 2));
}
}
class TsConfigStep implements BuildStep {
async execute(outputDir: string, _options: GeneratorOptions): Promise<void> {
const tsconfig = {
compilerOptions: {
target: 'ESNext',
module: 'ESNext',
moduleResolution: 'bundler',
outDir: 'dist',
rootDir: 'src',
strict: true,
esModuleInterop: true,
skipLibCheck: true,
},
include: ['src'],
};
await writeFile(join(outputDir, 'tsconfig.json'), JSON.stringify(tsconfig, null, 2));
}
}
class EnvExampleStep implements BuildStep {
async execute(outputDir: string, options: GeneratorOptions): Promise<void> {
const databaseUrlExample: Record<GeneratorOptions['database'], string> = {
postgres: `postgresql://app:app@localhost:5432/${options.projectName}`,
mysql: `mysql://app:app@localhost:3307/${options.projectName}`,
sqlite: 'file:./data.db',
};
await writeFile(
join(outputDir, '.env.example'),
dedent`
PIPEDRIVE_CLIENT_ID=
PIPEDRIVE_CLIENT_SECRET=
PIPEDRIVE_REDIRECT_URI=http://localhost:3000/oauth/callback
DATABASE_URL=${databaseUrlExample[options.database]}
PORT=3000
`,
);
}
}
export class NodeProjectBuilder {
private steps: BuildStep[] = [];
constructor(
private outputDir: string,
private options: GeneratorOptions,
) {}
addStep(step: BuildStep): this {
this.steps.push(step);
return this;
}
addOAuth(): this {
return this.addStep(new OAuthStep());
}
addDatabase(): this {
return this.addStep(new DatabaseStep());
}
addApp(): this {
return this.addStep(new AppStep());
}
addWebhooks(): this {
return this.addStep(new WebhooksStep());
}
addAppExtensions(): this {
return this.addStep(new AppExtensionsStep());
}
addPipedriveClient(): this {
return this.addStep(new PipedriveClientStep());
}
addServerEntry(): this {
return this.addStep(new ServerEntryStep());
}
addPackageJson(): this {
return this.addStep(new PackageJsonStep());
}
addTsConfig(): this {
return this.addStep(new TsConfigStep());
}
addEnvExample(): this {
return this.addStep(new EnvExampleStep());
}
when(condition: boolean, fn: (b: this) => void): this {
if (condition) fn(this);
return this;
}
async build(): Promise<void> {
for (const step of this.steps) {
await step.execute(this.outputDir, this.options);
}
}
}