-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
104 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ jobs: | |
with: | ||
node-version: "20.x" | ||
- run: npm ci | ||
- run: npm run build:cli | ||
- run: npm run check:ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import path from "node:path"; | ||
import { getTemplatePaths, readJSON, writeJSON } from "./util"; | ||
|
||
export type LintConfig = { | ||
templateDirectory: string; | ||
fix: boolean; | ||
}; | ||
|
||
export function lint(config: LintConfig) { | ||
const templatePaths = getTemplatePaths(config.templateDirectory); | ||
const results = templatePaths.flatMap((templatePath) => | ||
lintTemplate(templatePath, config.fix), | ||
); | ||
if (results.length > 0) { | ||
results.forEach(({ filePath, problems }) => { | ||
console.error(`Problems with ${filePath}`); | ||
problems.forEach((problem) => console.log(` - ${problem}`)); | ||
}); | ||
process.exit(1); | ||
} | ||
} | ||
const RULES = { | ||
"wrangler.json": [lintCompatibilityDate], | ||
}; | ||
const TARGET_COMPATIBILITY_DATE = "2024-11-01"; | ||
|
||
type FileDiagnostic = { | ||
filePath: string; | ||
problems: string[]; | ||
}; | ||
|
||
function lintTemplate(templatePath: string, fix: boolean): FileDiagnostic[] { | ||
return Object.entries(RULES).flatMap(([file, linters]) => { | ||
const filePath = path.join(templatePath, file); | ||
const problems = linters.flatMap((linter) => linter(filePath, fix)); | ||
return problems.length > 0 ? [{ filePath, problems }] : []; | ||
}); | ||
} | ||
|
||
function lintCompatibilityDate(filePath: string, fix: boolean): string[] { | ||
const wrangler = readJSON<{ compatibility_date?: string }>(filePath); | ||
if (wrangler["compatibility_date"] !== TARGET_COMPATIBILITY_DATE && !fix) { | ||
return [ | ||
`"compatibility_date" should be set to "${TARGET_COMPATIBILITY_DATE}"`, | ||
]; | ||
} | ||
wrangler["compatibility_date"] = TARGET_COMPATIBILITY_DATE; | ||
writeJSON(filePath, wrangler); | ||
return []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
const TEMPLATE_DIRECTORY_SUFFIX = "-template"; | ||
|
||
export function getTemplatePaths(templateDirectory: string): string[] { | ||
return fs | ||
.readdirSync(templateDirectory) | ||
.filter( | ||
(file) => | ||
file.endsWith(TEMPLATE_DIRECTORY_SUFFIX) && | ||
fs.statSync(file).isDirectory(), | ||
) | ||
.map((template) => path.join(templateDirectory, template)); | ||
} | ||
|
||
export function readJSON<T = unknown>(filePath: string): T { | ||
return JSON.parse(fs.readFileSync(filePath, { encoding: "utf-8" })); | ||
} | ||
|
||
export function writeJSON<T>(filePath: string, object: T) { | ||
fs.writeFileSync(filePath, JSON.stringify(object, undefined, 2) + "\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters