Skip to content

Commit

Permalink
feat: add lint & format commands
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Mar 6, 2024
1 parent 1ee9c4c commit 0485af5
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
25 changes: 25 additions & 0 deletions src/cli/commands/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Command } from "../deps.ts";
import { GlobalOpts, initProject } from "../common.ts";
import { listSourceFiles } from "../../project/mod.ts";

export const formatCommand = new Command<GlobalOpts>()
.option("--check, -c", "Check if files are formatted")
.action(
async (opts) => {
const project = await initProject(opts);

const sourceFiles = await listSourceFiles(project, { localOnly: true });

const cmd = await new Deno.Command("deno", {
args: [
"fmt",
...opts.check ? ["--check"] : [],
...sourceFiles,
],
stdout: "inherit",
stderr: "inherit",
})
.output();
if (!cmd.success) throw new Error("Check failed");
},
);
24 changes: 24 additions & 0 deletions src/cli/commands/lint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Command } from "../deps.ts";
import { GlobalOpts, initProject } from "../common.ts";
import { listSourceFiles } from "../../project/mod.ts";

export const lintCommand = new Command<GlobalOpts>()
.action(
async (opts) => {
const project = await initProject(opts);

const sourceFiles = await listSourceFiles(project, { localOnly: true });
console.log(sourceFiles);

const cmd = await new Deno.Command("deno", {
args: [
"lint",
...sourceFiles,
],
stdout: "inherit",
stderr: "inherit",
})
.output();
if (!cmd.success) throw new Error("Link failed");
},
);
6 changes: 5 additions & 1 deletion src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import { dbCommand } from "./commands/db.ts";
import { testCommand } from "./commands/test.ts";
import { sdkCommand } from "./commands/sdk.ts";
import { createCommand } from "./commands/create.ts";
import { lintCommand } from "./commands/lint.ts";
import { formatCommand } from "./commands/format.ts";

const command = await new Command();
const command = new Command();
command.action(() => command.showHelp())
.globalOption("-p, --path <path>", "Path to project root")
.command("start", startCommand)
.command("test", testCommand)
.command("db", dbCommand)
.command("build", buildCommand)
.command("lint", lintCommand)
.command("format, fmt", formatCommand)
.command("sdk", sdkCommand)
.command("create", createCommand)
.command("help", new HelpCommand().global())
Expand Down
31 changes: 30 additions & 1 deletion src/project/project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname, exists, join, copy } from "../deps.ts";
import { bold, brightRed } from "./deps.ts";
import { glob, bold, brightRed } from "./deps.ts";
import { readConfig as readProjectConfig } from "../config/project.ts";
import { ProjectConfig } from "../config/project.ts";
import { loadModule, Module } from "./module.ts";
Expand Down Expand Up @@ -168,3 +168,32 @@ export function genRuntimePath(project: Project): string {
export function genRuntimeModPath(project: Project): string {
return join(project.path, "_gen", "runtime", "src", "runtime", "mod.ts");
}

export interface ListSourceFileOpts {
/**
* Only include local files.
*
* Useful for commands that are only relevant to modules written by the user.
*/
localOnly?: boolean;
}

/**
* List all paths to TypeScript files in the project.
*/
export async function listSourceFiles(
project: Project,
opts: ListSourceFileOpts = {},
): Promise<string[]> {
const files: string[] = [];
for (const module of project.modules.values()) {
// Skip non-local files
if (opts.localOnly && !("local" in module.registry.config)) continue;

const moduleFiles =
(await glob.glob("**/*.ts", { cwd: module.path, ignore: "_gen/**" }))
.map((x) => join(module.path, x));
files.push(...moduleFiles);
}
return files;
}
38 changes: 19 additions & 19 deletions tests/test_project/deno.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"lint": {
"include": [
"src/"
],
"exclude": [
"tests/"
],
"rules": {
"exclude": [
"no-empty-interface",
"no-explicit-any",
"require-await"
]
}
},
"fmt": {
"useTabs": true
}
}
"lint": {
"include": [
"src/"
],
"exclude": [
"tests/"
],
"rules": {
"exclude": [
"no-empty-interface",
"no-explicit-any",
"require-await"
]
}
},
"fmt": {
"useTabs": true
}
}

0 comments on commit 0485af5

Please sign in to comment.