Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve isGitIgnored #224

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 20 additions & 25 deletions gitignore.js
Expand Up @@ -7,9 +7,9 @@ import slash from 'slash';
import {toPath} from './utilities.js';

const DEFAULT_IGNORE = [
'**/node_modules/**',
'**/flow-typed/**',
'**/coverage/**',
'**/node_modules',
'**/flow-typed',
'**/coverage',
'**/.git',
Copy link
Collaborator Author

@fisker fisker Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrmlnc

Is dir and dir/** in ignore option behave different?

This explanation says they are different when writing negative pattern, but not clear to me if it makes difference when the patterns used in ignore option, I assume we should prefer dir over dir/** in ignore option too?

];

Expand Down Expand Up @@ -58,37 +58,32 @@ const ensureAbsolutePathForCwd = (cwd, p) => {

const getIsIgnoredPredicate = (ignores, cwd) => p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, toPath(p)))));

const getFile = async (file, cwd) => {
const filePath = path.join(cwd, file);
const content = await fs.promises.readFile(filePath, 'utf8');
const getFile = async (filePath, cwd) => ({
cwd,
filePath,
content: await fs.promises.readFile(filePath, 'utf8'),
});

return {
cwd,
filePath,
content,
};
};

const getFileSync = (file, cwd) => {
const filePath = path.join(cwd, file);
const content = fs.readFileSync(filePath, 'utf8');

return {
cwd,
filePath,
content,
};
};
const getFileSync = (filePath, cwd) => ({
cwd,
filePath,
content: fs.readFileSync(filePath, 'utf8'),
});

const normalizeOptions = ({
ignore = [],
cwd = slash(process.cwd()),
} = {}) => ({ignore: [...DEFAULT_IGNORE, ...ignore], cwd: toPath(cwd)});

const getGitignoreGlobOptions = options => ({
...options,
absolute: true,
});

export const isGitIgnored = async options => {
options = normalizeOptions(options);

const paths = await fastGlob('**/.gitignore', options);
const paths = await fastGlob('**/.gitignore', getGitignoreGlobOptions(options));

const files = await Promise.all(paths.map(file => getFile(file, options.cwd)));
const ignores = reduceIgnore(files);
Expand All @@ -99,7 +94,7 @@ export const isGitIgnored = async options => {
export const isGitIgnoredSync = options => {
options = normalizeOptions(options);

const paths = fastGlob.sync('**/.gitignore', options);
const paths = fastGlob.sync('**/.gitignore', getGitignoreGlobOptions(options));

const files = paths.map(file => getFileSync(file, options.cwd));
const ignores = reduceIgnore(files);
Expand Down