Skip to content

Instantly share code, notes, and snippets.

@samrocksc
Last active April 11, 2020 05:00
Show Gist options
  • Save samrocksc/7d324d12c324f4db0ad518acff00237d to your computer and use it in GitHub Desktop.
Save samrocksc/7d324d12c324f4db0ad518acff00237d to your computer and use it in GitHub Desktop.
const fsx = require("fs-extra");
const glob = require("glob");
const fs = require("fs");
const path = require("path");
const mm = require("minimatch");
const getIgnoredFiles = (path) => {
const str = fsx.readFileSync(path).toString();
const lines = str
.split(/\r?\n/)
.filter((line) => line.trim() !== "" && !line.startsWith("#"));
return Array.from(new Set(lines));
};
const generateFileFilter = () => false;
const generateFolderFilter = () => false;
const generateGlobFilter = (globPattern) => return (fileName) => mm(globPattern, fileName);
const generateFilters = (ignoredFiles) =>
ignoredFiles.map((ignore) => {
if (glob.hasMagic(ignore)) {
console.log("found a glob path", ignore);
return generateGlobFilter(ignore);
}
if (ignore.endsWith("/")) {
console.log("found a folder filter", ignore);
return generateFolderFilter(ignore);
}
if (!fs.existsSync(ignore) && !ignore.includes("*")) {
console.log("found a file", ignore);
return generateFileFilter(ignore);
}
console.log("not sure what to do with", ignore);
});
const main = async (copy, to, ignoreFile) => {
console.log(`${path.resolve(copy)}/${ignoreFile}`);
const ignoredFiles = getIgnoredFiles(`${path.resolve(copy)}/${ignoreFile}`);
console.log("ignoreFile", ignoredFiles);
const filters = generateFilters(ignoredFiles);
await fsx.copy(
copy,
to,
{
filter: (copy, to) => {
if (copy.endsWith(ignoreFile)) {
return true;
}
if (copy.indexOf(copy) > 1) {
return false;
}
return true;
},
},
(err) => {
if (err) {
console.log("err", err);
}
}
); // copies code into node_modules
};
main("initial", "output", ".npmignore");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment