-
-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@blitzjs/installer
improvements (#2887)
- Loading branch information
Showing
66 changed files
with
1,379 additions
and
682 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
89 changes: 27 additions & 62 deletions
89
packages/installer/src/transforms/add-blitz-middleware.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 |
---|---|---|
@@ -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 | ||
}) |
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,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" |
Oops, something went wrong.