Skip to content

Commit

Permalink
Add basic template linting to CLI
Browse files Browse the repository at this point in the history
- Start with checking compatibility date
  • Loading branch information
maxwellpeterson committed Nov 21, 2024
1 parent 0f32b6e commit 95324ba
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/branches.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ jobs:
with:
node-version: "20.x"
- run: npm ci
- run: npm run build:cli
- run: npm ci
- run: npm run check:ci
2 changes: 2 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
with:
node-version: "20.x"
- run: npm ci
- run: npm run build:cli
- run: npm ci
- run: npm run check:ci
- run: npm run deploy
- run: npm run upload
17 changes: 16 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Command } from "commander";
import { upload } from "./upload";
import { lint } from "./lint";

const program = new Command();

program.name("cli").description("A handy CLI for developing templates.");
program.name("cli").description("a handy CLI for developing templates");

program
.command("upload")
.description("upload templates to the templates API")
.argument(
"[path-to-templates]",
"path to directory containing templates",
Expand All @@ -16,4 +18,17 @@ program
upload({ templateDirectory, apiBaseUrl: "TODO" }),
);

program
.command("lint")
.description("find and fix template style problems")
.argument(
"[path-to-templates]",
"path to directory containing templates",
".",
)
.option("--fix", "fix problems that can be automatically fixed")
.action((templateDirectory, options) =>
lint({ templateDirectory, fix: options.fix }),
);

program.parse();
50 changes: 50 additions & 0 deletions cli/src/lint.ts
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 [];
}
12 changes: 2 additions & 10 deletions cli/src/upload.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import fs from "node:fs";
import path from "node:path";
import subprocess from "node:child_process";

const TEMPLATE_DIRECTORY_SUFFIX = "-template";
import { getTemplatePaths } from "./util";

export type UploadConfig = {
templateDirectory: string;
apiBaseUrl: string;
};

export async function upload(config: UploadConfig) {
const templatePaths = fs
.readdirSync(config.templateDirectory)
.filter(
(file) =>
file.endsWith(TEMPLATE_DIRECTORY_SUFFIX) &&
fs.statSync(file).isDirectory(),
)
.map((template) => path.join(config.templateDirectory, template));
const templatePaths = getTemplatePaths(config.templateDirectory);
const results = await Promise.allSettled(
templatePaths.map((templatePath) => uploadTemplate(templatePath, config)),
);
Expand Down
23 changes: 23 additions & 0 deletions cli/src/util.ts
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");
}
2 changes: 1 addition & 1 deletion d1-template/wrangler.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compatibility_date": "2024-11-15",
"compatibility_date": "2024-11-01",
"main": "src/index.ts",
"name": "d1-template",
"upload_source_maps": true,
Expand Down
2 changes: 1 addition & 1 deletion image-classification-template/wrangler.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compatibility_date": "2024-11-15",
"compatibility_date": "2024-11-01",
"main": "src/index.ts",
"name": "image-classification-template",
"upload_source_maps": true,
Expand Down
2 changes: 1 addition & 1 deletion llm-template/wrangler.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compatibility_date": "2024-11-15",
"compatibility_date": "2024-11-01",
"main": "src/index.ts",
"name": "llm-template",
"upload_source_maps": true,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
},
"packageManager": "[email protected]",
"scripts": {
"check:ci": "prettier . --check && syncpack lint && turbo run build check types && git diff --exit-code",
"build:cli": "turbo run build --filter=cli",
"check:ci": "cli lint . && prettier . --check && syncpack lint && turbo run build check types && git diff --exit-code",
"deploy": "turbo run deploy",
"fix:ci": "prettier . --write && syncpack format && syncpack fix-mismatches && turbo run types",
"preupload": "turbo run build --filter=cli",
"fix:ci": "cli lint . --fix && prettier . --write && syncpack format && syncpack fix-mismatches && turbo run types",
"upload": "cli upload ."
},
"workspaces": [
Expand Down
2 changes: 1 addition & 1 deletion speech-to-text-template/wrangler.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compatibility_date": "2024-11-15",
"compatibility_date": "2024-11-01",
"main": "src/index.ts",
"name": "speech-to-text-template",
"upload_source_maps": true,
Expand Down
2 changes: 1 addition & 1 deletion text-classification-template/wrangler.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compatibility_date": "2024-11-15",
"compatibility_date": "2024-11-01",
"main": "src/index.ts",
"name": "text-classification-template",
"upload_source_maps": true,
Expand Down
2 changes: 1 addition & 1 deletion text-to-image-template/wrangler.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compatibility_date": "2024-11-15",
"compatibility_date": "2024-11-01",
"main": "src/index.ts",
"name": "text-to-image-template",
"upload_source_maps": true,
Expand Down
2 changes: 1 addition & 1 deletion translation-template/wrangler.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compatibility_date": "2024-11-15",
"compatibility_date": "2024-11-01",
"main": "src/index.ts",
"name": "translation-template",
"upload_source_maps": true,
Expand Down
2 changes: 1 addition & 1 deletion vector-embedding-template/wrangler.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compatibility_date": "2024-11-15",
"compatibility_date": "2024-11-01",
"main": "src/index.ts",
"name": "vector-embedding-template",
"upload_source_maps": true,
Expand Down

0 comments on commit 95324ba

Please sign in to comment.