Skip to content

Instantly share code, notes, and snippets.

@shidhincr
Last active June 15, 2017 05:08
Show Gist options
  • Save shidhincr/3816f8ed5f03c10405892641e60f94d6 to your computer and use it in GitHub Desktop.
Save shidhincr/3816f8ed5f03c10405892641e60f94d6 to your computer and use it in GitHub Desktop.
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