Skip to content

Commit

Permalink
feat: add --peek flag
Browse files Browse the repository at this point in the history
closes #8
  • Loading branch information
balazsorban44 committed Oct 5, 2023
1 parent c2ebbd2 commit e18b970
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
21 changes: 17 additions & 4 deletions packages/monorepo-release/src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import gitLog from "git-log-parser"
import streamToArray from "stream-to-array"
import { type Package, getPackages } from "@manypkg/get-packages"
import { getDependentsGraph } from "@changesets/get-dependents-graph"
import { exit } from "./index.js"

export async function analyze(config: Config): Promise<PackageToRelease[]> {
const { BREAKING_COMMIT_MSG, RELEASE_COMMIT_MSG, RELEASE_COMMIT_TYPES } =
Expand Down Expand Up @@ -62,14 +63,14 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {
)
log.debug(
"Analyzing the following commits:",
commitsSinceLatestTag.map((c) => ` ${c.subject}`).join("\n"),
...commitsSinceLatestTag.map((c) => ` ${c.subject}`),
)

const lastCommit = commitsSinceLatestTag[0]

if (lastCommit?.parsed.raw === RELEASE_COMMIT_MSG) {
log.debug("Already released.")
process.exit(0)
exit()
}

log.debug("Identifying commits that modified package code.")
Expand Down Expand Up @@ -168,7 +169,7 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {
)
} else {
log.info("No need to release, exiting.")
process.exit(0)
exit()
}

const packagesToRelease: Map<string, PackageToRelease> = new Map()
Expand Down Expand Up @@ -209,7 +210,19 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {
)
}

return Array.from(packagesToRelease.values())
const result = Array.from(packagesToRelease.values())

if (config.peek) {
log.peekInfo(
"Following packages can be released:\n",
...result.map(
(p) => ` - ${bold(p.name)}: ${p.oldVersion} -> ${p.newVersion}`,
),
)
exit()
}

return result
}

function addToPackagesToRelease(
Expand Down
2 changes: 2 additions & 0 deletions packages/monorepo-release/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Config {
noVerify: boolean
dryRun: boolean
verbose: boolean
peek: boolean
}

const json = await pkgJson.read("./")
Expand All @@ -23,5 +24,6 @@ export const defaultConfig: Config = {
!!process.env.DRY_RUN ||
process.argv.includes("--dry-run"),
verbose: !!process.env.VERBOSE || process.argv.includes("--verbose"),
peek: !!process.env.PEEK || process.argv.includes("--peek"),
...json.release,
}
19 changes: 12 additions & 7 deletions packages/monorepo-release/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ import { shouldSkip } from "./skip.js"
import { analyze } from "./analyze.js"
import { publish } from "./publish.js"
import { log } from "./utils.js"
import { bold, green } from "yoctocolors"
import { bold } from "yoctocolors"

const userConfig = {} // TODO: Allow user config
const config = { ...defaultConfig, ...userConfig }

console.time(green(bold("Done")))
const endMsg = bold("Done")

console.time(endMsg)
export function exit() {
console.log()
console.timeEnd(endMsg)
process.exit(0)
}

if (config.dryRun) {
log.info(bold(green("Performing dry run, no packages will be released!\n")))
log.peekInfo(bold("Performing dry run, no packages will be released!\n"))
} else {
log.info(bold(green("Let's release some packages!\n")))
log.info(bold("Let's release some packages!\n"))
}

log.debug("Configuration:", JSON.stringify(config, null, 2))

if (shouldSkip({ releaseBranches: config.releaseBranches })) {
process.exit(0)
}
if (shouldSkip({ releaseBranches: config.releaseBranches })) exit()

if (config.dryRun) {
log.debug("Dry run, skipping token validation...")
Expand Down
5 changes: 3 additions & 2 deletions packages/monorepo-release/src/publish.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { bold, green } from "yoctocolors"
import { bold } from "yoctocolors"
import { type Config } from "./config.js"
import type { Commit, PackageToRelease } from "./types.js"

import { log, pkgJson, execSync } from "./utils.js"
import { exit } from "./index.js"

/** Make sure that packages that depend on other packages are released last. */
async function sortByDependency(pkgs: PackageToRelease[]) {
Expand Down Expand Up @@ -91,7 +92,7 @@ export async function publish(packages: PackageToRelease[], options: Config) {
execSync(`git push`)
}

console.timeEnd(green(bold("Done")))
exit()
}

function createChangelog(pkg: PackageToRelease) {
Expand Down
5 changes: 5 additions & 0 deletions packages/monorepo-release/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ export const log = {
console.log(gray("[debug]"), `${first}\n${rest.join("\n")}`.trim())
},
info(...args) {
if (defaultConfig.peek) return
console.log(blue("[info]"), ...purpleNumber(args))
},
/** Runs even if `config.peek` is set */
peekInfo(...args) {
console.log(args.join("\n"))
},
error(error: Error) {
console.error(red("\n[error]"), error, "\n")
},
Expand Down

0 comments on commit e18b970

Please sign in to comment.