-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace vercel flow with rest calls (#28)
- Loading branch information
Showing
7 changed files
with
201 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { execSync } from 'child_process'; | ||
import chalk from 'chalk'; | ||
import { logWithColoredPrefix } from '../../../utils/logWithColoredPrefix'; | ||
|
||
const getUsername = (): string | null => { | ||
try { | ||
const user = execSync('npx vercel whoami', { stdio: 'pipe', encoding: 'utf-8' }).trim(); | ||
return user || null; | ||
} catch { | ||
return null; | ||
} | ||
}; | ||
|
||
const loginIfNeeded = () => { | ||
logWithColoredPrefix('vercel', 'Logging in...'); | ||
try { | ||
execSync('npx vercel login', { stdio: 'inherit' }); | ||
} catch (error) { | ||
logWithColoredPrefix('vercel', [ | ||
'Oops! Something went wrong while logging in...', | ||
'\nYou might already be logged in with this email in another project.', | ||
'\nIn this case, select "Continue with Email" and enter the email you\'re already logged in with.\n', | ||
]); | ||
try { | ||
execSync('npx vercel login', { stdio: 'inherit' }); | ||
} catch { | ||
logWithColoredPrefix('vercel', [ | ||
'Please check the error above and try again.', | ||
'\nAfter successfully logging in with "vercel login", please run create-stapler-app again.\n', | ||
]); | ||
process.exit(1); | ||
} | ||
} | ||
}; | ||
|
||
export const linkVercelProject = async () => { | ||
let vercelUserName = getUsername(); | ||
|
||
if (!vercelUserName) { | ||
loginIfNeeded(); | ||
vercelUserName = getUsername(); // Retry getting username after login | ||
} | ||
|
||
logWithColoredPrefix('vercel', `You are logged in as ${chalk.cyan(vercelUserName)}`); | ||
|
||
logWithColoredPrefix('vercel', 'Linking project...'); | ||
execSync('npx vercel link --yes', { stdio: 'ignore' }); | ||
}; |
47 changes: 0 additions & 47 deletions
47
packages/core/installMachine/installSteps/vercel/setupAndCreate.ts
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
packages/core/installMachine/installSteps/vercel/updateProjectSettings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import fs from 'fs/promises'; | ||
import path from 'path'; | ||
import { getGlobalPathConfig } from './utils/getGlobalPathConfig'; | ||
import { logWithColoredPrefix } from '../../../utils/logWithColoredPrefix'; | ||
|
||
const getTokenFromAuthFile = async (filePath: string): Promise<string | null> => { | ||
try { | ||
const data = await fs.readFile(filePath, 'utf-8'); | ||
const jsonData = JSON.parse(data); | ||
return jsonData.token || null; | ||
} catch (error) { | ||
console.error('Failed to read or parse auth.json:', `\n${error}`); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
const getProjectIdFromVercelConfig = async (): Promise<string | null> => { | ||
const data = await fs.readFile('.vercel/project.json', 'utf-8'); | ||
try { | ||
const jsonData = JSON.parse(data); | ||
return jsonData.projectId; | ||
} catch (error) { | ||
console.error('Failed to read or parse vercel.json:', `\n${error}`); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
export const updateVercelProjectSettings = async () => { | ||
logWithColoredPrefix('vercel', 'Changing project settings...'); | ||
const globalPath = await getGlobalPathConfig(); | ||
if (!globalPath) { | ||
console.error('Global path not found. Cannot update project properties.'); | ||
process.exit(1); | ||
} | ||
const filePath = path.join(globalPath, 'auth.json'); | ||
|
||
const token = await getTokenFromAuthFile(filePath); | ||
if (!token) { | ||
console.error('Token not found. Cannot update project properties.'); | ||
process.exit(1); | ||
} | ||
|
||
const projectId = await getProjectIdFromVercelConfig(); | ||
|
||
const response = await fetch(`https://api.vercel.com/v9/projects/${projectId}`, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
body: JSON.stringify({ | ||
framework: 'nextjs', | ||
rootDirectory: 'apps/web', | ||
}), | ||
method: 'PATCH', | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to update project properties: ${response.statusText}`); | ||
} | ||
}; |
53 changes: 53 additions & 0 deletions
53
packages/core/installMachine/installSteps/vercel/utils/getGlobalPathConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { homedir } from 'os'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
const getXDGPaths = (appName: string) => { | ||
const homeDir = homedir(); | ||
|
||
if (process.platform === 'win32') { | ||
// Windows paths, typically within %AppData% | ||
return { | ||
dataDirs: [path.join(process.env.APPDATA || path.join(homeDir, 'AppData', 'Roaming'), appName)], | ||
configDirs: [path.join(process.env.APPDATA || path.join(homeDir, 'AppData', 'Roaming'), appName)], | ||
cacheDir: path.join(process.env.LOCALAPPDATA || path.join(homeDir, 'AppData', 'Local'), appName, 'Cache'), | ||
}; | ||
} else if (process.platform === 'darwin') { | ||
// macOS paths, typically in ~/Library/Application Support | ||
return { | ||
dataDirs: [path.join(homeDir, 'Library', 'Application Support', appName)], | ||
configDirs: [path.join(homeDir, 'Library', 'Application Support', appName)], | ||
cacheDir: path.join(homeDir, 'Library', 'Caches', appName), | ||
}; | ||
} else { | ||
// Linux/Unix paths, following the XDG Base Directory Specification | ||
return { | ||
dataDirs: [process.env.XDG_DATA_HOME || path.join(homeDir, '.local', 'share', appName)], | ||
configDirs: [process.env.XDG_CONFIG_HOME || path.join(homeDir, '.config', appName)], | ||
cacheDir: process.env.XDG_CACHE_HOME || path.join(homeDir, '.cache', appName), | ||
}; | ||
} | ||
}; | ||
|
||
// Returns whether a directory exists | ||
const isDirectory = (path: string): boolean => { | ||
try { | ||
return fs.lstatSync(path).isDirectory(); | ||
} catch (_) { | ||
// We don't care which kind of error occured, it isn't a directory anyway. | ||
return false; | ||
} | ||
}; | ||
|
||
// Returns in which directory the config should be present | ||
export const getGlobalPathConfig = async (): Promise<string> => { | ||
const vercelDirectories = getXDGPaths('com.vercel.cli').dataDirs; | ||
|
||
const possibleConfigPaths = [ | ||
...vercelDirectories, // latest vercel directory | ||
path.join(homedir(), '.now'), // legacy config in user's home directory | ||
...getXDGPaths('now').dataDirs, // legacy XDG directory | ||
]; | ||
|
||
return possibleConfigPaths.find((configPath) => isDirectory(configPath)) || vercelDirectories[0]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters