Last active
June 15, 2017 05:08
-
-
Save shidhincr/3816f8ed5f03c10405892641e60f94d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const prettierOptions = '--single-quote --jsx-bracket-same-line --trailing-comma es5'; | |
const generateBlocks = (pkg, keys) => { | |
keys.forEach(key => { | |
if (!pkg[key]) pkg[key] = {}; | |
}); | |
}; | |
const _message = message => { | |
console.log('-> ', message); | |
}; | |
try { | |
let pkg = require('./package.json'); | |
let fs = require('fs'); | |
let exec = require('child_process').exec; | |
let isYarn = fs.existsSync('./yarn.lock'); | |
let args = process.argv.slice(2); | |
if (!isYarn) { | |
isYarn = args.indexOf('--npm') === -1; | |
} | |
generateBlocks(pkg, ['devDependencies', 'scripts', 'lint-staged']); | |
_message('Configuring pre-commit ...'); | |
pkg.devDependencies['lint-staged'] = '*'; | |
pkg.devDependencies['husky'] = '*'; | |
pkg.devDependencies['prettier'] = '*'; | |
pkg.scripts.jsformat = `prettier --write ${prettierOptions} `; | |
pkg.scripts.precommit = 'lint-staged'; | |
pkg['lint-staged']['*.js'] = [`prettier --write ${prettierOptions}`, 'git add']; | |
_message('Writing package.json ...'); | |
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2)); | |
if (isYarn) { | |
_message('Installing dependencies using YARN...'); | |
exec('yarn', { shell: '/bin/sh' }, (err, stdout, stderr) => { | |
console.log('Installed the dependencies', stdout, stderr); | |
}); | |
} else { | |
_message('Installing dependencies using NPM...'); | |
exec( | |
'npm install --only=dev', | |
{ shell: '/bin/sh' }, | |
(err, stdout, stderr) => { | |
console.log('Installed the dependencies', stdout, stderr); | |
} | |
); | |
} | |
} catch (err) { | |
_message('Error occured :: ', err.toString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment