Skip to content

Commit

Permalink
Resolves #59 . Resolves #58
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Hernandez committed May 8, 2018
1 parent 11c68e4 commit e98d390
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 95 deletions.
27 changes: 8 additions & 19 deletions generators/common/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
const Generator = require('yeoman-generator')

const { copy, copyTpl } = require('../utils')

module.exports = class Common extends Generator {
writing () {
return new Promise(resolve => {
const { staticFiles, tplFiles } = getFiles()
const data = getData(this)

staticFiles.map(file => copy(this, file))
tplFiles.map(file => copyTpl(this, data, file))
copy(this, '_gitignore', '.gitignore')
const { staticFiles, tplFiles } = getFiles()
const data = getData(this)

resolve()
})
staticFiles.map(file => copy(this, file))
tplFiles.map(file => copyTpl(this, data, file))
copy(this, '_gitignore', '.gitignore')
}

install () {
Expand All @@ -25,17 +22,9 @@ module.exports = class Common extends Generator {
'nyc@latest',
'standard@latest',
'prettier@latest'
]
].concat(gitHooks ? ['husky@next', 'lint-staged@latest'] : [])

if (gitHooks) {
this.npmInstall([...packages, 'husky@next', 'lint-staged@latest'], {
saveDev: true
})
} else {
this.npmInstall(packages, {
saveDev: true
})
}
this.npmInstall(packages, { saveDev: true })
}
}

Expand Down
19 changes: 5 additions & 14 deletions generators/generic/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Generator = require('yeoman-generator')

const { copy } = require('../utils')

module.exports = class Generic extends Generator {
Expand All @@ -19,12 +20,8 @@ module.exports = class Generic extends Generator {
}

writing () {
return new Promise(resolve => {
const staticFiles = getStaticFiles(this.props)
staticFiles.map(file => copy(this, file))

resolve()
})
const staticFiles = getStaticFiles(this.props)
staticFiles.map(file => copy(this, file))
}

install () {
Expand All @@ -36,12 +33,6 @@ module.exports = class Generic extends Generator {
}
}

function getStaticFiles (props) {
let files = ['src/index.js']

if (props.cli) {
return [...files, 'src/cli.js']
} else {
return files
}
function getStaticFiles ({ cli }) {
return ['src/index.js'].concat(cli ? ['src/cli.js'] : [])
}
30 changes: 15 additions & 15 deletions generators/package/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Generator = require('yeoman-generator')

const { sortObj } = require('../utils')

module.exports = class Package extends Generator {
Expand All @@ -20,22 +21,19 @@ module.exports = class Package extends Generator {
}

writing () {
return new Promise(resolve => {
/* Set basic info */
const tpl = this.fs.readJSON(this.templatePath('package.json'))
let info = this.getBaseInfo()
/* Set basic info */
const tpl = this.fs.readJSON(this.templatePath('package.json'))
let info = this.getBaseInfo()

info = Object.assign({}, info, tpl)
info = projectAdjustments(
info,
this.props,
this.config.get('projectType'),
this.config.get('gitHooks')
)
info = Object.assign({}, info, tpl)
info = projectAdjustments(
info,
this.props,
this.config.get('projectType'),
this.config.get('gitHooks')
)

this.fs.writeJSON(this.destinationPath('package.json'), info)
resolve()
})
this.fs.writeJSON(this.destinationPath('package.json'), info)
}

getBaseInfo () {
Expand Down Expand Up @@ -75,7 +73,9 @@ function projectAdjustments (info, props, type, gitHooks) {
case 'static-site':
scripts = Object.assign({}, scripts, {
dev: 'webpack-dev-server --config webpack.dev.js --open',
build: 'webpack --config webpack.prod.js'
build: 'webpack --config webpack.prod.js',
test:
'standard src && cross-env NODE_ENV=test nyc mocha --require babel-register'
})
standard = Object.assign({}, standard, {
parser: 'babel-eslint'
Expand Down
8 changes: 3 additions & 5 deletions generators/server/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const Generator = require('yeoman-generator')

const { copy } = require('../utils')

module.exports = class Server extends Generator {
writing () {
return new Promise(resolve => {
const staticFiles = ['src/routes/index.js', 'src/app.js', 'src/index.js']
const staticFiles = ['src/routes/index.js', 'src/app.js', 'src/index.js']

staticFiles.map(file => copy(this, file))
resolve()
})
staticFiles.map(file => copy(this, file))
}

install () {
Expand Down
102 changes: 60 additions & 42 deletions generators/static-site/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Generator = require('yeoman-generator')

const { copy } = require('../utils')

module.exports = class StaticSite extends Generator {
Expand All @@ -12,43 +13,24 @@ module.exports = class StaticSite extends Generator {
}
]

return this.prompt(prompts).then(props => (this.props = props))
return this.prompt(prompts).then(props => {
this.props = props
})
}

writing () {
return new Promise(resolve => {
const babelPresets = [
[
'env',
{
modules: false,
loose: true,
useBuiltIns: true,
targets: {
browsers: ['defaults']
}
}
]
]

const staticFiles = [
'pages/index.html',
'styles/main.less',
'src/index.js',
'webpack.dev.js',
'webpack.prod.js',
'webpack.common.js'
]

staticFiles.map(file => copy(this, file))

this.fs.writeJSON(this.destinationPath('.babelrc'), {
presets: this.props.react ? [...babelPresets, 'react'] : babelPresets,
plugins: ['syntax-dynamic-import']
})

resolve()
})
const staticFiles = [
'pages/index.html',
'styles/main.less',
'src/index.js',
'webpack.dev.js',
'webpack.prod.js',
'webpack.common.js'
]

staticFiles.map(file => copy(this, file))

this.fs.writeJSON(this.destinationPath('.babelrc'), getBabelrc(this.props))
}

install () {
Expand All @@ -59,6 +41,47 @@ module.exports = class StaticSite extends Generator {
}
}

function getBabelrc ({ react }) {
const basePresets = [
[
'env',
{
modules: false,
loose: true,
useBuiltIns: false,
targets: { browsers: ['defaults'] }
}
]
].concat(react ? ['react'] : [])

const basePlugins = ['syntax-dynamic-import']

const testPresets = [
[
'env',
{
modules: 'commonjs',
loose: true,
useBuiltIns: false,
targets: { node: 'current' }
}
]
].concat(react ? ['react'] : [])

const testPlugins = ['dynamic-import-node']

return {
presets: basePresets,
plugins: basePlugins,
env: {
test: {
presets: testPresets,
plugins: testPlugins
}
}
}
}

function getAllDependencies (props) {
const devDependencies = getDevDeps(props)
const dependencies = getDependencies(props)
Expand All @@ -67,7 +90,7 @@ function getAllDependencies (props) {
}

function getDevDeps ({ react }) {
const list = [
return [
'babel-core@latest',
'babel-eslint@latest',
'babel-loader@latest',
Expand All @@ -76,6 +99,7 @@ function getDevDeps ({ react }) {
'babel-polyfill@latest',
'babel-preset-env@latest',
'babel-register@latest',
'cross-env@latest',
'postcss-loader@latest',
'autoprefixer@latest',
'css-loader@latest',
Expand All @@ -90,13 +114,7 @@ function getDevDeps ({ react }) {
'webpack@latest',
'webpack-cli@latest',
'webpack-merge@latest'
]

if (react) {
return [...list, 'babel-preset-react@latest']
} else {
return list
}
].concat(react ? ['babel-preset-react@latest'] : [])
}

function getDependencies ({ react }) {
Expand Down

0 comments on commit e98d390

Please sign in to comment.