Skip to content

Commit 02b878f

Browse files
committed
FileGlobs.py: Recursively extract .npmignore globs
Looks for .npmignore in dirs and subdirs recursively and extract file globs Closes #109
1 parent 4520477 commit 02b878f

File tree

16 files changed

+156
-6
lines changed

16 files changed

+156
-6
lines changed

coala_quickstart/generation/FileGlobs.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22

33
from coalib.parsing.Globbing import glob_escape
4-
from coala_quickstart.generation.Utilities import get_gitignore_glob
4+
from coala_quickstart.generation.Utilities import (
5+
get_gitignore_glob, get_npmignore_glob)
56
from coala_utils.Question import ask_question
67
from coala_quickstart.Strings import GLOB_HELP
78
from coalib.collecting.Collectors import collect_files
@@ -30,6 +31,21 @@ def get_project_files(log_printer, printer, project_dir, non_interactive=False):
3031
color="green")
3132
ignore_globs = get_gitignore_glob(project_dir)
3233

34+
npmignore_dir_list = []
35+
36+
for dir_name, subdir_name, files in os.walk(project_dir):
37+
if(os.path.isfile(os.path.join(dir_name, ".npmignore"))):
38+
npmignore_dir_list += [dir_name]
39+
40+
if(npmignore_dir_list):
41+
printer.print("The contents of your .npmignore file for the project "
42+
"will be automatically loaded as files to ignore.",
43+
color="green")
44+
if ignore_globs is None:
45+
ignore_globs = get_npmignore_glob(project_dir, npmignore_dir_list)
46+
else:
47+
ignore_globs += get_npmignore_glob(project_dir, npmignore_dir_list)
48+
3349
if non_interactive and not ignore_globs:
3450
ignore_globs = []
3551

coala_quickstart/generation/Utilities.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ def is_glob_exp(line):
1818
return sum(1 for x in results) != 0
1919

2020

21-
def parse_gitignore_line(line):
21+
def parse_ignore_line(line):
2222
"""
23-
Parses the line from ``.gitignore`` and returns a list of globs.
23+
Parses the line from ``.gitignore`` and ``.npmignore``
24+
and returns a list of globs.
2425
25-
:param line: A line from the project's ``.gitignore`` file.
26+
:param line: A line from the project's ``.gitignore`` or ``.npmignore`` file
2627
:return: A list of glob expressions translated to the
2728
syntax used in coala globbing.
2829
"""
@@ -72,10 +73,31 @@ def get_gitignore_glob(project_dir, filename=".gitignore"):
7273

7374
with open(gitignore, "r") as file:
7475
for line in file:
75-
for glob in parse_gitignore_line(line):
76+
for glob in parse_ignore_line(line):
7677
yield os.path.join(project_dir, glob)
7778

7879

80+
def get_npmignore_glob(project_dir, npmignore_dir_list, filename=".npmignore"):
81+
"""
82+
Generates a list of glob expressions equivalent to the
83+
contents of the user's project's ``.npmignore`` file.
84+
85+
:param project_dir:
86+
The user's project directory.
87+
:param npmignore_dir_list:
88+
A list of directories in project containing .npmignore
89+
:return:
90+
A list generator of glob expressions generated from the
91+
``.npmignore`` file.
92+
"""
93+
for dir_name in npmignore_dir_list:
94+
npmignore = os.path.join(dir_name, filename)
95+
with open(npmignore, "r") as file:
96+
for line in file:
97+
for glob in parse_ignore_line(line):
98+
yield os.path.join(dir_name, glob)
99+
100+
79101
def split_by_language(project_files):
80102
"""
81103
Splits the given files based on language. This ignores unknown extensions.

tests/file_globs_npmignore_testfiles/.coafile

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
# Start of npmignore
3+
build
4+
ignore.c
5+
/tests
6+
/upload.c
7+
/*.py
8+
*.pyc
9+
__pycache__
10+
# End of npmignore

tests/file_globs_npmignore_testfiles/glob2.py

Whitespace-only changes.

tests/file_globs_npmignore_testfiles/ignore.c

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
#Start of npmignore
3+
*.html
4+
#End of npmignore

tests/file_globs_npmignore_testfiles/other_folder/new_file.c

Whitespace-only changes.

tests/file_globs_npmignore_testfiles/other_folder/test.html

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
#Start of npmignore
3+
*.css
4+
#End of npmignore

0 commit comments

Comments
 (0)