-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgitignore.js
More file actions
167 lines (165 loc) · 6.06 KB
/
gitignore.js
File metadata and controls
167 lines (165 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Downloaded from wget https://stopsopa.github.io/gitignore_to_find/gitignore.js
* Just exist here to spare us the effort - it's not a source of truth for this module/tool
*/
/*!
* @homepage https://github.com/stopsopa/gitignore_to_find
*/
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// node_modules/gitignore-parser/lib/index.js
var require_lib = __commonJS({
"node_modules/gitignore-parser/lib/index.js"(exports) {
exports.compile = function(content) {
var parsed = exports.parse(content), positives = parsed[0], negatives = parsed[1];
return {
accepts: function(input) {
if (input[0] === "/") input = input.slice(1);
return negatives[0].test(input) || !positives[0].test(input);
},
denies: function(input) {
if (input[0] === "/") input = input.slice(1);
return !(negatives[0].test(input) || !positives[0].test(input));
},
maybe: function(input) {
if (input[0] === "/") input = input.slice(1);
return negatives[1].test(input) || !positives[1].test(input);
}
};
};
exports.parse = function(content) {
return content.split("\n").map(function(line) {
line = line.trim();
return line;
}).filter(function(line) {
return line && line[0] !== "#";
}).reduce(function(lists, line) {
var isNegative = line[0] === "!";
if (isNegative) {
line = line.slice(1);
}
if (line[0] === "/")
line = line.slice(1);
if (isNegative) {
lists[1].push(line);
} else {
lists[0].push(line);
}
return lists;
}, [[], []]).map(function(list) {
return list.sort().map(prepareRegexes).reduce(function(list2, prepared) {
list2[0].push(prepared[0]);
list2[1].push(prepared[1]);
return list2;
}, [[], [], []]);
}).map(function(item) {
return [
item[0].length > 0 ? new RegExp("^((" + item[0].join(")|(") + "))") : new RegExp("$^"),
item[1].length > 0 ? new RegExp("^((" + item[1].join(")|(") + "))") : new RegExp("$^")
];
});
};
function prepareRegexes(pattern) {
return [
// exact regex
prepareRegexPattern(pattern),
// partial regex
preparePartialRegex(pattern)
];
}
function prepareRegexPattern(pattern) {
return escapeRegex(pattern).replace("**", "(.+)").replace("*", "([^\\/]+)");
}
function preparePartialRegex(pattern) {
return pattern.split("/").map(function(item, index) {
if (index)
return "([\\/]?(" + prepareRegexPattern(item) + "\\b|$))";
else
return "(" + prepareRegexPattern(item) + "\\b)";
}).join("");
}
function escapeRegex(pattern) {
return pattern.replace(/[\-\[\]\/\{\}\(\)\+\?\.\\\^\$\|]/g, "\\$&");
}
}
});
// gitignore.ts
var import_gitignore_parser = __toESM(require_lib(), 1);
import fs from "fs";
import readline from "readline";
var gitignorePath = process.argv[2];
process.stdout.on("error", (err) => {
if (err.code === "EPIPE") process.exit(0);
});
if (!gitignorePath || process.stdin.isTTY) {
console.error(
`Usage: find . -type f | /bin/bash ts.sh gitignore.ts <path_to_gitignore>`
);
process.exit(1);
}
if (!fs.existsSync(gitignorePath)) {
console.error(
`gitignore.ts error: .gitignore file not found: ${gitignorePath}`
);
process.exit(1);
}
var ig;
try {
const gitignoreContent = fs.readFileSync(gitignorePath, "utf8");
ig = import_gitignore_parser.default.compile(gitignoreContent);
} catch (e) {
console.error(
`gitignore.ts error: failed to read or parse gitignore file: ${e.message}`
);
process.exit(1);
}
var rl = readline.createInterface({
input: process.stdin,
terminal: false
});
rl.on("line", (line) => {
const trimmed = line.trim();
if (!trimmed) return;
const cleanPath = trimmed.replace(/^\.\//, "");
if (ig.accepts(cleanPath)) {
process.stdout.write(cleanPath + "\n");
}
});
rl.on("close", () => {
process.exit(0);
});
/*!
* @homepage https://github.com/stopsopa/gitignore_to_find
*/