Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: adds changesets to release pipeline #72

Merged
merged 15 commits into from
Apr 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions bin/changeset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env sh

userGrantsPermission() {
echo "Proceed? (Y/n)"
read -s -n 1 input
if [[("$input" = "y") || ("$input" = "Y") || ("$input" = "")]]; then
return 0
else
return 1
fi
}

execChangeset() {
echo "This script will create a changeset on your behalf"
if userGrantsPermission; then
echo "Creating a changeset"
pnpm changeset
else
echo "Skipped; a changeset was not generated."
fi
}

main() {
execChangeset;
}

main;
14 changes: 14 additions & 0 deletions bin/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env pnpx tsx
import * as Shell from 'node:child_process'

export interface CLI {
exec(cmd: string, args?: Shell.ExecOptions): () => string | Buffer
}

export const $: CLI = ({
exec: (cmd: string, args?: Shell.ExecSyncOptions) => () =>
Shell.execSync(
cmd,
{ stdio: "inherit", ...args },
)
})
27 changes: 27 additions & 0 deletions bin/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env sh

userGrantsPermission() {
echo "Proceed? (Y/n)"
read -s -n 1 input
if [[("$input" = "y") || ("$input" = "Y") || ("$input" = "")]]; then
return 0
else
return 1
fi
}

execVersion() {
echo "This script will version your package(s) according to the changeset you provided"
if userGrantsPermission; then
echo "Bumping local version(s)"
pnpm version
else
echo "Versioning skipped; your changeset has not been executed."
fi
}

main() {
execVersion;
}

main;
68 changes: 30 additions & 38 deletions bin/version.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@
import * as FileSystem from "node:fs"
import * as Path from "node:path"
import * as OS from "node:os"
import * as Shell from 'node:child_process'

import { $ } from "./cli"

type Result<ok, err> = Ok<ok> | Err<err>

@@ -49,18 +50,6 @@ export function flatMap<a, b, err>(f: (a: a) => Task<b, err>) {
.then((a) => isOk(a) ? run(f(a.ok)) : a)
}

interface CLI {
exec(cmd: string, args?: Shell.ExecOptions): () => string | Buffer
}

const $: CLI = ({
exec: (cmd: string, args?: Shell.ExecSyncOptions) => () =>
Shell.execSync(
cmd,
{ stdio: "inherit", ...args },
)
})

type intercalate<acc extends string, xs extends readonly unknown[], btwn extends string>
= xs extends readonly [infer head extends string, ...infer tail]
? intercalate<acc extends "" ? `${head}` : `${acc}${btwn}${head}`, tail, btwn>
@@ -173,46 +162,49 @@ const writeVersion = (v: string) => {
v && writeFile(versionFile)(versionTemplate(v))
}

const version = $.exec(`pnpm changeset`)
// const version = $.exec(`pnpm changeset`)
const changeset = $.exec(`./bin/changeset.sh`)
const version = $.exec(`./bin/version.sh`)

function commitVersion(version: string) {
run($.exec(`git add src/version.ts && git commit -m "automated: writes version ${version} to 'src/version.ts'"`))
}

const main = () => {
log(`bumping version...`)
version()
const prev = readPackageVersion()

const v = readPackageVersion()
log(`releasing version v${v} 🤞`)

log(`bumping version...`)
changeset()
version()

log(`Writing package version \`${v}\` to:${OS.EOL}\t${versionFile}`)
writeVersion(v)
const next = readPackageVersion()

if (prev === next) {
logError(`No version change detected comparing previous version (\`v${prev}\`) with the current version (\`v${next}\`)`)
process.exit(1)
}

log(`Committing with changes to ${versionFile}`)
try { commitVersion(v) }
catch (e) { log(`Nothing to commit!`) }
// log(`Writing package version \`v${next}\` to:${OS.EOL}\t${versionFile}`)
// writeVersion(next)

// log(`Committing with changes to ${versionFile}`)
// try { commitVersion(next) }
// catch (e) { log(`Nothing to commit!`) }

log(`kicking off build script`)
try { run($.exec("pnpm build")) }
catch (e) { logError("pnpm build", e) }
// log(`kicking off build script`)
// try { run($.exec("pnpm build")) }
// catch (e) { logError("pnpm build", e) }

log(`Done! Run ${OS.EOL}${OS.EOL}\tpnpm publish${OS.EOL}${OS.EOL}to push things to npm.`)
// log(`Done! Run ${OS.EOL}${OS.EOL}\tpnpm publish${OS.EOL}${OS.EOL}to push things to npm.`)

// TODO: get publishing working (probably just need to do this via a shell file)
/**
* log(`publishing...`)
* try {
* log(`successfully published \`any-ts′ version \`${v}\` 😊`)
* log(`https://www.npmjs.com/package/any-ts/v/${v}`)
* }
* catch (e) { logError("pnpm publish", e) }
*/
// // TODO: get publishing working (probably just need to do this via a shell file)
// /**
// * log(`publishing...`)
// * try {
// * log(`successfully published \`any-ts′ version \`${v}\` 😊`)
// * log(`https://www.npmjs.com/package/any-ts/v/${v}`)
// * }
// * catch (e) { logError("pnpm publish", e) }
// */
}

run(main)