forked from vercel/turborepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental turbo-ignore package (vercel#1154)
This package can be used to automatically cancel irrelevant builds on Vercel via Ignored Build Step Command
- Loading branch information
1 parent
edc5698
commit 40dd995
Showing
3 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/env node | ||
|
||
const { exec } = require("child_process"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { findRootSync } = require("@manypkg/find-root"); | ||
|
||
console.log( | ||
"≫ Using Turborepo to determine if this project is affected by the commit..." | ||
); | ||
const root = getRoot(); | ||
const scope = getScope(); | ||
const command = `npx turbo run build --filter=${scope}...[HEAD^] --dry=json`; | ||
console.log(`≫ Analyzing results of \`${command}\`...`); | ||
exec( | ||
command, | ||
{ | ||
cwd: root, | ||
}, | ||
(error, stdout, stderr) => { | ||
if (error) { | ||
console.error(`exec error: ${error}`); | ||
return; | ||
} | ||
|
||
try { | ||
const parsed = JSON.parse(stdout); | ||
if (parsed == null) { | ||
console.error(`≫ Failed to parse JSON output from \`${command}\`.`); | ||
console.error(`≫ Proceeding with build to be safe...`); | ||
process.exit(1); | ||
} | ||
const { packages } = parsed; | ||
if (packages && packages.length > 0) { | ||
console.log( | ||
`≫ The commit affects this project and/or its ${ | ||
packages.length - 1 | ||
} dependencies` | ||
); | ||
console.log(`≫ Proceeding with build...`); | ||
process.exit(1); | ||
} else { | ||
console.log("≫ This project and its dependencies are not affected"); | ||
console.log("≫ Ignoring the change"); | ||
process.exit(0); | ||
} | ||
} catch (e) { | ||
console.error(`≫ Failed to parse JSON output from \`${command}\`.`); | ||
console.error(e); | ||
console.error(`≫ Proceeding with build to be safe...`); | ||
process.exit(1); | ||
} | ||
} | ||
); | ||
|
||
function searchUp(pathName, cwd) { | ||
const root = path.parse(cwd).root; | ||
|
||
let found = false; | ||
|
||
while (!found && cwd !== root) { | ||
if (fs.existsSync(path.join(cwd, pathName))) { | ||
found = true; | ||
break; | ||
} | ||
|
||
cwd = path.dirname(cwd); | ||
} | ||
|
||
if (found) { | ||
return cwd; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function getScope() { | ||
if (process.argv.length > 1 && process.argv[2] != null) { | ||
return process.argv[2]; | ||
} | ||
const raw = fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8"); | ||
const pkgJSON = JSON.parse(raw); | ||
console.log(`≫ Inferred \`${pkgJSON.name}\` as scope from "./package.json"`); | ||
return pkgJSON.name; | ||
} | ||
|
||
function getRoot() { | ||
let root = searchUp("turbo.json", process.cwd()); | ||
|
||
if (!root) { | ||
root = findRootSync(process.cwd()); | ||
if (!root) { | ||
console.error( | ||
"Error: workspace root not found. turbo-ignore inferencing failed, proceeding with build." | ||
); | ||
console.error(""); | ||
process.exit(1); | ||
} | ||
} | ||
return root; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "turbo-ignore", | ||
"version": "0.0.39", | ||
"description": "", | ||
"main": "index.js", | ||
"bin": "index.js", | ||
"keywords": [], | ||
"author": "Jared Palmer", | ||
"license": "MPL-2.0", | ||
"scripts": { | ||
"build": "tsup index.js --format=cjs && cp ./package.json ./dist/package.json", | ||
"release": "pnpm build && cd dist && npm publish" | ||
}, | ||
"devDependencies": { | ||
"@manypkg/find-root": "^1.1.0", | ||
"esbuild": "^0.14.38", | ||
"tsup": "^5.12.1" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.