diff --git a/README.md b/README.md index b1e14e0..8a45af2 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,12 @@ npx @typeofweb/ignore-monorepo-buildstep ![Ignore Build Step settings in Vercel](./docs/vercel_settings.png) +By default, `ignore-monorepo-buildstep` will compare `HEAD^` and `HEAD`. You can override this and use an env variable exposed by Vercel: + +``` +npx @typeofweb/ignore-monorepo-buildstep $VERCEL_GIT_PREVIOUS_SHA +``` + ### Result When any changes are introduced to `packages/common`, both apps `a` and `b` will be built: diff --git a/src/git.ts b/src/git.ts new file mode 100644 index 0000000..d1aeb5b --- /dev/null +++ b/src/git.ts @@ -0,0 +1,24 @@ +import { execFile } from "node:child_process"; +import { promisify } from "node:util"; +const execFileAsync = promisify(execFile); + +export const compare = ({ + from, + to, + paths, + pathsToIgnore, +}: { + from: string; + to: string; + paths: string[]; + pathsToIgnore: string[]; +}) => { + return execFileAsync(`git`, [ + `diff`, + from, + to, + `--quiet`, + ...paths, + ...pathsToIgnore.map((path) => `:^${path}`), + ]); +}; diff --git a/src/index.ts b/src/index.ts index 39093e4..683ea69 100755 --- a/src/index.ts +++ b/src/index.ts @@ -2,18 +2,19 @@ import { existsSync } from "node:fs"; import Path from "node:path"; -import { execFile } from "node:child_process"; -import { promisify } from "node:util"; import { promiseErrorToSettled } from "./utils.js"; import { readWorkspaceDirs, readWorkspaceSettings, resolveWorkspaceDeps, } from "./pnpmWorkspace.js"; -const execFileAsync = promisify(execFile); +import { compare } from "./git.js"; const cwd = process.cwd(); +const [_node, _bin, gitFromPointer = "HEAD^", gitToPointer = "HEAD"] = + process.argv; + const rootDir = cwd .split(Path.sep) .map((_, idx) => Path.join(cwd, "../".repeat(idx))) @@ -41,28 +42,30 @@ const result = await Promise.all([ ...workspaceDepsRelativePaths.map(async (path) => { return { result: await promiseErrorToSettled( - execFileAsync(`git`, [`diff`, `HEAD^`, `HEAD`, `--quiet`, path]), + compare({ + from: gitFromPointer, + to: gitToPointer, + paths: [path], + pathsToIgnore: [], + }), ), path, }; }), (async () => { - const pathsToIgnore = (await readWorkspaceDirs({ rootDir, cwd })) - .map((path) => Path.relative(cwd, path)) - .map((path) => `:^${path}`); + const pathsToIgnore = (await readWorkspaceDirs({ rootDir, cwd })).map( + (path) => Path.relative(cwd, path), + ); const relativeRoot = Path.relative(cwd, rootDir); return { result: await promiseErrorToSettled( - execFileAsync(`git`, [ - `diff`, - `HEAD^`, - `HEAD`, - `--quiet`, - `--`, - relativeRoot, - ...pathsToIgnore, - ]), + compare({ + from: gitFromPointer, + to: gitToPointer, + paths: [relativeRoot], + pathsToIgnore, + }), ), path: relativeRoot, };