Skip to content

Commit

Permalink
Set correct packages versions in package.json with upgrade-legacy cod…
Browse files Browse the repository at this point in the history
…emod (#3678)
  • Loading branch information
beerose authored Aug 8, 2022
1 parent dd299ae commit 8dfaad0
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-jobs-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---

Set correct packages versions in package.json with upgrade-legacy codemod
10 changes: 6 additions & 4 deletions packages/blitz/src/cli/utils/read-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ export function readVersions() {
return versions
}

export function resolveVersionType(version: string) {
export function resolveVersionType(
version: string,
): "alpha" | "beta" | "canary" | "stable" | "danger" | "latest" {
if (version.includes("alpha")) {
return "alpha" as const
return "alpha"
}

if (version.includes("beta")) {
return "beta" as const
return "beta"
}

if (version.includes("danger")) {
return "danger" as const
return "danger"
}

if (version.includes("canary")) {
Expand Down
22 changes: 8 additions & 14 deletions packages/codemod/src/upgrade-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import {
getAllFiles,
getCollectionFromSource,
wrapDeclaration,
findIdentifier,
removeImport,
replaceImport,
replaceIdentifiers,
replaceBlitzPkgsVersions,
} from "./utils"
import {log} from "blitz"

const CURRENT_BLITZ_TAG = "alpha"

const isInternalBlitzMonorepoDevelopment = fs.existsSync(
path.join(__dirname, "../../../blitz-next"),
)
Expand Down Expand Up @@ -81,19 +83,11 @@ const upgradeLegacy = async () => {
steps.push({
name: "update dependencies in package.json",
action: async () => {
let packageJsonPath = require(path.resolve("package.json"))
packageJsonPath.dependencies["react"] = "latest"
packageJsonPath.dependencies["react-dom"] = "latest"
packageJsonPath.dependencies["@blitzjs/next"] = "alpha"
packageJsonPath.dependencies["@blitzjs/rpc"] = "alpha"
packageJsonPath.dependencies["@blitzjs/auth"] = "alpha"
packageJsonPath.dependencies["blitz"] = "alpha"
packageJsonPath.dependencies["next"] = "latest"
packageJsonPath.dependencies["prisma"] = "latest"
packageJsonPath.dependencies["@prisma/client"] = "latest"
packageJsonPath.devDependencies["typescript"] = isTypescript && "latest"

fs.writeFileSync(path.resolve("package.json"), JSON.stringify(packageJsonPath, null, " "))
const packageJson = require(path.resolve("package.json"))

const newPackageJson = await replaceBlitzPkgsVersions(packageJson, CURRENT_BLITZ_TAG)

fs.writeFileSync(path.resolve("package.json"), JSON.stringify(newPackageJson, null, " "))
},
})

Expand Down
29 changes: 29 additions & 0 deletions packages/codemod/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import j, {
Identifier,
} from "jscodeshift"
import {parseSync} from "@babel/core"
import {fetchDistTags} from "@blitzjs/generator"
import {PromiseReturnType} from "blitz"

export function findIdentifier(program: Collection<any>, name: string): Collection<Identifier> {
return program.find(j.Identifier, (node) => node.name === name)
Expand Down Expand Up @@ -326,3 +328,30 @@ export function replaceIdentifiers(
path.value.name = "NextApiRequest"
})
}

export const replaceBlitzPkgsVersions = async (
packageJson: {dependencies?: Record<string, any>},
npmTag: string,
) => {
let blitzPkgVersion = npmTag
const result = await fetchDistTags("blitz")
if (npmTag in result) {
blitzPkgVersion = result[npmTag]
}

if (!packageJson.dependencies) {
packageJson.dependencies = {}
}

packageJson.dependencies["@blitzjs/next"] = blitzPkgVersion
packageJson.dependencies["@blitzjs/rpc"] = blitzPkgVersion
packageJson.dependencies["@blitzjs/auth"] = blitzPkgVersion
packageJson.dependencies["blitz"] = blitzPkgVersion
packageJson.dependencies["next"] = "12.2.0"

// for zod, we want to use the latest version
const zodResult = await fetchDistTags("zod")
packageJson.dependencies["zod"] = zodResult["latest"] || "latest"

return packageJson
}
90 changes: 90 additions & 0 deletions packages/codemod/tests/codemod.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {describe, it, expect, vi, afterAll} from "vitest"
import {replaceBlitzPkgsVersions} from "../src/utils"

describe("replaceBlitzPkgsVersions", () => {
afterAll(() => {
vi.clearAllMocks()
})

vi.mock("@blitzjs/generator", async () => {
return {
fetchDistTags: vi.fn((pkg: string) => {
if (pkg === "blitz") {
return {alpha: "1.0.0", beta: "2.0.0", danger: "3.0.0"}
}
if (pkg === "zod") {
return {latest: "1.2.3"}
}
}),
}
})

const pkgJson = {
dependencies: {},
}

it("correctly updates versions with the alpha tag", async () => {
expect(await replaceBlitzPkgsVersions(pkgJson, "alpha")).toEqual({
dependencies: {
blitz: "1.0.0",
"@blitzjs/rpc": "1.0.0",
"@blitzjs/auth": "1.0.0",
"@blitzjs/next": "1.0.0",
next: "12.2.0",
zod: "1.2.3",
},
})
})

it("correctly updates versions with the beta tag", async () => {
expect(await replaceBlitzPkgsVersions(pkgJson, "beta")).toEqual({
dependencies: {
blitz: "2.0.0",
"@blitzjs/rpc": "2.0.0",
"@blitzjs/auth": "2.0.0",
"@blitzjs/next": "2.0.0",
next: "12.2.0",
zod: "1.2.3",
},
})
})

it("correctly updates versions with the danger tag", async () => {
expect(await replaceBlitzPkgsVersions(pkgJson, "danger")).toEqual({
dependencies: {
blitz: "3.0.0",
"@blitzjs/rpc": "3.0.0",
"@blitzjs/auth": "3.0.0",
"@blitzjs/next": "3.0.0",
next: "12.2.0",
zod: "1.2.3",
},
})
})

it("sets version as a provided tag if it wasn't found with fetchDistTag function", async () => {
expect(await replaceBlitzPkgsVersions(pkgJson, "custom")).toEqual({
dependencies: {
blitz: "custom",
"@blitzjs/rpc": "custom",
"@blitzjs/auth": "custom",
"@blitzjs/next": "custom",
next: "12.2.0",
zod: "1.2.3",
},
})
})

it("handles package.json without dependecies key", async () => {
expect(await replaceBlitzPkgsVersions({}, "custom")).toEqual({
dependencies: {
blitz: "custom",
"@blitzjs/rpc": "custom",
"@blitzjs/auth": "custom",
"@blitzjs/next": "custom",
next: "12.2.0",
zod: "1.2.3",
},
})
})
})
4 changes: 2 additions & 2 deletions packages/generator/src/generators/app-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface AppGeneratorOptions extends GeneratorOptions {
version: string
skipInstall: boolean
skipGit: boolean
form?: "finalform" | "hookform" | "formik"
form: "finalform" | "hookform" | "formik"
onPostInstall?: () => Promise<void>
}

Expand Down Expand Up @@ -120,7 +120,7 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
] = await Promise.all([
fetchLatestVersionsFor(pkg.dependencies),
fetchLatestVersionsFor(pkg.devDependencies),
getBlitzDependencyVersion(this.options.version),
getBlitzDependencyVersion(),
])

pkg.dependencies = newDependencies
Expand Down
2 changes: 2 additions & 0 deletions packages/generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export * from "./generators/form-generator"
export * from "./generator"
export * from "./conflict-checker"
export {getLatestVersion} from "./utils/get-latest-version"
export * from "./utils/npm-fetch"
export * from "./utils/get-blitz-dependency-version"
export {
singleCamel,
singlePascal,
Expand Down
14 changes: 6 additions & 8 deletions packages/generator/src/utils/get-blitz-dependency-version.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {Fallbackable} from "./fallbackable"
import {logFailedVersionFetch} from "./get-latest-version"
import {fetchDistTags} from "./npm-fetch"

export const getBlitzDependencyVersion = async (
cliVersion: string,
): Promise<Fallbackable<string>> => {
const CURRENT_BLITZ_TAG = "alpha"

export const getBlitzDependencyVersion = async () => {
try {
// TODO: Need to update this to handle alpha, beta and major
const {alpha} = await fetchDistTags("blitz")
const result = await fetchDistTags("blitz")

if (alpha) {
return {value: alpha}
if (CURRENT_BLITZ_TAG in result) {
return {value: result}
}

logFailedVersionFetch("blitz")
Expand Down
12 changes: 5 additions & 7 deletions packages/generator/src/utils/npm-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import got from "got"

type PackageInformation = any
type NpmDepResponse = {versions: Record<string, PackageInformation>}
export type NpmDepResponse = {versions: Record<string, PackageInformation>}

export const fetchAllVersions = async (dependency: string) => {
const res = await got(`https://registry.npmjs.org/${dependency}`, {
const res = (await got(`https://registry.npmjs.org/${dependency}`, {
retry: {limit: 3},
timeout: 3000,
responseType: "json",
}).json<NpmDepResponse>()
}).json<NpmDepResponse>()) as unknown as NpmDepResponse
return Object.keys(res.versions)
}

type NpmDistTagsResponse = {latest: string; canary: string; alpha: string}

export const fetchDistTags = async (dependency: string) => {
const res = await got(`https://registry.npmjs.org/-/package/${dependency}/dist-tags`, {
const res = (await got(`https://registry.npmjs.org/-/package/${dependency}/dist-tags`, {
retry: {limit: 3},
timeout: 3000,
responseType: "json",
}).json<NpmDistTagsResponse>()
}).json()) as unknown as Record<string, any>
return res
}

Expand Down

0 comments on commit 8dfaad0

Please sign in to comment.