aboutsummaryrefslogtreecommitdiff
path: root/packages/iku/src/cli.ts
blob: 3feddc320795cac201e87dcae43beb7630a5e53c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env node

import { existsSync } from "node:fs";
import { dirname, join } from "node:path";
import { type CheckerOptions, checkDirectory } from "./checker.js";

function findMonorepoRoot(startDirectory: string): string | null {
	let current = startDirectory;

	while (current !== dirname(current)) {
		const packagesPath = join(current, "packages");
		const packageJsonPath = join(current, "package.json");

		if (existsSync(packagesPath) && existsSync(packageJsonPath)) {
			return current;
		}

		current = dirname(current);
	}

	return null;
}

function parseArguments(arguments_: string[]): {
	directory?: string;
	options: CheckerOptions;
} {
	const options: CheckerOptions = {
		noComments: true,
	};
	let directory: string | undefined;

	for (const argument of arguments_) {
		if (argument === "--allow-comments") {
			options.noComments = false;
		} else if (!argument.startsWith("-")) {
			directory = argument;
		}
	}

	return { directory, options };
}

const monorepoRoot = findMonorepoRoot(process.cwd());

if (!monorepoRoot) {
	console.error("Could not find monorepo root");
	process.exit(1);
}

const { directory, options } = parseArguments(process.argv.slice(2));
const targetDirectory = directory ?? join(monorepoRoot, "packages");
const { errors, hasErrors } = checkDirectory(targetDirectory, options);

for (const error of errors) {
	console.error(error);
}

if (hasErrors) {
	process.exit(1);
} else {
	console.log("Iku formatting check passed");
}