Skip to content
This repository was archived by the owner on Jun 3, 2023. It is now read-only.

Commit 36b665f

Browse files
committed
refactor: consolidate reporter into one helper
1 parent e4b1a67 commit 36b665f

File tree

10 files changed

+63
-55
lines changed

10 files changed

+63
-55
lines changed

bin/helpers/cmd.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { exec } from "./exec-promise.js"
2-
import { reporter as defaultReporter, exitWithError } from "./reporter.js"
2+
import { report, exitWithError } from "./reporter.js"
3+
4+
export async function cmd(command, config, indent = false) {
5+
const info = { m: command, type: "info", indent }
36

4-
export async function cmd(command, config, reporter = defaultReporter) {
57
if (config.dryRun) {
68
if (config.verbose) {
7-
reporter.info(command)
9+
report(info)
810
}
911
} else {
1012
try {
1113
if (config.verbose) {
12-
reporter.info(command)
14+
report(info)
1315
}
1416

1517
await exec(command)
@@ -19,10 +21,10 @@ export async function cmd(command, config, reporter = defaultReporter) {
1921
}
2022
}
2123

22-
export async function reportCmd(command, config, reporter = defaultReporter) {
23-
reporter.start(config.step)
24+
export async function reportCmd(command, config, indent = false) {
25+
report({ m: config.step, type: "start", indent })
2426

25-
await cmd(command, config, reporter)
27+
await cmd(command, config, indent)
2628

27-
reporter.succeed(`${config.step} successful`)
29+
report({ m: `${config.step} successful`, type: "succeed", indent })
2830
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ export function getPushCmd() {
1313
export function getAddCommand() {
1414
return "git add . -u"
1515
}
16+
17+
export function getPublishCommand(name, tag) {
18+
return `npm publish -w ${name} --tag ${tag}`
19+
}
20+
21+
export function getVersionCommand(name, version) {
22+
return `npm version -w ${name} ${version} --no-git-tag-version`
23+
}

bin/helpers/npm-commands.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

bin/helpers/reporter.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22

33
import ora from "ora"
44

5-
export const reporter = ora()
6-
export const pkgReporter = ora({ indent: 4 })
5+
const reporter = ora()
6+
const indentReporter = ora({ indent: 4 })
7+
8+
export function report({ m, type = "info", indent = false }) {
9+
return indent ? indentReporter[type](m) : reporter[type](m)
10+
}
711

812
function newConsoleLine() {
913
/* eslint-disable-next-line no-console */
1014
console.log("")
1115
}
1216

13-
export function exitWithError(error, message) {
17+
export function exitWithError(error, m) {
1418
newConsoleLine()
1519
/* eslint-disable-next-line no-console */
1620
console.error("Error:", error)
17-
reporter.fail(message)
21+
reporter({ m, type: "fail" })
1822
process.exit(1)
1923
}

bin/helpers/set-root-version.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
ROOT_PACKAGE_FILE,
99
} from "./constants.js"
1010
import { cmd } from "./cmd.js"
11-
import { reporter, exitWithError } from "./reporter.js"
11+
import { report, exitWithError } from "./reporter.js"
1212

1313
export async function setRootVersion(config) {
14-
reporter.start("Update root version")
14+
report({ m: "Update root version", type: "start" })
1515

1616
let updatePkg = false,
1717
updateConfig = false
@@ -48,7 +48,7 @@ export async function setRootVersion(config) {
4848

4949
if (config.dryRun) {
5050
if (config.verbose) {
51-
reporter.info(configCmd)
51+
report({ m: configCmd, type: "info" })
5252
}
5353
} else {
5454
try {
@@ -72,7 +72,7 @@ export async function setRootVersion(config) {
7272

7373
if (config.dryRun) {
7474
if (config.verbose) {
75-
reporter.info(packageCmd)
75+
report({ m: packageCmd, type: "info" })
7676
}
7777
} else {
7878
try {
@@ -93,5 +93,5 @@ export async function setRootVersion(config) {
9393
const addChangesCommand = "git add . -u"
9494
await cmd(addChangesCommand, config)
9595

96-
reporter.succeed()
96+
report({ type: "succeed" })
9797
}

bin/modules/commit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
import { ReportSteps } from "../helpers/constants.js"
4-
import { getCommitCmd, getTagCmd, getPushCmd } from "../helpers/git-commands.js"
4+
import { getCommitCmd, getTagCmd, getPushCmd } from "../helpers/commands.js"
55
import { setVersionToString } from "../helpers/transformers.js"
66
import { reportCmd } from "../helpers/cmd.js"
77

bin/modules/increment.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import fs from "fs"
44
import path from "path"
5-
import { exitWithError, pkgReporter } from "../helpers/reporter.js"
6-
import { ROOT_PACKAGE_FILE } from "../helpers/constants.js"
5+
import { getVersionCommand } from "../helpers/commands.js"
6+
import { exitWithError, report } from "../helpers/reporter.js"
77
import { cmd } from "../helpers/cmd.js"
8-
import { getVersionCommand } from "../helpers/npm-commands.js"
8+
import { ROOT_PACKAGE_FILE } from "../helpers/constants.js"
99

1010
function getSemverRangePart(version) {
1111
let rangePart = ""
@@ -66,12 +66,12 @@ function setDependencies(config, entry) {
6666

6767
if (config.dryRun) {
6868
if (config.verbose) {
69-
pkgReporter.info(writeCommand)
69+
report({ m: writeCommand, type: "info", indent: true })
7070
}
7171
} else {
7272
try {
7373
if (config.verbose) {
74-
pkgReporter.info(writeCommand)
74+
report({ m: writeCommand, type: "info", indent: true })
7575
}
7676

7777
fs.writeFileSync(
@@ -87,15 +87,11 @@ function setDependencies(config, entry) {
8787
}
8888

8989
export async function runIncrement(config, entry) {
90-
pkgReporter.start("Version")
90+
report({ m: "Version", type: "start", indent: true })
9191

92-
await cmd(
93-
getVersionCommand(entry.name, config.releaseVersion),
94-
config,
95-
pkgReporter
96-
)
92+
await cmd(getVersionCommand(entry.name, config.releaseVersion), config, true)
9793

9894
setDependencies(config, entry)
9995

100-
pkgReporter.succeed("Version successful")
96+
report({ m: "Version successful", type: "succeed", indent: true })
10197
}

bin/modules/initialize.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { normalizeConfig } from "../helpers/normalize-config.js"
22
import { checkUnstaged, checkRefStatus } from "../helpers/git-helpers.js"
3-
import { reporter } from "../helpers/reporter.js"
3+
import { report } from "../helpers/reporter.js"
44

55
export async function initialize(config) {
6-
reporter.start("Preparing to release")
6+
report({ m: "Preparing to release", type: "start" })
77

88
normalizeConfig(config)
99

@@ -12,5 +12,5 @@ export async function initialize(config) {
1212
await checkRefStatus(config)
1313
}
1414

15-
reporter.succeed("Ready to release!")
15+
report({ m: "Ready to release!", type: "succeed" })
1616
}

bin/modules/npm.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { reporter } from "../helpers/reporter.js"
3+
import { report } from "../helpers/reporter.js"
44
import { runIncrement } from "./increment.js"
55
import { runPublish } from "./publish.js"
66

@@ -12,9 +12,12 @@ export async function runNpm(config) {
1212
const entry = packages[i]
1313
const { name } = entry
1414

15-
reporter.stopAndPersist({
16-
text: `${name}@${config.releaseVersion}`,
17-
symbol: "📦",
15+
report({
16+
m: {
17+
text: `${name}@${config.releaseVersion}`,
18+
symbol: "📦",
19+
},
20+
type: "stopAndPersist",
1821
})
1922

2023
if (config.npm.increment) {
@@ -26,5 +29,8 @@ export async function runNpm(config) {
2629
}
2730
}
2831

29-
reporter.succeed("All packages versioned/published without errors")
32+
report({
33+
m: "All packages versioned/published without errors",
34+
type: "succeed",
35+
})
3036
}

bin/modules/publish.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#!/usr/bin/env node
22

33
import semver from "semver"
4-
import { getAddCommand } from "../helpers/git-commands.js"
4+
import { getAddCommand, getPublishCommand } from "../helpers/commands.js"
55
import { cmd } from "../helpers/cmd.js"
6-
import { pkgReporter } from "../helpers/reporter.js"
7-
import { getPublishCommand } from "../helpers/npm-commands.js"
6+
import { report } from "../helpers/reporter.js"
87

98
function parsePreId(version) {
109
const parts = semver.prerelease(version) || []
1110
return parts[0]
1211
}
1312

1413
export async function runPublish(config, entry) {
15-
pkgReporter.start("Publish")
14+
report({ m: "Publish", type: "start", indent: true })
1615

1716
const isPrivate = entry.getPackage().private
1817
const tag =
@@ -21,12 +20,12 @@ export async function runPublish(config, entry) {
2120
parsePreId(config.releaseVersion) ||
2221
"latest"
2322

24-
if (!isPrivate) {
25-
await cmd(getAddCommand(), config, pkgReporter)
26-
await cmd(getPublishCommand(entry.name, tag), config, pkgReporter)
27-
28-
pkgReporter.succeed("Publish successful")
23+
if (isPrivate) {
24+
report({ m: "Publish skipped (private)", type: "succeed", indent: true })
2925
} else {
30-
pkgReporter.succeed("Publish skipped (private)")
26+
await cmd(getAddCommand(), config, true)
27+
await cmd(getPublishCommand(entry.name, tag), config, true)
28+
29+
report({ m: "Publish successful", type: "succeed", indent: true })
3130
}
3231
}

0 commit comments

Comments
 (0)