-
-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major cleanup to
blitz new
dependency update + graceful failure (Cl…
…oses #202, #284) Co-Authored-By: Adam Markon <[email protected]> (patch)
- Loading branch information
Showing
17 changed files
with
452 additions
and
111 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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
"name": "Using fixtures to represent data", | ||
"email": "[email protected]", | ||
"body": "Fixtures are a great way to mock data for responses to routes" | ||
} | ||
} |
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
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,4 @@ | ||
export interface Fallbackable<T> { | ||
value: T | ||
isFallback: boolean | ||
} |
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,31 @@ | ||
import {getLatestVersion} from './get-latest-version' | ||
import {Fallbackable} from './fallbackable' | ||
|
||
export const fetchLatestVersionsFor = async <T extends Record<string, string>>( | ||
dependencies: T, | ||
): Promise<Fallbackable<T>> => { | ||
const entries = Object.entries(dependencies) | ||
|
||
let fallbackUsed = false | ||
|
||
const updated = await Promise.all( | ||
entries.map(async ([dep, version]) => { | ||
if (version.match(/\d.x/)) { | ||
const {value: latestVersion, isFallback} = await getLatestVersion(dep, version) | ||
|
||
if (isFallback) { | ||
fallbackUsed = true | ||
} | ||
|
||
return [dep, latestVersion] | ||
} else { | ||
return [dep, version] | ||
} | ||
}), | ||
) | ||
|
||
return { | ||
isFallback: fallbackUsed, | ||
value: Object.fromEntries(updated), | ||
} | ||
} |
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,19 @@ | ||
import {fetchDistTags} from './npm-fetch' | ||
import {Fallbackable} from './fallbackable' | ||
import {logFailedVersionFetch} from './get-latest-version' | ||
|
||
export const getBlitzDependencyVersion = async (cliVersion: string): Promise<Fallbackable<string>> => { | ||
try { | ||
const {latest, canary} = await fetchDistTags('blitz') | ||
|
||
if (cliVersion.includes('canary')) { | ||
return {value: canary, isFallback: false} | ||
} | ||
|
||
return {value: latest, isFallback: false} | ||
} catch (error) { | ||
const fallback = 'latest' | ||
logFailedVersionFetch('blitz', fallback) | ||
return {value: fallback, isFallback: true} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,24 +1,45 @@ | ||
import {fetchAllVersions, fetchLatestDistVersion} from './npm-fetch' | ||
import {log} from '@blitzjs/server' | ||
import {Fallbackable} from './fallbackable' | ||
import chalk from 'chalk' | ||
|
||
export const getLatestVersion = async (dependency: string, templateVersion: string) => { | ||
export const logFailedVersionFetch = (dependency: string, fallback: string) => { | ||
log.clearLine( | ||
log.withWarning( | ||
`Failed to fetch latest version of '${chalk.bold(dependency)}', falling back to '${chalk.bold( | ||
fallback, | ||
)}'.\n`, | ||
), | ||
) | ||
} | ||
|
||
export const getLatestVersion = async ( | ||
dependency: string, | ||
templateVersion: string = '', | ||
): Promise<Fallbackable<string>> => { | ||
const major = templateVersion.replace('.x', '') | ||
const allVersions = await fetchAllVersions(dependency) | ||
const latestDistVersion = await fetchLatestDistVersion(dependency) | ||
|
||
if (!allVersions || !latestDistVersion) { | ||
return templateVersion | ||
} | ||
try { | ||
const [allVersions, latestDistVersion] = await Promise.all([ | ||
fetchAllVersions(dependency), | ||
fetchLatestDistVersion(dependency), | ||
]) | ||
|
||
const latestVersion = Object.keys(allVersions) | ||
.filter((version) => version.startsWith(major)) | ||
.sort((a, b) => a.localeCompare(b, undefined, {numeric: true})) | ||
.reverse()[0] | ||
const latestVersion = Object.keys(allVersions) | ||
.filter((version) => version.startsWith(major)) | ||
.sort((a, b) => a.localeCompare(b, undefined, {numeric: true})) | ||
.reverse()[0] | ||
|
||
// If the latest tagged version matches our pinned major, use that, otherwise use the | ||
// latest untagged which does | ||
if (latestDistVersion.startsWith(major)) { | ||
return latestDistVersion | ||
} else { | ||
return latestVersion | ||
// If the latest tagged version matches our pinned major, use that, otherwise use the | ||
// latest untagged which does | ||
if (latestDistVersion.startsWith(major)) { | ||
return {value: latestDistVersion, isFallback: false} | ||
} else { | ||
return {value: latestVersion, isFallback: false} | ||
} | ||
} catch (error) { | ||
const fallback = templateVersion | ||
logFailedVersionFetch(dependency, fallback) | ||
return {value: fallback, isFallback: false} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,21 +1,27 @@ | ||
import fetch from 'node-fetch' | ||
import got from 'got' | ||
|
||
type PackageInformation = any | ||
type NpmDepResponse = {versions: Record<string, PackageInformation>} | ||
|
||
export const fetchAllVersions = async (dependency: string) => { | ||
const res = await fetch(`https://registry.npmjs.org/${dependency}`) | ||
if (res.ok) { | ||
const json = await res.json() | ||
return json.versions as string[] | ||
} else { | ||
return | ||
} | ||
const res = await got(`https://registry.npmjs.org/${dependency}`, { | ||
retry: {limit: 3}, | ||
responseType: 'json', | ||
}).json<NpmDepResponse>() | ||
return Object.keys(res.versions) | ||
} | ||
|
||
type NpmDistTagsResponse = {latest: string; canary: string} | ||
|
||
export const fetchDistTags = async (dependency: string) => { | ||
const res = await got(`https://registry.npmjs.org/-/package/${dependency}/dist-tags`, { | ||
retry: {limit: 3}, | ||
responseType: 'json', | ||
}).json<NpmDistTagsResponse>() | ||
return res | ||
} | ||
|
||
export const fetchLatestDistVersion = async (dependency: string) => { | ||
const res = await fetch(`https://registry.npmjs.org/-/package/${dependency}/dist-tags`) | ||
if (res.ok) { | ||
const json = await res.json() | ||
return json.latest as string | ||
} else { | ||
return | ||
} | ||
const res = await fetchDistTags(dependency) | ||
return res.latest | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.