Skip to content

Commit

Permalink
Try to install plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Nov 2, 2020
1 parent 7e374d7 commit 8e458f5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
21 changes: 15 additions & 6 deletions packages/create-gatsby/src/index.ts
Expand Up @@ -3,7 +3,9 @@ import cmses from "./cmses.json"
import styles from "./styles.json"
import features from "./features.json"
import { initStarter } from "./init-starter"
import { installPlugins } from "./install-plugins"
import c from "ansi-colors"
import path from "path"

const makeChoices = (
options: Record<string, string>
Expand Down Expand Up @@ -72,10 +74,14 @@ export async function run(): Promise<void> {
const messages: Array<string> = [
`🛠 Create a new Gatsby site in the folder ${c.blueBright(data.project)}`,
]

const plugins = []

if (data.cms && data.cms !== `none`) {
messages.push(
`📚 Install and configure the plugin for ${c.red(cmses[data.cms])}`
)
plugins.push(data.cms)
}

if (data.styling && data.styling !== `none`) {
Expand All @@ -84,12 +90,16 @@ export async function run(): Promise<void> {
styles[data.styling]
)} for styling your site`
)
plugins.push(data.styling)
}

if (data.features?.length) {
messages.push(
`🔌 Install ${data.features.map(feat => c.magenta(feat)).join(`, `)}`
`🔌 Install ${data.features
.map((feat: string) => c.magenta(feat))
.join(`, `)}`
)
plugins.push(...data.features)
}

console.log(`
Expand All @@ -115,9 +125,8 @@ ${c.bold(`Thanks! Here's what we'll now do:`)}
`https://github.com/gatsbyjs/gatsby-starter-hello-world.git`,
data.project
)
console.log(
`This is the point where we'd then install the plugins, but ${c.redBright(
`gatsby plugin add`
)} hasn't been implemented yet`
)

console.log(c.bold.green(`Installing plugins...`))

await installPlugins(plugins, path.resolve(data.project))
}
26 changes: 3 additions & 23 deletions packages/create-gatsby/src/init-starter.ts
Expand Up @@ -170,29 +170,14 @@ interface IGetPaths {
selectedOtherStarter: boolean
}

const successMessage = (): void => {
const successMessage = (rootPath: string): void => {
reporter.info(`
Your new Gatsby site has been successfully bootstrapped. Start developing it by running:
cd ${rootPath}
gatsby develop
`)
}

export async function installPlugins(
plugins: Array<string>,
rootPath: string
): Promise<void> {
const command = require.resolve(`gatsby-cli/lib/create-cli`, {
paths: [rootPath],
})

if (!command) {
reporter.error(`Did not install Gatsby`)
return void 0
}
const { createCli } = require(command)
return createCli([process.argv[0], command, `plugin`, `add`, ...plugins])
}

/**
* Main function that clones or copies the starter.
*/
Expand All @@ -204,11 +189,6 @@ export async function initStarter(

await clone(starter, sitePath)

// await installPlugins(
// [`gatsby-plugin-image`, `gatsby-transformer-sharp`],
// sitePath
// )

const sitePackageJson = await fs
.readJSON(path.join(sitePath, `package.json`))
.catch(() => {
Expand All @@ -226,6 +206,6 @@ export async function initStarter(
false
)

successMessage()
successMessage(rootPath)
// trackCli(`NEW_PROJECT_END`);
}
27 changes: 27 additions & 0 deletions packages/create-gatsby/src/install-plugins.ts
@@ -0,0 +1,27 @@
import { reporter } from "./reporter"

export async function installPlugins(
plugins: Array<string>,
rootPath: string
): Promise<void> {
let installPluginCommand

try {
installPluginCommand = require.resolve(`gatsby-cli/lib/plugin-add`, {
paths: [rootPath],
})
} catch (e) {
// The file is missing
}

if (!installPluginCommand) {
reporter.error(
`Did not install Gatsby, or the version of gatsby-cli is too old`
)
return void 0
}

const { addPlugins } = require(installPluginCommand)

return addPlugins(plugins, rootPath)
}

0 comments on commit 8e458f5

Please sign in to comment.