44import path from 'node:path'
55import { execSync } from 'node:child_process'
66import chalk from 'chalk'
7- import jsonfile from 'jsonfile'
87import * as semver from 'semver'
98import currentGitBranch from 'current-git-branch'
109import { parse as parseCommit } from '@commitlint/parse'
1110import log from 'git-log-parser'
1211import streamToArray from 'stream-to-array'
1312import { 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}
0 commit comments