Skip to content

Commit

Permalink
@blitzjs/installer improvements (#2887)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanM04 authored Nov 12, 2021
1 parent f85b0ef commit 18c6981
Show file tree
Hide file tree
Showing 66 changed files with 1,379 additions and 682 deletions.
2 changes: 1 addition & 1 deletion examples/i18n-next-rosetta/app/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Document, Html, DocumentHead, Main, BlitzScript /*DocumentContext*/} from 'blitz'
import { Document, Html, DocumentHead, Main, BlitzScript /*DocumentContext*/ } from "blitz"

class MyDocument extends Document {
// Only uncomment if you need to customize this behaviour
Expand Down
16 changes: 11 additions & 5 deletions examples/i18n-next-rosetta/test/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export * from "@testing-library/react"
// router: { pathname: '/my-custom-pathname' },
// });
// --------------------------------------------------
export function render(ui: RenderUI, { wrapper, router, dehydratedState, ...options }: RenderOptions = {}) {
export function render(
ui: RenderUI,
{ wrapper, router, dehydratedState, ...options }: RenderOptions = {}
) {
if (!wrapper) {
// Add a default context wrapper if one isn't supplied from the test
wrapper = ({ children }) => (
Expand All @@ -51,7 +54,7 @@ export function render(ui: RenderUI, { wrapper, router, dehydratedState, ...opti
// --------------------------------------------------
export function renderHook(
hook: RenderHook,
{ wrapper, router, dehydratedState,...options }: RenderHookOptions = {}
{ wrapper, router, dehydratedState, ...options }: RenderHookOptions = {}
) {
if (!wrapper) {
// Add a default context wrapper if one isn't supplied from the test
Expand All @@ -66,7 +69,7 @@ export function renderHook(
return defaultRenderHook(hook, { wrapper, ...options })
}

export const mockRouter: BlitzRouter = {
export const mockRouter: BlitzRouter = {
basePath: "",
pathname: "/",
route: "/",
Expand All @@ -92,8 +95,11 @@ export const mockRouter: BlitzRouter = {

type DefaultParams = Parameters<typeof defaultRender>
type RenderUI = DefaultParams[0]
type RenderOptions = DefaultParams[1] & { router?: Partial<BlitzRouter>, dehydratedState?: unknown }
type RenderOptions = DefaultParams[1] & { router?: Partial<BlitzRouter>; dehydratedState?: unknown }

type DefaultHookParams = Parameters<typeof defaultRenderHook>
type RenderHook = DefaultHookParams[0]
type RenderHookOptions = DefaultHookParams[1] & { router?: Partial<BlitzRouter>, dehydratedState?: unknown }
type RenderHookOptions = DefaultHookParams[1] & {
router?: Partial<BlitzRouter>
dehydratedState?: unknown
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { namedTypes } from 'ast-types'
// eslint-disable-next-line import/no-extraneous-dependencies
import { NodePath } from 'ast-types/lib/node-path'
import { types } from 'next/dist/compiled/recast'
import {
CONFORMANCE_ERROR_PREFIX,
CONFORMANCE_WARNING_PREFIX,
Expand Down Expand Up @@ -155,13 +154,12 @@ export class DuplicatePolyfillsConformanceCheck
{
visitor: 'visitCallExpression',
inspectNode: (path: NodePath) => {
const { node }: { node: types.namedTypes.CallExpression } = path
const { node }: { node: namedTypes.CallExpression } = path
if (!node.arguments || node.arguments.length < 2) {
return EARLY_EXIT_SUCCESS_RESULT
}
if (isNodeCreatingScriptElement(node)) {
const propsNode = node
.arguments[1] as types.namedTypes.ObjectExpression
const propsNode = node.arguments[1] as namedTypes.ObjectExpression
if (!propsNode.properties) {
return EARLY_EXIT_SUCCESS_RESULT
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { namedTypes } from 'ast-types'
import { types } from 'next/dist/compiled/recast'

export function isNodeCreatingScriptElement(node: namedTypes.CallExpression) {
const callee = node.callee as namedTypes.Identifier
Expand All @@ -15,9 +14,7 @@ export function isNodeCreatingScriptElement(node: namedTypes.CallExpression) {
return callee.name === '__jsx' && componentNode.value === 'script'
}

export function reducePropsToObject(
propsNode: types.namedTypes.ObjectExpression
) {
export function reducePropsToObject(propsNode: namedTypes.ObjectExpression) {
return propsNode.properties.reduce((originalProps, prop: any) => {
// @ts-ignore
originalProps[prop.key.name] = prop.value.value
Expand Down
2 changes: 1 addition & 1 deletion nextjs/packages/next/compiled/recast/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nextjs/packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"@node-rs/helper": "1.2.1",
"@types/secure-password": "3.1.0",
"assert": "2.0.0",
"ast-types": "0.13.2",
"ast-types": "0.14.2",
"b64-lite": "^1.4.0",
"bad-behavior": "^1.0.1",
"browserify-zlib": "0.2.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"@babel/preset-env": "7.12.11",
"@babel/preset-react": "7.12.10",
"@babel/preset-typescript": "7.12.7",
"@juanm04/cpx": "2.0.0",
"@juanm04/cpx": "2.0.1",
"@manypkg/cli": "0.17.0",
"@preconstruct/cli": "2.0.7",
"@rollup/pluginutils": "4.1.0",
Expand Down
14 changes: 7 additions & 7 deletions packages/cli/test/commands/install.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as path from "path"
// import * as path from "path"
import {Install, RecipeLocation} from "../../src/commands/install"
import tempRecipe from "../__fixtures__/installer"
// import tempRecipe from "../__fixtures__/installer"

describe("`install` command", () => {
afterAll(() => {
jest.resetAllMocks()
})

it("runs local installer", async () => {
jest.spyOn(tempRecipe, "run")
await Install.run([path.resolve(__dirname, "../__fixtures__/installer")])
expect(tempRecipe.run).toHaveBeenCalledWith({})
})
// it("runs local installer", async () => {
// jest.spyOn(tempRecipe, "run")
// await Install.run([path.resolve(__dirname, "../__fixtures__/installer")])
// expect(tempRecipe.run).toHaveBeenCalledWith({})
// })

it("properly parses remote installer args", () => {
const normalizePath = Install.prototype.normalizeRecipePath
Expand Down
8 changes: 4 additions & 4 deletions packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@
"@babel/plugin-transform-typescript": "7.12.1",
"@blitzjs/display": "0.42.4",
"@blitzjs/server": "0.42.4",
"@mrleebo/prisma-ast": "^0.2.4",
"@types/jscodeshift": "0.7.2",
"@mrleebo/prisma-ast": "0.2.4",
"@types/jscodeshift": "0.11.2",
"chalk": "^4.1.0",
"cross-spawn": "7.0.3",
"diff": "5.0.0",
"enquirer": "2.3.6",
"fs-extra": "^9.1.0",
"got": "^11.8.1",
"jscodeshift": "0.11.0",
"jscodeshift": "0.13.0",
"mem-fs": "1.2.0",
"mem-fs-editor": "8.0.0",
"npm-which": "^3.0.1",
"pluralize": "^8.0.0",
"recast": "0.20.4",
"recast": "0.20.5",
"username": "^5.1.0",
"vinyl": "2.2.1"
},
Expand Down
13 changes: 7 additions & 6 deletions packages/installer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@
"@babel/plugin-transform-typescript": "7.12.1",
"@blitzjs/display": "0.42.4",
"@blitzjs/generator": "0.42.4",
"@mrleebo/prisma-ast": "^0.2.4",
"@mrleebo/prisma-ast": "0.2.4",
"@prisma/sdk": "2.19.0",
"@types/jscodeshift": "0.7.2",
"@types/jscodeshift": "0.11.2",
"ast-types": "0.14.2",
"cross-spawn": "7.0.3",
"diff": "5.0.0",
"enquirer": "2.3.6",
"fs-extra": "^9.1.0",
"globby": "11.0.2",
"ink": "3.0.8",
"ink-spinner": "4.0.1",
"ink": "3.2.0",
"ink-spinner": "4.0.3",
"ink-testing-library": "2.1.0",
"jscodeshift": "0.11.0",
"recast": "0.20.4"
"jscodeshift": "0.13.0",
"recast": "0.20.5"
},
"gitHead": "d3b9fce0bdd251c2b1890793b0aa1cd77c1c0922"
}
6 changes: 2 additions & 4 deletions packages/installer/src/executors/add-dependency-executor.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {spawn} from "cross-spawn"
import * as fs from "fs-extra"
import {Box, Text} from "ink"
// TODO: Once the dependency issue of ink-spinner is resolved, you should uncomment it (https://github.com/blitz-js/blitz/issues/2793)
// import Spinner from "ink-spinner"
import Spinner from "ink-spinner"
import * as path from "path"
import * as React from "react"
import {Newline} from "../components/newline"
Expand All @@ -27,12 +26,11 @@ export function isAddDependencyExecutor(executor: ExecutorConfig): executor is C

export const type = "add-dependency"

// TODO: Once the dependency issue of ink-spinner is resolved, you should change "loading.." to <Spinner /> (https://github.com/blitz-js/blitz/issues/2793)
function Package({pkg, loading}: {pkg: NpmPackage; loading: boolean}) {
return (
<Text>
{` `}
{loading ? "Loading..." : "📦"}
{loading ? <Spinner /> : "📦"}
{` ${pkg.name}@${pkg.version}`}
</Text>
)
Expand Down
9 changes: 3 additions & 6 deletions packages/installer/src/executors/file-transform-executor.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {createPatch} from "diff"
import * as fs from "fs-extra"
import {Box, Text} from "ink"
// TODO: Once the dependency issue of ink-spinner is resolved, you should uncomment it (https://github.com/blitz-js/blitz/issues/2793)
// import Spinner from "ink-spinner"
import Spinner from "ink-spinner"
import * as React from "react"
import {Newline} from "../components/newline"
import {
Expand Down Expand Up @@ -60,12 +59,11 @@ export const Propose: Executor["Propose"] = ({cliArgs, onProposalAccepted, step}
// process would just hang.
if (error) throw error

// TODO: Once the dependency issue of ink-spinner is resolved, you should uncomment it (https://github.com/blitz-js/blitz/issues/2793)
if (!diff) {
return (
<Box>
<Text>
{/* <Spinner /> */}
<Spinner />
Generating file diff...
</Text>
</Box>
Expand Down Expand Up @@ -116,11 +114,10 @@ export const Commit: Executor["Commit"] = ({onChangeCommitted, proposalData: fil
})()
}, [filePath, step])

// TODO: Once the dependency issue of ink-spinner is resolved, you should uncomment it (https://github.com/blitz-js/blitz/issues/2793
if (loading) {
return (
<Box>
{/* <Spinner /> */}
<Spinner />
<Text>Applying file changes</Text>
</Box>
)
Expand Down
2 changes: 1 addition & 1 deletion packages/installer/src/recipe-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function RecipeRenderer({cliArgs, steps, recipeMeta}: RecipeProps) {
React.useEffect(() => {
if (
state.current === state.steps.length - 1 &&
state.steps[state.current].status === Status.Committed
state.steps[state.current]?.status === Status.Committed
) {
exit()
}
Expand Down
34 changes: 0 additions & 34 deletions packages/installer/src/transforms/add-babel-plugin.ts

This file was deleted.

89 changes: 27 additions & 62 deletions packages/installer/src/transforms/add-blitz-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,31 @@
import type {ExpressionKind} from "ast-types/gen/kinds"
import j from "jscodeshift"
import {Collection} from "jscodeshift"
import {transformBlitzConfig} from "."

export function addBlitzMiddleware(program: Collection<j.Program>, middleware: any) {
program
.find(j.AssignmentExpression, {
operator: "=",
left: {object: {name: "module"}, property: {name: "exports"}},
right: {},
})
.forEach((moduleExportsExpression) => {
let config: j.ObjectExpression | undefined = undefined
const obj = moduleExportsExpression.value.right
// There are a few ways the config could be defined - this function only supports two at the moment.
// There are probably a few other methods, but it probably starts to go out of scope for a recipe.
if (obj.type === "Identifier") {
// Default method, reference a variable.
j(obj).forEach(({node}) => {
// Lets find the variable by that name
const configobj = program.find(j.VariableDeclarator, {
id: {name: node.name},
})
export const addBlitzMiddleware = (program: j.Collection<j.Program>, middleware: ExpressionKind) =>
transformBlitzConfig(program, (config) => {
// Locate the middleware property
const middlewareProp = config.properties.find(
(value) =>
value.type === "ObjectProperty" &&
value.key.type === "Identifier" &&
value.key.name === "middleware",
) as j.ObjectProperty | undefined

// Now read it in and check it is an ObjectExpression
configobj.forEach((path) => {
if (path.value.init?.type === "ObjectExpression") {
config = path.value.init
}
})
})
} else if (obj.type === "ObjectExpression") {
// Alternative method, they're just returning object.
config = obj
} else {
// TODO: handle more types if people need it
console.warn("unhandled blitz config type: " + obj.type)
}
if (middlewareProp && middlewareProp.value.type === "ArrayExpression") {
// We found it, pop on our middleware.
middlewareProp.value.elements.push(middleware)
} else {
// No middleware prop, add our own.
config.properties.push(
j.property("init", j.identifier("middleware"), {
type: "ArrayExpression",
elements: [middleware],
loc: null,
comments: null,
}),
)
}

if (config) {
// Locate the middleware property
const middlewareProp = config.properties.find(
(value) =>
value.type === "ObjectProperty" &&
value.key.type === "Identifier" &&
value.key.name === "middleware",
) as j.ObjectProperty | undefined

if (middlewareProp && middlewareProp.value.type === "ArrayExpression") {
// We found it, pop on our middleware.
middlewareProp.value.elements.push(middleware)
} else {
// No middleware prop, add our own.
config.properties.push(
j.property("init", j.identifier("middleware"), {
type: "ArrayExpression",
elements: [middleware],
loc: null,
comments: null,
}),
)
}
}
})

return program
}
return config
})
4 changes: 3 additions & 1 deletion packages/installer/src/transforms/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from "./add-import"
export * from "./add-babel-plugin"
export * from "./add-blitz-middleware"
export * from "./find-module-exports-expressions"
export * from "./prisma"
export * from "./transform-blitz-config"
export * from "./update-babel-config"
export * from "./wrap-blitz-config"
Loading

0 comments on commit 18c6981

Please sign in to comment.