repopo 0.4.0 → 0.6.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 +79 -0
- package/README.md +40 -23
- package/THIRD-PARTY-LICENSES.txt +21 -11
- package/esm/api.d.ts +2 -0
- package/esm/api.d.ts.map +1 -0
- package/esm/api.js +2 -0
- package/esm/api.js.map +1 -0
- package/esm/baseCommand.d.ts +13 -43
- package/esm/baseCommand.d.ts.map +1 -1
- package/esm/baseCommand.js +31 -67
- package/esm/baseCommand.js.map +1 -1
- package/esm/commands/check.d.ts +17 -12
- package/esm/commands/check.d.ts.map +1 -1
- package/esm/commands/check.js +147 -115
- package/esm/commands/check.js.map +1 -1
- package/esm/commands/list.d.ts +1 -2
- package/esm/commands/list.d.ts.map +1 -1
- package/esm/commands/list.js +5 -6
- package/esm/commands/list.js.map +1 -1
- package/esm/config.d.ts +9 -15
- package/esm/config.d.ts.map +1 -1
- package/esm/config.js +3 -2
- package/esm/config.js.map +1 -1
- package/esm/context.d.ts +29 -0
- package/esm/context.d.ts.map +1 -0
- package/esm/context.js +2 -0
- package/esm/context.js.map +1 -0
- package/esm/generators.d.ts +21 -0
- package/esm/generators.d.ts.map +1 -0
- package/esm/generators.js +2 -0
- package/esm/generators.js.map +1 -0
- package/esm/index.d.ts +5 -4
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +3 -1
- package/esm/index.js.map +1 -1
- package/esm/makePolicy.d.ts +9 -0
- package/esm/makePolicy.d.ts.map +1 -0
- package/esm/makePolicy.js +28 -0
- package/esm/makePolicy.js.map +1 -0
- package/esm/perf.d.ts.map +1 -1
- package/esm/perf.js +1 -5
- package/esm/perf.js.map +1 -1
- package/esm/policies/HtmlFileHeaders.d.ts +8 -0
- package/esm/policies/HtmlFileHeaders.d.ts.map +1 -0
- package/esm/policies/HtmlFileHeaders.js +17 -0
- package/esm/policies/HtmlFileHeaders.js.map +1 -0
- package/esm/policies/JsTsFileHeaders.d.ts +8 -0
- package/esm/policies/JsTsFileHeaders.d.ts.map +1 -0
- package/esm/policies/JsTsFileHeaders.js +24 -0
- package/esm/policies/JsTsFileHeaders.js.map +1 -0
- package/esm/policies/NoJsFileExtensions.d.ts +4 -2
- package/esm/policies/NoJsFileExtensions.d.ts.map +1 -1
- package/esm/policies/NoJsFileExtensions.js +15 -15
- package/esm/policies/NoJsFileExtensions.js.map +1 -1
- package/esm/policies/PackageJsonProperties.d.ts +6 -8
- package/esm/policies/PackageJsonProperties.d.ts.map +1 -1
- package/esm/policies/PackageJsonProperties.js +37 -40
- package/esm/policies/PackageJsonProperties.js.map +1 -1
- package/esm/policies/PackageJsonRepoDirectoryProperty.d.ts +3 -2
- package/esm/policies/PackageJsonRepoDirectoryProperty.d.ts.map +1 -1
- package/esm/policies/PackageJsonRepoDirectoryProperty.js +40 -37
- package/esm/policies/PackageJsonRepoDirectoryProperty.js.map +1 -1
- package/esm/policies/PackageJsonSorted.d.ts +7 -0
- package/esm/policies/PackageJsonSorted.d.ts.map +1 -0
- package/esm/policies/PackageJsonSorted.js +45 -0
- package/esm/policies/PackageJsonSorted.js.map +1 -0
- package/esm/policies/PackageScripts.d.ts +7 -0
- package/esm/policies/PackageScripts.d.ts.map +1 -0
- package/esm/policies/PackageScripts.js +25 -0
- package/esm/policies/PackageScripts.js.map +1 -0
- package/esm/policies/constants.d.ts +5 -0
- package/esm/policies/constants.d.ts.map +1 -0
- package/esm/policies/constants.js +5 -0
- package/esm/policies/constants.js.map +1 -0
- package/esm/policies.d.ts +8 -0
- package/esm/policies.d.ts.map +1 -0
- package/esm/policies.js +8 -0
- package/esm/policies.js.map +1 -0
- package/esm/policy.d.ts +60 -15
- package/esm/policy.d.ts.map +1 -1
- package/esm/policy.js +21 -22
- package/esm/policy.js.map +1 -1
- package/esm/policyDefiners/defineFileHeaderPolicy.d.ts +45 -0
- package/esm/policyDefiners/defineFileHeaderPolicy.d.ts.map +1 -0
- package/esm/policyDefiners/defineFileHeaderPolicy.js +61 -0
- package/esm/policyDefiners/defineFileHeaderPolicy.js.map +1 -0
- package/esm/policyDefiners/definePackagePolicy.d.ts +15 -0
- package/esm/policyDefiners/definePackagePolicy.d.ts.map +1 -0
- package/esm/policyDefiners/definePackagePolicy.js +20 -0
- package/esm/policyDefiners/definePackagePolicy.js.map +1 -0
- package/esm/tsdoc-metadata.json +1 -1
- package/oclif.manifest.json +45 -53
- package/package.json +39 -23
- package/esm/policies/PackageJsonSortedPolicy.d.ts +0 -6
- package/esm/policies/PackageJsonSortedPolicy.d.ts.map +0 -1
- package/esm/policies/PackageJsonSortedPolicy.js +0 -49
- package/esm/policies/PackageJsonSortedPolicy.js.map +0 -1
- package/esm/policies/SortTsconfigs.d.ts +0 -10
- package/esm/policies/SortTsconfigs.d.ts.map +0 -1
- package/esm/policies/SortTsconfigs.js +0 -33
- package/esm/policies/SortTsconfigs.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PackageJsonSorted.js","sourceRoot":"","sources":["../../src/policies/PackageJsonSorted.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAE/E;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,mBAAmB,CACnD,mBAAmB,EACnB,KAAK,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE;IACjC,MAAM,UAAU,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;IACzC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IAErE,IAAI,QAAQ,EAAE,CAAC;QACd,OAAO,IAAI,CAAC;IACb,CAAC;IAED,IAAI,OAAO,EAAE,CAAC;QACb,IAAI,CAAC;YACJ,MAAM,qBAAqB,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;YAClE,MAAM,MAAM,GAAoB;gBAC/B,IAAI,EAAE,iBAAiB,CAAC,IAAI;gBAC5B,IAAI;gBACJ,QAAQ,EAAE,IAAI;aACd,CAAC;YACF,OAAO,MAAM,CAAC;QACf,CAAC;QAAC,OAAO,KAAc,EAAE,CAAC;YACzB,MAAM,MAAM,GAAoB;gBAC/B,IAAI,EAAE,iBAAiB,CAAC,IAAI;gBAC5B,IAAI;gBACJ,QAAQ,EAAE,KAAK;gBACf,WAAW,EAAE,IAAI;gBACjB,YAAY,EAAG,KAAe,CAAC,OAAO;aACtC,CAAC;YACF,OAAO,MAAM,CAAC;QACf,CAAC;IACF,CAAC;SAAM,CAAC;QACP,MAAM,MAAM,GAAkB;YAC7B,IAAI,EAAE,iBAAiB,CAAC,IAAI;YAC5B,IAAI;YACJ,WAAW,EAAE,IAAI;SACjB,CAAC;QACF,OAAO,MAAM,CAAC;IACf,CAAC;AACF,CAAC,CACD,CAAC","sourcesContent":["import { updatePackageJsonFile } from \"@tylerbu/cli-api\";\nimport { sortPackageJson } from \"sort-package-json\";\nimport type { PolicyFailure, PolicyFixResult } from \"../policy.js\";\nimport { definePackagePolicy } from \"../policyDefiners/definePackagePolicy.js\";\n\n/**\n * A repo policy that checks if package.json files in the repo are sorted using sort-package-json.\n *\n * @alpha\n */\nexport const PackageJsonSorted = definePackagePolicy(\n\t\"PackageJsonSorted\",\n\tasync (json, { file, resolve }) => {\n\t\tconst sortedJson = sortPackageJson(json);\n\t\tconst isSorted = JSON.stringify(sortedJson) === JSON.stringify(json);\n\n\t\tif (isSorted) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (resolve) {\n\t\t\ttry {\n\t\t\t\tawait updatePackageJsonFile(file, (json) => json, { sort: true });\n\t\t\t\tconst result: PolicyFixResult = {\n\t\t\t\t\tname: PackageJsonSorted.name,\n\t\t\t\t\tfile,\n\t\t\t\t\tresolved: true,\n\t\t\t\t};\n\t\t\t\treturn result;\n\t\t\t} catch (error: unknown) {\n\t\t\t\tconst result: PolicyFixResult = {\n\t\t\t\t\tname: PackageJsonSorted.name,\n\t\t\t\t\tfile,\n\t\t\t\t\tresolved: false,\n\t\t\t\t\tautoFixable: true,\n\t\t\t\t\terrorMessage: (error as Error).message,\n\t\t\t\t};\n\t\t\t\treturn result;\n\t\t\t}\n\t\t} else {\n\t\t\tconst result: PolicyFailure = {\n\t\t\t\tname: PackageJsonSorted.name,\n\t\t\t\tfile,\n\t\t\t\tautoFixable: true,\n\t\t\t};\n\t\t\treturn result;\n\t\t}\n\t},\n);\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PackageScripts.d.ts","sourceRoot":"","sources":["../../src/policies/PackageScripts.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,eAAO,MAAM,cAAc,oDA+B1B,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { definePackagePolicy } from "../policyDefiners/definePackagePolicy.js";
|
|
2
|
+
const expectedScripts = ["build", "clean"];
|
|
3
|
+
/**
|
|
4
|
+
* A RepoPolicy that checks that package.json properties in packages match expected values.
|
|
5
|
+
*
|
|
6
|
+
* @alpha
|
|
7
|
+
*/
|
|
8
|
+
export const PackageScripts = definePackagePolicy("PackageScripts", async (json, { file }) => {
|
|
9
|
+
const failResult = {
|
|
10
|
+
name: PackageScripts.name,
|
|
11
|
+
file,
|
|
12
|
+
autoFixable: false,
|
|
13
|
+
};
|
|
14
|
+
const hasScriptsField = Object.prototype.hasOwnProperty.call(json, "scripts");
|
|
15
|
+
const missingScripts = [];
|
|
16
|
+
if (hasScriptsField) {
|
|
17
|
+
missingScripts.push(...expectedScripts.filter((script) => !Object.prototype.hasOwnProperty.call(json.scripts, script)));
|
|
18
|
+
}
|
|
19
|
+
if (missingScripts.length > 0) {
|
|
20
|
+
failResult.errorMessage = `missing the following scripts: \n\t${missingScripts.join("\n\t")}`;
|
|
21
|
+
return failResult;
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
});
|
|
25
|
+
//# sourceMappingURL=PackageScripts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PackageScripts.js","sourceRoot":"","sources":["../../src/policies/PackageScripts.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAE/E,MAAM,eAAe,GAAG,CAAC,OAAO,EAAE,OAAO,CAAU,CAAC;AAEpD;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,mBAAmB,CAChD,gBAAgB,EAChB,KAAK,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE;IACxB,MAAM,UAAU,GAAkB;QACjC,IAAI,EAAE,cAAc,CAAC,IAAI;QACzB,IAAI;QACJ,WAAW,EAAE,KAAK;KAClB,CAAC;IAEF,MAAM,eAAe,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAC3D,IAAI,EACJ,SAAS,CACT,CAAC;IAEF,MAAM,cAAc,GAAa,EAAE,CAAC;IACpC,IAAI,eAAe,EAAE,CAAC;QACrB,cAAc,CAAC,IAAI,CAClB,GAAG,eAAe,CAAC,MAAM,CACxB,CAAC,MAAM,EAAE,EAAE,CACV,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAC5D,CACD,CAAC;IACH,CAAC;IAED,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/B,UAAU,CAAC,YAAY,GAAG,sCAAsC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAC9F,OAAO,UAAU,CAAC;IACnB,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC,CACD,CAAC","sourcesContent":["import type { PolicyFailure } from \"../policy.js\";\nimport { definePackagePolicy } from \"../policyDefiners/definePackagePolicy.js\";\n\nconst expectedScripts = [\"build\", \"clean\"] as const;\n\n/**\n * A RepoPolicy that checks that package.json properties in packages match expected values.\n *\n * @alpha\n */\nexport const PackageScripts = definePackagePolicy(\n\t\"PackageScripts\",\n\tasync (json, { file }) => {\n\t\tconst failResult: PolicyFailure = {\n\t\t\tname: PackageScripts.name,\n\t\t\tfile,\n\t\t\tautoFixable: false,\n\t\t};\n\n\t\tconst hasScriptsField = Object.prototype.hasOwnProperty.call(\n\t\t\tjson,\n\t\t\t\"scripts\",\n\t\t);\n\n\t\tconst missingScripts: string[] = [];\n\t\tif (hasScriptsField) {\n\t\t\tmissingScripts.push(\n\t\t\t\t...expectedScripts.filter(\n\t\t\t\t\t(script) =>\n\t\t\t\t\t\t!Object.prototype.hasOwnProperty.call(json.scripts, script),\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\n\t\tif (missingScripts.length > 0) {\n\t\t\tfailResult.errorMessage = `missing the following scripts: \\n\\t${missingScripts.join(\"\\n\\t\")}`;\n\t\t\treturn failResult;\n\t\t}\n\n\t\treturn true;\n\t},\n);\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/policies/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,qBAAqB,QAAyB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/policies/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,sBAAsB,CAAC","sourcesContent":["/**\n * A regular expression that matches package.json files in path strings.\n */\nexport const PackageJsonRegexMatch = /(^|\\/)package\\.json/i;\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { DefaultPolicies } from "./policy.js";
|
|
2
|
+
export { HtmlFileHeaders } from "./policies/HtmlFileHeaders.js";
|
|
3
|
+
export { JsTsFileHeaders } from "./policies/JsTsFileHeaders.js";
|
|
4
|
+
export { NoJsFileExtensions } from "./policies/NoJsFileExtensions.js";
|
|
5
|
+
export { PackageJsonProperties } from "./policies/PackageJsonProperties.js";
|
|
6
|
+
export { PackageJsonRepoDirectoryProperty } from "./policies/PackageJsonRepoDirectoryProperty.js";
|
|
7
|
+
export { PackageJsonSorted } from "./policies/PackageJsonSorted.js";
|
|
8
|
+
//# sourceMappingURL=policies.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policies.d.ts","sourceRoot":"","sources":["../src/policies.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAC;AAC5E,OAAO,EAAE,gCAAgC,EAAE,MAAM,gDAAgD,CAAC;AAClG,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC"}
|
package/esm/policies.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { DefaultPolicies } from "./policy.js";
|
|
2
|
+
export { HtmlFileHeaders } from "./policies/HtmlFileHeaders.js";
|
|
3
|
+
export { JsTsFileHeaders } from "./policies/JsTsFileHeaders.js";
|
|
4
|
+
export { NoJsFileExtensions } from "./policies/NoJsFileExtensions.js";
|
|
5
|
+
export { PackageJsonProperties } from "./policies/PackageJsonProperties.js";
|
|
6
|
+
export { PackageJsonRepoDirectoryProperty } from "./policies/PackageJsonRepoDirectoryProperty.js";
|
|
7
|
+
export { PackageJsonSorted } from "./policies/PackageJsonSorted.js";
|
|
8
|
+
//# sourceMappingURL=policies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policies.js","sourceRoot":"","sources":["../src/policies.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAC;AAC5E,OAAO,EAAE,gCAAgC,EAAE,MAAM,gDAAgD,CAAC;AAClG,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC","sourcesContent":["export { DefaultPolicies } from \"./policy.js\";\n\nexport { HtmlFileHeaders } from \"./policies/HtmlFileHeaders.js\";\nexport { JsTsFileHeaders } from \"./policies/JsTsFileHeaders.js\";\nexport { NoJsFileExtensions } from \"./policies/NoJsFileExtensions.js\";\nexport { PackageJsonProperties } from \"./policies/PackageJsonProperties.js\";\nexport { PackageJsonRepoDirectoryProperty } from \"./policies/PackageJsonRepoDirectoryProperty.js\";\nexport { PackageJsonSorted } from \"./policies/PackageJsonSorted.js\";\n"]}
|
package/esm/policy.d.ts
CHANGED
|
@@ -5,28 +5,44 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export type PolicyName = string;
|
|
7
7
|
/**
|
|
8
|
+
* Arguments passed to policy functions.
|
|
9
|
+
*
|
|
8
10
|
* @alpha
|
|
9
11
|
*/
|
|
10
|
-
export interface PolicyFunctionArguments<C
|
|
12
|
+
export interface PolicyFunctionArguments<C> {
|
|
13
|
+
/**
|
|
14
|
+
* Path to the file, relative to the repo root.
|
|
15
|
+
*/
|
|
11
16
|
file: string;
|
|
17
|
+
/**
|
|
18
|
+
* Absolute path to the root of the repo.
|
|
19
|
+
*/
|
|
12
20
|
root: string;
|
|
21
|
+
/**
|
|
22
|
+
* If true, the handler should resolve any violations automatically if possible.
|
|
23
|
+
*/
|
|
13
24
|
resolve: boolean;
|
|
14
|
-
|
|
25
|
+
/**
|
|
26
|
+
* @remarks
|
|
27
|
+
*
|
|
28
|
+
* Note that the handler function receives the config as an argument.
|
|
29
|
+
*/
|
|
30
|
+
config?: C | undefined;
|
|
15
31
|
}
|
|
16
32
|
/**
|
|
17
33
|
* A policy handler is a function that is called to check policy against a file.
|
|
18
34
|
*
|
|
19
35
|
* @alpha
|
|
20
36
|
*/
|
|
21
|
-
export type PolicyHandler<C = unknown | undefined> = (args: PolicyFunctionArguments<C>) => Promise<
|
|
37
|
+
export type PolicyHandler<C = unknown | undefined> = (args: PolicyFunctionArguments<C>) => Promise<PolicyHandlerResult>;
|
|
22
38
|
/**
|
|
23
39
|
* A standalone function that can be called to resolve a policy failure.
|
|
24
40
|
*
|
|
25
41
|
* @alpha
|
|
26
42
|
*/
|
|
27
|
-
export type PolicyStandaloneResolver<C =
|
|
43
|
+
export type PolicyStandaloneResolver<C = undefined> = (args: Omit<PolicyFunctionArguments<C>, "resolve">) => Promise<PolicyFixResult>;
|
|
28
44
|
/**
|
|
29
|
-
* A
|
|
45
|
+
* A RepoPolicyDefinition checks and applies policies to files in the repository.
|
|
30
46
|
*
|
|
31
47
|
* Each policy has a name and a match regex for matching which files it should apply to. Every file in th repo is
|
|
32
48
|
* enumerated and if it matches the regex for a policy, that policy is applied.
|
|
@@ -38,7 +54,7 @@ export type PolicyStandaloneResolver<C = unknown | undefined> = (args: Omit<Poli
|
|
|
38
54
|
*
|
|
39
55
|
* @alpha
|
|
40
56
|
*/
|
|
41
|
-
export interface
|
|
57
|
+
export interface PolicyDefinition<C = undefined> {
|
|
42
58
|
/**
|
|
43
59
|
* The name of the policy; displayed in UI and used in settings.
|
|
44
60
|
*/
|
|
@@ -46,7 +62,7 @@ export interface RepoPolicy<C = any | undefined> {
|
|
|
46
62
|
/**
|
|
47
63
|
* A more detailed description of the policy and its intended function.
|
|
48
64
|
*/
|
|
49
|
-
description?: string;
|
|
65
|
+
description?: string | undefined;
|
|
50
66
|
/**
|
|
51
67
|
* A regular expression that is used to match files in the repo.
|
|
52
68
|
*/
|
|
@@ -68,15 +84,31 @@ export interface RepoPolicy<C = any | undefined> {
|
|
|
68
84
|
* @returns true if the file passed the policy; otherwise a PolicyFailure object will be returned.
|
|
69
85
|
*/
|
|
70
86
|
resolver?: PolicyStandaloneResolver<C> | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* A default config that will be used if none is provided.
|
|
89
|
+
*/
|
|
90
|
+
defaultConfig?: C | undefined;
|
|
71
91
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
92
|
+
/**
|
|
93
|
+
* @alpha
|
|
94
|
+
*/
|
|
95
|
+
export interface PolicyInstanceSettings<C> {
|
|
96
|
+
/**
|
|
97
|
+
* An array of strings/regular expressions. File paths that match any of these expressions will be completely excluded
|
|
98
|
+
* from policy.
|
|
99
|
+
*
|
|
100
|
+
* Paths will be matched relative to the root of the repo.
|
|
101
|
+
*/
|
|
102
|
+
excludeFiles?: (string | RegExp)[];
|
|
103
|
+
/**
|
|
104
|
+
* The config that is applied to the policy instance.
|
|
105
|
+
*/
|
|
106
|
+
config?: C | undefined;
|
|
79
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* @alpha
|
|
110
|
+
*/
|
|
111
|
+
export type PolicyInstance<C = undefined> = PolicyDefinition<C> & PolicyInstanceSettings<C>;
|
|
80
112
|
/**
|
|
81
113
|
* A policy failure.
|
|
82
114
|
*
|
|
@@ -111,11 +143,24 @@ export interface PolicyFixResult extends PolicyFailure {
|
|
|
111
143
|
*/
|
|
112
144
|
resolved: boolean;
|
|
113
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* @alpha
|
|
148
|
+
*/
|
|
149
|
+
export type PolicyHandlerResult = true | PolicyFailure | PolicyFixResult;
|
|
114
150
|
export declare function isPolicyFixResult(toCheck: any): toCheck is PolicyFixResult;
|
|
115
151
|
/**
|
|
116
152
|
* Default policies included with repopo.
|
|
117
153
|
*
|
|
118
154
|
* @alpha
|
|
119
155
|
*/
|
|
120
|
-
export declare const DefaultPolicies:
|
|
156
|
+
export declare const DefaultPolicies: PolicyDefinition<any>[];
|
|
157
|
+
export declare abstract class Policy<C> implements PolicyDefinition<C> {
|
|
158
|
+
readonly name: string;
|
|
159
|
+
readonly match: RegExp;
|
|
160
|
+
readonly handler: PolicyHandler<C>;
|
|
161
|
+
readonly description?: string | undefined;
|
|
162
|
+
readonly defaultConfig?: C | undefined;
|
|
163
|
+
readonly resolver?: PolicyStandaloneResolver<C> | undefined;
|
|
164
|
+
constructor(name: string, match: RegExp, handler: PolicyHandler<C>, description?: string | undefined, defaultConfig?: C | undefined, resolver?: PolicyStandaloneResolver<C> | undefined);
|
|
165
|
+
}
|
|
121
166
|
//# sourceMappingURL=policy.d.ts.map
|
package/esm/policy.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"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"}
|
package/esm/policy.js
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
1
|
import { NoJsFileExtensions } from "./policies/NoJsFileExtensions.js";
|
|
2
|
-
import { PackageJsonProperties } from "./policies/PackageJsonProperties.js";
|
|
3
2
|
import { PackageJsonRepoDirectoryProperty } from "./policies/PackageJsonRepoDirectoryProperty.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
export class RepoPolicyClass {
|
|
7
|
-
name;
|
|
8
|
-
match;
|
|
9
|
-
handler;
|
|
10
|
-
resolver;
|
|
11
|
-
static createRepoPolicy(name, match, handler, resolver) {
|
|
12
|
-
return new RepoPolicyClass(name, match, handler, resolver);
|
|
13
|
-
}
|
|
14
|
-
constructor(name, match, handler, resolver) {
|
|
15
|
-
this.name = name;
|
|
16
|
-
this.match = match;
|
|
17
|
-
this.handler = handler;
|
|
18
|
-
this.resolver = resolver;
|
|
19
|
-
// empty
|
|
20
|
-
}
|
|
21
|
-
}
|
|
3
|
+
import { PackageJsonSorted } from "./policies/PackageJsonSorted.js";
|
|
4
|
+
import { PackageScripts } from "./policies/PackageScripts.js";
|
|
22
5
|
// biome-ignore lint/suspicious/noExplicitAny: type guard
|
|
23
6
|
export function isPolicyFixResult(toCheck) {
|
|
24
7
|
if (typeof toCheck !== "object") {
|
|
@@ -31,11 +14,27 @@ export function isPolicyFixResult(toCheck) {
|
|
|
31
14
|
*
|
|
32
15
|
* @alpha
|
|
33
16
|
*/
|
|
17
|
+
// biome-ignore lint/suspicious/noExplicitAny: FIXME
|
|
34
18
|
export const DefaultPolicies = [
|
|
35
19
|
NoJsFileExtensions,
|
|
36
20
|
PackageJsonRepoDirectoryProperty,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
SortTsconfigs,
|
|
21
|
+
PackageJsonSorted,
|
|
22
|
+
PackageScripts,
|
|
40
23
|
];
|
|
24
|
+
export class Policy {
|
|
25
|
+
name;
|
|
26
|
+
match;
|
|
27
|
+
handler;
|
|
28
|
+
description;
|
|
29
|
+
defaultConfig;
|
|
30
|
+
resolver;
|
|
31
|
+
constructor(name, match, handler, description, defaultConfig, resolver) {
|
|
32
|
+
this.name = name;
|
|
33
|
+
this.match = match;
|
|
34
|
+
this.handler = handler;
|
|
35
|
+
this.description = description;
|
|
36
|
+
this.defaultConfig = defaultConfig;
|
|
37
|
+
this.resolver = resolver;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
41
40
|
//# sourceMappingURL=policy.js.map
|
package/esm/policy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,gCAAgC,EAAE,MAAM,gDAAgD,CAAC;AAClG,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AA4L9D,yDAAyD;AACzD,MAAM,UAAU,iBAAiB,CAAC,OAAY;IAC7C,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QACjC,OAAO,KAAK,CAAC;IACd,CAAC;IACD,OAAO,UAAU,IAAI,OAAO,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AAEH,oDAAoD;AACpD,MAAM,CAAC,MAAM,eAAe,GAA4B;IACvD,kBAAkB;IAClB,gCAAgC;IAChC,iBAAiB;IACjB,cAAc;CACL,CAAC;AAEX,MAAM,OAAgB,MAAM;IAEV;IACA;IACA;IACA;IACA;IACA;IANjB,YACiB,IAAY,EACZ,KAAa,EACb,OAAyB,EACzB,WAAoB,EACpB,aAAiB,EACjB,QAAsC;QALtC,SAAI,GAAJ,IAAI,CAAQ;QACZ,UAAK,GAAL,KAAK,CAAQ;QACb,YAAO,GAAP,OAAO,CAAkB;QACzB,gBAAW,GAAX,WAAW,CAAS;QACpB,kBAAa,GAAb,aAAa,CAAI;QACjB,aAAQ,GAAR,QAAQ,CAA8B;IACpD,CAAC;CACJ","sourcesContent":["import { NoJsFileExtensions } from \"./policies/NoJsFileExtensions.js\";\nimport { PackageJsonRepoDirectoryProperty } from \"./policies/PackageJsonRepoDirectoryProperty.js\";\nimport { PackageJsonSorted } from \"./policies/PackageJsonSorted.js\";\nimport { PackageScripts } from \"./policies/PackageScripts.js\";\n\n/**\n * A type representing a policy name.\n *\n * @alpha\n */\nexport type PolicyName = string;\n\n/**\n * Arguments passed to policy functions.\n *\n * @alpha\n */\n\nexport interface PolicyFunctionArguments<C> {\n\t/**\n\t * Path to the file, relative to the repo root.\n\t */\n\tfile: string;\n\n\t/**\n\t * Absolute path to the root of the repo.\n\t */\n\troot: string;\n\n\t/**\n\t * If true, the handler should resolve any violations automatically if possible.\n\t */\n\tresolve: boolean;\n\n\t/**\n\t * @remarks\n\t *\n\t * Note that the handler function receives the config as an argument.\n\t */\n\tconfig?: C | undefined;\n}\n\n/**\n * A policy handler is a function that is called to check policy against a file.\n *\n * @alpha\n */\n\nexport type PolicyHandler<C = unknown | undefined> = (\n\targs: PolicyFunctionArguments<C>,\n) => Promise<PolicyHandlerResult>;\n\n// export type PolicyCheckOnly = (\n// \tfile: string,\n// \troot: string,\n// ) => Promise<PolicyHandlerResult>;\n\n/**\n * A standalone function that can be called to resolve a policy failure.\n *\n * @alpha\n */\nexport type PolicyStandaloneResolver<C = undefined> = (\n\targs: Omit<PolicyFunctionArguments<C>, \"resolve\">,\n) => Promise<PolicyFixResult>;\n\n// function isPolicyHandler(input: PolicyHandler | PolicyCheckOnly): input is PolicyHandler\n\n/**\n * A RepoPolicyDefinition checks and applies policies to files in the repository.\n *\n * Each policy has a name and a match regex for matching which files it should apply to. Every file in th repo is\n * enumerated and if it matches the regex for a policy, that policy is applied.\n *\n * Each policy includes a handler function that checks a file against the policy and can optionally resolve any problems\n * (automated resolutions depend on the policy implementation).\n *\n * @typeParam C - type of configuration object used by the policy\n *\n * @alpha\n */\nexport interface PolicyDefinition<C = undefined> {\n\t/**\n\t * The name of the policy; displayed in UI and used in settings.\n\t */\n\tname: PolicyName;\n\n\t/**\n\t * A more detailed description of the policy and its intended function.\n\t */\n\tdescription?: string | undefined;\n\n\t/**\n\t * A regular expression that is used to match files in the repo.\n\t */\n\tmatch: RegExp;\n\n\t/**\n\t * A handler function that checks if a file is compliant with the policy.\n\t *\n\t * @param file - Repo-relative path to the file to check.\n\t * @param root - Absolute path to the root of the repo.\n\t * @param resolve - If true, automated policy fixes will be applied. Not all policies support automated fixes.\n\t * @returns True if the file passed the policy; otherwise a PolicyFailure object will be returned.\n\t */\n\n\thandler: PolicyHandler<C>;\n\n\t/**\n\t * A resolver function that can be used to automatically address the policy violation.\n\t *\n\t * @param file - Repo-relative path to the file to check.\n\t * @param root - Absolute path to the root of the repo.\n\t * @returns true if the file passed the policy; otherwise a PolicyFailure object will be returned.\n\t */\n\tresolver?: PolicyStandaloneResolver<C> | undefined;\n\n\t/**\n\t * A default config that will be used if none is provided.\n\t */\n\tdefaultConfig?: C | undefined;\n}\n\n/**\n * @alpha\n */\nexport interface PolicyInstanceSettings<C> {\n\t/**\n\t * An array of strings/regular expressions. File paths that match any of these expressions will be completely excluded\n\t * from policy.\n\t *\n\t * Paths will be matched relative to the root of the repo.\n\t */\n\texcludeFiles?: (string | RegExp)[];\n\n\t/**\n\t * The config that is applied to the policy instance.\n\t */\n\tconfig?: C | undefined;\n}\n\n/**\n * @alpha\n */\nexport type PolicyInstance<C = undefined> = PolicyDefinition<C> &\n\tPolicyInstanceSettings<C>;\n\n/**\n * A policy failure.\n *\n * @alpha\n */\nexport interface PolicyFailure {\n\t/**\n\t * Name of the policy that failed.\n\t */\n\tname: PolicyName;\n\n\t/**\n\t * Path to the file that failed the policy.\n\t */\n\tfile: string;\n\n\t/**\n\t * Set to `true` if the policy can be fixed automatically.\n\t */\n\tautoFixable?: boolean | undefined;\n\n\t/**\n\t * An optional error message accompanying the failure.\n\t */\n\terrorMessage?: string | undefined;\n}\n\n/**\n * The result of an automatic fix for a failing policy.\n *\n * @alpha\n */\nexport interface PolicyFixResult extends PolicyFailure {\n\t/**\n\t * Set to true if the failure was resolved by the automated fixer.\n\t */\n\tresolved: boolean;\n}\n\n/**\n * @alpha\n */\nexport type PolicyHandlerResult = true | PolicyFailure | PolicyFixResult;\n\n// biome-ignore lint/suspicious/noExplicitAny: type guard\nexport function isPolicyFixResult(toCheck: any): toCheck is PolicyFixResult {\n\tif (typeof toCheck !== \"object\") {\n\t\treturn false;\n\t}\n\treturn \"resolved\" in toCheck;\n}\n\n/**\n * Default policies included with repopo.\n *\n * @alpha\n */\n\n// biome-ignore lint/suspicious/noExplicitAny: FIXME\nexport const DefaultPolicies: PolicyDefinition<any>[] = [\n\tNoJsFileExtensions,\n\tPackageJsonRepoDirectoryProperty,\n\tPackageJsonSorted,\n\tPackageScripts,\n] as const;\n\nexport abstract class Policy<C> implements PolicyDefinition<C> {\n\tpublic constructor(\n\t\tpublic readonly name: string,\n\t\tpublic readonly match: RegExp,\n\t\tpublic readonly handler: PolicyHandler<C>,\n\t\tpublic readonly description?: string,\n\t\tpublic readonly defaultConfig?: C,\n\t\tpublic readonly resolver?: PolicyStandaloneResolver<C>,\n\t) {}\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { PolicyDefinition } from "../policy.js";
|
|
2
|
+
/**
|
|
3
|
+
* @alpha
|
|
4
|
+
*/
|
|
5
|
+
export interface FileHeaderPolicyConfig {
|
|
6
|
+
/**
|
|
7
|
+
* The text to use as the header.
|
|
8
|
+
*/
|
|
9
|
+
headerText: string;
|
|
10
|
+
/**
|
|
11
|
+
* An optional string that will be appended to the headerText.
|
|
12
|
+
*/
|
|
13
|
+
autoGenText?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* @alpha
|
|
17
|
+
*/
|
|
18
|
+
export interface FileHeaderGeneratorConfig extends Partial<FileHeaderPolicyConfig> {
|
|
19
|
+
match: RegExp;
|
|
20
|
+
/**
|
|
21
|
+
* Regex matching header prefix (e.g. `/*!\r?\n`)
|
|
22
|
+
*/
|
|
23
|
+
headerStart?: RegExp;
|
|
24
|
+
/**
|
|
25
|
+
* Regex matching beginning of each line (e.g. ' * ')
|
|
26
|
+
*/
|
|
27
|
+
lineStart: RegExp;
|
|
28
|
+
/**
|
|
29
|
+
* Regex matching the end of each line (e.g., `\r?\n`)
|
|
30
|
+
*/
|
|
31
|
+
lineEnd: RegExp;
|
|
32
|
+
/**
|
|
33
|
+
* Regex matching the header postfix.
|
|
34
|
+
*/
|
|
35
|
+
headerEnd?: RegExp;
|
|
36
|
+
replacer: (content: string, config: FileHeaderPolicyConfig) => string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Given a {@link FileHeaderPolicyConfig}, produces a function that detects correct file headers
|
|
40
|
+
* and returns an error string if the header is missing or incorrect.
|
|
41
|
+
*
|
|
42
|
+
* @alpha
|
|
43
|
+
*/
|
|
44
|
+
export declare function defineFileHeaderPolicy(name: string, config: FileHeaderGeneratorConfig): PolicyDefinition<FileHeaderPolicyConfig>;
|
|
45
|
+
//# sourceMappingURL=defineFileHeaderPolicy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineFileHeaderPolicy.d.ts","sourceRoot":"","sources":["../../src/policyDefiners/defineFileHeaderPolicy.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACX,gBAAgB,EAGhB,MAAM,cAAc,CAAC;AAItB;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACtC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,yBAChB,SAAQ,OAAO,CAAC,sBAAsB,CAAC;IACvC,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,sBAAsB,KAAK,MAAM,CAAC;CACtE;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACrC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,yBAAyB,GAC/B,gBAAgB,CAAC,sBAAsB,CAAC,CAiE1C"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { EOL as newline } from "node:os";
|
|
3
|
+
import { extname } from "pathe";
|
|
4
|
+
const trailingSpaces = /\s*\\r\?\\n/;
|
|
5
|
+
/**
|
|
6
|
+
* Given a {@link FileHeaderPolicyConfig}, produces a function that detects correct file headers
|
|
7
|
+
* and returns an error string if the header is missing or incorrect.
|
|
8
|
+
*
|
|
9
|
+
* @alpha
|
|
10
|
+
*/
|
|
11
|
+
export function defineFileHeaderPolicy(name, config) {
|
|
12
|
+
const pre = config.headerStart?.source ?? "";
|
|
13
|
+
const start = config.lineStart.source;
|
|
14
|
+
const end = config.lineEnd.source;
|
|
15
|
+
const post = config.headerEnd?.source ?? "";
|
|
16
|
+
// Helper which constructs a matching RegExp from a given multiline string.
|
|
17
|
+
// (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping)
|
|
18
|
+
const toRegex = (text) => text
|
|
19
|
+
.split(newline)
|
|
20
|
+
.map((line) => `${start}${line.replace(/[$()*+.?[\\\]^{|}]/g, "\\$&")}${end}`)
|
|
21
|
+
.join("")
|
|
22
|
+
.replace(trailingSpaces, "\\r?\\n"); // Trim trailing spaces at end-of-line.
|
|
23
|
+
// Detection regex matches the header start (if any), followed by lines in 'headerText',
|
|
24
|
+
// optionally followed by lines in the 'autoGenText', and finally the header end (if any).
|
|
25
|
+
const regex = new RegExp(`^${pre}${toRegex(config.headerText ?? "")}(${toRegex(config.autoGenText ?? "")})?${post}`);
|
|
26
|
+
return {
|
|
27
|
+
name,
|
|
28
|
+
match: config.match,
|
|
29
|
+
handler: async ({ file, resolve, config: policyConfig }) => {
|
|
30
|
+
if (policyConfig === undefined) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
const failResult = {
|
|
34
|
+
name,
|
|
35
|
+
file,
|
|
36
|
+
autoFixable: true,
|
|
37
|
+
};
|
|
38
|
+
// TODO: Consider reading only the first 512B or so since headers are typically
|
|
39
|
+
// at the beginning of the file.
|
|
40
|
+
const content = await readFile(file, { encoding: "utf8" });
|
|
41
|
+
const failed = !regex.test(content);
|
|
42
|
+
if (failed) {
|
|
43
|
+
failResult.errorMessage = `${extname(file)} file missing header`;
|
|
44
|
+
}
|
|
45
|
+
if (failed) {
|
|
46
|
+
if (resolve) {
|
|
47
|
+
const newContent = config.replacer(content, policyConfig);
|
|
48
|
+
await writeFile(file, newContent);
|
|
49
|
+
const fixResult = {
|
|
50
|
+
...failResult,
|
|
51
|
+
resolved: true,
|
|
52
|
+
};
|
|
53
|
+
return fixResult;
|
|
54
|
+
}
|
|
55
|
+
return failResult;
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=defineFileHeaderPolicy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineFileHeaderPolicy.js","sourceRoot":"","sources":["../../src/policyDefiners/defineFileHeaderPolicy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAOhC,MAAM,cAAc,GAAG,aAAa,CAAC;AA+CrC;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CACrC,IAAY,EACZ,MAAiC;IAEjC,MAAM,GAAG,GAAG,MAAM,CAAC,WAAW,EAAE,MAAM,IAAI,EAAE,CAAC;IAC7C,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC;IACtC,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;IAClC,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,EAAE,MAAM,IAAI,EAAE,CAAC;IAE5C,2EAA2E;IAC3E,mGAAmG;IACnG,MAAM,OAAO,GAAG,CAAC,IAAY,EAAU,EAAE,CACxC,IAAI;SACF,KAAK,CAAC,OAAO,CAAC;SACd,GAAG,CACH,CAAC,IAAI,EAAE,EAAE,CACR,GAAG,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,GAAG,GAAG,EAAE,CAC/D;SACA,IAAI,CAAC,EAAE,CAAC;SACR,OAAO,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC,CAAC,uCAAuC;IAE9E,wFAAwF;IACxF,0FAA0F;IAC1F,MAAM,KAAK,GAAG,IAAI,MAAM,CACvB,IAAI,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,WAAW,IAAI,EAAE,CAAC,KAAK,IAAI,EAAE,CAC1F,CAAC;IAEF,OAAO;QACN,IAAI;QACJ,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE;YAC1D,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;gBAChC,OAAO,IAAI,CAAC;YACb,CAAC;YAED,MAAM,UAAU,GAAkB;gBACjC,IAAI;gBACJ,IAAI;gBACJ,WAAW,EAAE,IAAI;aACjB,CAAC;YAEF,+EAA+E;YAC/E,gCAAgC;YAChC,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;YAC3D,MAAM,MAAM,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAEpC,IAAI,MAAM,EAAE,CAAC;gBACZ,UAAU,CAAC,YAAY,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,sBAAsB,CAAC;YAClE,CAAC;YAED,IAAI,MAAM,EAAE,CAAC;gBACZ,IAAI,OAAO,EAAE,CAAC;oBACb,MAAM,UAAU,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;oBAC1D,MAAM,SAAS,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;oBAElC,MAAM,SAAS,GAAoB;wBAClC,GAAG,UAAU;wBACb,QAAQ,EAAE,IAAI;qBACd,CAAC;oBAEF,OAAO,SAAS,CAAC;gBAClB,CAAC;gBACD,OAAO,UAAU,CAAC;YACnB,CAAC;YAED,OAAO,IAAI,CAAC;QACb,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { readFile, writeFile } from \"node:fs/promises\";\nimport { EOL as newline } from \"node:os\";\nimport { extname } from \"pathe\";\nimport type {\n\tPolicyDefinition,\n\tPolicyFailure,\n\tPolicyFixResult,\n} from \"../policy.js\";\n\nconst trailingSpaces = /\\s*\\\\r\\?\\\\n/;\n\n/**\n * @alpha\n */\nexport interface FileHeaderPolicyConfig {\n\t/**\n\t * The text to use as the header.\n\t */\n\theaderText: string;\n\n\t/**\n\t * An optional string that will be appended to the headerText.\n\t */\n\tautoGenText?: string;\n}\n\n/**\n * @alpha\n */\nexport interface FileHeaderGeneratorConfig\n\textends Partial<FileHeaderPolicyConfig> {\n\tmatch: RegExp;\n\n\t/**\n\t * Regex matching header prefix (e.g. `/*!\\r?\\n`)\n\t */\n\theaderStart?: RegExp;\n\n\t/**\n\t * Regex matching beginning of each line (e.g. ' * ')\n\t */\n\tlineStart: RegExp;\n\n\t/**\n\t * Regex matching the end of each line (e.g., `\\r?\\n`)\n\t */\n\tlineEnd: RegExp;\n\n\t/**\n\t * Regex matching the header postfix.\n\t */\n\theaderEnd?: RegExp;\n\n\treplacer: (content: string, config: FileHeaderPolicyConfig) => string;\n}\n\n/**\n * Given a {@link FileHeaderPolicyConfig}, produces a function that detects correct file headers\n * and returns an error string if the header is missing or incorrect.\n *\n * @alpha\n */\nexport function defineFileHeaderPolicy(\n\tname: string,\n\tconfig: FileHeaderGeneratorConfig,\n): PolicyDefinition<FileHeaderPolicyConfig> {\n\tconst pre = config.headerStart?.source ?? \"\";\n\tconst start = config.lineStart.source;\n\tconst end = config.lineEnd.source;\n\tconst post = config.headerEnd?.source ?? \"\";\n\n\t// Helper which constructs a matching RegExp from a given multiline string.\n\t// (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping)\n\tconst toRegex = (text: string): string =>\n\t\ttext\n\t\t\t.split(newline)\n\t\t\t.map(\n\t\t\t\t(line) =>\n\t\t\t\t\t`${start}${line.replace(/[$()*+.?[\\\\\\]^{|}]/g, \"\\\\$&\")}${end}`,\n\t\t\t)\n\t\t\t.join(\"\")\n\t\t\t.replace(trailingSpaces, \"\\\\r?\\\\n\"); // Trim trailing spaces at end-of-line.\n\n\t// Detection regex matches the header start (if any), followed by lines in 'headerText',\n\t// optionally followed by lines in the 'autoGenText', and finally the header end (if any).\n\tconst regex = new RegExp(\n\t\t`^${pre}${toRegex(config.headerText ?? \"\")}(${toRegex(config.autoGenText ?? \"\")})?${post}`,\n\t);\n\n\treturn {\n\t\tname,\n\t\tmatch: config.match,\n\t\thandler: async ({ file, resolve, config: policyConfig }) => {\n\t\t\tif (policyConfig === undefined) {\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\tconst failResult: PolicyFailure = {\n\t\t\t\tname,\n\t\t\t\tfile,\n\t\t\t\tautoFixable: true,\n\t\t\t};\n\n\t\t\t// TODO: Consider reading only the first 512B or so since headers are typically\n\t\t\t// at the beginning of the file.\n\t\t\tconst content = await readFile(file, { encoding: \"utf8\" });\n\t\t\tconst failed = !regex.test(content);\n\n\t\t\tif (failed) {\n\t\t\t\tfailResult.errorMessage = `${extname(file)} file missing header`;\n\t\t\t}\n\n\t\t\tif (failed) {\n\t\t\t\tif (resolve) {\n\t\t\t\t\tconst newContent = config.replacer(content, policyConfig);\n\t\t\t\t\tawait writeFile(file, newContent);\n\n\t\t\t\t\tconst fixResult: PolicyFixResult = {\n\t\t\t\t\t\t...failResult,\n\t\t\t\t\t\tresolved: true,\n\t\t\t\t\t};\n\n\t\t\t\t\treturn fixResult;\n\t\t\t\t}\n\t\t\t\treturn failResult;\n\t\t\t}\n\n\t\t\treturn true;\n\t\t},\n\t};\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PackageJson } from "type-fest";
|
|
2
|
+
import type { PolicyDefinition, PolicyFunctionArguments, PolicyHandlerResult } from "../policy.js";
|
|
3
|
+
/**
|
|
4
|
+
* A policy handler especially for policies that target package.json.
|
|
5
|
+
*
|
|
6
|
+
* @alpha
|
|
7
|
+
*/
|
|
8
|
+
export type PackageJsonHandler<J, C> = (json: J, args: PolicyFunctionArguments<C>) => Promise<PolicyHandlerResult>;
|
|
9
|
+
/**
|
|
10
|
+
* Define a repo policy for package.json files.
|
|
11
|
+
*
|
|
12
|
+
* @alpha
|
|
13
|
+
*/
|
|
14
|
+
export declare function definePackagePolicy<J = PackageJson, C = undefined>(name: string, packagePolicy: PackageJsonHandler<J, C>): PolicyDefinition<C>;
|
|
15
|
+
//# sourceMappingURL=definePackagePolicy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definePackagePolicy.d.ts","sourceRoot":"","sources":["../../src/policyDefiners/definePackagePolicy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAE7C,OAAO,KAAK,EACX,gBAAgB,EAChB,uBAAuB,EACvB,mBAAmB,EACnB,MAAM,cAAc,CAAC;AAItB;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,CAAC,CAAC,EAAE,CAAC,IAAI,CACtC,IAAI,EAAE,CAAC,EACP,IAAI,EAAE,uBAAuB,CAAC,CAAC,CAAC,KAC5B,OAAO,CAAC,mBAAmB,CAAC,CAAC;AAElC;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,GAAG,WAAW,EAAE,CAAC,GAAG,SAAS,EACjE,IAAI,EAAE,MAAM,EACZ,aAAa,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,GAErC,gBAAgB,CAAC,CAAC,CAAC,CAUrB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import jsonfile from "jsonfile";
|
|
2
|
+
import { PackageJsonRegexMatch } from "../policies/constants.js";
|
|
3
|
+
const { readFile: readJson } = jsonfile;
|
|
4
|
+
/**
|
|
5
|
+
* Define a repo policy for package.json files.
|
|
6
|
+
*
|
|
7
|
+
* @alpha
|
|
8
|
+
*/
|
|
9
|
+
export function definePackagePolicy(name, packagePolicy) {
|
|
10
|
+
// const func = () => handler(json, args);
|
|
11
|
+
return {
|
|
12
|
+
name,
|
|
13
|
+
match: PackageJsonRegexMatch,
|
|
14
|
+
handler: async (innerArgs) => {
|
|
15
|
+
const json = await readJson(innerArgs.file);
|
|
16
|
+
return packagePolicy(json, innerArgs);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=definePackagePolicy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definePackagePolicy.js","sourceRoot":"","sources":["../../src/policyDefiners/definePackagePolicy.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AAOjE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC;AAYxC;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAClC,IAAY,EACZ,aAAuC;IAGvC,0CAA0C;IAC1C,OAAO;QACN,IAAI;QACJ,KAAK,EAAE,qBAAqB;QAC5B,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE;YAC5B,MAAM,IAAI,GAAM,MAAM,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;YAC/C,OAAO,aAAa,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QACvC,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import jsonfile from \"jsonfile\";\nimport type { PackageJson } from \"type-fest\";\nimport { PackageJsonRegexMatch } from \"../policies/constants.js\";\nimport type {\n\tPolicyDefinition,\n\tPolicyFunctionArguments,\n\tPolicyHandlerResult,\n} from \"../policy.js\";\n\nconst { readFile: readJson } = jsonfile;\n\n/**\n * A policy handler especially for policies that target package.json.\n *\n * @alpha\n */\nexport type PackageJsonHandler<J, C> = (\n\tjson: J,\n\targs: PolicyFunctionArguments<C>,\n) => Promise<PolicyHandlerResult>;\n\n/**\n * Define a repo policy for package.json files.\n *\n * @alpha\n */\nexport function definePackagePolicy<J = PackageJson, C = undefined>(\n\tname: string,\n\tpackagePolicy: PackageJsonHandler<J, C>,\n\t// args: PolicyFunctionArguments<C>,\n): PolicyDefinition<C> {\n\t// const func = () => handler(json, args);\n\treturn {\n\t\tname,\n\t\tmatch: PackageJsonRegexMatch,\n\t\thandler: async (innerArgs) => {\n\t\t\tconst json: J = await readJson(innerArgs.file);\n\t\t\treturn packagePolicy(json, innerArgs);\n\t\t},\n\t};\n}\n"]}
|