Skip to content

Commit

Permalink
Default to using the local install of gatsby and warn if there's not one
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed Mar 27, 2016
1 parent 24a4869 commit 8196916
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 128 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"presets": ['react', 'es2015', 'stage-0']
"presets": ['react', 'es2015', 'stage-0'],
"plugins": [
'transform-object-rest-spread'
]
}
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parser": "babel-eslint",
"extends": "eslint-config-airbnb",
"rules": {
"indent": [2, 2, {"SwitchCase": 1}],
Expand Down
22 changes: 0 additions & 22 deletions bin/build.js

This file was deleted.

24 changes: 0 additions & 24 deletions bin/develop.js

This file was deleted.

4 changes: 0 additions & 4 deletions bin/gatsby-build.js

This file was deleted.

4 changes: 0 additions & 4 deletions bin/gatsby-develop.js

This file was deleted.

4 changes: 0 additions & 4 deletions bin/gatsby-new.js

This file was deleted.

48 changes: 47 additions & 1 deletion bin/gatsby.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
#!/usr/bin/env node

/*eslint-disable */
require('babel-core/register')
require('./index')

global.appStartTime = Date.now()

var sysPath = require('path')
var fs = require('fs')
var version = process.version
var verDigit = parseInt(version.match(/^v(\d+)\./)[1], 10)

if (verDigit < 0.12) {
console.error(
'Error: Gatsby 0.9+ requires node.js v0.12 or higher (you have ' + version + ') ' +
'Upgrade node to the latest stable release.'
)
process.exit()
}

var cwd = sysPath.resolve('.')
var cliFile = sysPath.join('dist', 'bin', 'cli.js')
var localPath = sysPath.join(cwd, 'node_modules', 'gatsby', cliFile)

var loadGatsby = function (path) {
require(path)
}

var loadGlobalGatsby = function () {
fs.realpath(__dirname, function (err, real) {
if (err) throw err
loadGatsby(sysPath.join(real, '..', cliFile))
})
}

fs.access(localPath, function (error) {
if (error) {
loadGlobalGatsby()
} else {
try {
loadGatsby(localPath)
} catch(error) {
console.error(
'Gatsby: Local install exists but failed to load it. ' +
'Continuing with global install:', error
)
loadGlobalGatsby()
}
}
})
32 changes: 0 additions & 32 deletions bin/index.js

This file was deleted.

31 changes: 0 additions & 31 deletions bin/new.js

This file was deleted.

79 changes: 79 additions & 0 deletions lib/bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const program = require('commander')
const packageJson = require('../../package.json')
const _ = require('lodash')
const fs = require('fs-extra')
const path = require('path')

// Copy our load-context function to root of site in a dot file.
const gatsbyFile = `${__dirname}/../utils/load-context.js`
const siteDirectory = path.resolve('.')
const fileName = `${siteDirectory}/.gatsby-context.js`
fs.copy(gatsbyFile, fileName)

const defaultHost = process.platform === 'win32'
? 'localhost'
: '0.0.0.0'

const directory = path.resolve('.')

program
.version(packageJson.version)
.usage('[command] [options]')

program.command('develop')
.description('Start development server. Watches files and rebuilds and hot reloads if something changes') // eslint-disable-line max-len
.option('-h, --host <url>',
`Set host. Defaults to ${defaultHost}`,
defaultHost
)
.option('-p, --port <port>', 'Set port. Defaults to 8000', '8000')
.action((command) => {
const develop = require('../utils/develop')
const p = {
...command,
directory,
}
develop(p)
})

program.command('build')
.description('Build a Gatsby project.')
.option('--prefix-links', 'Build site with links prefixed (set prefix in your config).')
.action((command) => {
const build = require('../utils/build')
const p = {
...command,
directory,
}
console.log('running build')
build(p, (err) => {
if (err) {
throw err
} else {
console.log('Done')
}
})
})

program
.command('new [rootPath] [starter]')
.description('Create new Gatsby project in path [.].')
.action((rootPath, starter) => {
console.log(rootPath, starter)
const newCommand = require('../utils/new')
console.log('running new')
newCommand(rootPath, starter)
})


// If the user types an unknown sub-command, just display the help.
const subCmd = process.argv.slice(2, 3)[0]
const cmds = _.map(program.commands, '_name')
console.log(subCmd, cmds)

if (!_.includes(cmds, subCmd)) {
console.log('help me')
program.help()
} else {
program.parse(process.argv)
}
File renamed without changes.
10 changes: 8 additions & 2 deletions lib/utils/glob-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import frontMatter from 'front-matter'
import htmlFrontMatter from 'html-frontmatter'
import objectAssign from 'object-assign'
const debug = require('debug')('gatsby:glob')
const gatsbyNodeConfig = path.resolve(process.cwd(), './gatsby-node.js')
const { rewritePath } = require(gatsbyNodeConfig)
let rewritePath
try {
const gatsbyNodeConfig = path.resolve(process.cwd(), './gatsby-node.js')
const nodeConfig = require(gatsbyNodeConfig)
rewritePath = nodeConfig.rewritePath
} catch (e) {
// Ignore
}

module.exports = (directory, callback) => {
const pagesData = []
Expand Down
17 changes: 17 additions & 0 deletions lib/utils/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const loggy = require('loggy')

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

module.exports = (rootPath, starter='gh:gatsbyjs/gatsby-starter-default') => {
initStarter(
starter,
{
rootPath,
logger: loggy,
}, (error) => {
if (error) {
loggy.error(error)
}
}
)
}
10 changes: 8 additions & 2 deletions lib/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import ExtractTextPlugin from 'extract-text-webpack-plugin'
import Config from 'webpack-configurator'
const debug = require('debug')('gatsby:webpack-config')
import path from 'path'
const gatsbyNodeConfig = path.resolve(process.cwd(), './gatsby-node.js')
const { modifyWebpackConfig } = require(gatsbyNodeConfig)
let modifyWebpackConfig
try {
const gatsbyNodeConfig = path.resolve(process.cwd(), './gatsby-node.js')
const nodeConfig = require(gatsbyNodeConfig)
modifyWebpackConfig = nodeConfig.modifyWebpackConfig
} catch (e) {
// Ignore
}

module.exports = (program, directory, stage, webpackPort = 1500, routes = []) => {
debug(`Loading webpack config for stage "${stage}"`)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"babel-core": "^6.7.2",
"babel-loader": "^6.2.4",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-transform-object-rest-spread": "^6.6.5",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-react-hmre": "^1.1.1",
Expand All @@ -22,7 +23,7 @@
"cjsx-loader": "^2.0.1",
"coffee-loader": "^0.7.2",
"coffee-script": "^1.9.3",
"commander": "^2.8.1",
"commander": "^2.9.0",
"css-loader": "^0.23.1",
"cssnano": "^3.5.2",
"debug": "^2.2.0",
Expand Down

0 comments on commit 8196916

Please sign in to comment.