Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[legacy-framework] Allow user to select a recipe with blitz install command #2828

Merged
merged 38 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ab76352
add -list flags
mochi-sann Oct 9, 2021
04334f1
Create a function to display a list of official recipes
mochi-sann Oct 9, 2021
934fe8d
set to callback null
mochi-sann Oct 9, 2021
41b5257
Array without undefinded
mochi-sann Oct 9, 2021
81bd445
show official list in cli table
mochi-sann Oct 9, 2021
a902f94
fix: recipe list Head text
mochi-sann Oct 9, 2021
ee3f540
Merge branch 'canary' into blitz-cli-add-import_-list
mochi-sann Oct 9, 2021
a91ea88
fix(cli): Don't include tsconfig.json in Recipe list.
mochi-sann Oct 11, 2021
2187d1d
fix(cli): change table head
mochi-sann Oct 11, 2021
620ab7f
use to reduce Update packages/cli/src/commands/install.ts
mochi-sann Oct 12, 2021
be2ef2d
Change table display from cli-table to @blitzjs/display
mochi-sann Oct 12, 2021
3c5e0fc
Merge branch 'blitz-cli-add-import_-list' of github.com:mochi-sann/bl…
mochi-sann Oct 12, 2021
a3d7f4b
add return
mochi-sann Oct 12, 2021
de3e0ed
Fix debugging code
mochi-sann Oct 13, 2021
2e7ab4b
add test of Get an array of the Recipe list
mochi-sann Oct 13, 2021
2a3e266
Separate into functions for easy testing
mochi-sann Oct 13, 2021
115a407
Added tests for List flag table
mochi-sann Oct 13, 2021
a2cac59
fix :tsconfig.json is no longer included in the Recipe list.
mochi-sann Oct 14, 2021
f07c227
Add a test to check if tsconfig.json is not included
mochi-sann Oct 14, 2021
f73fe89
fix : Remove unneeded variables and asynchronous syntax
mochi-sann Oct 14, 2021
a725bb2
Fix: Fixed variable names, etc.
mochi-sann Oct 14, 2021
3a7b378
fix tests to work correctly.
mochi-sann Oct 14, 2021
47d61cf
Merge branch 'canary' into blitz-cli-add-import_-list
beerose Oct 15, 2021
dd63761
Recipe can now be installed from select prompt.
mochi-sann Oct 16, 2021
f3e0765
Added the ability to select a recipe when there is nothing in args.
mochi-sann Oct 16, 2021
d26d9a0
remove debug
mochi-sann Oct 16, 2021
3ef5556
Remove the test
mochi-sann Oct 16, 2021
ebf437d
Update packages/cli/src/commands/install.ts
mochi-sann Oct 18, 2021
be69493
Modify as suggested
mochi-sann Oct 18, 2021
9661bf3
Remove the list flag because the behavior is the same whether the lis…
mochi-sann Oct 18, 2021
3a04e8a
minor changes
beerose Oct 18, 2021
1f35d3f
Merge branch 'canary' into blitz-cli-add-import_-list
beerose Oct 18, 2021
7c04687
Merge branch 'canary' into blitz-cli-add-import_-list
beerose Oct 18, 2021
08cdf28
fix console message
beerose Oct 18, 2021
57a4588
Merge branch 'blitz-cli-add-import_-list' of github.com:mochi-sann/bl…
beerose Oct 18, 2021
d582aab
Update packages/cli/src/commands/install.ts
beerose Oct 18, 2021
d23f966
Fix typo
beerose Oct 18, 2021
875d41e
Update packages/cli/test/commands/install.test.ts
beerose Oct 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 78 additions & 5 deletions packages/cli/src/commands/install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {getConfig} from "@blitzjs/config"
import {log} from "@blitzjs/display"
import {log, table as Table} from "@blitzjs/display"
beerose marked this conversation as resolved.
Show resolved Hide resolved
import type {RecipeExecutor} from "@blitzjs/installer"
import {flags} from "@oclif/command"
import {bootstrap} from "global-agent"
Expand Down Expand Up @@ -66,19 +66,36 @@ interface RecipeMeta {
location: RecipeLocation
}

interface Tree {
path: string
mode: string
type: string
sha: string
size: number
url: string
}

interface GithubRepoAPITrees {
sha: string
url: string
tree: Tree[]
truncated: boolean
}

