readme-guide 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +46 -2
- package/bin/readme-guide.mjs +125 -0
- package/package.json +43 -4
- package/rules/blocks.json +123 -0
- package/rules/conventions.json +39 -0
- package/rules/headings.json +556 -0
- package/rules/project-types.json +37 -0
- package/rules/rules.json +3575 -0
- package/src/checks/ai.js +41 -0
- package/src/checks/commands.js +248 -0
- package/src/checks/env.js +112 -0
- package/src/checks/folders.js +138 -0
- package/src/checks/license.js +137 -0
- package/src/checks/media.js +110 -0
- package/src/checks/structure.js +248 -0
- package/src/checks/types.js +55 -0
- package/src/checks/util.js +148 -0
- package/src/checks/versions.js +109 -0
- package/src/data.js +76 -0
- package/src/detect.js +83 -0
- package/src/explain.js +150 -0
- package/src/format.js +153 -0
- package/src/lint.js +142 -0
- package/src/markdown.js +424 -0
- package/src/project.js +86 -0
- package/src/sections.js +167 -0
- package/src/semver.js +104 -0
package/src/semver.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// 只做到比對 Node.js 版本需要的版本範圍判斷:>=、>、<=、<、=、^、~、x、*、||、a - b。
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 解析版本字串(可以只有主版號)。
|
|
5
|
+
* @param {string} text 例如 "22"、"v22.21.1"、"20.x"
|
|
6
|
+
* @returns {{major:number, minor:number|null, patch:number|null}|null}
|
|
7
|
+
*/
|
|
8
|
+
export function parseVersion(text) {
|
|
9
|
+
const m = String(text).trim().match(/^v?(\d+)(?:\.(\d+|x|\*))?(?:\.(\d+|x|\*))?/i);
|
|
10
|
+
if (!m) return null;
|
|
11
|
+
const num = (s) => (s === undefined || /^[x*]$/i.test(s) ? null : Number(s));
|
|
12
|
+
return { major: Number(m[1]), minor: num(m[2]), patch: num(m[3]) };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function compare(a, b) {
|
|
16
|
+
for (const key of ['major', 'minor', 'patch']) {
|
|
17
|
+
const diff = (a[key] ?? 0) - (b[key] ?? 0);
|
|
18
|
+
if (diff !== 0) return diff;
|
|
19
|
+
}
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function full(v) {
|
|
24
|
+
return { major: v.major, minor: v.minor ?? 0, patch: v.patch ?? 0 };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 把一個比較式轉成 [下限, 下限含不含, 上限, 上限含不含] 的條件 */
|
|
28
|
+
function comparatorToTests(op, v) {
|
|
29
|
+
const lower = full(v);
|
|
30
|
+
const tests = [];
|
|
31
|
+
const partialUpper = () => {
|
|
32
|
+
if (v.minor === null) return { major: v.major + 1, minor: 0, patch: 0 };
|
|
33
|
+
if (v.patch === null) return { major: v.major, minor: v.minor + 1, patch: 0 };
|
|
34
|
+
return null;
|
|
35
|
+
};
|
|
36
|
+
switch (op) {
|
|
37
|
+
case '>=':
|
|
38
|
+
tests.push((x) => compare(x, lower) >= 0);
|
|
39
|
+
break;
|
|
40
|
+
case '>': {
|
|
41
|
+
const up = partialUpper();
|
|
42
|
+
tests.push(up ? (x) => compare(x, up) >= 0 : (x) => compare(x, lower) > 0);
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case '<=': {
|
|
46
|
+
const up = partialUpper();
|
|
47
|
+
tests.push(up ? (x) => compare(x, up) < 0 : (x) => compare(x, lower) <= 0);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case '<':
|
|
51
|
+
tests.push((x) => compare(x, lower) < 0);
|
|
52
|
+
break;
|
|
53
|
+
case '^': {
|
|
54
|
+
let upper;
|
|
55
|
+
if (v.major > 0 || v.minor === null) upper = { major: v.major + 1, minor: 0, patch: 0 };
|
|
56
|
+
else if (v.minor > 0 || v.patch === null) upper = { major: 0, minor: v.minor + 1, patch: 0 };
|
|
57
|
+
else upper = { major: 0, minor: 0, patch: (v.patch ?? 0) + 1 };
|
|
58
|
+
tests.push((x) => compare(x, lower) >= 0 && compare(x, upper) < 0);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
case '~': {
|
|
62
|
+
const upper = v.minor === null ? { major: v.major + 1, minor: 0, patch: 0 } : { major: v.major, minor: v.minor + 1, patch: 0 };
|
|
63
|
+
tests.push((x) => compare(x, lower) >= 0 && compare(x, upper) < 0);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
default: {
|
|
67
|
+
// 沒有符號:完整版本就是剛好等於;只有部分版本就是那一段
|
|
68
|
+
const up = partialUpper();
|
|
69
|
+
tests.push(up ? (x) => compare(x, lower) >= 0 && compare(x, up) < 0 : (x) => compare(x, lower) === 0);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return tests;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 解析版本範圍,傳回判斷函式;看不懂時傳回 null。
|
|
77
|
+
* @param {string} range 例如 ">=22"、"^20 || ^22"、">=18 <23"、"22.x"
|
|
78
|
+
*/
|
|
79
|
+
export function parseRange(range) {
|
|
80
|
+
const text = String(range).trim();
|
|
81
|
+
if (text === '' || text === '*' || /^x$/i.test(text)) return () => true;
|
|
82
|
+
const alternatives = text.split('||').map((s) => s.trim());
|
|
83
|
+
const sets = [];
|
|
84
|
+
for (const alt of alternatives) {
|
|
85
|
+
const hyphen = alt.match(/^(\S+)\s+-\s+(\S+)$/);
|
|
86
|
+
let comparators;
|
|
87
|
+
if (hyphen) {
|
|
88
|
+
comparators = [`>=${hyphen[1]}`, `<=${hyphen[2]}`];
|
|
89
|
+
} else {
|
|
90
|
+
comparators = alt.replace(/(>=|<=|>|<|=|\^|~)\s+/g, '$1').split(/\s+/).filter(Boolean);
|
|
91
|
+
}
|
|
92
|
+
const tests = [];
|
|
93
|
+
for (const c of comparators) {
|
|
94
|
+
const m = c.match(/^(>=|<=|>|<|=|\^|~)?(.+)$/);
|
|
95
|
+
if (!m) return null;
|
|
96
|
+
if (m[2] === '*' || /^x$/i.test(m[2])) continue;
|
|
97
|
+
const v = parseVersion(m[2]);
|
|
98
|
+
if (!v) return null;
|
|
99
|
+
tests.push(...comparatorToTests(m[1] === '=' ? '' : (m[1] ?? ''), v));
|
|
100
|
+
}
|
|
101
|
+
sets.push(tests);
|
|
102
|
+
}
|
|
103
|
+
return (version) => sets.some((tests) => tests.every((t) => t(full(version))));
|
|
104
|
+
}
|