find-cli 0.0.3 → 1.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -5
- package/bin/abbreviations.js +10 -0
- package/bin/action/addRootDirectoryPath.js +31 -0
- package/bin/action/find.js +46 -0
- package/bin/action/help.js +40 -0
- package/bin/action/initialise.js +18 -0
- package/bin/action/listRootDirectoryPaths.js +18 -0
- package/bin/action/removeRootDirectoryPath.js +33 -0
- package/bin/action/version.js +15 -0
- package/bin/commands.js +19 -0
- package/bin/configuration/version_1_1.js +26 -0
- package/bin/configuration/version_1_2.js +48 -0
- package/bin/configuration/version_1_3.js +22 -0
- package/bin/configuration/version_1_4.js +58 -0
- package/bin/configuration.js +230 -0
- package/bin/constants.js +19 -0
- package/bin/converter/alternates.js +29 -0
- package/bin/converter/characterClass.js +45 -0
- package/bin/converter/default.js +19 -0
- package/bin/converter/directory.js +19 -0
- package/bin/converter/escapedCharacter.js +19 -0
- package/bin/converter/period.js +19 -0
- package/bin/converter/questionMark.js +19 -0
- package/bin/converter/repeatedDirectories.js +19 -0
- package/bin/converter/repeatedWildcard.js +19 -0
- package/bin/converter/singleDirectory.js +19 -0
- package/bin/converter/singleWildcard.js +19 -0
- package/bin/converter.js +48 -0
- package/bin/converters.js +29 -0
- package/bin/defaults.js +13 -0
- package/bin/descriptions.js +11 -0
- package/bin/find.js +118 -0
- package/bin/isIgnored/asynchronous.js +59 -0
- package/bin/isIgnored/synchronous.js +104 -0
- package/bin/main.js +85 -0
- package/bin/messages.js +37 -0
- package/bin/occurrence.js +38 -0
- package/bin/operation/addRootDirectoryPath.js +16 -0
- package/bin/operation/find.js +201 -0
- package/bin/operation/listRootDirectoryPaths.js +34 -0
- package/bin/operation/prompt/addRootDirectoryPath.js +48 -0
- package/bin/operation/prompt/ignoreOrPermitPath.js +46 -0
- package/bin/operation/prompt/removeRootDirectoryPath.js +61 -0
- package/bin/operation/prompt/rule.js +50 -0
- package/bin/operation/readConfiguration.js +27 -0
- package/bin/operation/removeRootDirectoryPath.js +29 -0
- package/bin/operation/removeRootDirectoryPathByIndex.js +34 -0
- package/bin/operation/rule.js +24 -0
- package/bin/operation/updatePatRules.js +54 -0
- package/bin/operation/validateRootDirectoryPath.js +30 -0
- package/bin/options.js +13 -0
- package/bin/prepare.js +51 -0
- package/bin/rule/glob.js +165 -0
- package/bin/rule/regex.js +136 -0
- package/bin/rule/string.js +112 -0
- package/bin/types.js +11 -0
- package/bin/utilities/literal.js +19 -0
- package/bin/utilities/log.js +26 -0
- package/bin/utilities/operation.js +38 -0
- package/bin/utilities/path.js +58 -0
- package/bin/utilities/prompt.js +7 -0
- package/bin/utilities/rule.js +18 -0
- package/bin/utilities/string.js +36 -0
- package/bin/utilities/validate.js +56 -0
- package/bin/versions.js +15 -0
- package/find.js +18 -0
- package/package.json +5 -2
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { retrieveRootDirectoryPaths, updateRootDirectoryPaths } = require("../configuration");
|
|
4
|
+
|
|
5
|
+
function removeRootDirectoryPathByIndexOperation(proceed, abort, context) {
|
|
6
|
+
const { index } = context;
|
|
7
|
+
|
|
8
|
+
if (index === null) {
|
|
9
|
+
proceed();
|
|
10
|
+
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
removeRootDirectoryPathByIndex(index);
|
|
15
|
+
|
|
16
|
+
proceed();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function removeRootDirectoryPathByIndex(index) {
|
|
20
|
+
const start = index, ///
|
|
21
|
+
deleteCount = 1,
|
|
22
|
+
rootDirectoryPaths = retrieveRootDirectoryPaths()
|
|
23
|
+
|
|
24
|
+
rootDirectoryPaths.splice(start, deleteCount);
|
|
25
|
+
|
|
26
|
+
updateRootDirectoryPaths(rootDirectoryPaths);
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = removeRootDirectoryPathByIndexOperation;
|
|
31
|
+
|
|
32
|
+
Object.assign(module.exports, {
|
|
33
|
+
removeRootDirectoryPathByIndex
|
|
34
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { ruleFromStringAnchoredAndDirectory } = require("../utilities/rule");
|
|
4
|
+
|
|
5
|
+
function ruleOperation(proceed, abort, context) {
|
|
6
|
+
const { string } = context,
|
|
7
|
+
directory = false,
|
|
8
|
+
anchored = false,
|
|
9
|
+
rule = ruleFromStringAnchoredAndDirectory(string, anchored, directory);
|
|
10
|
+
|
|
11
|
+
if (rule === null) {
|
|
12
|
+
abort();
|
|
13
|
+
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
Object.assign(context, {
|
|
18
|
+
rule
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
proceed();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = ruleOperation;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { updateIgnoredFilePathRules,
|
|
4
|
+
updatePermittedFilePathRules,
|
|
5
|
+
updateIgnoredDirectoryPathRules,
|
|
6
|
+
updatePermittedDirectoryPathRules } = require("../configuration");
|
|
7
|
+
|
|
8
|
+
function updatePathRulesOperation(proceed, abort, context) {
|
|
9
|
+
const { rule, directory, pathIgnored } = context;
|
|
10
|
+
|
|
11
|
+
pathIgnored ?
|
|
12
|
+
addIgnoreRule(rule, directory, context) :
|
|
13
|
+
addPermitRule(rule, directory, context);
|
|
14
|
+
|
|
15
|
+
proceed();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = updatePathRulesOperation;
|
|
19
|
+
|
|
20
|
+
function addIgnoreRule(rule, directory, context) {
|
|
21
|
+
if (directory) {
|
|
22
|
+
const { ignoredDirectoryPathRules } = context,
|
|
23
|
+
ignoredDirectoryPathRule = rule; ///
|
|
24
|
+
|
|
25
|
+
ignoredDirectoryPathRules.push(ignoredDirectoryPathRule);
|
|
26
|
+
|
|
27
|
+
updateIgnoredDirectoryPathRules(ignoredDirectoryPathRules);
|
|
28
|
+
} else {
|
|
29
|
+
const { ignoredFilePathRules } = context,
|
|
30
|
+
ignoredFilePathRule = rule; ///
|
|
31
|
+
|
|
32
|
+
ignoredFilePathRules.push(ignoredFilePathRule);
|
|
33
|
+
|
|
34
|
+
updateIgnoredFilePathRules(ignoredFilePathRules);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function addPermitRule(rule, directory, context) {
|
|
39
|
+
if (directory) {
|
|
40
|
+
const { permittedDirectoryPathRules } = context,
|
|
41
|
+
permittedDirectoryPathRule = rule; ///
|
|
42
|
+
|
|
43
|
+
permittedDirectoryPathRules.push(permittedDirectoryPathRule);
|
|
44
|
+
|
|
45
|
+
updatePermittedDirectoryPathRules(permittedDirectoryPathRules);
|
|
46
|
+
} else {
|
|
47
|
+
const { permittedFilePathRules } = context,
|
|
48
|
+
permittedFilePathRule = rule; ///
|
|
49
|
+
|
|
50
|
+
permittedFilePathRules.push(permittedFilePathRule);
|
|
51
|
+
|
|
52
|
+
updatePermittedFilePathRules(permittedFilePathRules);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { validateRootDirectoryPath } = require("../utilities/validate"),
|
|
4
|
+
{ INVALID_ROOT_DIRECTORY_PATH_MESSAGE } = require("../messages");
|
|
5
|
+
|
|
6
|
+
function validateRootDirectoryPathOperation(proceed, abort, context) {
|
|
7
|
+
const { rootDirectoryPath } = context;
|
|
8
|
+
|
|
9
|
+
if (rootDirectoryPath === null) {
|
|
10
|
+
proceed();
|
|
11
|
+
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const valid = validateRootDirectoryPath(rootDirectoryPath);
|
|
16
|
+
|
|
17
|
+
if (!valid) {
|
|
18
|
+
console.log(INVALID_ROOT_DIRECTORY_PATH_MESSAGE);
|
|
19
|
+
|
|
20
|
+
const rootDirectoryPath = null;
|
|
21
|
+
|
|
22
|
+
Object.assign(context, {
|
|
23
|
+
rootDirectoryPath
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
proceed();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = validateRootDirectoryPathOperation;
|
package/bin/options.js
ADDED
package/bin/prepare.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { migrateConfigurationFile } = require("./configuration"),
|
|
4
|
+
{ DEFAULT_HELP, DEFAULT_VERSION } = require("./defaults"),
|
|
5
|
+
{ FIND_COMMAND,
|
|
6
|
+
HELP_COMMAND,
|
|
7
|
+
VERSION_COMMAND,
|
|
8
|
+
INITIALISE_COMMAND,
|
|
9
|
+
ADD_ROOT_DIRECTORY_PATH_COMMAND,
|
|
10
|
+
LIST_ROOT_DIRECTORY_PATHS_COMMAND,
|
|
11
|
+
REMOVE_ROOT_DIRECTORY_PATH_COMMAND } = require("./commands");
|
|
12
|
+
|
|
13
|
+
function prepare(command, argument, options, main) {
|
|
14
|
+
const { help = DEFAULT_HELP, version = DEFAULT_VERSION } = options;
|
|
15
|
+
|
|
16
|
+
if (false) {
|
|
17
|
+
///
|
|
18
|
+
} else if (help) {
|
|
19
|
+
command = HELP_COMMAND;
|
|
20
|
+
} else if (version) {
|
|
21
|
+
command = VERSION_COMMAND;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if ((command === HELP_COMMAND) ||
|
|
25
|
+
(command === VERSION_COMMAND) ||
|
|
26
|
+
(command === INITIALISE_COMMAND)) {
|
|
27
|
+
|
|
28
|
+
main(command, argument, options);
|
|
29
|
+
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
migrateConfigurationFile();
|
|
34
|
+
|
|
35
|
+
if ((command === ADD_ROOT_DIRECTORY_PATH_COMMAND) ||
|
|
36
|
+
(command === LIST_ROOT_DIRECTORY_PATHS_COMMAND) ||
|
|
37
|
+
(command === REMOVE_ROOT_DIRECTORY_PATH_COMMAND)) {
|
|
38
|
+
|
|
39
|
+
main(command, argument, options);
|
|
40
|
+
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
argument = command; ///
|
|
45
|
+
|
|
46
|
+
command = FIND_COMMAND;
|
|
47
|
+
|
|
48
|
+
main(command, argument, options);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = prepare;
|
package/bin/rule/glob.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const converters = require("../converters"),
|
|
4
|
+
Occurrence = require("../occurrence");
|
|
5
|
+
|
|
6
|
+
const { GLOB_TYPE } = require("../types"),
|
|
7
|
+
{ EMPTY_STRING } = require("../constants"),
|
|
8
|
+
{ isStringGlobLiteral } = require("../utilities/literal"),
|
|
9
|
+
{ addAnchors, addTrailingForwardSlash, removeTrailingForwardSlash } = require("../utilities/string");
|
|
10
|
+
|
|
11
|
+
class GlobRule {
|
|
12
|
+
constructor(glob, pattern, regExp) {
|
|
13
|
+
this.glob = glob;
|
|
14
|
+
this.pattern = pattern;
|
|
15
|
+
this.regExp = regExp;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getGlob() {
|
|
19
|
+
return this.glob;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getPattern() {
|
|
23
|
+
return this.pattern;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getRegExp() {
|
|
27
|
+
return this.regExp;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
toJSON() {
|
|
31
|
+
const type = GLOB_TYPE,
|
|
32
|
+
glob = this.glob,
|
|
33
|
+
pattern = this.pattern,
|
|
34
|
+
json = {
|
|
35
|
+
type,
|
|
36
|
+
glob,
|
|
37
|
+
pattern
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
return json;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
find(string) {
|
|
44
|
+
const occurrences = [];
|
|
45
|
+
|
|
46
|
+
let result = string.match(this.regExp);
|
|
47
|
+
|
|
48
|
+
while (result !== null) {
|
|
49
|
+
const occurrence = Occurrence.fromResult(result),
|
|
50
|
+
end = occurrence.getEnd(),
|
|
51
|
+
start = end; ///
|
|
52
|
+
|
|
53
|
+
string = string.substring(start); ///
|
|
54
|
+
|
|
55
|
+
occurrences.push(occurrence);
|
|
56
|
+
|
|
57
|
+
result = string.match(this.regExp);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return occurrences;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
match(string) {
|
|
64
|
+
const matches = this.regExp.test(string);
|
|
65
|
+
|
|
66
|
+
return matches;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
asString() {
|
|
70
|
+
const string = this.glob; ///
|
|
71
|
+
|
|
72
|
+
return string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static fromJSON(json) {
|
|
76
|
+
let globRule = null;
|
|
77
|
+
|
|
78
|
+
const { type } = json;
|
|
79
|
+
|
|
80
|
+
if (type === GLOB_TYPE) {
|
|
81
|
+
const { glob, pattern } = json,
|
|
82
|
+
regExp = new RegExp(pattern);
|
|
83
|
+
|
|
84
|
+
globRule = new GlobRule(glob, pattern, regExp);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return globRule;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static fromStringAnchoredAndDirectory(string, anchored, directory) {
|
|
91
|
+
let globRule = null;
|
|
92
|
+
|
|
93
|
+
const glob = globFromStringAndDirectory(string, directory);
|
|
94
|
+
|
|
95
|
+
if (glob !== null) {
|
|
96
|
+
const pattern = patternFromGlobAndAnchored(glob, anchored),
|
|
97
|
+
regExp = new RegExp(pattern);
|
|
98
|
+
|
|
99
|
+
globRule = new GlobRule(glob, pattern, regExp);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return globRule;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports = GlobRule;
|
|
107
|
+
|
|
108
|
+
function patternFromGlobAndAnchored(glob, anchored) {
|
|
109
|
+
let pattern = EMPTY_STRING;
|
|
110
|
+
|
|
111
|
+
const characters = [ ...glob ];
|
|
112
|
+
|
|
113
|
+
for (;;) {
|
|
114
|
+
const converted = converters.some((converter) => {
|
|
115
|
+
const result = converter.match(characters);
|
|
116
|
+
|
|
117
|
+
if (result !== null) {
|
|
118
|
+
pattern = `${pattern}${result}`;
|
|
119
|
+
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
if (!converted) {
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (anchored) {
|
|
130
|
+
pattern = addAnchors(pattern);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return pattern;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function globFromStringAndDirectory(string, directory) {
|
|
137
|
+
let glob;
|
|
138
|
+
|
|
139
|
+
const stringGlobLiteral = isStringGlobLiteral(string);
|
|
140
|
+
|
|
141
|
+
if (stringGlobLiteral) {
|
|
142
|
+
const globLiteral = string; ///
|
|
143
|
+
|
|
144
|
+
glob = globLiteral; ///
|
|
145
|
+
|
|
146
|
+
glob = removeTrailingForwardSlash(glob); ///
|
|
147
|
+
|
|
148
|
+
if (directory) {
|
|
149
|
+
glob = addTrailingForwardSlash(glob); ///
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const anchored = false,
|
|
154
|
+
pattern = patternFromGlobAndAnchored(glob, anchored);
|
|
155
|
+
|
|
156
|
+
new RegExp(pattern);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
glob = null;
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
glob = null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return glob;
|
|
165
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const Occurrence = require("../occurrence");
|
|
4
|
+
|
|
5
|
+
const { REGEX_TYPE } = require("../types"),
|
|
6
|
+
{ isStringRegexLiteral } = require("../utilities/literal"),
|
|
7
|
+
{ addAnchors,
|
|
8
|
+
removeAnchors,
|
|
9
|
+
addForwardSlashes,
|
|
10
|
+
removeForwardSlashes,
|
|
11
|
+
addTrailingEscapedForwardSlash,
|
|
12
|
+
removeTrailingEscapedForwardSlash } = require("../utilities/string");
|
|
13
|
+
|
|
14
|
+
class RegexRule {
|
|
15
|
+
constructor(pattern, regExp) {
|
|
16
|
+
this.pattern = pattern;
|
|
17
|
+
this.regExp = regExp;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
getPattern() {
|
|
21
|
+
return this.pattern;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getRegExp() {
|
|
25
|
+
return this.regExp;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
toJSON() {
|
|
29
|
+
const type = REGEX_TYPE,
|
|
30
|
+
pattern = this.pattern,
|
|
31
|
+
json = {
|
|
32
|
+
type,
|
|
33
|
+
pattern
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
return json;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
find(string) {
|
|
40
|
+
const occurrences = [];
|
|
41
|
+
|
|
42
|
+
let result = string.match(this.regExp);
|
|
43
|
+
|
|
44
|
+
while (result !== null) {
|
|
45
|
+
const occurrence = Occurrence.fromResult(result),
|
|
46
|
+
end = occurrence.getEnd(),
|
|
47
|
+
start = end; ///
|
|
48
|
+
|
|
49
|
+
string = string.substring(start); ///
|
|
50
|
+
|
|
51
|
+
occurrences.push(occurrence);
|
|
52
|
+
|
|
53
|
+
result = string.match(this.regExp);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return occurrences;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
match(string) {
|
|
60
|
+
const matches = this.regExp.test(string);
|
|
61
|
+
|
|
62
|
+
return matches;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
asString() {
|
|
66
|
+
const string = addForwardSlashes(this.pattern); ///
|
|
67
|
+
|
|
68
|
+
return string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static fromJSON(json) {
|
|
72
|
+
let regexRule = null;
|
|
73
|
+
|
|
74
|
+
const { type } = json;
|
|
75
|
+
|
|
76
|
+
if (type === REGEX_TYPE) {
|
|
77
|
+
const { pattern } = json,
|
|
78
|
+
regExp = new RegExp(pattern);
|
|
79
|
+
|
|
80
|
+
regexRule = new RegexRule(pattern, regExp);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return regexRule;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static fromStringAnchoredAndDirectory(string, anchored, directory) {
|
|
87
|
+
let regexRule = null;
|
|
88
|
+
|
|
89
|
+
const pattern = patternFromStringAndDirectory(string, anchored, directory);
|
|
90
|
+
|
|
91
|
+
if (pattern !== null) {
|
|
92
|
+
const regExp = new RegExp(pattern);
|
|
93
|
+
|
|
94
|
+
regexRule = new RegexRule(pattern, regExp);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return regexRule;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
module.exports = RegexRule;
|
|
102
|
+
|
|
103
|
+
function patternFromStringAndDirectory(string, anchored, directory) {
|
|
104
|
+
let pattern;
|
|
105
|
+
|
|
106
|
+
const stringRegexLiteral = isStringRegexLiteral(string);
|
|
107
|
+
|
|
108
|
+
if (stringRegexLiteral) {
|
|
109
|
+
const regexLiteral = string; ///
|
|
110
|
+
|
|
111
|
+
pattern = removeForwardSlashes(regexLiteral);
|
|
112
|
+
|
|
113
|
+
pattern = removeAnchors(pattern); ///
|
|
114
|
+
|
|
115
|
+
pattern = removeTrailingEscapedForwardSlash(pattern); ///
|
|
116
|
+
|
|
117
|
+
if (directory) {
|
|
118
|
+
pattern = addTrailingEscapedForwardSlash(pattern); ///
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (anchored) {
|
|
122
|
+
pattern = addAnchors(pattern);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
new RegExp(pattern);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
pattern = null;
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
pattern = null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return pattern;
|
|
135
|
+
}
|
|
136
|
+
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const Occurrence = require("../occurrence");
|
|
4
|
+
|
|
5
|
+
const { STRING_TYPE } = require("../types"),
|
|
6
|
+
{ isStringStringLiteral } = require("../utilities/literal"),
|
|
7
|
+
{ addDoubleQuotes, removeDoubleQuotes, addTrailingForwardSlash, removeTrailingForwardSlash } = require("../utilities/string");
|
|
8
|
+
|
|
9
|
+
class StringRule {
|
|
10
|
+
constructor(string) {
|
|
11
|
+
this.string = string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
getString() {
|
|
15
|
+
return this.string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
toJSON() {
|
|
19
|
+
const type = STRING_TYPE,
|
|
20
|
+
string = this.string,
|
|
21
|
+
json = {
|
|
22
|
+
type,
|
|
23
|
+
string
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
return json;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
find(string) {
|
|
30
|
+
const occurrences = [];
|
|
31
|
+
|
|
32
|
+
let index = string.indexOf(this.string);
|
|
33
|
+
|
|
34
|
+
while (index !== -1) {
|
|
35
|
+
const occurrence = Occurrence.fromIndexAndString(index, string),
|
|
36
|
+
end = occurrence.getEnd(),
|
|
37
|
+
start = end; ///
|
|
38
|
+
|
|
39
|
+
string = string.substring(start); ///
|
|
40
|
+
|
|
41
|
+
occurrences.push(occurrence);
|
|
42
|
+
|
|
43
|
+
index = string.match(this.regExp);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return occurrences;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
match(string) {
|
|
50
|
+
this.string = removeDoubleQuotes(this.string); ///
|
|
51
|
+
|
|
52
|
+
const matches = (this.string === string);
|
|
53
|
+
|
|
54
|
+
this.string = addDoubleQuotes(this.string); ///
|
|
55
|
+
|
|
56
|
+
return matches;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
asString() {
|
|
60
|
+
return this.string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static fromJSON(json) {
|
|
64
|
+
let stringRule = null;
|
|
65
|
+
|
|
66
|
+
const { type } = json;
|
|
67
|
+
|
|
68
|
+
if (type === STRING_TYPE) {
|
|
69
|
+
const { string } = json;
|
|
70
|
+
|
|
71
|
+
stringRule = new StringRule(string);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return stringRule;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static fromStringAnchoredAndDirectory(string, anchored, directory) {
|
|
78
|
+
let stringRule = null;
|
|
79
|
+
|
|
80
|
+
string = stringFromStringAndDirectory(string, directory); ///
|
|
81
|
+
|
|
82
|
+
if (string !== null) {
|
|
83
|
+
stringRule = new StringRule(string);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return stringRule;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
module.exports = StringRule;
|
|
91
|
+
|
|
92
|
+
function stringFromStringAndDirectory(string, directory) {
|
|
93
|
+
const stringStringLiteral = isStringStringLiteral(string);
|
|
94
|
+
|
|
95
|
+
if (stringStringLiteral) {
|
|
96
|
+
const stringLiteral = string; ///
|
|
97
|
+
|
|
98
|
+
string = removeDoubleQuotes(stringLiteral); ///
|
|
99
|
+
|
|
100
|
+
string = removeTrailingForwardSlash(string); ///
|
|
101
|
+
|
|
102
|
+
if (directory) {
|
|
103
|
+
string = addTrailingForwardSlash(string); ///
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
string = addDoubleQuotes(string); ///
|
|
107
|
+
} else {
|
|
108
|
+
string = null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return string;
|
|
112
|
+
}
|
package/bin/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function isStringGlobLiteral(string) {
|
|
4
|
+
const stringStringLiteral = isStringStringLiteral(string),
|
|
5
|
+
stringRegexLiteral = isStringRegexLiteral(string),
|
|
6
|
+
stringGlobLiteral = (!stringStringLiteral && !stringRegexLiteral);
|
|
7
|
+
|
|
8
|
+
return stringGlobLiteral;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function isStringRegexLiteral(string) { return /^\/.*?\/$/.test(string); }
|
|
12
|
+
|
|
13
|
+
function isStringStringLiteral(string) { return /^".*?"$/.test(string); }
|
|
14
|
+
|
|
15
|
+
module.exports = {
|
|
16
|
+
isStringGlobLiteral,
|
|
17
|
+
isStringRegexLiteral,
|
|
18
|
+
isStringStringLiteral
|
|
19
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const GREY_FOREGROUND_COLOUR = "\u001b[90m",
|
|
4
|
+
RED_FOREGROUND_COLOUR = "\u001b[31m",
|
|
5
|
+
BLUE_FOREGROUND_COLOUR = "\u001b[34m",
|
|
6
|
+
GREEN_FOREGROUND_COLOUR = "\u001b[32m",
|
|
7
|
+
YELLOW_FOREGROUND_COLOUR = "\u001b[33m",
|
|
8
|
+
RESET_FOREGROUND_COLOUR = "\u001b[39m";
|
|
9
|
+
|
|
10
|
+
function grey(string) { return `${GREY_FOREGROUND_COLOUR}${string}${RESET_FOREGROUND_COLOUR}`; }
|
|
11
|
+
|
|
12
|
+
function red(string) { return `${RED_FOREGROUND_COLOUR}${string}${RESET_FOREGROUND_COLOUR}`; }
|
|
13
|
+
|
|
14
|
+
function blue(string) { return `${BLUE_FOREGROUND_COLOUR}${string}${RESET_FOREGROUND_COLOUR}`; }
|
|
15
|
+
|
|
16
|
+
function green(string) { return `${GREEN_FOREGROUND_COLOUR}${string}${RESET_FOREGROUND_COLOUR}`; }
|
|
17
|
+
|
|
18
|
+
function yellow(string) { return `${YELLOW_FOREGROUND_COLOUR}${string}${RESET_FOREGROUND_COLOUR}`; }
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
grey,
|
|
22
|
+
red,
|
|
23
|
+
blue,
|
|
24
|
+
green,
|
|
25
|
+
yellow
|
|
26
|
+
};
|