Skip to content

Commit e48603a

Browse files
refactor: Split helper functions out of main export (#22)
1 parent b150954 commit e48603a

2 files changed

Lines changed: 82 additions & 72 deletions

File tree

src/publish/index.js

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,26 @@
44
import path from 'node:path'
55
import { execSync } from 'node:child_process'
66
import chalk from 'chalk'
7-
import jsonfile from 'jsonfile'
87
import * as semver from 'semver'
98
import currentGitBranch from 'current-git-branch'
109
import { parse as parseCommit } from '@commitlint/parse'
1110
import log from 'git-log-parser'
1211
import streamToArray from 'stream-to-array'
1312
import { DateTime } from 'luxon'
14-
15-
/** @param {string} version */
16-
const releaseCommitMsg = (version) => `release: v${version}`
17-
18-
/** @param {string} str */
19-
function capitalize(str) {
20-
return str.slice(0, 1).toUpperCase() + str.slice(1)
21-
}
22-
23-
/**
24-
* @param {string} pathName
25-
* @returns {Promise<import('type-fest').PackageJson>}
26-
*/
27-
async function readPackageJson(pathName) {
28-
return await jsonfile.readFile(pathName)
29-
}
30-
31-
/**
32-
* @param {string} pathName
33-
* @param {(json: import('type-fest').PackageJson) => Promise<void> | void} transform
34-
*/
35-
async function updatePackageJson(pathName, transform) {
36-
const json = await readPackageJson(pathName)
37-
await transform(json)
38-
await jsonfile.writeFile(pathName, json, {
39-
spaces: 2,
40-
})
41-
}
42-
43-
/**
44-
* @template TItem
45-
* @param {((d: TItem) => any)[]} sorters
46-
* @returns {(a: TItem, b: TItem) => number}
47-
*/
48-
function getSorterFn(sorters) {
49-
return (a, b) => {
50-
let i = 0
51-
52-
sorters.some((sorter) => {
53-
const sortedA = sorter(a)
54-
const sortedB = sorter(b)
55-
if (sortedA > sortedB) {
56-
i = 1
57-
return true
58-
}
59-
if (sortedA < sortedB) {
60-
i = -1
61-
return true
62-
}
63-
return false
64-
})
65-
66-
return i
67-
}
68-
}
13+
import {
14+
capitalize,
15+
getSorterFn,
16+
readPackageJson,
17+
releaseCommitMsg,
18+
updatePackageJson,
19+
} from './utils.js'
6920

7021
/**
7122
* Execute a script being published
7223
* @param {import('./types.js').RunOptions} options
7324
* @returns {Promise<void>}
7425
*/
75-
export async function publish(options) {
26+
export const publish = async (options) => {
7627
const { branchConfigs, packages, rootDir, branch, tag, ghToken } = options
7728
const branchName = /** @type {string} */ (branch ?? currentGitBranch())
7829
/** @type {import('./types.js').BranchConfig | undefined} */
@@ -393,9 +344,9 @@ export async function publish(options) {
393344
`Version ${version} - ${DateTime.now().toLocaleString(
394345
DateTime.DATETIME_SHORT,
395346
)}`,
396-
`## Changes`,
347+
'## Changes',
397348
changelogCommitsMd,
398-
`## Packages`,
349+
'## Packages',
399350
changedPackages.map((d) => `- ${d.name}@${version}`).join('\n'),
400351
].join('\n\n')
401352

@@ -430,12 +381,12 @@ export async function publish(options) {
430381
}
431382

432383
console.info()
433-
console.info(`Publishing all packages to npm`)
384+
console.info('Publishing all packages to npm')
434385

435386
// Publish each package
436387
changedPackages.forEach((pkg) => {
437388
const packageDir = path.join(rootDir, pkg.packageDir)
438-
const tagParam = branchConfig.previousVersion ? `` : `--tag ${npmTag}`
389+
const tagParam = branchConfig.previousVersion ? '' : `--tag ${npmTag}`
439390

440391
const cmd = `cd ${packageDir} && pnpm publish ${tagParam} --access=public --no-git-checks`
441392
console.info(` Publishing ${pkg.name}@${version} to npm "${tagParam}"...`)
@@ -446,26 +397,26 @@ export async function publish(options) {
446397

447398
console.info()
448399

449-
console.info(`Committing changes...`)
400+
console.info('Committing changes...')
450401
execSync(`git add -A && git commit -m "${releaseCommitMsg(version)}"`)
451402
console.info()
452-
console.info(` Committed Changes.`)
403+
console.info(' Committed Changes.')
453404

454-
console.info(`Pushing changes...`)
455-
execSync(`git push`)
405+
console.info('Pushing changes...')
406+
execSync('git push')
456407
console.info()
457-
console.info(` Changes pushed.`)
408+
console.info(' Changes pushed.')
458409

459410
console.info(`Creating new git tag v${version}`)
460411
execSync(`git tag -a -m "v${version}" v${version}`)
461412

462-
console.info(`Pushing tags...`)
463-
execSync(`git push --tags`)
413+
console.info('Pushing tags...')
414+
execSync('git push --tags')
464415
console.info()
465-
console.info(` Tags pushed.`)
416+
console.info(' Tags pushed.')
466417

467418
if (ghToken) {
468-
console.info(`Creating github release...`)
419+
console.info('Creating github release...')
469420

470421
// Stringify the markdown to escape any quotes
471422
execSync(
@@ -474,8 +425,9 @@ export async function publish(options) {
474425
} --notes '${changelogMd.replace(/'/g, '"')}'`,
475426
{ env: { ...process.env, GH_TOKEN: ghToken } },
476427
)
477-
console.info(` Github release created.`)
428+
console.info(' Github release created.')
478429
}
479430

480-
console.info(`All done!`)
431+
console.info('All done!')
432+
return
481433
}

src/publish/utils.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// @ts-check
2+
3+
import jsonfile from 'jsonfile'
4+
5+
/** @param {string} version */
6+
export const releaseCommitMsg = (version) => `release: v${version}`
7+
8+
/** @param {string} str */
9+
export const capitalize = (str) => {
10+
return str.slice(0, 1).toUpperCase() + str.slice(1)
11+
}
12+
13+
/**
14+
* @param {string} pathName
15+
* @returns {Promise<import('type-fest').PackageJson>}
16+
*/
17+
export const readPackageJson = async (pathName) => {
18+
return await jsonfile.readFile(pathName)
19+
}
20+
21+
/**
22+
* @param {string} pathName
23+
* @param {(json: import('type-fest').PackageJson) => Promise<void> | void} transform
24+
*/
25+
export const updatePackageJson = async (pathName, transform) => {
26+
const json = await readPackageJson(pathName)
27+
await transform(json)
28+
await jsonfile.writeFile(pathName, json, {
29+
spaces: 2,
30+
})
31+
}
32+
33+
/**
34+
* @template TItem
35+
* @param {((d: TItem) => any)[]} sorters
36+
* @returns {(a: TItem, b: TItem) => number}
37+
*/
38+
export const getSorterFn = (sorters) => {
39+
return (a, b) => {
40+
let i = 0
41+
42+
sorters.some((sorter) => {
43+
const sortedA = sorter(a)
44+
const sortedB = sorter(b)
45+
if (sortedA > sortedB) {
46+
i = 1
47+
return true
48+
}
49+
if (sortedA < sortedB) {
50+
i = -1
51+
return true
52+
}
53+
return false
54+
})
55+
56+
return i
57+
}
58+
}

0 commit comments

Comments
 (0)