This repository was archived by the owner on Feb 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-new-project.ts
More file actions
189 lines (158 loc) · 5.5 KB
/
init-new-project.ts
File metadata and controls
189 lines (158 loc) · 5.5 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
#!/usr/bin/env node
import { Vercel } from '@vercel/sdk'
import constants from './constants.json' with { type: 'json' }
import { readFileSync, readdirSync } from 'fs'
import { join, relative } from 'path'
const { projectSettings, teamId } = constants
const VERCEL_TOKEN = process.env.VERCEL_TOKEN!
const PROJECT_DIR = process.argv[2]!
if (!PROJECT_DIR) {
console.error('Usage: node deploy.ts <directory-name>')
console.error('Example: node deploy.ts angular-v16')
process.exit(1)
}
if (!VERCEL_TOKEN) {
console.error('Error: VERCEL_TOKEN is not set in .env')
process.exit(1)
}
const vercel = new Vercel({ bearerToken: VERCEL_TOKEN })
async function getOrCreateProject(
projectName: string,
): Promise<{ id: string; name: string }> {
console.log('\nStep 1: Creating/linking Vercel project...')
try {
// Try to find existing project first
const projects = await vercel.projects.getProjects({
teamId: teamId,
search: projectName,
})
const existingProject = projects.projects?.find(
(p) => p.name === projectName,
)
if (existingProject && existingProject.id) {
console.log(`✓ Project found: ${projectName} (ID: ${existingProject.id})`)
return { id: existingProject.id, name: projectName }
}
// Create new project if it doesn't exist
const newProject = await vercel.projects.createProject({
teamId: teamId,
requestBody: {
name: projectName,
...projectSettings,
// TODO: does this actually unset the framework?
// I'm not sure because the types don't allow for null
// which is what is used in update-all-projects.ts to unset the framework.
framework: undefined,
},
})
if (!newProject.id) {
throw new Error('Failed to create project - no ID returned')
}
console.log(`✓ Project created: ${projectName} (ID: ${newProject.id})`)
return { id: newProject.id, name: projectName }
} catch (error) {
console.error('Error creating/finding project:', error)
throw error
}
}
function getAllFiles(dir: string): Array<{ file: string; data: string }> {
const files: Array<{ file: string; data: string }> = []
const entries = readdirSync(dir, { withFileTypes: true, recursive: true })
for (const entry of entries) {
if (entry.isFile()) {
const fullPath = join(entry.path || dir, entry.name)
const relativePath = relative(dir, fullPath)
const content = readFileSync(fullPath, 'utf-8')
files.push({
file: relativePath,
data: content,
})
}
}
return files
}
async function setEnvironmentVariables(projectId: string): Promise<void> {
console.log('\nStep 2: Setting environment variables...')
console.log(' Setting VERCEL_FORCE_NO_BUILD_CACHE=1 for all environments...')
await vercel.projects.createProjectEnv({
idOrName: projectId,
teamId: teamId,
upsert: 'true',
requestBody: {
key: 'VERCEL_FORCE_NO_BUILD_CACHE',
value: '1',
type: 'plain',
target: ['production', 'preview', 'development'],
},
})
console.log('✓ VERCEL_FORCE_NO_BUILD_CACHE set for all environments')
}
async function createInitialDeployment(
projectName: string,
projectDir: string,
options:
| { target: 'production' | 'preview' }
| { customEnvironmentSlugOrId: string },
): Promise<string> {
const environmentType = 'target' in options ? options.target : 'custom'
try {
// Get all files from the project directory
const projectPath = join(process.cwd(), 'packages', projectDir)
const files = getAllFiles(projectPath)
console.log(` Found ${files.length} files to deploy`)
// Create deployment with all project files
const deployment = await vercel.deployments.createDeployment({
teamId: teamId,
skipAutoDetectionConfirmation: '1',
requestBody: {
name: projectName,
...options,
files: files.map((f) => ({
file: f.file,
data: f.data,
encoding: 'utf-8',
})),
projectSettings: {
...projectSettings,
},
},
})
return deployment.url || ''
} catch (error) {
console.error(`Error creating ${environmentType} deployment:`, error)
throw error
}
}
async function triggerDeployments(
projectName: string,
projectDir: string,
): Promise<{ production: string }> {
console.log('\nStep 3: Creating initial deployments...')
console.log(' Creating production deployment...')
const prodUrl = await createInitialDeployment(projectName, projectDir, {
target: 'production',
})
console.log(`✓ Production deployment created: https://${prodUrl}`)
return { production: prodUrl }
}
async function main() {
const projectName = `benchmark-${PROJECT_DIR}`
console.log('========================================')
console.log(`Setting up Vercel project: ${projectName}`)
console.log('========================================')
try {
const project = await getOrCreateProject(projectName)
await setEnvironmentVariables(project.id)
const { production } = await triggerDeployments(projectName, PROJECT_DIR)
console.log('\n========================================')
console.log('✓ All done!')
console.log('========================================')
console.log(`Project: ${projectName} (ID: ${project.id})`)
console.log(`Production: https://${production}`)
console.log('========================================')
} catch (error) {
console.error('\nError:', error instanceof Error ? error.message : error)
process.exit(1)
}
}
main()