@ranger1/dx 0.1.55 → 0.1.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/vercel-deploy.js +24 -66
- package/package.json +1 -1
package/lib/vercel-deploy.js
CHANGED
|
@@ -11,10 +11,12 @@ const TARGET_CONFIGS = {
|
|
|
11
11
|
front: {
|
|
12
12
|
configFile: 'vercel.front.json',
|
|
13
13
|
projectIdEnvVar: 'VERCEL_PROJECT_ID_FRONT',
|
|
14
|
+
deployCwd: 'apps/front',
|
|
14
15
|
},
|
|
15
16
|
admin: {
|
|
16
17
|
configFile: 'vercel.admin.json',
|
|
17
18
|
projectIdEnvVar: 'VERCEL_PROJECT_ID_ADMIN',
|
|
19
|
+
deployCwd: 'apps/admin-front',
|
|
18
20
|
},
|
|
19
21
|
'telegram-bot': {
|
|
20
22
|
configFile: 'vercel.telegram-bot.json',
|
|
@@ -22,9 +24,6 @@ const TARGET_CONFIGS = {
|
|
|
22
24
|
},
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
const EXPLICIT_ENV_VARS = ['APP_ENV', 'NODE_ENV']
|
|
26
|
-
const PUBLIC_ENV_PREFIXES = ['NEXT_PUBLIC_', 'VITE_']
|
|
27
|
-
|
|
28
27
|
const APP_ENV_MAP = {
|
|
29
28
|
development: 'dev',
|
|
30
29
|
staging: 'staging',
|
|
@@ -88,53 +87,19 @@ function listMissingConfigs(targetConfigs, projectRoot) {
|
|
|
88
87
|
return missing
|
|
89
88
|
}
|
|
90
89
|
|
|
91
|
-
function appendTargetArgs(baseArgs, {
|
|
90
|
+
function appendTargetArgs(baseArgs, { orgId }) {
|
|
92
91
|
const args = [...baseArgs]
|
|
93
92
|
|
|
94
|
-
if (explicitEnvArgs.length > 0) {
|
|
95
|
-
args.push(...explicitEnvArgs)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (cwd) {
|
|
99
|
-
args.push('--cwd', cwd)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
93
|
if (orgId) {
|
|
103
94
|
args.push('--scope', orgId)
|
|
104
95
|
}
|
|
105
96
|
|
|
106
|
-
if (projectId) {
|
|
107
|
-
args.push('--project', projectId)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
97
|
return args
|
|
111
98
|
}
|
|
112
99
|
|
|
113
|
-
function
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
Object.keys(sourceEnv).forEach(name => {
|
|
117
|
-
if (PUBLIC_ENV_PREFIXES.some(prefix => name.startsWith(prefix))) {
|
|
118
|
-
includeKeys.add(name)
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
return [...includeKeys]
|
|
123
|
-
.filter(name => {
|
|
124
|
-
const value = sourceEnv[name]
|
|
125
|
-
return !(
|
|
126
|
-
value === undefined ||
|
|
127
|
-
value === null ||
|
|
128
|
-
envManager.isPlaceholderEnvValue(value)
|
|
129
|
-
)
|
|
130
|
-
})
|
|
131
|
-
.sort()
|
|
132
|
-
.map(name => `${name}=${String(sourceEnv[name])}`)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function withEnvFlag(entries, flag) {
|
|
136
|
-
if (!Array.isArray(entries) || entries.length === 0) return []
|
|
137
|
-
return entries.flatMap(entry => [flag, entry])
|
|
100
|
+
export function resolveTargetRunCwd(projectRoot, targetConfig) {
|
|
101
|
+
if (!targetConfig?.deployCwd) return projectRoot
|
|
102
|
+
return join(projectRoot, targetConfig.deployCwd)
|
|
138
103
|
}
|
|
139
104
|
|
|
140
105
|
function maskIdentifier(value) {
|
|
@@ -143,8 +108,8 @@ function maskIdentifier(value) {
|
|
|
143
108
|
return `${raw.slice(0, 6)}...${raw.slice(-4)}`
|
|
144
109
|
}
|
|
145
110
|
|
|
146
|
-
function readLinkedProjectContext(
|
|
147
|
-
const path = join(
|
|
111
|
+
function readLinkedProjectContext(contextRoot) {
|
|
112
|
+
const path = join(contextRoot, VERCEL_PROJECT_LINK_PATH)
|
|
148
113
|
if (!existsSync(path)) {
|
|
149
114
|
return { exists: false, path, orgId: null, projectId: null, parseError: null }
|
|
150
115
|
}
|
|
@@ -170,8 +135,8 @@ function readLinkedProjectContext(projectRoot) {
|
|
|
170
135
|
}
|
|
171
136
|
}
|
|
172
137
|
|
|
173
|
-
function clearLinkedProjectContext(
|
|
174
|
-
const path = join(
|
|
138
|
+
function clearLinkedProjectContext(contextRoot) {
|
|
139
|
+
const path = join(contextRoot, VERCEL_PROJECT_LINK_PATH)
|
|
175
140
|
rmSync(path, { force: true })
|
|
176
141
|
}
|
|
177
142
|
|
|
@@ -229,7 +194,7 @@ export async function deployPrebuiltWithFallback(options) {
|
|
|
229
194
|
run = runVercel,
|
|
230
195
|
cleanupArchiveParts = () => {
|
|
231
196
|
try {
|
|
232
|
-
execSync('rm -f .vercel/source.tgz.part*', { stdio: 'ignore' })
|
|
197
|
+
execSync('rm -f .vercel/source.tgz.part*', { stdio: 'ignore', cwd: cwd || process.cwd() })
|
|
233
198
|
} catch {
|
|
234
199
|
// ignore
|
|
235
200
|
}
|
|
@@ -308,7 +273,7 @@ export async function deployToVercel(target, options = {}) {
|
|
|
308
273
|
logger.info(' 1. VERCEL_TOKEN: vercel login 后查看 ~/Library/Application Support/com.vercel.cli/auth.json')
|
|
309
274
|
logger.info(' 2. PROJECT_ID: vercel project ls --scope <org> 或通过 Vercel Dashboard 获取')
|
|
310
275
|
logger.info('')
|
|
311
|
-
logger.info('提示:部署命令会显式校验 --scope
|
|
276
|
+
logger.info('提示:部署命令会显式校验 --scope 与环境变量上下文,避免环境漂移。')
|
|
312
277
|
}
|
|
313
278
|
|
|
314
279
|
if (missingConfigFiles.length > 0) {
|
|
@@ -336,7 +301,15 @@ export async function deployToVercel(target, options = {}) {
|
|
|
336
301
|
const projectId = process.env[targetConfig.projectIdEnvVar]
|
|
337
302
|
const configFile = targetConfig.configFile
|
|
338
303
|
const configPath = join(projectRoot, configFile)
|
|
339
|
-
const
|
|
304
|
+
const runCwd = resolveTargetRunCwd(projectRoot, targetConfig)
|
|
305
|
+
|
|
306
|
+
if (!existsSync(runCwd)) {
|
|
307
|
+
logger.error(`部署目录不存在: target=${t} deployCwd=${targetConfig.deployCwd || '.'} resolved=${runCwd}`)
|
|
308
|
+
process.exitCode = 1
|
|
309
|
+
return
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const linkedContext = readLinkedProjectContext(runCwd)
|
|
340
313
|
|
|
341
314
|
const linkedMismatch =
|
|
342
315
|
linkedContext.exists &&
|
|
@@ -373,16 +346,6 @@ export async function deployToVercel(target, options = {}) {
|
|
|
373
346
|
`[deploy-context] env=${environment} target=${t} strict=${strictContext ? 1 : 0} org=${maskIdentifier(orgId)} project=${maskIdentifier(projectId)} linked=${linkedContext.exists ? 'yes' : 'no'} token=env`,
|
|
374
347
|
)
|
|
375
348
|
|
|
376
|
-
const explicitEnvEntries = collectExplicitVercelEnvEntries({
|
|
377
|
-
...process.env,
|
|
378
|
-
APP_ENV: buildEnv,
|
|
379
|
-
NODE_ENV: envManager.mapAppEnvToNodeEnv(environment),
|
|
380
|
-
VERCEL_ORG_ID: orgId,
|
|
381
|
-
VERCEL_PROJECT_ID: projectId,
|
|
382
|
-
})
|
|
383
|
-
|
|
384
|
-
const deployEnvArgs = withEnvFlag(explicitEnvEntries, '--env')
|
|
385
|
-
|
|
386
349
|
const envVars = {
|
|
387
350
|
...process.env,
|
|
388
351
|
VERCEL_PROJECT_ID: projectId,
|
|
@@ -411,7 +374,7 @@ export async function deployToVercel(target, options = {}) {
|
|
|
411
374
|
|
|
412
375
|
try {
|
|
413
376
|
if (strictContext && process.env.DX_VERCEL_KEEP_LINK !== '1') {
|
|
414
|
-
clearLinkedProjectContext(
|
|
377
|
+
clearLinkedProjectContext(runCwd)
|
|
415
378
|
}
|
|
416
379
|
|
|
417
380
|
// 第一步:本地构建
|
|
@@ -419,9 +382,7 @@ export async function deployToVercel(target, options = {}) {
|
|
|
419
382
|
const buildArgs = appendTargetArgs(
|
|
420
383
|
['build', '--local-config', configPath, '--yes'],
|
|
421
384
|
{
|
|
422
|
-
cwd: projectRoot,
|
|
423
385
|
orgId,
|
|
424
|
-
projectId,
|
|
425
386
|
},
|
|
426
387
|
)
|
|
427
388
|
|
|
@@ -430,7 +391,7 @@ export async function deployToVercel(target, options = {}) {
|
|
|
430
391
|
buildArgs.push('--prod')
|
|
431
392
|
}
|
|
432
393
|
|
|
433
|
-
await run(buildArgs, { env: envVars, cwd:
|
|
394
|
+
await run(buildArgs, { env: envVars, cwd: runCwd })
|
|
434
395
|
logger.success(`${t} 本地构建成功`)
|
|
435
396
|
|
|
436
397
|
// 第二步:上传预构建产物
|
|
@@ -438,10 +399,7 @@ export async function deployToVercel(target, options = {}) {
|
|
|
438
399
|
const baseDeployArgs = appendTargetArgs(
|
|
439
400
|
['deploy', '--prebuilt', '--local-config', configPath, '--yes'],
|
|
440
401
|
{
|
|
441
|
-
cwd: projectRoot,
|
|
442
402
|
orgId,
|
|
443
|
-
projectId,
|
|
444
|
-
explicitEnvArgs: deployEnvArgs,
|
|
445
403
|
},
|
|
446
404
|
)
|
|
447
405
|
|
|
@@ -453,7 +411,7 @@ export async function deployToVercel(target, options = {}) {
|
|
|
453
411
|
const deployResult = await deployPrebuiltWithFallback({
|
|
454
412
|
baseArgs: baseDeployArgs,
|
|
455
413
|
env: envVars,
|
|
456
|
-
cwd:
|
|
414
|
+
cwd: runCwd,
|
|
457
415
|
run,
|
|
458
416
|
})
|
|
459
417
|
|