Created
June 18, 2024 05:05
-
-
Save kentcdodds/186b057dac127429263a79046e017745 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Name: EpicShop Update | |
// Description: Update the EpicShop workshop app in all the epic web workshop repos | |
// Author: Kent C. Dodds | |
// Twitter: @kentcdodds | |
import '@johnlindquist/kit' | |
import {globby} from 'globby' | |
import {execa} from 'execa' | |
const workshopDirs = [ | |
// Volume 1 | |
'full-stack-foundations', | |
'web-forms', | |
'data-modeling', | |
'web-auth', | |
'full-stack-testing', | |
// Epic React | |
'react-fundamentals', | |
'react-hooks', | |
'advanced-react-apis', | |
'advanced-react-patterns', | |
'react-performance', | |
'react-suspense', | |
'react-server-components', | |
].map(dir => home('code', 'epicweb-dev', dir)) | |
const version = ( | |
await execaCommand('npm show @epic-web/workshop-app version') | |
).stdout.trim() | |
for (const workshopDir of workshopDirs) { | |
const workshopDirName = path.basename(workshopDir) | |
const pkgs = await globby(`**/package.json`, { | |
cwd: workshopDir, | |
gitignore: true, | |
}) | |
let changed = false | |
console.log(`π ${workshopDirName} - updating version`) | |
const hasChanges = | |
( | |
await execa('git', ['status', '--porcelain'], {env: {}, cwd: workshopDir}) | |
).stdout.trim() !== '' | |
if (hasChanges) { | |
try { | |
await execa('git', ['stash'], {env: {}, cwd: workshopDir, all: true}) | |
} catch (error) { | |
console.log(error.all) | |
throw `β ${workshopDirName} failed to stash properly` | |
} | |
} | |
for (const pkg of pkgs) { | |
const pkgPath = path.join(workshopDir, pkg) | |
const contents = await readFile(pkgPath, 'utf8') | |
const newContents = contents | |
.replace(/(@epic-web\/workshop-app":\s*")([^"]+)"/, `$1^${version}"`) | |
.replace(/(@epic-web\/workshop-utils":\s*")([^"]+)"/, `$1^${version}"`) | |
.replace(/(@epic-web\/workshop-presence":\s*")([^"]+)"/, `$1^${version}"`) | |
if (contents === newContents) continue | |
await writeFile(pkgPath, newContents) | |
changed = true | |
} | |
if (changed) { | |
try { | |
await execa('npm', ['install'], {env: {}, cwd: workshopDir, all: true}) | |
const pkgLocks = await globby('**/package-lock.json', { | |
cwd: workshopDir, | |
gitignore: true, | |
}) | |
await execa('git', ['add', ...pkgLocks, ...pkgs], { | |
env: {}, | |
cwd: workshopDir, | |
all: true, | |
}) | |
await execa( | |
'git', | |
['commit', '-m', 'chore: update @epic-web/workshop-app'], | |
{env: {}, cwd: workshopDir, all: true}, | |
) | |
await execa('git', ['pull'], {env: {}, cwd: workshopDir, all: true}) | |
await execa('git', ['push'], {env: {}, cwd: workshopDir, all: true}) | |
if (hasChanges) { | |
await execa('git', ['stash', 'pop'], { | |
env: {}, | |
cwd: workshopDir, | |
all: true, | |
}) | |
} | |
console.log(`β ${workshopDirName} finished`) | |
} catch (updateErrorResult) { | |
console.log(updateErrorResult.all) | |
throw `β ${workshopDirName} failed` | |
} | |
} else { | |
console.log(`π’ ${workshopDirName} already up to date`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment