Skip to content

Commit

Permalink
Allow overriding from and to git pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
typeofweb committed Oct 16, 2022
1 parent 678e053 commit 8d6341e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions 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}`),
]);
};
35 changes: 19 additions & 16 deletions src/index.ts
Expand Up @@ -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)))
Expand Down Expand Up @@ -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,
};
Expand Down

0 comments on commit 8d6341e

Please sign in to comment.