-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextlintrc-to-pacakge-list.js
More file actions
64 lines (62 loc) · 2.23 KB
/
textlintrc-to-pacakge-list.js
File metadata and controls
64 lines (62 loc) · 2.23 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
// LICENSE : MIT
function validRulePackageKey(key) {
// valid: @scope/name
if (key.charAt(0) === "@") {
return key.split("/").length === 2;
}
// invalid rule/key
// just ignore
return key.split("/").length === 1;
}
export const PackageNamePrefix = {
config: "textlint-config-",
rule: "textlint-rule-",
filterRule: "textlint-filter-rule-",
rulePreset: "textlint-rule-preset-",
plugin: "textlint-plugin-"
};
/**
* Create full package name and return
* @param {string} prefix
* @param {string} name
* @returns {string}
*/
export const createFullPackageName = (prefix, name) => {
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(/^@([^/]+)\/(.*)$/, (all, scope, name) => {
// already has prefixed
if (name.startsWith(prefix)) {
return `@${scope}/${name}`
}
return `@${scope}/${prefix}${name}`;
});
}
}
// already has prefixed - do not add prefix
if (name.startsWith(prefix)) {
return `${name}`
}
return `${prefix}${name}`;
};
export function listPackageNames(configJSON) {
const plugins = Array.isArray(configJSON["plugins"]) ? configJSON["plugins"] : [];
const rules = configJSON["rules"] || {};
const filterRules = configJSON["filters"] || {};
const pluginsNameList = plugins.map(key => {
return createFullPackageName(PackageNamePrefix.plugin, key);
});
const ruleNameList = Object.keys(rules).filter(validRulePackageKey).map(key => {
return createFullPackageName(PackageNamePrefix.rule, key);
});
const filterRuleNameList = Object.keys(filterRules).filter(validRulePackageKey).map(key => {
return createFullPackageName(PackageNamePrefix.filterRule, key);
});
return [].concat(pluginsNameList, filterRuleNameList, ruleNameList);
}