/
textlint-module-resolver.ts
227 lines (218 loc) · 8.74 KB
/
textlint-module-resolver.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// LICENSE : MIT
"use strict";
const assert = require("assert");
const path = require("path");
const tryResolve = require("try-resolve");
const debug = require("debug")("textlint:module-resolver");
const validateConfigConstructor = (ConfigConstructor: any) => {
assert(
ConfigConstructor.CONFIG_PACKAGE_PREFIX &&
ConfigConstructor.FILTER_RULE_NAME_PREFIX &&
ConfigConstructor.RULE_NAME_PREFIX &&
ConfigConstructor.RULE_PRESET_NAME_PREFIX &&
ConfigConstructor.PLUGIN_NAME_PREFIX
);
};
/**
* Create full package name and return
* @param {string} prefix
* @param {string} name
* @returns {string}
*/
export const createFullPackageName = (prefix: string, name: string): string => {
if (name.charAt(0) === "@") {
const scopedPackageNameRegex = new RegExp(`^${prefix}(-|$)`);
// if @scope/<name> -> @scope/<prefix><name>
if (!scopedPackageNameRegex.test(name.split("/")[1])) {
/*
* for scoped packages, insert the textlint-rule after the first / unless
* the path is already @scope/<name> or @scope/textlint-rule-<name>
*/
return name.replace(/^@([^/]+)\/(.*)$/, `@$1/${prefix}$2`);
}
}
return `${prefix}${name}`;
};
export interface ConfigModulePrefix {
CONFIG_PACKAGE_PREFIX: string;
FILTER_RULE_NAME_PREFIX: string;
RULE_NAME_PREFIX: string;
RULE_PRESET_NAME_PREFIX: string;
PLUGIN_NAME_PREFIX: string;
}
/**
* This class aim to resolve textlint's package name and get the module path.
*
* Define
*
* - `package` is npm package
* - `module` is package's main module
*
* ## Support
*
* - textlint-rule-*
* - textlint-preset-*
* - textlint-plugin-*
* - textlint-config-*
*/
export class TextLintModuleResolver {
private baseDirectory: string;
private PLUGIN_NAME_PREFIX: string;
private RULE_PRESET_NAME_PREFIX: string;
private FILTER_RULE_NAME_PREFIX: string;
private RULE_NAME_PREFIX: string;
private CONFIG_PACKAGE_PREFIX: string;
/**
*
* @param {Config|*} ConfigConstructor config constructor like object
* It has static property like CONFIG_PACKAGE_PREFIX etc...
* @param {string} [baseDirectory]
* @constructor
*/
constructor(ConfigConstructor: ConfigModulePrefix, baseDirectory: string = "") {
validateConfigConstructor(ConfigConstructor);
/**
* @type {string} config package prefix
*/
this.CONFIG_PACKAGE_PREFIX = ConfigConstructor.CONFIG_PACKAGE_PREFIX;
/**
* @type {string} rule package's name prefix
*/
this.RULE_NAME_PREFIX = ConfigConstructor.RULE_NAME_PREFIX;
/**
* @type {string} filter rule package's name prefix
*/
this.FILTER_RULE_NAME_PREFIX = ConfigConstructor.FILTER_RULE_NAME_PREFIX;
/**
* @type {string} rule preset package's name prefix
*/
this.RULE_PRESET_NAME_PREFIX = ConfigConstructor.RULE_PRESET_NAME_PREFIX;
/**
* @type {string} plugins package's name prefix
*/
this.PLUGIN_NAME_PREFIX = ConfigConstructor.PLUGIN_NAME_PREFIX;
/**
* @type {string} baseDirectory for resolving
*/
this.baseDirectory = baseDirectory;
}
/**
* Take package name, and return path to module.
* @param {string} packageName
* @returns {string} return path to module
*/
resolveRulePackageName(packageName: string): string {
const baseDir = this.baseDirectory;
const PREFIX = this.RULE_NAME_PREFIX;
const fullPackageName = createFullPackageName(PREFIX, packageName);
// <rule-name> or textlint-rule-<rule-name>
const pkgPath = tryResolve(path.join(baseDir, fullPackageName)) || tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
debug(`rule fullPackageName: ${fullPackageName}`);
throw new ReferenceError(`Failed to load textlint's rule module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
/**
* Take package name, and return path to module.
* @param {string} packageName
* @returns {string} return path to module
*/
resolveFilterRulePackageName(packageName: string): string {
const baseDir = this.baseDirectory;
const PREFIX = this.FILTER_RULE_NAME_PREFIX;
const fullPackageName = createFullPackageName(PREFIX, packageName);
// <rule-name> or textlint-filter-rule-<rule-name> or @scope/<rule-name>
const pkgPath = tryResolve(path.join(baseDir, fullPackageName)) || tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
debug(`filter rule fullPackageName: ${fullPackageName}`);
throw new ReferenceError(`Failed to load textlint's filter rule module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
/**
* Take package name, and return path to module.
* @param {string} packageName
* @returns {string} return path to module
*/
resolvePluginPackageName(packageName: string): string {
const baseDir = this.baseDirectory;
const PREFIX = this.PLUGIN_NAME_PREFIX;
const fullPackageName = createFullPackageName(PREFIX, packageName);
// <plugin-name> or textlint-plugin-<rule-name>
const pkgPath = tryResolve(path.join(baseDir, fullPackageName)) || tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
debug(`plugin fullPackageName: ${fullPackageName}`);
throw new ReferenceError(`Failed to load textlint's plugin module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
/**
* Take package name, and return path to module.
* @param {string} packageName
* The user must specify preset- prefix to these `packageName`.
* @returns {string} return path to module
*/
resolvePresetPackageName(packageName: string): string {
const baseDir = this.baseDirectory;
const PREFIX = this.RULE_PRESET_NAME_PREFIX;
/* Implementation Note
preset name is defined in config file:
In the case, `packageName` is "preset-gizmo"
TextLintModuleResolver resolve "preset-gizmo" to "textlint-rule-preset-gizmo"
{
"rules": {
"preset-gizmo": {
"ruleA": false
}
}
}
*/
// preset-<name> or textlint-rule-preset-<name>
// @scope/preset-<name> or @scope/textlint-rule-preset-<name>
const packageNameWithoutPreset = packageName
.replace(/^preset-/, "")
.replace(/^@([^/]+)\/preset-(.*)$/, `@$1/$2`);
const fullPackageName = createFullPackageName(PREFIX, packageNameWithoutPreset);
const fullFullPackageName = `${PREFIX}${packageNameWithoutPreset}`;
// preset-<preset-name> or textlint-rule-preset-<preset-name>
const pkgPath =
tryResolve(path.join(baseDir, fullFullPackageName)) ||
tryResolve(path.join(baseDir, packageNameWithoutPreset)) ||
// <preset-name> or textlint-rule-preset-<rule-name>
tryResolve(path.join(baseDir, fullPackageName)) ||
tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
debug(`preset fullPackageName: ${fullPackageName}`);
debug(`preset fullFullPackageName: ${fullFullPackageName}`);
throw new ReferenceError(`Failed to load textlint's preset module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
/**
* Take Config package name, and return path to module.
* @param {string} packageName
* @returns {string} return path to module
*/
resolveConfigPackageName(packageName: string): string {
const baseDir = this.baseDirectory;
const PREFIX = this.CONFIG_PACKAGE_PREFIX;
const fullPackageName = createFullPackageName(PREFIX, packageName);
// <plugin-name> or textlint-config-<rule-name>
const pkgPath = tryResolve(path.join(baseDir, fullPackageName)) || tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
throw new ReferenceError(`Failed to load textlint's config module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
}