Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Last active December 20, 2018 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badsyntax/42ec3d1fe6df7ff6fa0a9d773c0107a6 to your computer and use it in GitHub Desktop.
Save badsyntax/42ec3d1fe6df7ff6fa0a9d773c0107a6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const walk = require('walk');
const yargs = require('yargs');
const getOwnersForFile = require('./get-owners-for-file');
const argv = yargs.command('$0 [options] <path>', false, _yargs => {
_yargs
.option('owner', {
alias: 'o',
describe: 'Find by owner',
type: 'string',
demand: true
})
.positional('path', {
describe: 'The path to the source repo',
type: 'string'
})
.example('$0 -o @org/team /path/to/repo');
}).argv;
const { path: sourcePath, owner } = argv;
if (!fs.existsSync(sourcePath)) {
console.error(`Path does not exist: ${sourcePath}`);
process.exit(1);
}
const codeowners = fs.readFileSync(path.join(sourcePath, 'CODEOWNERS'), 'utf8');
const walker = walk.walk(sourcePath, {
followLinks: false,
filters: ['.git']
});
walker.on('file', (root, fileStats, next) => {
const relativeRoot = path.relative(sourcePath, root);
const filePath = path.join(relativeRoot, fileStats.name);
const owners = getOwnersForFile(codeowners, filePath);
if (owners.indexOf(owner) !== -1) {
console.log(filePath);
}
next();
});
const ignore = require('ignore');
const isLineComment = /^\s*#/;
const getOwnersForFile = (codeownersText, filePath) => {
const reversedCodeowners = (codeownersText || '')
.split('\n')
.filter(line => line.trim() && !isLineComment.test(line))
.reverse();
const ownersLine = reversedCodeowners.find(line => {
const ignorePattern = line.split(' ')[0];
return ignore()
.add(ignorePattern)
.ignores(filePath);
});
const owners = (ownersLine || '')
.split(' ')
.slice(1) // remove ignore pattern
.map(owner => owner.trim())
.filter(owner => owner);
return owners;
};
module.exports = getOwnersForFile;
{
"name": "find-configs",
"version": "1.0.0",
"description": "Find files by owner in a source repo",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Quantum Squad <quantumsquad@skyscanner.net>",
"license": "ISC",
"bin": {
"find-configs": "./cli.js"
},
"dependencies": {
"ignore": "^5.0.4",
"walk": "^2.3.14",
"yargs": "^12.0.5"
},
"devDependencies": {
"@zeit/eslint-config-node": "^0.3.0",
"eslint": "^5.10.0",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-prettier": "^3.0.0",
"prettier": "^1.15.3"
},
"eslintConfig": {
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": [
"error",
{
"singleQuote": true
}
]
},
"extends": [
"@zeit/eslint-config-node",
"prettier"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment