Skip to content

Instantly share code, notes, and snippets.

@Crawleyprint
Last active September 28, 2017 02:37
Show Gist options
  • Save Crawleyprint/f8b8f83bfaed80510256 to your computer and use it in GitHub Desktop.
Save Crawleyprint/f8b8f83bfaed80510256 to your computer and use it in GitHub Desktop.
Frontend automation with git hooks and npm
var path = require('path');
// we need to require fs.extra because node's fs module doesn not have copy
// method.
// We could create our own, but this works as well.
var fs = require('fs.extra');
var appRoot = path.resolve(__dirname);
// 2 arguments are needed, one for determining hook type
// and other one for source file name
if (process.argv.length < 4) {
throw new Error('Please provide hook type and source file name.');
}
var hookType = process.argv[2];
var hookSourceName = process.argv[3];
// desired file in .git/hooks (hook type)
var targetPath = path.resolve(appRoot + '/../.git/hooks/' + hookType);
// file in which actual source is stored and that's being copied from
var sourcePath = path.resolve(appRoot + '/hooks/' + hookSourceName);
if (fs.existsSync(targetPath)) {
// if desired hook type exists, do nothing.
// we don't want to overwrite anything blindly...
console.log('Target file already exists, please complete hooking by hand');
} else {
if (!fs.existsSync(sourcePath)) {
// handle missing filename case in util/hooks
throw new Error('No target hook in utils folder...');
} else {
// copy the file
fs.copy(sourcePath, targetPath, function() {
console.log('Hook copy complete.')
// and set it's permissions to executable.
fs.chmodSync(targetPath, 0777);
});
}
}
// scripts section
"scripts": {
"start": "npm install && bower install && npm run-script copyHooks && gulp",
"copyHooks": "node ./util/copy-hooks.js post-checkout post-checkout && node ./util/copy-hooks.js post-merge post-checkout"
},
#! /bin/sh
npm install && bower install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment