Reputation:
Say I have a .gitignore or .npmignore file with the following contents:
logs
*.sh
*.log
not that .npmignore files are designed to be interpreted just like .gitignore files.
My question is - how can I use the contents of a standard .npmignore / .gitignore file and map them to --exclude
options for a tar command?
basically I need to do something like this:
find -not -path (whatever is in gitignore) | tar
or just this:
tar --exclude="whatever matches gitignore/npmignore"
the latter just seems fine, but how can I map what's in .gitignore / .npmignore to the command line? Is there some package that can do that?
Upvotes: 0
Views: 477
Reputation:
As I suspected, it looks .gitignore / .npmignore are designed to map directly to command line tools, like find
, tar
, etc.
So in Node.js/TypeScript code, I just have this, and it seems to work:
const keep: Array<RegExp> = [
/^README/i,
/^package\.json$/,
/^LICENSE/i,
/^LICENCE/i,
/^CHANGELOG/i
];
export const npmIgnoreToArray = function (npmignore: string): Array<string> {
return String(npmignore).trim().split('\n').map(v => String(v).trim())
.filter(Boolean)
.filter(v => !v.startsWith('#'))
.filter(function (v) {
return !keep.some(function (rgx) {
return rgx.test(v);
})
});
};
export const npmIgnoreToTARExclude = function (npmignore: Array<string>): string {
return npmignore.map(function (v) {
return `--exclude='${v}'`;
})
.join(' ')
};
Upvotes: 0