-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codemod): change package version to latest (#131)
- Loading branch information
Showing
4 changed files
with
98 additions
and
15 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
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,61 @@ | ||
import chalk from 'chalk'; | ||
import ora from 'ora'; | ||
|
||
export async function fetchPackageLatestVersion(packageName: string): Promise<string> { | ||
const text = `Fetching ${packageName} version`; | ||
const spinner = ora({ | ||
discardStdin: false, | ||
spinner: { | ||
frames: [ | ||
`⠋ ${chalk.gray(`${text}.`)}`, | ||
`⠙ ${chalk.gray(`${text}..`)}`, | ||
`⠹ ${chalk.gray(`${text}...`)}`, | ||
`⠸ ${chalk.gray(`${text}.`)}`, | ||
`⠼ ${chalk.gray(`${text}..`)}`, | ||
`⠴ ${chalk.gray(`${text}...`)}`, | ||
`⠦ ${chalk.gray(`${text}.`)}`, | ||
`⠧ ${chalk.gray(`${text}..`)}`, | ||
`⠇ ${chalk.gray(`${text}...`)}`, | ||
`⠏ ${chalk.gray(`${text}.`)}` | ||
], | ||
interval: 150 | ||
} | ||
}); | ||
|
||
spinner.start(); | ||
|
||
try { | ||
const controller = new AbortController(); | ||
const timeoutId = setTimeout(() => controller.abort(), 15000); // 15 seconds timeout | ||
|
||
const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`, { | ||
headers: { | ||
Accept: 'application/json' | ||
}, | ||
signal: controller.signal | ||
}); | ||
|
||
clearTimeout(timeoutId); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Request failed with status ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
return (data as {version: string}).version; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
if (error.name === 'AbortError') { | ||
throw new Error('Request timeout'); | ||
} | ||
if (error.message.includes('fetch failed')) { | ||
throw new Error('Connection failed. Please check your network connection.'); | ||
} | ||
throw error; | ||
} | ||
throw new Error('Parse version info failed'); | ||
} finally { | ||
spinner.stop(); | ||
} | ||
} |
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