repopo 0.8.0 → 0.10.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/CHANGELOG.md +67 -0
- package/THIRD-PARTY-LICENSES.txt +4 -3
- package/esm/adapters/fluidFramework.js +8 -3
- package/esm/adapters/fluidFramework.js.map +1 -1
- package/esm/baseCommand.d.ts.map +1 -1
- package/esm/baseCommand.js +2 -1
- package/esm/baseCommand.js.map +1 -1
- package/esm/commands/check.d.ts +1 -0
- package/esm/commands/check.d.ts.map +1 -1
- package/esm/commands/check.js +114 -52
- package/esm/commands/check.js.map +1 -1
- package/esm/commands/list.d.ts.map +1 -1
- package/esm/commands/list.js +3 -1
- package/esm/commands/list.js.map +1 -1
- package/esm/config.d.ts +19 -4
- package/esm/config.d.ts.map +1 -1
- package/esm/config.js +2 -3
- package/esm/config.js.map +1 -1
- package/esm/generators.d.ts +30 -2
- package/esm/generators.d.ts.map +1 -1
- package/esm/generators.js +31 -1
- package/esm/generators.js.map +1 -1
- package/esm/index.d.ts +14 -4
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +17 -2
- package/esm/index.js.map +1 -1
- package/esm/makePolicy.d.ts +74 -3
- package/esm/makePolicy.d.ts.map +1 -1
- package/esm/makePolicy.js +87 -13
- package/esm/makePolicy.js.map +1 -1
- package/esm/perf.d.ts +3 -2
- package/esm/perf.d.ts.map +1 -1
- package/esm/perf.js +2 -2
- package/esm/perf.js.map +1 -1
- package/esm/policies/ConditionalScripts.d.ts +94 -0
- package/esm/policies/ConditionalScripts.d.ts.map +1 -0
- package/esm/policies/ConditionalScripts.js +144 -0
- package/esm/policies/ConditionalScripts.js.map +1 -0
- package/esm/policies/ExactScripts.d.ts +51 -0
- package/esm/policies/ExactScripts.d.ts.map +1 -0
- package/esm/policies/ExactScripts.js +107 -0
- package/esm/policies/ExactScripts.js.map +1 -0
- package/esm/policies/HtmlFileHeaders.d.ts +1 -1
- package/esm/policies/HtmlFileHeaders.d.ts.map +1 -1
- package/esm/policies/HtmlFileHeaders.js +11 -7
- package/esm/policies/HtmlFileHeaders.js.map +1 -1
- package/esm/policies/JsTsFileHeaders.d.ts +1 -1
- package/esm/policies/JsTsFileHeaders.d.ts.map +1 -1
- package/esm/policies/JsTsFileHeaders.js +11 -7
- package/esm/policies/JsTsFileHeaders.js.map +1 -1
- package/esm/policies/LicenseFileExists.d.ts.map +1 -1
- package/esm/policies/LicenseFileExists.js +22 -18
- package/esm/policies/LicenseFileExists.js.map +1 -1
- package/esm/policies/MutuallyExclusiveScripts.d.ts +46 -0
- package/esm/policies/MutuallyExclusiveScripts.d.ts.map +1 -0
- package/esm/policies/MutuallyExclusiveScripts.js +52 -0
- package/esm/policies/MutuallyExclusiveScripts.js.map +1 -0
- package/esm/policies/NoJsFileExtensions.d.ts.map +1 -1
- package/esm/policies/NoJsFileExtensions.js +16 -12
- package/esm/policies/NoJsFileExtensions.js.map +1 -1
- package/esm/policies/NoLargeBinaryFiles.d.ts.map +1 -1
- package/esm/policies/NoLargeBinaryFiles.js +35 -31
- package/esm/policies/NoLargeBinaryFiles.js.map +1 -1
- package/esm/policies/NoPrivateWorkspaceDependencies.d.ts +1 -1
- package/esm/policies/NoPrivateWorkspaceDependencies.d.ts.map +1 -1
- package/esm/policies/NoPrivateWorkspaceDependencies.js +53 -45
- package/esm/policies/NoPrivateWorkspaceDependencies.js.map +1 -1
- package/esm/policies/PackageAllowedScopes.d.ts +1 -1
- package/esm/policies/PackageAllowedScopes.d.ts.map +1 -1
- package/esm/policies/PackageAllowedScopes.js +45 -40
- package/esm/policies/PackageAllowedScopes.js.map +1 -1
- package/esm/policies/PackageEsmType.d.ts +1 -1
- package/esm/policies/PackageEsmType.d.ts.map +1 -1
- package/esm/policies/PackageEsmType.js +48 -44
- package/esm/policies/PackageEsmType.js.map +1 -1
- package/esm/policies/PackageFolderName.d.ts +1 -1
- package/esm/policies/PackageFolderName.d.ts.map +1 -1
- package/esm/policies/PackageFolderName.js +38 -34
- package/esm/policies/PackageFolderName.js.map +1 -1
- package/esm/policies/PackageJsonProperties.d.ts +1 -1
- package/esm/policies/PackageJsonProperties.d.ts.map +1 -1
- package/esm/policies/PackageJsonProperties.js +36 -31
- package/esm/policies/PackageJsonProperties.js.map +1 -1
- package/esm/policies/PackageJsonRepoDirectoryProperty.d.ts +1 -1
- package/esm/policies/PackageJsonRepoDirectoryProperty.d.ts.map +1 -1
- package/esm/policies/PackageJsonRepoDirectoryProperty.js +41 -38
- package/esm/policies/PackageJsonRepoDirectoryProperty.js.map +1 -1
- package/esm/policies/PackageJsonSorted.d.ts +1 -1
- package/esm/policies/PackageJsonSorted.d.ts.map +1 -1
- package/esm/policies/PackageJsonSorted.js +40 -36
- package/esm/policies/PackageJsonSorted.js.map +1 -1
- package/esm/policies/PackageLicense.d.ts +1 -1
- package/esm/policies/PackageLicense.d.ts.map +1 -1
- package/esm/policies/PackageLicense.js +58 -54
- package/esm/policies/PackageLicense.js.map +1 -1
- package/esm/policies/PackagePrivateField.d.ts +1 -1
- package/esm/policies/PackagePrivateField.d.ts.map +1 -1
- package/esm/policies/PackagePrivateField.js +58 -54
- package/esm/policies/PackagePrivateField.js.map +1 -1
- package/esm/policies/PackageReadme.d.ts +1 -1
- package/esm/policies/PackageReadme.d.ts.map +1 -1
- package/esm/policies/PackageReadme.js +30 -26
- package/esm/policies/PackageReadme.js.map +1 -1
- package/esm/policies/PackageScripts.d.ts +1 -1
- package/esm/policies/PackageScripts.d.ts.map +1 -1
- package/esm/policies/PackageScripts.js +39 -35
- package/esm/policies/PackageScripts.js.map +1 -1
- package/esm/policies/PackageTestScripts.d.ts +1 -1
- package/esm/policies/PackageTestScripts.d.ts.map +1 -1
- package/esm/policies/PackageTestScripts.js +46 -40
- package/esm/policies/PackageTestScripts.js.map +1 -1
- package/esm/policies/RequiredGitignorePatterns.d.ts.map +1 -1
- package/esm/policies/RequiredGitignorePatterns.js +20 -15
- package/esm/policies/RequiredGitignorePatterns.js.map +1 -1
- package/esm/policies/RequiredScripts.d.ts +71 -0
- package/esm/policies/RequiredScripts.d.ts.map +1 -0
- package/esm/policies/RequiredScripts.js +120 -0
- package/esm/policies/RequiredScripts.js.map +1 -0
- package/esm/policies/ScriptContains.d.ts +64 -0
- package/esm/policies/ScriptContains.d.ts.map +1 -0
- package/esm/policies/ScriptContains.js +62 -0
- package/esm/policies/ScriptContains.js.map +1 -0
- package/esm/policies.d.ts +6 -1
- package/esm/policies.d.ts.map +1 -1
- package/esm/policies.js +18 -0
- package/esm/policies.js.map +1 -1
- package/esm/policy.d.ts +193 -74
- package/esm/policy.d.ts.map +1 -1
- package/esm/policy.js +110 -19
- package/esm/policy.js.map +1 -1
- package/esm/policyDefiners/defineFileHeaderPolicy.d.ts +49 -2
- package/esm/policyDefiners/defineFileHeaderPolicy.d.ts.map +1 -1
- package/esm/policyDefiners/defineFileHeaderPolicy.js +21 -4
- package/esm/policyDefiners/defineFileHeaderPolicy.js.map +1 -1
- package/esm/policyDefiners/definePackagePolicy.d.ts +57 -3
- package/esm/policyDefiners/definePackagePolicy.d.ts.map +1 -1
- package/esm/policyDefiners/definePackagePolicy.js +50 -5
- package/esm/policyDefiners/definePackagePolicy.js.map +1 -1
- package/esm/tsdoc-metadata.json +1 -1
- package/oclif.manifest.json +1 -1
- package/package.json +6 -4
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import jsonfile from "jsonfile";
|
|
2
|
+
import { definePackagePolicy } from "../policyDefiners/definePackagePolicy.js";
|
|
3
|
+
const POLICY_NAME = "ConditionalScripts";
|
|
4
|
+
/**
|
|
5
|
+
* Extracts script name and optional default value from a ConditionalScriptEntry.
|
|
6
|
+
*/
|
|
7
|
+
function parseEntry(entry) {
|
|
8
|
+
if (typeof entry === "string") {
|
|
9
|
+
return { name: entry };
|
|
10
|
+
}
|
|
11
|
+
const keys = Object.keys(entry);
|
|
12
|
+
if (keys.length !== 1) {
|
|
13
|
+
throw new Error(`Invalid conditional script entry: expected single key-value pair, got ${keys.length} keys`);
|
|
14
|
+
}
|
|
15
|
+
// biome-ignore lint/style/noNonNullAssertion: we verified length is 1
|
|
16
|
+
const name = keys[0];
|
|
17
|
+
const defaultValue = entry[name];
|
|
18
|
+
return defaultValue !== undefined ? { name, defaultValue } : { name };
|
|
19
|
+
}
|
|
20
|
+
function validateConditionalRules(scripts, rules) {
|
|
21
|
+
const violations = [];
|
|
22
|
+
const missingWithDefaults = [];
|
|
23
|
+
const seenMissing = new Set();
|
|
24
|
+
for (const rule of rules) {
|
|
25
|
+
// Skip if the triggering script doesn't exist
|
|
26
|
+
if (!Object.hasOwn(scripts, rule.ifPresent)) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const missingScripts = [];
|
|
30
|
+
for (const entry of rule.requires) {
|
|
31
|
+
const { name, defaultValue } = parseEntry(entry);
|
|
32
|
+
if (!Object.hasOwn(scripts, name)) {
|
|
33
|
+
missingScripts.push(name);
|
|
34
|
+
if (defaultValue !== undefined && !seenMissing.has(name)) {
|
|
35
|
+
missingWithDefaults.push({ name, defaultValue });
|
|
36
|
+
seenMissing.add(name);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (missingScripts.length > 0) {
|
|
41
|
+
violations.push({
|
|
42
|
+
triggerScript: rule.ifPresent,
|
|
43
|
+
missingScripts,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { violations, missingWithDefaults };
|
|
48
|
+
}
|
|
49
|
+
function formatViolationErrors(violations) {
|
|
50
|
+
return violations.map((v) => `Script "${v.triggerScript}" is present, but required companion script(s) missing: ${v.missingScripts.join(", ")}`);
|
|
51
|
+
}
|
|
52
|
+
async function fixMissingScripts(json, file, missingWithDefaults, existingScripts) {
|
|
53
|
+
const updatedScripts = {};
|
|
54
|
+
// Copy existing scripts
|
|
55
|
+
for (const [key, value] of Object.entries(existingScripts)) {
|
|
56
|
+
if (value !== undefined) {
|
|
57
|
+
updatedScripts[key] = value;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Add missing scripts with defaults
|
|
61
|
+
const fixedScripts = [];
|
|
62
|
+
for (const { name, defaultValue } of missingWithDefaults) {
|
|
63
|
+
updatedScripts[name] = defaultValue;
|
|
64
|
+
fixedScripts.push(name);
|
|
65
|
+
}
|
|
66
|
+
await jsonfile.writeFile(file, { ...json, scripts: updatedScripts }, { spaces: 2 });
|
|
67
|
+
return fixedScripts;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A policy that validates conditional script requirements in package.json.
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* Use this policy when you want to enforce that the presence of one script
|
|
74
|
+
* requires other scripts to also exist. For example, if "build" exists,
|
|
75
|
+
* then "clean" must also exist.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* import { policy } from "repopo";
|
|
80
|
+
* import { ConditionalScripts } from "repopo/policies";
|
|
81
|
+
*
|
|
82
|
+
* policy(ConditionalScripts, {
|
|
83
|
+
* rules: [
|
|
84
|
+
* // If "build" exists, "clean" must also exist (with auto-fix default)
|
|
85
|
+
* { ifPresent: "build", requires: [{ clean: "rimraf dist" }] },
|
|
86
|
+
* // If "test" exists, "test:coverage" must also exist (no auto-fix)
|
|
87
|
+
* { ifPresent: "test", requires: ["test:coverage"] },
|
|
88
|
+
* ],
|
|
89
|
+
* })
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @alpha
|
|
93
|
+
*/
|
|
94
|
+
export const ConditionalScripts = definePackagePolicy({
|
|
95
|
+
name: POLICY_NAME,
|
|
96
|
+
description: "Validates that if certain scripts exist, other required scripts must also exist.",
|
|
97
|
+
handler: async (json, { file, resolve, config }) => {
|
|
98
|
+
if (config === undefined || config.rules.length === 0) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
const scripts = json.scripts ?? {};
|
|
102
|
+
const validation = validateConditionalRules(scripts, config.rules);
|
|
103
|
+
if (validation.violations.length === 0) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
const errors = formatViolationErrors(validation.violations);
|
|
107
|
+
const hasAutoFixableErrors = validation.missingWithDefaults.length > 0;
|
|
108
|
+
if (resolve && hasAutoFixableErrors) {
|
|
109
|
+
try {
|
|
110
|
+
const fixedScripts = await fixMissingScripts(json, file, validation.missingWithDefaults, scripts);
|
|
111
|
+
// Re-validate after fix to check for remaining errors
|
|
112
|
+
const updatedScripts = { ...scripts };
|
|
113
|
+
for (const name of fixedScripts) {
|
|
114
|
+
const entry = validation.missingWithDefaults.find((m) => m.name === name);
|
|
115
|
+
if (entry) {
|
|
116
|
+
updatedScripts[name] = entry.defaultValue;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const revalidation = validateConditionalRules(updatedScripts, config.rules);
|
|
120
|
+
const remainingErrors = formatViolationErrors(revalidation.violations);
|
|
121
|
+
return {
|
|
122
|
+
name: POLICY_NAME,
|
|
123
|
+
file,
|
|
124
|
+
resolved: remainingErrors.length === 0,
|
|
125
|
+
errorMessages: remainingErrors.length > 0
|
|
126
|
+
? [
|
|
127
|
+
`Fixed scripts: ${fixedScripts.join(", ")}. Remaining errors:\n${remainingErrors.join("\n")}`,
|
|
128
|
+
]
|
|
129
|
+
: [`Fixed scripts: ${fixedScripts.join(", ")}`],
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// Fall through to return regular failure
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
name: POLICY_NAME,
|
|
138
|
+
file,
|
|
139
|
+
autoFixable: hasAutoFixableErrors,
|
|
140
|
+
errorMessages: errors,
|
|
141
|
+
};
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
//# sourceMappingURL=ConditionalScripts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConditionalScripts.js","sourceRoot":"","sources":["../../src/polic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esContent":["import jsonfile from \"jsonfile\";\nimport type { PackageJson } from \"type-fest\";\nimport { definePackagePolicy } from \"../policyDefiners/definePackagePolicy.js\";\n\n/**\n * A required script entry for conditional requirements. Can be either:\n * - A string (script name only, no default value)\n * - An object with a single key-value pair (script name → default value for auto-fix)\n *\n * @example\n * ```typescript\n * // Script must exist, no auto-fix available\n * \"test\"\n *\n * // Script must exist, auto-fix will add this value if missing\n * { clean: \"rimraf dist\" }\n * ```\n *\n * @alpha\n */\nexport type ConditionalScriptEntry = string | Record<string, string>;\n\n/**\n * Configuration for a conditional script requirement.\n *\n * @alpha\n */\nexport interface ConditionalScriptRule {\n\t/**\n\t * The script that triggers the requirement.\n\t * If this script exists, then `requires` must also exist.\n\t */\n\tifPresent: string;\n\n\t/**\n\t * Scripts that must be present if `ifPresent` script exists.\n\t *\n\t * @remarks\n\t * Each entry can be:\n\t * - A string: Script must exist, no auto-fix available\n\t * - An object `{ scriptName: \"default value\" }`: Script must exist, auto-fix adds the\n\t * default value if missing\n\t *\n\t * @example\n\t * ```typescript\n\t * requires: [\"test\", { clean: \"rimraf dist\" }]\n\t * ```\n\t */\n\trequires: ConditionalScriptEntry[];\n}\n\n/**\n * Configuration for the ConditionalScripts policy.\n *\n * @alpha\n */\nexport interface ConditionalScriptsConfig {\n\t/**\n\t * Conditional script requirements. If a script is present, other scripts must also exist.\n\t *\n\t * @remarks\n\t * This is useful for enforcing that packages with a \"build\" script also have a \"clean\" script,\n\t * or packages with \"test\" also have \"test:coverage\".\n\t *\n\t * @example\n\t * ```typescript\n\t * rules: [\n\t * { ifPresent: \"build\", requires: [{ clean: \"rimraf dist\" }] },\n\t * { ifPresent: \"test\", requires: [\"test:coverage\"] },\n\t * ]\n\t * ```\n\t */\n\trules: ConditionalScriptRule[];\n}\n\nconst POLICY_NAME = \"ConditionalScripts\";\n\ninterface ParsedEntry {\n\tname: string;\n\tdefaultValue?: string;\n}\n\n/**\n * Extracts script name and optional default value from a ConditionalScriptEntry.\n */\nfunction parseEntry(entry: ConditionalScriptEntry): ParsedEntry {\n\tif (typeof entry === \"string\") {\n\t\treturn { name: entry };\n\t}\n\tconst keys = Object.keys(entry);\n\tif (keys.length !== 1) {\n\t\tthrow new Error(\n\t\t\t`Invalid conditional script entry: expected single key-value pair, got ${keys.length} keys`,\n\t\t);\n\t}\n\t// biome-ignore lint/style/noNonNullAssertion: we verified length is 1\n\tconst name = keys[0]!;\n\tconst defaultValue = entry[name];\n\treturn defaultValue !== undefined ? { name, defaultValue } : { name };\n}\n\ninterface RuleViolation {\n\ttriggerScript: string;\n\tmissingScripts: string[];\n}\n\ninterface ValidationResult {\n\tviolations: RuleViolation[];\n\tmissingWithDefaults: Array<{ name: string; defaultValue: string }>;\n}\n\nfunction validateConditionalRules(\n\tscripts: Record<string, string | undefined>,\n\trules: ConditionalScriptRule[],\n): ValidationResult {\n\tconst violations: RuleViolation[] = [];\n\tconst missingWithDefaults: Array<{ name: string; defaultValue: string }> = [];\n\tconst seenMissing = new Set<string>();\n\n\tfor (const rule of rules) {\n\t\t// Skip if the triggering script doesn't exist\n\t\tif (!Object.hasOwn(scripts, rule.ifPresent)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst missingScripts: string[] = [];\n\t\tfor (const entry of rule.requires) {\n\t\t\tconst { name, defaultValue } = parseEntry(entry);\n\t\t\tif (!Object.hasOwn(scripts, name)) {\n\t\t\t\tmissingScripts.push(name);\n\t\t\t\tif (defaultValue !== undefined && !seenMissing.has(name)) {\n\t\t\t\t\tmissingWithDefaults.push({ name, defaultValue });\n\t\t\t\t\tseenMissing.add(name);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (missingScripts.length > 0) {\n\t\t\tviolations.push({\n\t\t\t\ttriggerScript: rule.ifPresent,\n\t\t\t\tmissingScripts,\n\t\t\t});\n\t\t}\n\t}\n\n\treturn { violations, missingWithDefaults };\n}\n\nfunction formatViolationErrors(violations: RuleViolation[]): string[] {\n\treturn violations.map(\n\t\t(v) =>\n\t\t\t`Script \"${v.triggerScript}\" is present, but required companion script(s) missing: ${v.missingScripts.join(\", \")}`,\n\t);\n}\n\nasync function fixMissingScripts(\n\tjson: PackageJson,\n\tfile: string,\n\tmissingWithDefaults: Array<{ name: string; defaultValue: string }>,\n\texistingScripts: Record<string, string | undefined>,\n): Promise<string[]> {\n\tconst updatedScripts: Record<string, string> = {};\n\n\t// Copy existing scripts\n\tfor (const [key, value] of Object.entries(existingScripts)) {\n\t\tif (value !== undefined) {\n\t\t\tupdatedScripts[key] = value;\n\t\t}\n\t}\n\n\t// Add missing scripts with defaults\n\tconst fixedScripts: string[] = [];\n\tfor (const { name, defaultValue } of missingWithDefaults) {\n\t\tupdatedScripts[name] = defaultValue;\n\t\tfixedScripts.push(name);\n\t}\n\n\tawait jsonfile.writeFile(\n\t\tfile,\n\t\t{ ...json, scripts: updatedScripts },\n\t\t{ spaces: 2 },\n\t);\n\n\treturn fixedScripts;\n}\n\n/**\n * A policy that validates conditional script requirements in package.json.\n *\n * @remarks\n * Use this policy when you want to enforce that the presence of one script\n * requires other scripts to also exist. For example, if \"build\" exists,\n * then \"clean\" must also exist.\n *\n * @example\n * ```typescript\n * import { policy } from \"repopo\";\n * import { ConditionalScripts } from \"repopo/policies\";\n *\n * policy(ConditionalScripts, {\n * rules: [\n * // If \"build\" exists, \"clean\" must also exist (with auto-fix default)\n * { ifPresent: \"build\", requires: [{ clean: \"rimraf dist\" }] },\n * // If \"test\" exists, \"test:coverage\" must also exist (no auto-fix)\n * { ifPresent: \"test\", requires: [\"test:coverage\"] },\n * ],\n * })\n * ```\n *\n * @alpha\n */\nexport const ConditionalScripts = definePackagePolicy<\n\tPackageJson,\n\tConditionalScriptsConfig | undefined\n>({\n\tname: POLICY_NAME,\n\tdescription:\n\t\t\"Validates that if certain scripts exist, other required scripts must also exist.\",\n\thandler: async (json, { file, resolve, config }) => {\n\t\tif (config === undefined || config.rules.length === 0) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst scripts = json.scripts ?? {};\n\t\tconst validation = validateConditionalRules(scripts, config.rules);\n\n\t\tif (validation.violations.length === 0) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst errors = formatViolationErrors(validation.violations);\n\t\tconst hasAutoFixableErrors = validation.missingWithDefaults.length > 0;\n\n\t\tif (resolve && hasAutoFixableErrors) {\n\t\t\ttry {\n\t\t\t\tconst fixedScripts = await fixMissingScripts(\n\t\t\t\t\tjson,\n\t\t\t\t\tfile,\n\t\t\t\t\tvalidation.missingWithDefaults,\n\t\t\t\t\tscripts,\n\t\t\t\t);\n\n\t\t\t\t// Re-validate after fix to check for remaining errors\n\t\t\t\tconst updatedScripts = { ...scripts };\n\t\t\t\tfor (const name of fixedScripts) {\n\t\t\t\t\tconst entry = validation.missingWithDefaults.find(\n\t\t\t\t\t\t(m) => m.name === name,\n\t\t\t\t\t);\n\t\t\t\t\tif (entry) {\n\t\t\t\t\t\tupdatedScripts[name] = entry.defaultValue;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst revalidation = validateConditionalRules(\n\t\t\t\t\tupdatedScripts,\n\t\t\t\t\tconfig.rules,\n\t\t\t\t);\n\t\t\t\tconst remainingErrors = formatViolationErrors(revalidation.violations);\n\n\t\t\t\treturn {\n\t\t\t\t\tname: POLICY_NAME,\n\t\t\t\t\tfile,\n\t\t\t\t\tresolved: remainingErrors.length === 0,\n\t\t\t\t\terrorMessages:\n\t\t\t\t\t\tremainingErrors.length > 0\n\t\t\t\t\t\t\t? [\n\t\t\t\t\t\t\t\t\t`Fixed scripts: ${fixedScripts.join(\", \")}. Remaining errors:\\n${remainingErrors.join(\"\\n\")}`,\n\t\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t: [`Fixed scripts: ${fixedScripts.join(\", \")}`],\n\t\t\t\t};\n\t\t\t} catch {\n\t\t\t\t// Fall through to return regular failure\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tname: POLICY_NAME,\n\t\t\tfile,\n\t\t\tautoFixable: hasAutoFixableErrors,\n\t\t\terrorMessages: errors,\n\t\t};\n\t},\n});\n"]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for the ExactScripts policy.
|
|
3
|
+
*
|
|
4
|
+
* @alpha
|
|
5
|
+
*/
|
|
6
|
+
export interface ExactScriptsConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Scripts that must exist AND have exact content.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* Unlike RequiredScripts, scripts in `exact` are validated against
|
|
12
|
+
* the specified value. If a script exists but has different content, it fails validation.
|
|
13
|
+
* Both missing and mismatched scripts are auto-fixable.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* scripts: {
|
|
18
|
+
* clean: "rimraf dist esm",
|
|
19
|
+
* format: "biome format --write .",
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
scripts: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A policy that validates scripts exist AND have exact content in package.json.
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* Use this policy when you want to enforce exact script content, not just presence.
|
|
30
|
+
* This is useful for scripts that must be exactly the same across packages.
|
|
31
|
+
*
|
|
32
|
+
* For scripts where you only care about presence (not content), use RequiredScripts instead.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { policy } from "repopo";
|
|
37
|
+
* import { ExactScripts } from "repopo/policies";
|
|
38
|
+
*
|
|
39
|
+
* policy(ExactScripts, {
|
|
40
|
+
* scripts: {
|
|
41
|
+
* clean: "rimraf dist esm",
|
|
42
|
+
* format: "biome format --write .",
|
|
43
|
+
* lint: "biome lint .",
|
|
44
|
+
* },
|
|
45
|
+
* })
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @alpha
|
|
49
|
+
*/
|
|
50
|
+
export declare const ExactScripts: import("../policy.js").PolicyShape<ExactScriptsConfig | undefined>;
|
|
51
|
+
//# sourceMappingURL=ExactScripts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExactScripts.d.ts","sourceRoot":"","sources":["../../src/policies/ExactScripts.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IAClC;;;;;;;;;;;;;;;OAeG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC;AAuED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,YAAY,oEAqDvB,CAAC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import jsonfile from "jsonfile";
|
|
2
|
+
import { definePackagePolicy } from "../policyDefiners/definePackagePolicy.js";
|
|
3
|
+
const POLICY_NAME = "ExactScripts";
|
|
4
|
+
function validateScripts(scripts, expectedScripts) {
|
|
5
|
+
const errors = [];
|
|
6
|
+
const missingScripts = [];
|
|
7
|
+
const mismatchedScripts = [];
|
|
8
|
+
for (const [scriptName, expectedValue] of Object.entries(expectedScripts)) {
|
|
9
|
+
if (Object.hasOwn(scripts, scriptName)) {
|
|
10
|
+
const actualValue = scripts[scriptName];
|
|
11
|
+
if (actualValue !== expectedValue) {
|
|
12
|
+
mismatchedScripts.push(scriptName);
|
|
13
|
+
errors.push(`Script "${scriptName}" must be "${expectedValue}", but found "${actualValue}"`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
missingScripts.push(scriptName);
|
|
18
|
+
errors.push(`Missing required script: ${scriptName}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return { errors, missingScripts, mismatchedScripts };
|
|
22
|
+
}
|
|
23
|
+
async function fixScripts(json, file, scriptsToFix, expectedScripts, existingScripts) {
|
|
24
|
+
const updatedScripts = {};
|
|
25
|
+
// Copy existing scripts
|
|
26
|
+
for (const [key, value] of Object.entries(existingScripts)) {
|
|
27
|
+
if (value !== undefined) {
|
|
28
|
+
updatedScripts[key] = value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// Add/update scripts
|
|
32
|
+
const fixedScripts = [];
|
|
33
|
+
for (const scriptName of scriptsToFix) {
|
|
34
|
+
const expectedValue = expectedScripts[scriptName];
|
|
35
|
+
if (expectedValue !== undefined) {
|
|
36
|
+
updatedScripts[scriptName] = expectedValue;
|
|
37
|
+
fixedScripts.push(scriptName);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
await jsonfile.writeFile(file, { ...json, scripts: updatedScripts }, { spaces: 2 });
|
|
41
|
+
return fixedScripts;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A policy that validates scripts exist AND have exact content in package.json.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* Use this policy when you want to enforce exact script content, not just presence.
|
|
48
|
+
* This is useful for scripts that must be exactly the same across packages.
|
|
49
|
+
*
|
|
50
|
+
* For scripts where you only care about presence (not content), use RequiredScripts instead.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import { policy } from "repopo";
|
|
55
|
+
* import { ExactScripts } from "repopo/policies";
|
|
56
|
+
*
|
|
57
|
+
* policy(ExactScripts, {
|
|
58
|
+
* scripts: {
|
|
59
|
+
* clean: "rimraf dist esm",
|
|
60
|
+
* format: "biome format --write .",
|
|
61
|
+
* lint: "biome lint .",
|
|
62
|
+
* },
|
|
63
|
+
* })
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @alpha
|
|
67
|
+
*/
|
|
68
|
+
export const ExactScripts = definePackagePolicy({
|
|
69
|
+
name: POLICY_NAME,
|
|
70
|
+
description: "Validates that scripts exist and have exact content in package.json.",
|
|
71
|
+
handler: async (json, { file, resolve, config }) => {
|
|
72
|
+
if (config === undefined || Object.keys(config.scripts).length === 0) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
const scripts = json.scripts ?? {};
|
|
76
|
+
const validation = validateScripts(scripts, config.scripts);
|
|
77
|
+
if (validation.errors.length === 0) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
const scriptsToFix = [
|
|
81
|
+
...validation.missingScripts,
|
|
82
|
+
...validation.mismatchedScripts,
|
|
83
|
+
];
|
|
84
|
+
const hasAutoFixableErrors = scriptsToFix.length > 0;
|
|
85
|
+
if (resolve && hasAutoFixableErrors) {
|
|
86
|
+
try {
|
|
87
|
+
const fixedScripts = await fixScripts(json, file, scriptsToFix, config.scripts, scripts);
|
|
88
|
+
return {
|
|
89
|
+
name: POLICY_NAME,
|
|
90
|
+
file,
|
|
91
|
+
resolved: true,
|
|
92
|
+
errorMessages: [`Fixed scripts: ${fixedScripts.join(", ")}`],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// Fall through to return regular failure
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
name: POLICY_NAME,
|
|
101
|
+
file,
|
|
102
|
+
autoFixable: hasAutoFixableErrors,
|
|
103
|
+
errorMessages: validation.errors,
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
//# sourceMappingURL=ExactScripts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExactScripts.js","sourceRoot":"","sources":["../../src/policies/ExactScripts.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AA2B/E,MAAM,WAAW,GAAG,cAAc,CAAC;AAQnC,SAAS,eAAe,CACvB,OAA2C,EAC3C,eAAuC;IAEvC,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,cAAc,GAAa,EAAE,CAAC;IACpC,MAAM,iBAAiB,GAAa,EAAE,C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,OAAO,CACP,CAAC;gBAEF,OAAO;oBACN,IAAI,EAAE,WAAW;oBACjB,IAAI;oBACJ,QAAQ,EAAE,IAAI;oBACd,aAAa,EAAE,CAAC,kBAAkB,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;iBAC5D,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACR,yCAAyC;YAC1C,CAAC;QACF,CAAC;QAED,OAAO;YACN,IAAI,EAAE,WAAW;YACjB,IAAI;YACJ,WAAW,EAAE,oBAAoB;YACjC,aAAa,EAAE,UAAU,CAAC,MAAM;SAChC,CAAC;IACH,CAAC;CACD,CAAC,CAAC","sourcesContent":["import jsonfile from \"jsonfile\";\nimport type { PackageJson } from \"type-fest\";\nimport { definePackagePolicy } from \"../policyDefiners/definePackagePolicy.js\";\n\n/**\n * Configuration for the ExactScripts policy.\n *\n * @alpha\n */\nexport interface ExactScriptsConfig {\n\t/**\n\t * Scripts that must exist AND have exact content.\n\t *\n\t * @remarks\n\t * Unlike RequiredScripts, scripts in `exact` are validated against\n\t * the specified value. If a script exists but has different content, it fails validation.\n\t * Both missing and mismatched scripts are auto-fixable.\n\t *\n\t * @example\n\t * ```typescript\n\t * scripts: {\n\t * clean: \"rimraf dist esm\",\n\t * format: \"biome format --write .\",\n\t * }\n\t * ```\n\t */\n\tscripts: Record<string, string>;\n}\n\nconst POLICY_NAME = \"ExactScripts\";\n\ninterface ValidationResult {\n\terrors: string[];\n\tmissingScripts: string[];\n\tmismatchedScripts: string[];\n}\n\nfunction validateScripts(\n\tscripts: Record<string, string | undefined>,\n\texpectedScripts: Record<string, string>,\n): ValidationResult {\n\tconst errors: string[] = [];\n\tconst missingScripts: string[] = [];\n\tconst mismatchedScripts: string[] = [];\n\n\tfor (const [scriptName, expectedValue] of Object.entries(expectedScripts)) {\n\t\tif (Object.hasOwn(scripts, scriptName)) {\n\t\t\tconst actualValue = scripts[scriptName];\n\t\t\tif (actualValue !== expectedValue) {\n\t\t\t\tmismatchedScripts.push(scriptName);\n\t\t\t\terrors.push(\n\t\t\t\t\t`Script \"${scriptName}\" must be \"${expectedValue}\", but found \"${actualValue}\"`,\n\t\t\t\t);\n\t\t\t}\n\t\t} else {\n\t\t\tmissingScripts.push(scriptName);\n\t\t\terrors.push(`Missing required script: ${scriptName}`);\n\t\t}\n\t}\n\n\treturn { errors, missingScripts, mismatchedScripts };\n}\n\nasync function fixScripts(\n\tjson: PackageJson,\n\tfile: string,\n\tscriptsToFix: string[],\n\texpectedScripts: Record<string, string>,\n\texistingScripts: Record<string, string | undefined>,\n): Promise<string[]> {\n\tconst updatedScripts: Record<string, string> = {};\n\n\t// Copy existing scripts\n\tfor (const [key, value] of Object.entries(existingScripts)) {\n\t\tif (value !== undefined) {\n\t\t\tupdatedScripts[key] = value;\n\t\t}\n\t}\n\n\t// Add/update scripts\n\tconst fixedScripts: string[] = [];\n\tfor (const scriptName of scriptsToFix) {\n\t\tconst expectedValue = expectedScripts[scriptName];\n\t\tif (expectedValue !== undefined) {\n\t\t\tupdatedScripts[scriptName] = expectedValue;\n\t\t\tfixedScripts.push(scriptName);\n\t\t}\n\t}\n\n\tawait jsonfile.writeFile(\n\t\tfile,\n\t\t{ ...json, scripts: updatedScripts },\n\t\t{ spaces: 2 },\n\t);\n\n\treturn fixedScripts;\n}\n\n/**\n * A policy that validates scripts exist AND have exact content in package.json.\n *\n * @remarks\n * Use this policy when you want to enforce exact script content, not just presence.\n * This is useful for scripts that must be exactly the same across packages.\n *\n * For scripts where you only care about presence (not content), use RequiredScripts instead.\n *\n * @example\n * ```typescript\n * import { policy } from \"repopo\";\n * import { ExactScripts } from \"repopo/policies\";\n *\n * policy(ExactScripts, {\n * scripts: {\n * clean: \"rimraf dist esm\",\n * format: \"biome format --write .\",\n * lint: \"biome lint .\",\n * },\n * })\n * ```\n *\n * @alpha\n */\nexport const ExactScripts = definePackagePolicy<\n\tPackageJson,\n\tExactScriptsConfig | undefined\n>({\n\tname: POLICY_NAME,\n\tdescription:\n\t\t\"Validates that scripts exist and have exact content in package.json.\",\n\thandler: async (json, { file, resolve, config }) => {\n\t\tif (config === undefined || Object.keys(config.scripts).length === 0) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst scripts = json.scripts ?? {};\n\t\tconst validation = validateScripts(scripts, config.scripts);\n\n\t\tif (validation.errors.length === 0) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst scriptsToFix = [\n\t\t\t...validation.missingScripts,\n\t\t\t...validation.mismatchedScripts,\n\t\t];\n\t\tconst hasAutoFixableErrors = scriptsToFix.length > 0;\n\n\t\tif (resolve && hasAutoFixableErrors) {\n\t\t\ttry {\n\t\t\t\tconst fixedScripts = await fixScripts(\n\t\t\t\t\tjson,\n\t\t\t\t\tfile,\n\t\t\t\t\tscriptsToFix,\n\t\t\t\t\tconfig.scripts,\n\t\t\t\t\tscripts,\n\t\t\t\t);\n\n\t\t\t\treturn {\n\t\t\t\t\tname: POLICY_NAME,\n\t\t\t\t\tfile,\n\t\t\t\t\tresolved: true,\n\t\t\t\t\terrorMessages: [`Fixed scripts: ${fixedScripts.join(\", \")}`],\n\t\t\t\t};\n\t\t\t} catch {\n\t\t\t\t// Fall through to return regular failure\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tname: POLICY_NAME,\n\t\t\tfile,\n\t\t\tautoFixable: hasAutoFixableErrors,\n\t\t\terrorMessages: validation.errors,\n\t\t};\n\t},\n});\n"]}
|
|
@@ -4,5 +4,5 @@ import { type FileHeaderPolicyConfig } from "../policyDefiners/defineFileHeaderP
|
|
|
4
4
|
*
|
|
5
5
|
* @alpha
|
|
6
6
|
*/
|
|
7
|
-
export declare const HtmlFileHeaders: import("../policy.js").
|
|
7
|
+
export declare const HtmlFileHeaders: import("../policy.js").PolicyShape<FileHeaderPolicyConfig>;
|
|
8
8
|
//# sourceMappingURL=HtmlFileHeaders.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HtmlFileHeaders.d.ts","sourceRoot":"","sources":["../../src/policies/HtmlFileHeaders.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,KAAK,sBAAsB,EAC3B,MAAM,6CAA6C,CAAC;AAErD;;;;GAIG;AACH,eAAO,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"HtmlFileHeaders.d.ts","sourceRoot":"","sources":["../../src/policies/HtmlFileHeaders.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,KAAK,sBAAsB,EAC3B,MAAM,6CAA6C,CAAC;AAErD;;;;GAIG;AACH,eAAO,MAAM,eAAe,4DAkB1B,CAAC"}
|
|
@@ -5,13 +5,17 @@ import { defineFileHeaderPolicy, } from "../policyDefiners/defineFileHeaderPolic
|
|
|
5
5
|
*
|
|
6
6
|
* @alpha
|
|
7
7
|
*/
|
|
8
|
-
export const HtmlFileHeaders = defineFileHeaderPolicy(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
export const HtmlFileHeaders = defineFileHeaderPolicy({
|
|
9
|
+
name: "HtmlFileHeaders",
|
|
10
|
+
description: "Ensures HTML files have the configured header comment.",
|
|
11
|
+
config: {
|
|
12
|
+
match: /(^|\/)[^/]+\.[mc]?[jt]sx?$/i,
|
|
13
|
+
lineStart: /<!-- /, // Lines begin with '<!-- '
|
|
14
|
+
lineEnd: / -->\r?\n/, // Lines end with ' -->' followed by CRLF or LF
|
|
15
|
+
replacer: (content, { headerText }) => {
|
|
16
|
+
const newContent = `<!-- ${headerText.replace(newline, ` -->${newline}<!-- `)} -->${newline}${newline}${content}`;
|
|
17
|
+
return newContent;
|
|
18
|
+
},
|
|
15
19
|
},
|
|
16
20
|
});
|
|
17
21
|
//# sourceMappingURL=HtmlFileHeaders.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HtmlFileHeaders.js","sourceRoot":"","sources":["../../src/policies/HtmlFileHeaders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EACN,sBAAsB,GAEtB,MAAM,6CAA6C,CAAC;AAErD;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAAC,iBAAiB,EAAE;
|
|
1
|
+
{"version":3,"file":"HtmlFileHeaders.js","sourceRoot":"","sources":["../../src/policies/HtmlFileHeaders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EACN,sBAAsB,GAEtB,MAAM,6CAA6C,CAAC;AAErD;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAAC;IACrD,IAAI,EAAE,iBAAiB;IACvB,WAAW,EAAE,wDAAwD;IACrE,MAAM,EAAE;QACP,KAAK,EAAE,6BAA6B;QACpC,SAAS,EAAE,OAAO,EAAE,2BAA2B;QAC/C,OAAO,EAAE,WAAW,EAAE,+CAA+C;QACrE,QAAQ,EAAE,CACT,OAAe,EACf,EAAE,UAAU,EAA0B,EAC7B,EAAE;YACX,MAAM,UAAU,GAAG,QAAQ,UAAU,CAAC,OAAO,CAC5C,OAAO,EACP,OAAO,OAAO,OAAO,CACrB,OAAO,OAAO,GAAG,OAAO,GAAG,OAAO,EAAE,CAAC;YACtC,OAAO,UAAU,CAAC;QACnB,CAAC;KACD;CACD,CAAC,CAAC","sourcesContent":["import { EOL as newline } from \"node:os\";\nimport {\n\tdefineFileHeaderPolicy,\n\ttype FileHeaderPolicyConfig,\n} from \"../policyDefiners/defineFileHeaderPolicy.js\";\n\n/**\n * A RepoPolicy that checks that HTML files have the configured header comment.\n *\n * @alpha\n */\nexport const HtmlFileHeaders = defineFileHeaderPolicy({\n\tname: \"HtmlFileHeaders\",\n\tdescription: \"Ensures HTML files have the configured header comment.\",\n\tconfig: {\n\t\tmatch: /(^|\\/)[^/]+\\.[mc]?[jt]sx?$/i,\n\t\tlineStart: /<!-- /, // Lines begin with '<!-- '\n\t\tlineEnd: / -->\\r?\\n/, // Lines end with ' -->' followed by CRLF or LF\n\t\treplacer: (\n\t\t\tcontent: string,\n\t\t\t{ headerText }: FileHeaderPolicyConfig,\n\t\t): string => {\n\t\t\tconst newContent = `<!-- ${headerText.replace(\n\t\t\t\tnewline,\n\t\t\t\t` -->${newline}<!-- `,\n\t\t\t)} -->${newline}${newline}${content}`;\n\t\t\treturn newContent;\n\t\t},\n\t},\n});\n"]}
|
|
@@ -4,5 +4,5 @@ import { type FileHeaderPolicyConfig } from "../policyDefiners/defineFileHeaderP
|
|
|
4
4
|
*
|
|
5
5
|
* @alpha
|
|
6
6
|
*/
|
|
7
|
-
export declare const JsTsFileHeaders: import("../policy.js").
|
|
7
|
+
export declare const JsTsFileHeaders: import("../policy.js").PolicyShape<FileHeaderPolicyConfig>;
|
|
8
8
|
//# sourceMappingURL=JsTsFileHeaders.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsTsFileHeaders.d.ts","sourceRoot":"","sources":["../../src/policies/JsTsFileHeaders.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,KAAK,sBAAsB,EAC3B,MAAM,6CAA6C,CAAC;AAkBrD;;;;GAIG;AACH,eAAO,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"JsTsFileHeaders.d.ts","sourceRoot":"","sources":["../../src/policies/JsTsFileHeaders.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,KAAK,sBAAsB,EAC3B,MAAM,6CAA6C,CAAC;AAkBrD;;;;GAIG;AACH,eAAO,MAAM,eAAe,4DAY1B,CAAC"}
|
|
@@ -13,12 +13,16 @@ const jsTsReplacer = (prevContent, { headerText }) => {
|
|
|
13
13
|
*
|
|
14
14
|
* @alpha
|
|
15
15
|
*/
|
|
16
|
-
export const JsTsFileHeaders = defineFileHeaderPolicy(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
export const JsTsFileHeaders = defineFileHeaderPolicy({
|
|
17
|
+
name: "JsTsFileHeaders",
|
|
18
|
+
description: "Ensures JavaScript and TypeScript source files have the configured header comment.",
|
|
19
|
+
config: {
|
|
20
|
+
match: /(^|\/)[^/]+\.[mc]?[jt]sx?$/i,
|
|
21
|
+
headerStart: /(#![^\n]*\r?\n)?\/\*!\r?\n/, // Begins with optional hashbang followed by '/*!'
|
|
22
|
+
lineStart: / \* /, // Subsequent lines begins with ' * '
|
|
23
|
+
lineEnd: /\r?\n/, // Subsequent lines end with CRLF or LF
|
|
24
|
+
headerEnd: / \*\/\r?\n\r?\n/, // Header ends with ' */' on a line by itself, followed by another newline
|
|
25
|
+
replacer: jsTsReplacer,
|
|
26
|
+
},
|
|
23
27
|
});
|
|
24
28
|
//# sourceMappingURL=JsTsFileHeaders.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsTsFileHeaders.js","sourceRoot":"","sources":["../../src/policies/JsTsFileHeaders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EACN,sBAAsB,GAEtB,MAAM,6CAA6C,CAAC;AAErD,MAAM,YAAY,GAAG,CACpB,WAAmB,EACnB,EAAE,UAAU,EAA0B,EAC7B,EAAE;IACX,qCAAqC;IACrC,MAAM,SAAS,GACd,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC;QAC3D,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,OAAO,GAAG,OAAO,CAAC;IACtB,MAAM,UAAU,GAAG,MAAM,OAAO,MAAM,UAAU,CAAC,OAAO,CACvD,OAAO,EACP,GAAG,OAAO,KAAK,CACf,GAAG,OAAO,MAAM,SAAS,GAAG,WAAW,EAAE,CAAC;IAC3C,OAAO,UAAU,CAAC;AACnB,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAAC,iBAAiB,EAAE;
|
|
1
|
+
{"version":3,"file":"JsTsFileHeaders.js","sourceRoot":"","sources":["../../src/policies/JsTsFileHeaders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EACN,sBAAsB,GAEtB,MAAM,6CAA6C,CAAC;AAErD,MAAM,YAAY,GAAG,CACpB,WAAmB,EACnB,EAAE,UAAU,EAA0B,EAC7B,EAAE;IACX,qCAAqC;IACrC,MAAM,SAAS,GACd,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC;QAC3D,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,OAAO,GAAG,OAAO,CAAC;IACtB,MAAM,UAAU,GAAG,MAAM,OAAO,MAAM,UAAU,CAAC,OAAO,CACvD,OAAO,EACP,GAAG,OAAO,KAAK,CACf,GAAG,OAAO,MAAM,SAAS,GAAG,WAAW,EAAE,CAAC;IAC3C,OAAO,UAAU,CAAC;AACnB,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAAC;IACrD,IAAI,EAAE,iBAAiB;IACvB,WAAW,EACV,oFAAoF;IACrF,MAAM,EAAE;QACP,KAAK,EAAE,6BAA6B;QACpC,WAAW,EAAE,4BAA4B,EAAE,kDAAkD;QAC7F,SAAS,EAAE,MAAM,EAAE,qCAAqC;QACxD,OAAO,EAAE,OAAO,EAAE,uCAAuC;QACzD,SAAS,EAAE,iBAAiB,EAAE,0EAA0E;QACxG,QAAQ,EAAE,YAAY;KACtB;CACD,CAAC,CAAC","sourcesContent":["import { EOL as newline } from \"node:os\";\nimport {\n\tdefineFileHeaderPolicy,\n\ttype FileHeaderPolicyConfig,\n} from \"../policyDefiners/defineFileHeaderPolicy.js\";\n\nconst jsTsReplacer = (\n\tprevContent: string,\n\t{ headerText }: FileHeaderPolicyConfig,\n): string => {\n\t// prepend header to existing content\n\tconst separator =\n\t\tprevContent.startsWith(\"\\r\") || prevContent.startsWith(\"\\n\")\n\t\t\t? newline\n\t\t\t: newline + newline;\n\tconst newContent = `/*!${newline} * ${headerText.replace(\n\t\tnewline,\n\t\t`${newline} * `,\n\t)}${newline} */${separator}${prevContent}`;\n\treturn newContent;\n};\n\n/**\n * A RepoPolicy that checks that JavaScript and TypeScript source files have the configured header comment\n *\n * @alpha\n */\nexport const JsTsFileHeaders = defineFileHeaderPolicy({\n\tname: \"JsTsFileHeaders\",\n\tdescription:\n\t\t\"Ensures JavaScript and TypeScript source files have the configured header comment.\",\n\tconfig: {\n\t\tmatch: /(^|\\/)[^/]+\\.[mc]?[jt]sx?$/i,\n\t\theaderStart: /(#![^\\n]*\\r?\\n)?\\/\\*!\\r?\\n/, // Begins with optional hashbang followed by '/*!'\n\t\tlineStart: / \\* /, // Subsequent lines begins with ' * '\n\t\tlineEnd: /\\r?\\n/, // Subsequent lines end with CRLF or LF\n\t\theaderEnd: / \\*\\/\\r?\\n\\r?\\n/, // Header ends with ' */' on a line by itself, followed by another newline\n\t\treplacer: jsTsReplacer,\n\t},\n});\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LicenseFileExists.d.ts","sourceRoot":"","sources":["../../src/policies/LicenseFileExists.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAiB,MAAM,cAAc,CAAC;AAEpE;;;;GAIG;AACH,MAAM,WAAW,yBAAyB;IACzC;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAgBD;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,EAAE,gBAAgB,CAAC,yBAAyB,
|
|
1
|
+
{"version":3,"file":"LicenseFileExists.d.ts","sourceRoot":"","sources":["../../src/policies/LicenseFileExists.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAiB,MAAM,cAAc,CAAC;AAEpE;;;;GAIG;AACH,MAAM,WAAW,yBAAyB;IACzC;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAgBD;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,EAAE,gBAAgB,CAAC,yBAAyB,CA8BvE,CAAC"}
|
|
@@ -20,23 +20,27 @@ const DEFAULT_LICENSE_NAMES = [
|
|
|
20
20
|
*
|
|
21
21
|
* @alpha
|
|
22
22
|
*/
|
|
23
|
-
export const LicenseFileExists = makePolicyDefinition(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
23
|
+
export const LicenseFileExists = makePolicyDefinition({
|
|
24
|
+
name: "LicenseFileExists",
|
|
25
|
+
description: "Ensures a LICENSE file exists in the repository root for open source projects.",
|
|
26
|
+
// Only trigger on package.json to check once per repository
|
|
27
|
+
match: /^package\.json$/,
|
|
28
|
+
handler: async ({ root, config }) => {
|
|
29
|
+
const acceptedNames = config?.acceptedNames ?? DEFAULT_LICENSE_NAMES;
|
|
30
|
+
// Check if any of the accepted license files exist in the root
|
|
31
|
+
const hasLicenseFile = acceptedNames.some((name) => existsSync(path.join(root, name)));
|
|
32
|
+
if (hasLicenseFile) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
const result = {
|
|
36
|
+
name: LicenseFileExists.name,
|
|
37
|
+
file: ".", // Report against repository root
|
|
38
|
+
autoFixable: false,
|
|
39
|
+
errorMessages: [
|
|
40
|
+
`No LICENSE file found in repository root. Expected one of: ${acceptedNames.join(", ")}`,
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
return result;
|
|
44
|
+
},
|
|
41
45
|
});
|
|
42
46
|
//# sourceMappingURL=LicenseFileExists.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LicenseFileExists.js","sourceRoot":"","sources":["../../src/policies/LicenseFileExists.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,IAAI,MAAM,OAAO,CAAC;AACzB,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAexD;;GAEG;AACH,MAAM,qBAAqB,GAAG;IAC7B,SAAS;IACT,aAAa;IACb,YAAY;IACZ,aAAa;IACb,SAAS;IACT,aAAa;IACb,YAAY;IACZ,aAAa;CACb,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAC7B,oBAAoB,
|
|
1
|
+
{"version":3,"file":"LicenseFileExists.js","sourceRoot":"","sources":["../../src/policies/LicenseFileExists.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,IAAI,MAAM,OAAO,CAAC;AACzB,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAexD;;GAEG;AACH,MAAM,qBAAqB,GAAG;IAC7B,SAAS;IACT,aAAa;IACb,YAAY;IACZ,aAAa;IACb,SAAS;IACT,aAAa;IACb,YAAY;IACZ,aAAa;CACb,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAC7B,oBAAoB,CAAC;IACpB,IAAI,EAAE,mBAAmB;IACzB,WAAW,EACV,gFAAgF;IACjF,4DAA4D;IAC5D,KAAK,EAAE,iBAAiB;IACxB,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE;QACnC,MAAM,aAAa,GAAG,MAAM,EAAE,aAAa,IAAI,qBAAqB,CAAC;QAErE,+DAA+D;QAC/D,MAAM,cAAc,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAClD,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CACjC,CAAC;QAEF,IAAI,cAAc,EAAE,CAAC;YACpB,OAAO,IAAI,CAAC;QACb,CAAC;QAED,MAAM,MAAM,GAAkB;YAC7B,IAAI,EAAE,iBAAiB,CAAC,IAAI;YAC5B,IAAI,EAAE,GAAG,EAAE,iCAAiC;YAC5C,WAAW,EAAE,KAAK;YAClB,aAAa,EAAE;gBACd,8DAA8D,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;aACxF;SACD,CAAC;QAEF,OAAO,MAAM,CAAC;IACf,CAAC;CACD,CAAC,CAAC","sourcesContent":["import { existsSync } from \"node:fs\";\nimport path from \"pathe\";\nimport { makePolicyDefinition } from \"../makePolicy.js\";\nimport type { PolicyDefinition, PolicyFailure } from \"../policy.js\";\n\n/**\n * Policy settings for the LicenseFileExists repo policy.\n *\n * @alpha\n */\nexport interface LicenseFileExistsSettings {\n\t/**\n\t * Array of acceptable license file names. Defaults to common variations.\n\t */\n\tacceptedNames?: string[];\n}\n\n/**\n * Default license file names to check for.\n */\nconst DEFAULT_LICENSE_NAMES = [\n\t\"LICENSE\",\n\t\"LICENSE.txt\",\n\t\"LICENSE.md\",\n\t\"LICENSE.rst\",\n\t\"LICENCE\",\n\t\"LICENCE.txt\",\n\t\"LICENCE.md\",\n\t\"LICENCE.rst\",\n];\n\n/**\n * A repo policy that checks that a LICENSE file exists in the repository root.\n * This is essential for open source projects to clarify usage rights and obligations.\n *\n * @alpha\n */\nexport const LicenseFileExists: PolicyDefinition<LicenseFileExistsSettings> =\n\tmakePolicyDefinition({\n\t\tname: \"LicenseFileExists\",\n\t\tdescription:\n\t\t\t\"Ensures a LICENSE file exists in the repository root for open source projects.\",\n\t\t// Only trigger on package.json to check once per repository\n\t\tmatch: /^package\\.json$/,\n\t\thandler: async ({ root, config }) => {\n\t\t\tconst acceptedNames = config?.acceptedNames ?? DEFAULT_LICENSE_NAMES;\n\n\t\t\t// Check if any of the accepted license files exist in the root\n\t\t\tconst hasLicenseFile = acceptedNames.some((name) =>\n\t\t\t\texistsSync(path.join(root, name)),\n\t\t\t);\n\n\t\t\tif (hasLicenseFile) {\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\tconst result: PolicyFailure = {\n\t\t\t\tname: LicenseFileExists.name,\n\t\t\t\tfile: \".\", // Report against repository root\n\t\t\t\tautoFixable: false,\n\t\t\t\terrorMessages: [\n\t\t\t\t\t`No LICENSE file found in repository root. Expected one of: ${acceptedNames.join(\", \")}`,\n\t\t\t\t],\n\t\t\t};\n\n\t\t\treturn result;\n\t\t},\n\t});\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for the MutuallyExclusiveScripts policy.
|
|
3
|
+
*
|
|
4
|
+
* @alpha
|
|
5
|
+
*/
|
|
6
|
+
export interface MutuallyExclusiveScriptsConfig {
|
|
7
|
+
/**
|
|
8
|
+
* List of mutually exclusive script groups. Each group is an array of script names
|
|
9
|
+
* where at most one script from the group can be present.
|
|
10
|
+
*
|
|
11
|
+
* Packages can have zero scripts from the group (all optional), but cannot have
|
|
12
|
+
* multiple scripts from the same group.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Allow "test:unit" OR "test:vitest" OR neither, but not both
|
|
17
|
+
* groups: [["test:unit", "test:vitest"]]
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
groups: string[][];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A policy that validates mutually exclusive script groups in package.json.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* Use this policy when you want to enforce that only one script from a group
|
|
27
|
+
* can exist in a package. This is useful for ensuring that packages don't have
|
|
28
|
+
* conflicting scripts (e.g., both "lint:eslint" and "lint:biome").
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { policy } from "repopo";
|
|
33
|
+
* import { MutuallyExclusiveScripts } from "repopo/policies";
|
|
34
|
+
*
|
|
35
|
+
* policy(MutuallyExclusiveScripts, {
|
|
36
|
+
* groups: [
|
|
37
|
+
* ["lint:eslint", "lint:biome"],
|
|
38
|
+
* ["test:jest", "test:vitest"],
|
|
39
|
+
* ],
|
|
40
|
+
* })
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @alpha
|
|
44
|
+
*/
|
|
45
|
+
export declare const MutuallyExclusiveScripts: import("../policy.js").PolicyShape<MutuallyExclusiveScriptsConfig | undefined>;
|
|
46
|
+
//# sourceMappingURL=MutuallyExclusiveScripts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MutuallyExclusiveScripts.d.ts","sourceRoot":"","sources":["../../src/policies/MutuallyExclusiveScripts.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,WAAW,8BAA8B;IAC9C;;;;;;;;;;;;OAYG;IACH,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC;CACnB;AAID;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,wBAAwB,gFAsCnC,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { definePackagePolicy } from "../policyDefiners/definePackagePolicy.js";
|
|
2
|
+
const POLICY_NAME = "MutuallyExclusiveScripts";
|
|
3
|
+
/**
|
|
4
|
+
* A policy that validates mutually exclusive script groups in package.json.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Use this policy when you want to enforce that only one script from a group
|
|
8
|
+
* can exist in a package. This is useful for ensuring that packages don't have
|
|
9
|
+
* conflicting scripts (e.g., both "lint:eslint" and "lint:biome").
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { policy } from "repopo";
|
|
14
|
+
* import { MutuallyExclusiveScripts } from "repopo/policies";
|
|
15
|
+
*
|
|
16
|
+
* policy(MutuallyExclusiveScripts, {
|
|
17
|
+
* groups: [
|
|
18
|
+
* ["lint:eslint", "lint:biome"],
|
|
19
|
+
* ["test:jest", "test:vitest"],
|
|
20
|
+
* ],
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @alpha
|
|
25
|
+
*/
|
|
26
|
+
export const MutuallyExclusiveScripts = definePackagePolicy({
|
|
27
|
+
name: POLICY_NAME,
|
|
28
|
+
description: "Validates that at most one script from each exclusive group exists in package.json.",
|
|
29
|
+
handler: async (json, { file, config }) => {
|
|
30
|
+
if (config === undefined || config.groups.length === 0) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
const scripts = json.scripts ?? {};
|
|
34
|
+
const errors = [];
|
|
35
|
+
for (const group of config.groups) {
|
|
36
|
+
const presentScripts = group.filter((scriptName) => Object.hasOwn(scripts, scriptName));
|
|
37
|
+
if (presentScripts.length > 1) {
|
|
38
|
+
errors.push(`Scripts are mutually exclusive, but found ${presentScripts.length}: ${presentScripts.join(", ")}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (errors.length === 0) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
name: POLICY_NAME,
|
|
46
|
+
file,
|
|
47
|
+
autoFixable: false,
|
|
48
|
+
errorMessages: errors,
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
//# sourceMappingURL=MutuallyExclusiveScripts.js.map
|