There are many use cases and projects where we want to place license in all the file as comment at the top of the file or bottom file. If one of the file missed or wrong license text is placed, it might be a problem for legal team or any legal action will dependent on this.
In this situation is really good idea to to check before you commit your code to repository that license text is there in all files. Yap! you got it, we need to place a pre-commit hook in our repository so that any file does not have that, we will not allow them to push code to repository.
There is a great module available in npm which does all in perfect way we want. Only thing is we have to tell him what we need to do.
So the idea is to place a pre-commit hook and assign some node script before code can be commited to the repository. so install pre-commit in your project as dependencies
npm install --save-dev pre-commit
https://www.npmjs.com/package/pre-commit
Now it time to fine license test in all the file. The below node script will happly do that for us and will let us know which file does not have the license text
if this did not solve your problem, put comment your case and I will try to resolve that.
In this situation is really good idea to to check before you commit your code to repository that license text is there in all files. Yap! you got it, we need to place a pre-commit hook in our repository so that any file does not have that, we will not allow them to push code to repository.
pre-commit hook
There is a great module available in npm which does all in perfect way we want. Only thing is we have to tell him what we need to do.So the idea is to place a pre-commit hook and assign some node script before code can be commited to the repository. so install pre-commit in your project as dependencies
npm install --save-dev pre-commit
https://www.npmjs.com/package/pre-commit
Hooking the script in pre-commit
Finding license test in all files
Now it time to fine license test in all the file. The below node script will happly do that for us and will let us know which file does not have the license text
(function () { var fs = require('fs'); var glob = require('glob-fs')(); var path = require('path'); var result = 0; var exclude = ['LICENSE', path.join('e2e', 'util', 'db-ca', 'rds-combined-ca-bundle.pem'), path.join('src', 'favicon.ico')]; var files = []; files = glob.readdirSync('**'); files.map((file) => { try { if (!fs.lstatSync(file).isDirectory() && file.indexOf('.json') === -1
&& exclude.indexOf(file) === -1) { var data = fs.readFileSync(file, 'utf8'); if (data.indexOf('Copyright 2017 candifood contributors') === -1) { console.log('Please add License text in coment in the file ' + file); result = 1; } } } catch (e) { console.log('Error:', e.stack); } }); process.exit(result); })();
So we all set! And its really batter then put a search query manually and find all these words. Here we will do all them even before our code to go to repository.
Now this script can be executed by node to find the license text in all file.
Now its time to hook it in the pre-commit.
Hooking the script in pre-commit
To hook the script in pre commit we need to create a script in package.json and tell
the pre-commit to use that before pre-commit.
{ "name": "project-name", "version": 1.0.0", "license": "Apache 2.0", "scripts": { "license-check": "node license-check", }, "private": true, "dependencies": { }, "devDependencies": { "pre-commit": "1.2.2", }, "pre-commit": [ "license-check" ] }Thats all! When we commit the code the pre-commit will call the node scriptand it will stop if any file there is no license test.Check if company confidential information is placed in code
Now its time to check if any specific company information is there in the code in same pre hook.So we will add another hook in the pre-commit and to check that.So the new package.json will be{ "name": "project-name", "version": 1.0.0", "license": "Apache 2.0", "scripts": { "license-check": "node license-check","license-check": "node prevent-secret",}, "private": true, "dependencies": { }, "devDependencies": { "pre-commit": "1.2.2", }, "pre-commit": [ "license-check","prevent-secret"
] }Prevent secret code
Now the below wcript will be executed after license check in pre-commit and will tell usif any keyword is confidential word is used in our code which may cause problem when the code will go to outside.(function () { var fs = require('fs'); var glob = require('glob-fs')(); var path = require('path'); var result = 0; var exclude = [ 'LICENSE', path.join('e2e', 'config', 'conf.e2e.json'), path.join('src', 'favicon.ico') ]; var files = []; files = glob.readdirSync('**'); var patternString = [ // 'i am oeky', // 'famous', 'secret', 'moana' ]; files.map((file) => { try { if (!fs.lstatSync(file).isDirectory() && exclude.indexOf(file) === -1) { fs.readFileSync(file).toString().split(/\r?\n/).forEach(function(line){ patternString.map((pattern) => { if (line.indexOf(pattern) !== -1) { console.log('' + file + ': `' + pattern + '` in line [' + line +']'); // result = 1; } }); }); } } catch (e) { console.log('Error:', e.stack); } }); process.exit(result); })();
if this did not solve your problem, put comment your case and I will try to resolve that.
No comments:
Post a Comment