Skip to content

Commit

Permalink
Copy over new script
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed Aug 2, 2017
1 parent cd867a9 commit d737cd3
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/gatsby-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
"dependencies": {
"babel-runtime": "^6.25.0",
"commander": "^2.11.0",
"fs-extra": "^4.0.1",
"hosted-git-info": "^2.5.0",
"lodash": "^4.17.4",
"resolve-cwd": "^2.0.0"
"resolve-cwd": "^2.0.0",
"tracer": "^0.8.9"
}
}
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ program
.command(`new [rootPath] [starter]`)
.description(`Create new Gatsby project.`)
.action((rootPath, starter) => {
const newCommand = require(`../utils/new`)
const newCommand = require(`./new`)
newCommand(rootPath, starter)
})

Expand Down
132 changes: 132 additions & 0 deletions packages/gatsby-cli/src/init-starter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* @flow weak */
import { exec, execSync } from "child_process"
import hostedGitInfo from "hosted-git-info"
import fs from "fs-extra"
import sysPath from "path"

let logger = console

// Checks the existence of yarn package
// We use yarnpkg instead of yarn to avoid conflict with Hadoop yarn
// Refer to https://github.com/yarnpkg/yarn/issues/673
//
// Returns true if yarn exists, false otherwise
const shouldUseYarn = () => {
try {
execSync(`yarnpkg --version`, { stdio: `ignore` })
return true
} catch (e) {
return false
}
}

// Executes `npm install` and `bower install` in rootPath.
//
// rootPath - String. Path to directory in which command will be executed.
// callback - Function. Takes stderr and stdout of executed process.
//
// Returns nothing.
const install = (rootPath, callback) => {
const prevDir = process.cwd()
logger.log(`Installing packages...`)
process.chdir(rootPath)
const installCmd = shouldUseYarn() ? `yarnpkg` : `npm install`
exec(installCmd, (error, stdout, stderr) => {
process.chdir(prevDir)
if (stdout) console.log(stdout.toString())
if (error !== null) {
const msg = stderr.toString()
callback(new Error(msg))
}
callback(null, stdout)
})
}

const ignored = path => !/^\.(git|hg)$/.test(sysPath.basename(path))

// Copy starter from file system.
//
// starterPath - String, file system path from which files will be taken.
// rootPath - String, directory to which starter files will be copied.
// callback - Function.
//
// Returns nothing.
const copy = (starterPath, rootPath, callback) => {
const copyDirectory = () => {
fs.copy(starterPath, rootPath, { filter: ignored }, error => {
if (error !== null) return callback(new Error(error))
logger.log(`Created starter directory layout`)
install(rootPath, callback)
return false
})
}

// Chmod with 755.
// 493 = parseInt('755', 8)
fs.mkdirp(rootPath, { mode: 493 }, error => {
if (error !== null) callback(new Error(error))
return fs.exists(starterPath, exists => {
if (!exists) {
const chmodError = `starter ${starterPath} doesn't exist`
return callback(new Error(chmodError))
}
logger.log(`Copying local starter to ${rootPath} ...`)

copyDirectory()
return true
})
})
}

// Clones starter from URI.
//
// address - String, URI. https:, github: or git: may be used.
// rootPath - String, directory to which starter files will be copied.
// callback - Function.
//
// Returns nothing.
const clone = (hostInfo, rootPath, callback) => {
const url = hostInfo.git({ noCommittish: true })
const branch = hostInfo.committish ? `-b ${hostInfo.committish}` : ``

logger.log(`Cloning git repo ${url} to ${rootPath}...`)
const cmd = `git clone ${branch} ${url} ${rootPath} --single-branch`

exec(cmd, (error, stdout, stderr) => {
if (error !== null) {
return callback(new Error(`Git clone error: ${stderr.toString()}`))
}
logger.log(`Created starter directory layout`)
return fs.remove(sysPath.join(rootPath, `.git`), removeError => {
if (error !== null) return callback(new Error(removeError))
install(rootPath, callback)
return true
})
})
}

// Main function that clones or copies the starter.
//
// starter - String, file system path or URI of starter.
// rootPath - String, directory to which starter files will be copied.
// callback - Function.
//
// Returns nothing.
const initStarter = (starter, options = {}) =>
new Promise((resolve, reject) => {
const callback = (err, value) => (err ? reject(err) : resolve(value))

const cwd = process.cwd()
const rootPath = options.rootPath || cwd
if (options.logger) logger = options.logger

if (fs.existsSync(sysPath.join(rootPath, `package.json`)))
throw new Error(`Directory ${rootPath} is already an npm project`)

const hostedInfo = hostedGitInfo.fromUrl(starter)

if (hostedInfo) clone(hostedInfo, rootPath, callback)
else copy(starter, rootPath, callback)
})

module.exports = initStarter
8 changes: 8 additions & 0 deletions packages/gatsby-cli/src/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* @flow weak */
const logger = require(`tracer`).colorConsole()

const initStarter = require(`./init-starter`)

module.exports = (rootPath, starter = `gatsbyjs/gatsby-starter-default`) => {
initStarter(starter, { rootPath, logger }).catch(error => logger.error(error))
}

0 comments on commit d737cd3

Please sign in to comment.