Skip to content

Instantly share code, notes, and snippets.

@balupton balupton/sprout.js
Last active Feb 15, 2017

Embed
What would you like to do?
automate bevry/base
'use strict'
const exec = require('exec-then')
// prefills values for questions to ask, such that if there is a non-null value, ask the question
// if the value has a value, skip the question
// alterantive would be a `value` property on questions, which if not null, then ask the question
exports.load = function (utils) {
return utils.target.read('package.json').then(function (source) {
const data = JSON.parse(source)
return {
name: data.name,
description: data.description,
nodeVersion: data.engines && data.engines.node && data.engines.node.replace('>=', '').trim(),
docs: data.devDepencencies && data.devDepencencies.documentation,
flowType: data.devDepencencies && data.devDepencencies['flow-bin'],
modules: data.editions && data.editions[0].syntaxes.indexOf('import') !== -1,
author: data.author,
maintainer: data.maintainers && data.maintainers[0]
}
}).catch(function (err) {
console.warn('an error occured reading the package.json file, assuming it is a new project', err)
})
}
// get the questions to ask, with some defaults prefilled
exports.ask = function () {
const defaults = {
userName: 'Benjamin Lupton',
userEmail: 'b@lupton.cc'
}
exec('git config --global user.name').then(({stdout}) => {
if ( stdout ) defaults.userName = stdout
}).then(function () {
return exec('git config --global user.email').then(({stdout}) => {
if ( stdout ) defaults.userEmail = stdout
})
}).then(function () {
return [
{
name: 'name',
message: 'Project name?'
},
{
name: 'description',
message: 'Project description?'
},
{
name: 'nodeVersion',
default: '0.8',
message: 'Minimum node version?',
validate (input) {
return (/^[0-9.]+$/).test(input)
}
},
{
type: 'confirm',
name: 'docs',
default: false,
message: 'Inline documentation?'
},
{
type: 'confirm',
name: 'flowType',
default: false,
message: 'Flow Type?'
},
{
type: 'confirm',
name: 'modules',
default: false,
message: 'ES6 Modules?'
},
{
type: 'confirm',
name: 'react',
default: false,
message: 'React?'
},
{
name: 'githubUser',
default: 'bevry',
message: 'GitHub organisation?'
},
{
name: 'author',
default: (new Date()).getFullYear() + 'Bevry Pty Ltd <us@bevry.me> (http://bevry.me)',
message: 'Author?'
},
{
name: 'maintainer',
default: `${defaults.userName} <${defaults.userEmail}>`,
message: 'Maintainer?'
}
]
})
}
exports.beforeRender = function (utils, config) {
// read target custom entries inside .gitignore and .npmignore in this step
return sprout.target.read('.gitignore', (data) => {
const lines = data.split('\n')
custom = lines.indexOf('# CUSTOM')
if ( custom !== -1 ) {
config.customGitIgnoreLines = custom.slice(custom + 1).join('\n')
}
})
.catch(function () {
// ignore
})
.then(function () {
return sprout.target.read('.npmignore', (data) => {
const lines = data.split('\n')
custom = lines.indexOf('# CUSTOM')
if ( custom !== -1 ) {
config.customNPMIgnoreLines = custom.slice(custom + 1).join('\n')
}
})
.catch(function () {
// ignore
})
})
// read the target package.json file in this step, and merge any non-automated properties back in the after step
.then(function () {
return sprout.target.read('package.json', (targetString) => {
config.theirPackageData = JSON.parse(targetString)
})
.catch(function () {
// ignore
})
})
// run the travis encrypt commands for the .travis yml parts, add the results to config
// overwrite .flowconfig, LICENSE.md, package.json, .editorconfig, .eslintrc.js, .gitignore, .npmignore, .travis.yml, CONTRIBUTING.md, index.js, text.js
// do not overwrite HISTORY.md, README.md, source/*
// for each overwrite, check the headers to see if they are auto-generated, otherwise emit a warning, and over-write them anyway
}
exports.after = function (utils, config) {
if ( !config.flowType ) {
utils.target.remove('.flowconfig')
}
// run the npm install command for the detected needed dependencies
const deps = ['editions']
const devDeps = ['eslint', 'joe', 'joe-reporter-console', 'assert-helpers', 'babel-cli', 'babel-preset-es2015', 'projectz']
if ( config.flowType ) devDeps.push('flow-bin')
if ( config.docs ) devDeps.push('documentation')
if ( config.react ) devDeps.push('eslint-plugin-react')
return new Promise(function (resolve, reject) {
if ( !deps.length ) return resolve()
return exec(`yarn add ${deps.join(' ')}`).then(resolve).catch(reject)
}).then(function () {
if ( !devDeps.length ) return
return exec(`yarn add --dev ${devDeps.join(' ')}`)
})
// run the `npm owner add $BEVRY_NPM_USERNAME` command
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.