Skip to content

Commit

Permalink
refactor(git-validator): clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Sep 23, 2023
1 parent d0fb541 commit 22b78b8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 42 deletions.
35 changes: 9 additions & 26 deletions packages/git-validator/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,12 @@
import { spawnSync } from "node:child_process";
import fs from "node:fs/promises";
import { createRequire } from "node:module";
import { dirname, join, resolve } from "node:path";
import { join, resolve } from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
import { cosmiconfig } from "cosmiconfig";
import { dir, exists, resolveConfig } from "./utils.js";

const __dirname = dirname(fileURLToPath(import.meta.url));
const requireResolve = createRequire(import.meta.url).resolve;

/**
* @param {"eslint"|"prettier"} module
*/
async function getConfigFilePath(module) {
return (await cosmiconfig(module).search(join(__dirname, "..")))?.filepath;
}

/**
* @param {string} filepath
*/
async function exists(filepath) {
return await fs
.access(filepath)
.then(() => true)
.catch(() => false);
}

/**
* @param {string} file
* @param {string} content
Expand All @@ -48,7 +29,7 @@ async function writeGitHook(file, content) {
async function writePreCommit() {
const content = [
"#!/bin/sh",
`npx lint-staged --config ${join(__dirname, "lint-staged.config.js")}`,
`npx lint-staged --config ${join(dir(import.meta.url), "lint-staged.config.js")}`,
].join("\n");

await writeGitHook("pre-commit", content);
Expand All @@ -57,7 +38,7 @@ async function writePreCommit() {
async function writeCommitMsg() {
const content = [
"#!/bin/sh",
`npx commitlint --config ${join(__dirname, "commitlint.config.js")} --edit`,
`npx commitlint --config ${join(dir(import.meta.url), "commitlint.config.js")} --edit`,
].join("\n");

await writeGitHook("commit-msg", content);
Expand Down Expand Up @@ -96,7 +77,7 @@ export async function lint(paths = [], options = {}) {
const cwd = process.cwd();
const ps = (paths.length === 0 ? [cwd] : paths).map((p) => resolve(cwd, p));

let configPath = await getConfigFilePath("eslint");
let configPath = (await resolveConfig("eslint"))?.filepath;
if (!configPath) {
process.env["ESLINT_USE_FLAT_CONFIG"] = "true";
configPath = requireResolve("@git-validator/eslint-config");
Expand All @@ -119,11 +100,13 @@ export async function format(paths = [], options = {}) {
const prettierIgnore = join(cwd, ".prettierignore");
const gitIgnore = join(cwd, ".gitignore");
const ignores = [
...((await exists(prettierIgnore)) ? [prettierIgnore] : [join(__dirname, "prettierignore")]),
...((await exists(prettierIgnore))
? [prettierIgnore]
: [join(dir(import.meta.url), "prettierignore")]),
...((await exists(gitIgnore)) ? [gitIgnore] : []),
].flatMap((p) => ["--ignore-path", p]);
const configPath =
(await getConfigFilePath("prettier")) ?? requireResolve("@git-validator/prettier-config");
(await resolveConfig("prettier"))?.filepath ?? requireResolve("@git-validator/prettier-config");

return spawnSync(
"npx",
Expand Down
10 changes: 2 additions & 8 deletions packages/git-validator/src/commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// @ts-check
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { cosmiconfig } from "cosmiconfig";

const __dirname = dirname(fileURLToPath(import.meta.url));
import { resolveConfig } from "./utils.js";

const defaultConfig = { extends: ["@commitlint/config-conventional"] };

export default (await cosmiconfig("commitlint").search(join(__dirname, "..")))?.config ??
defaultConfig;
export default (await resolveConfig("commitlint"))?.config ?? defaultConfig;
10 changes: 2 additions & 8 deletions packages/git-validator/src/lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// @ts-check
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { cosmiconfig } from "cosmiconfig";

const __dirname = dirname(fileURLToPath(import.meta.url));
import { resolveConfig } from "./utils.js";

const defaultConfig = { "*": ["npx git-validator -u"] };

export default (await cosmiconfig("lint-staged").search(join(__dirname, "..")))?.config ??
defaultConfig;
export default (await resolveConfig("lint-staged"))?.config ?? defaultConfig;
30 changes: 30 additions & 0 deletions packages/git-validator/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @ts-check
import fs from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { cosmiconfig } from "cosmiconfig";

/**
* @param {string} filepath
*/
export async function exists(filepath) {
return await fs
.access(filepath)
.then(() => true)
.catch(() => false);
}

/**
* @param {string} url
*/
export function dir(url) {
return dirname(fileURLToPath(url));
}

/**
* @param {string} module
* @param {string} dirname
*/
export async function resolveConfig(module, dirname = dir(import.meta.url)) {
return await cosmiconfig(module).search(join(dirname, ".."));
}

0 comments on commit 22b78b8

Please sign in to comment.