@aklinker1/check 1.2.0 → 1.3.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/dist/cli.mjs CHANGED
@@ -20,6 +20,11 @@ const main = defineCommand({
20
20
  type: "boolean",
21
21
  alias: "d",
22
22
  description: "Enable debug logs"
23
+ },
24
+ binDir: {
25
+ type: "string",
26
+ alias: "b",
27
+ description: "Directory where binaries are located"
23
28
  }
24
29
  },
25
30
  async run(ctx) {
package/dist/index.mjs CHANGED
@@ -5,12 +5,17 @@ import { createTaskList } from "./tasklist/index.mjs";
5
5
  import { relative, resolve, sep } from "node:path";
6
6
  import { isCI } from "ci-info";
7
7
  export async function check(options = {}) {
8
- const { debug: debug2, fix = !isCI, root } = options;
8
+ const {
9
+ debug: debug2,
10
+ fix = !isCI,
11
+ root = process.cwd(),
12
+ binDir = "node_modules/.bin"
13
+ } = options;
9
14
  if (debug2) {
10
15
  process.env.DEBUG = "true";
11
16
  }
12
17
  console.log();
13
- const tools = await findInstalledTools(root);
18
+ const tools = await findInstalledTools({ root, binDir });
14
19
  if (tools.length === 0) {
15
20
  console.log("No tools detected! Run with --debug for more info");
16
21
  console.log();
@@ -21,7 +26,7 @@ export async function check(options = {}) {
21
26
  async ({ input: tool, fail, succeed, warn }) => {
22
27
  const startTime = performance.now();
23
28
  const fn = fix ? tool.fix ?? tool.check : tool.check;
24
- const problems2 = await fn(root);
29
+ const problems2 = await fn();
25
30
  problems2.forEach((problem) => {
26
31
  problem.file = resolve(root ?? process.cwd(), problem.file);
27
32
  });
@@ -80,13 +85,11 @@ export async function check(options = {}) {
80
85
  console.log();
81
86
  process.exit(problems.length);
82
87
  }
83
- async function findInstalledTools(root) {
84
- const status = await p(ALL_TOOLS).map(async (tool) => {
85
- const t = typeof tool === "function" ? await tool(root) : tool;
86
- return {
87
- tool: t,
88
- isInstalled: await t.isInstalled(root)
89
- };
88
+ async function findInstalledTools(opts) {
89
+ const status = await p(ALL_TOOLS).map(async (def) => {
90
+ const tool = await def(opts);
91
+ const isInstalled = await tool.isInstalled();
92
+ return { tool, isInstalled };
90
93
  }).promise;
91
94
  if (isDebug()) {
92
95
  const getTools = (isInstalled) => status.filter((item) => item.isInstalled === isInstalled).map((item) => item.tool.name).join(", ");
@@ -1,3 +1,3 @@
1
- import type { OutputParser, Tool } from "../types";
2
- export declare const eslint: Tool;
1
+ import type { OutputParser, ToolDefinition } from "../types";
2
+ export declare const eslint: ToolDefinition;
3
3
  export declare const parseOuptut: OutputParser;
@@ -1,19 +1,23 @@
1
1
  import { isBinInstalled, execAndParse } from "../utils.mjs";
2
- const bin = "node_modules/.bin/eslint";
3
- const args = [
4
- ".",
5
- "--ext",
6
- ".js,.ts,.jsx,.tsx,.mjs,.mts,.cjs,.cts,.vue",
7
- "--format",
8
- "compact",
9
- "--max-warnings",
10
- "0"
11
- ];
12
- export const eslint = {
13
- name: "ESLint",
14
- isInstalled: (root) => isBinInstalled(bin, root),
15
- check: (root) => execAndParse(root, bin, args, parseOuptut),
16
- fix: (root) => execAndParse(root, bin, [...args, "--fix"], parseOuptut)
2
+ import { resolve } from "node:path";
3
+ export const eslint = ({ binDir, root }) => {
4
+ const bin = resolve(root, binDir, "eslint");
5
+ const checkArgs = [
6
+ ".",
7
+ "--ext",
8
+ ".js,.ts,.jsx,.tsx,.mjs,.mts,.cjs,.cts,.vue",
9
+ "--format",
10
+ "compact",
11
+ "--max-warnings",
12
+ "0"
13
+ ];
14
+ const fixArgs = [...checkArgs, "--fix"];
15
+ return {
16
+ name: "ESLint",
17
+ isInstalled: () => isBinInstalled(bin),
18
+ check: () => execAndParse(bin, checkArgs, root, parseOuptut),
19
+ fix: () => execAndParse(bin, fixArgs, root, parseOuptut)
20
+ };
17
21
  };
18
22
  export const parseOuptut = ({ stdout, stderr }) => {
19
23
  return `${stdout}
@@ -1 +1 @@
1
- export declare const ALL_TOOLS: (import("..").Tool | ((root: string | undefined) => Promise<import("..").Tool>))[];
1
+ export declare const ALL_TOOLS: import("..").ToolDefinition[];
@@ -1,3 +1,3 @@
1
- import type { Tool, OutputParser } from "../types";
2
- export declare const prettier: Tool;
1
+ import type { OutputParser, ToolDefinition } from "../types";
2
+ export declare const prettier: ToolDefinition;
3
3
  export declare const parseOuptut: OutputParser;
@@ -1,12 +1,15 @@
1
1
  import { execAndParse, isBinInstalled } from "../utils.mjs";
2
- const bin = "node_modules/.bin/prettier";
3
- const checkArgs = [".", "--list-different"];
4
- const fixArgs = [".", "-w"];
5
- export const prettier = {
6
- name: "Prettier",
7
- isInstalled: (root) => isBinInstalled(bin, root),
8
- check: (root) => execAndParse(root, bin, checkArgs, parseOuptut),
9
- fix: (root) => execAndParse(root, bin, fixArgs, parseOuptut)
2
+ import { resolve } from "node:path";
3
+ export const prettier = ({ binDir, root }) => {
4
+ const bin = resolve(root, binDir, "prettier");
5
+ const checkArgs = [".", "--list-different"];
6
+ const fixArgs = [".", "-w"];
7
+ return {
8
+ name: "Prettier",
9
+ isInstalled: () => isBinInstalled(bin),
10
+ check: () => execAndParse(bin, checkArgs, root, parseOuptut),
11
+ fix: () => execAndParse(bin, fixArgs, root, parseOuptut)
12
+ };
10
13
  };
11
14
  export const parseOuptut = ({ stdout, stderr }) => {
12
15
  if (stderr.trim()) {
@@ -1,3 +1,3 @@
1
- import type { OutputParser, Tool } from "../types";
2
- export declare const publint: Tool;
1
+ import type { OutputParser, ToolDefinition } from "../types";
2
+ export declare const publint: ToolDefinition;
3
3
  export declare const parseOuptut: OutputParser;
@@ -1,10 +1,15 @@
1
1
  import { isBinInstalled, execAndParse } from "../utils.mjs";
2
- const bin = "node_modules/.bin/publint";
2
+ import { resolve } from "node:path";
3
+ const bin = "publint";
3
4
  const args = [];
4
- export const publint = {
5
- name: "Publint",
6
- isInstalled: (root) => isBinInstalled(bin, root),
7
- check: (root) => execAndParse(root, bin, args, parseOuptut)
5
+ export const publint = ({ binDir, root }) => {
6
+ const bin2 = resolve(root, binDir, "publint");
7
+ const args2 = [];
8
+ return {
9
+ name: "Publint",
10
+ isInstalled: () => isBinInstalled(bin2),
11
+ check: () => execAndParse(bin2, args2, root, parseOuptut)
12
+ };
8
13
  };
9
14
  export const parseOuptut = ({ stdout }) => {
10
15
  let kind = "warning";
@@ -1,3 +1,3 @@
1
- import type { Tool, OutputParser } from "../types";
2
- export declare const typescript: (root: string | undefined) => Promise<Tool>;
1
+ import type { OutputParser, ToolDefinition } from "../types";
2
+ export declare const typescript: ToolDefinition;
3
3
  export declare const parseOuptut: OutputParser;
@@ -1,23 +1,27 @@
1
1
  import { debug, execAndParse, isBinInstalled } from "../utils.mjs";
2
- const tsc = {
3
- bin: "node_modules/.bin/tsc",
4
- args: ["--noEmit", "--pretty", "false"]
5
- };
6
- const vueTsc = {
7
- bin: "node_modules/.bin/vue-tsc",
8
- args: ["--noEmit", "--pretty", "false"]
9
- };
10
- export const typescript = async (root) => {
11
- const isVueTsc = await isBinInstalled(vueTsc.bin, root);
2
+ import { resolve } from "node:path";
3
+ export const typescript = async ({
4
+ root,
5
+ binDir
6
+ }) => {
7
+ const tsc = {
8
+ bin: resolve(root, binDir, "tsc"),
9
+ args: ["--noEmit", "--pretty", "false"]
10
+ };
11
+ const vueTsc = {
12
+ bin: resolve(root, binDir, "vue-tsc"),
13
+ args: ["--noEmit", "--pretty", "false"]
14
+ };
15
+ const isVueTsc = await isBinInstalled(vueTsc.bin);
12
16
  debug("TypeScript: Is vue-tsc installed? " + isVueTsc);
13
17
  const cmd = isVueTsc ? vueTsc : tsc;
14
18
  const name = isVueTsc ? "TypeScript (Vue)" : "Typescript";
15
19
  return {
16
20
  name,
17
21
  // Always check if tsc is installed
18
- isInstalled: (root2) => isBinInstalled(tsc.bin, root2),
22
+ isInstalled: () => isBinInstalled(tsc.bin),
19
23
  // Execute the other TSC binary if necessary
20
- check: async (root2) => execAndParse(root2, cmd.bin, cmd.args, parseOuptut)
24
+ check: async () => execAndParse(cmd.bin, cmd.args, root, parseOuptut)
21
25
  };
22
26
  };
23
27
  export const parseOuptut = ({ stdout }) => {
package/dist/types.d.ts CHANGED
@@ -13,7 +13,12 @@ export interface CheckOptions {
13
13
  * Set to true to enable debug logs.
14
14
  */
15
15
  debug?: boolean;
16
+ binDir?: string;
16
17
  }
18
+ export type ToolDefinition = (opts: {
19
+ root: string;
20
+ binDir: string;
21
+ }) => Promise<Tool> | Tool;
17
22
  export interface Tool {
18
23
  /**
19
24
  * Name of tool shown in the console output.
@@ -22,15 +27,15 @@ export interface Tool {
22
27
  /**
23
28
  * Check if the tool is installed.
24
29
  */
25
- isInstalled: (root: string | undefined) => Promise<boolean>;
30
+ isInstalled: () => Promise<boolean>;
26
31
  /**
27
32
  * Run the tool, only checking for problems.
28
33
  */
29
- check: (root: string | undefined) => Promise<Problem[]>;
34
+ check: () => Promise<Problem[]>;
30
35
  /**
31
36
  * Run the tool, but fix problems if possible. If the tool doesn't support fixing problems, `check` will be called instead.
32
37
  */
33
- fix?: (root: string | undefined) => Promise<Problem[]>;
38
+ fix?: () => Promise<Problem[]>;
34
39
  }
35
40
  export interface Problem {
36
41
  location?: CodeLocation;
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { OutputParser, Problem } from "./types";
2
- export declare function isBinInstalled(bin: string, root?: string): Promise<boolean>;
3
- export declare function execAndParse(root: string | undefined, bin: string, args: string[], parser: OutputParser): Promise<Problem[]>;
2
+ export declare function isBinInstalled(bin: string): Promise<boolean>;
3
+ export declare function execAndParse(bin: string, args: string[], cwd: string, parser: OutputParser): Promise<Problem[]>;
4
4
  export declare function isDebug(): boolean;
5
5
  export declare const bold: (str: string) => string;
6
6
  export declare const dim: (str: string) => string;
package/dist/utils.mjs CHANGED
@@ -1,22 +1,17 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { stat } from "fs/promises";
3
- import { resolve } from "node:path";
4
- function resolveRoot(root, ...path) {
5
- return root != null ? resolve(root, ...path) : resolve(...path);
6
- }
7
- export async function isBinInstalled(bin, root) {
3
+ export async function isBinInstalled(bin) {
8
4
  try {
9
- const binPath = resolveRoot(root, bin);
10
5
  if (isDebug())
11
- debug(`Checking if binary exists: ${binPath}`);
12
- await stat(resolveRoot(root, bin));
6
+ debug(`Checking if binary exists: ${bin}`);
7
+ await stat(bin);
13
8
  return true;
14
9
  } catch (err) {
15
10
  return false;
16
11
  }
17
12
  }
18
13
  function exec(cmd, args, opts) {
19
- return new Promise((resolve2, reject) => {
14
+ return new Promise((resolve, reject) => {
20
15
  const child = spawn(cmd, args, { ...opts, shell: true });
21
16
  let stderr = "";
22
17
  let stdout = "";
@@ -30,16 +25,16 @@ function exec(cmd, args, opts) {
30
25
  reject(error);
31
26
  });
32
27
  child.on("close", (exitCode) => {
33
- resolve2({ stderr, stdout, exitCode });
28
+ resolve({ stderr, stdout, exitCode });
34
29
  });
35
30
  });
36
31
  }
37
- export async function execAndParse(root, bin, args, parser) {
38
- const res = await exec(resolveRoot(root, bin), args, { cwd: root });
32
+ export async function execAndParse(bin, args, cwd, parser) {
33
+ const res = await exec(bin, args, { cwd });
39
34
  if (res.exitCode == null)
40
35
  throw Error("Exit code was null");
41
36
  if (isDebug())
42
- console.debug({ bin, args, root, ...res });
37
+ console.debug({ bin, args, cwd, ...res });
43
38
  return parser({
44
39
  code: res.exitCode,
45
40
  stderr: res.stderr,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aklinker1/check",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/aklinker1/check"