Skip to content

Commit ca246af

Browse files
authored
chore(clean-build): script to reset everything to a clean slate (#161)
With lefthook setup I kept running into an error after running `git clean -fdx && yarn && yarn build` where it couldn't run linting inside the RSC installer package. This new script fixes that, and also includes some nice safeguards that would have saved me some grief in the past had I had them in place then :D
1 parent 0e879c6 commit ca246af

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"scripts": {
1515
"build": "nx run-many -t build",
1616
"build:clean": "node ./tasks/clean.mjs",
17-
"build:clean:super": "git clean -fdx && yarn && yarn build",
1817
"build:pack": "nx run-many -t build:pack",
1918
"build:test-project": "node ./tasks/test-project/test-project",
2019
"build:watch": "lerna run build:watch --parallel; tsc --build",
@@ -27,6 +26,7 @@
2726
"format": "prettier . --write",
2827
"format:check": "prettier . --check",
2928
"generate-dependency-graph": "node ./tasks/generateDependencyGraph.mjs",
29+
"git-clean-build": "node $(git rev-parse --show-toplevel)/tasks/git-clean-build.mjs",
3030
"install:all": "concurrently -g -c auto -n install:fw \"yarn install\" npm:install:crwrsca",
3131
"install:ci": "concurrently -g -c auto -n install:fw:ci \"yarn install --inline-builds\" npm:install:crwrsca:ci",
3232
"install:crwrsca": "yarn --cwd packages/create-cedar-rsc-app install",

tasks/git-clean-build.mjs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env node
2+
/* eslint-env node */
3+
4+
import path from 'path'
5+
import { fileURLToPath } from 'url'
6+
7+
import { $, question } from 'zx'
8+
9+
// Get the directory of this script
10+
const __filename = fileURLToPath(import.meta.url)
11+
const __dirname = path.dirname(__filename)
12+
13+
// Get the repo root (parent of tasks directory)
14+
const repoRoot = path.resolve(__dirname, '..')
15+
16+
const originalCwd = process.cwd()
17+
18+
const restoreCwd = () => {
19+
process.chdir(originalCwd)
20+
}
21+
22+
process.on('exit', restoreCwd)
23+
process.on('SIGINT', restoreCwd)
24+
process.on('SIGTERM', restoreCwd)
25+
process.on('uncaughtException', (error) => {
26+
console.error('Uncaught exception:', error)
27+
restoreCwd()
28+
process.exit(1)
29+
})
30+
31+
try {
32+
console.log(`Original directory: ${originalCwd}`)
33+
console.log(`Repo root: ${repoRoot}`)
34+
35+
process.chdir(repoRoot)
36+
37+
console.log('Checking for untracked files...')
38+
const statusResult =
39+
await $`git status --porcelain --untracked-files=all`.quiet()
40+
const untrackedFiles = statusResult.stdout
41+
.trim()
42+
.split('\n')
43+
.filter((line) => line.startsWith('??'))
44+
.map((line) => line.substring(3))
45+
.filter((file) => file.trim())
46+
47+
if (untrackedFiles.length > 0) {
48+
console.log(
49+
`\n⚠️ WARNING: git clean -fdx will delete ${untrackedFiles.length} untracked files/directories:`,
50+
)
51+
52+
if (untrackedFiles.length <= 5) {
53+
untrackedFiles.forEach((file) => {
54+
console.log(` - ${file}`)
55+
})
56+
} else {
57+
console.log(
58+
` (${untrackedFiles.length} files/directories - too many to list)`,
59+
)
60+
}
61+
62+
const confirmed = await question('\nDo you want to proceed? (y/N): ')
63+
if (!confirmed || !['y', 'yes'].includes(confirmed.toLowerCase())) {
64+
console.log('Operation cancelled.')
65+
process.exit(0)
66+
}
67+
}
68+
69+
console.log('Running git clean -fdx...')
70+
await $`git clean -fdx`
71+
72+
console.log('Running yarn install...')
73+
await $`yarn install`
74+
75+
console.log('Running yarn build...')
76+
await $`yarn build`
77+
78+
console.log('Installing dependencies in packages/create-cedar-rsc-app...')
79+
const createCedarRscAppPath = path.join(
80+
repoRoot,
81+
'packages/create-cedar-rsc-app',
82+
)
83+
process.chdir(createCedarRscAppPath)
84+
await $`yarn install`
85+
86+
console.log('All tasks completed successfully!')
87+
console.log(`Returning to original directory: ${originalCwd}`)
88+
} catch (error) {
89+
console.error('Error during clean-build process:', error)
90+
process.exit(1)
91+
}

0 commit comments

Comments
 (0)