Skip to content

Commit

Permalink
fix: log tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Oct 3, 2023
1 parent 79e3eeb commit 1124df2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
12 changes: 6 additions & 6 deletions packages/monorepo-release/src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {

log.info(
commitsSinceLatestTag.length,
pluralize("commit", commitsSinceLatestTag.length),
` found since \`${bold(latestTag)}\``,
pluralize("commit", commitsSinceLatestTag),
`found since \`${bold(latestTag)}\``,
)
log.debug(
"Analyzing the following commits:",
Expand Down Expand Up @@ -91,8 +91,8 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {

log.info(
packageCommits.length,
pluralize("commit", packageCommits.length),
` touched package code`,
pluralize("commit", packageCommits),
`touched package code`,
)

log.debug("Identifying packages that need a new release...")
Expand Down Expand Up @@ -162,8 +162,8 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {
)
log.info(
allPackagesToRelease.size,
pluralize("package", allPackagesToRelease.size),
`need to be released:`,
pluralize("package", allPackagesToRelease),
`need${allPackagesToRelease.size > 1 ? "" : "s"} to be released:`,
Array.from(allPackagesToRelease).join(", "),
)
} else {
Expand Down
21 changes: 9 additions & 12 deletions packages/monorepo-release/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { bold, green } from "yoctocolors"
const userConfig = {} // TODO: Allow user config
const config = { ...defaultConfig, ...userConfig }

console.time(green(bold("Done")))

if (config.dryRun) {
log.info(bold(green("Performing dry run, no packages will be released!\n")))
} else {
Expand All @@ -21,9 +23,9 @@ if (shouldSkip({ releaseBranches: config.releaseBranches })) {
}

if (config.dryRun) {
log.debug("Dry run, skipping token validation...\n")
log.debug("Dry run, skipping token validation...")
} else if (config.noVerify) {
log.info("--no-verify or NO_VERIFY set, skipping token validation...\n")
log.info("--no-verify or NO_VERIFY set, skipping token validation...")
} else {
if (!process.env.NPM_TOKEN) throw new Error("NPM_TOKEN is not set")
if (!process.env.GITHUB_TOKEN) throw new Error("GITHUB_TOKEN is not set")
Expand All @@ -34,16 +36,11 @@ const packages = await analyze(defaultConfig)
log.debug(
"Packages to release:",
packages
.map((p) =>
JSON.stringify(
{
...p,
commits: `${p.commits.features.length} feature(s), ${p.commits.bugfixes.length} bugfixe(s), ${p.commits.other.length} other(s) and ${p.commits.breaking.length} breaking change(s)`,
},
null,
2,
),
)
.map((p) => {
const { features, bugfixes, other, breaking } = p.commits
const commits = `${features.length} feature(s), ${bugfixes.length} bugfixe(s), ${other.length} other(s) and ${breaking.length} breaking change(s)`
return JSON.stringify({ ...p, commits }, null, 2)
})
.join("\n"),
)

Expand Down
2 changes: 1 addition & 1 deletion packages/monorepo-release/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export async function publish(packages: PackageToRelease[], options: Config) {
execSync(`git push`)
}

log.info(green(bold("Done!")))
console.timeEnd(green(bold("Done")))
}

function createChangelog(pkg: PackageToRelease) {
Expand Down
26 changes: 21 additions & 5 deletions packages/monorepo-release/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PackageJson } from "type-fest"
import { gray, blue, red } from "yoctocolors"
import { gray, blue, red, magenta, bold } from "yoctocolors"
import fs from "node:fs/promises"
import path from "node:path"
import { execSync as nodeExecSync } from "node:child_process"
Expand Down Expand Up @@ -30,14 +30,20 @@ async function update(

export const pkgJson = { read, update }

function purpleNumber(args: any[]) {
return args.map((a) =>
typeof a === "number" ? bold(magenta(a.toString())) : a,
)
}

export const log = {
debug(...args) {
if (!defaultConfig.verbose) return
const [first, ...rest] = args
console.log(gray("[debug]"), `${first}\n${rest.join("\n")}`)
const [first, ...rest] = purpleNumber(args)
console.log(gray("[debug]"), `${first}\n${rest.join("\n")}`.trim())
},
info(...args) {
console.log(blue("[info]"), ...args)
console.log(blue("[info]"), ...purpleNumber(args))
},
error(error: Error) {
console.error(red("\n[error]"), error, "\n")
Expand All @@ -48,8 +54,18 @@ export function execSync(...args: Parameters<typeof nodeExecSync>) {
return nodeExecSync(args[0], { stdio: "inherit", ...args[1] })
}

export function pluralize(word: string, count: number) {
export function pluralize(
word: string,
count: number | Array<any> | Set<any> | Map<any, any>,
) {
const pluralRules = new Intl.PluralRules("en", { type: "cardinal" })
count =
typeof count === "number"
? count
: count instanceof Set || count instanceof Map
? count.size
: count.length

const pluralForm = pluralRules.select(count)
switch (pluralForm) {
case "one":
Expand Down

0 comments on commit 1124df2

Please sign in to comment.