export class Install extends Command {
static description = "Install a Recipe into your Blitz app"
static aliases = ["i"]
static strict = false

static flags = {
help: flags.help({char: "h"}),
list: flags.boolean({char: "l", description: "show all official recipe list", default: false}),
}

static args = [
{
name: "recipe",
required: true,
required: false,
description:
"Name of a Blitz recipe from @blitzjs/blitz/recipes, or a file path to a local recipe definition",
},
Expand Down Expand Up @@ -126,6 +143,53 @@ export class Install extends Command {
}
}

async getOfficialRecipeList(): Promise<string[]> {
return await gotJSON(`${API_ROOT}blitz-js/blitz/git/trees/canary?recursive=1`).then(
(release: GithubRepoAPITrees) =>
release.tree.reduce((recipieList: string[], item) => {
const filePath = item.path.split("/")
const [directory, recipieName] = filePath
if (directory === "recipes" && filePath.length === 2 && item.type === "tree") {
recipieList.push(recipieName)
}
return recipieList
beerose marked this conversation as resolved.
Show resolved Hide resolved
}, []),
)
}

async officialRecipeListTable(recipesList: string[]): Promise<string> {
const recipesTable = new Table({
columns: [
{
name: "Recipe name",
alignment: "left",
},
{
name: "Install command",
alignment: "left",
},
],
})
debug("recipesList", recipesList)
recipesTable.addRows(
recipesList.map((recipe) => {
return {
"Recipe name": recipe,
"Install command": `blitz install ${recipe}`,
}
}),
)

const installRecipe: any = await this.enquirer.prompt({
type: "select",
name: "recipeName",
message: "Select a recipe to install",
choices: recipesList,
})
mochi-sann marked this conversation as resolved.
Show resolved Hide resolved

return installRecipe.recipeName
}

/**
* Clones the repository into a temp directory, returning the path to the new directory
*
Expand Down Expand Up @@ -204,12 +268,21 @@ export class Install extends Command {

await this.setupProxySupport()

const {args} = this.parse(Install)
const {args, flags} = this.parse(Install)
debug(`flags`, flags)
let recipeInfo
// show all official recipes
if (flags.list || !args.recipe) {
const officialRecipeList = await this.getOfficialRecipeList()
recipeInfo = this.normalizeRecipePath(await this.officialRecipeListTable(officialRecipeList))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering do we still need the --list flag if it's handled in the same way as not passing the arg? What do you think?

Copy link
Author

@mochi-sann mochi-sann Oct 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Gatsby.js cli, which also implements Recipe, works in a similar way, but without the list flag, so we think the Blitz cli should be able to do the same without the list flag
screenshot 2021-10-18 12 04 38

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9661bf3
I removed list flag

} else {
recipeInfo = this.normalizeRecipePath(args.recipe)
}

const originalCwd = process.cwd()
const recipeInfo = this.normalizeRecipePath(args.recipe)

debug("recipeInfo", recipeInfo)
const chalk = (await import("chalk")).default

if (recipeInfo.location === RecipeLocation.Remote) {
const apiUrl = recipeInfo.path.replace(GH_ROOT, API_ROOT)
const rawUrl = recipeInfo.path.replace(GH_ROOT, RAW_ROOT)
Expand Down
36 changes: 36 additions & 0 deletions packages/cli/test/commands/install.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {expect as oclifExpect, test as oclifTest} from "@oclif/test"
beerose marked this conversation as resolved.
Show resolved Hide resolved
import * as path from "path"
import {Install, RecipeLocation} from "../../src/commands/install"
import tempRecipe from "../__fixtures__/installer"
Expand Down Expand Up @@ -29,4 +30,39 @@ describe("`install` command", () => {
location: RecipeLocation.Remote,
})
})
describe("run official recipes list flag ", () => {
it("list of official recipes ", () => {
const getOfficialRecipeList = Install.prototype.getOfficialRecipeList
return getOfficialRecipeList().then((recipeList) => {
expect(recipeList).toEqual(
expect.arrayContaining([
"base-web",
"bumbag-ui",
"chakra-ui",
"emotion",
"gh-action-yarn-mariadb",
"gh-action-yarn-postgres",
"ghost",
"graphql-apollo-server",
"logrocket",
"material-ui",
"quirrel",
"reflexjs",
"render",
"secureheaders",
"stitches",
"styled-components",
"tailwind",
"theme-ui",
]),
)
})
})
it("not to be included tsconfig.json of official recipes", () => {
const getOfficialRecipeList = Install.prototype.getOfficialRecipeList
return getOfficialRecipeList().then((recipeList) => {
expect(recipeList).toEqual(expect.not.arrayContaining(["tsconfig.json"]))
})
})
})
})