@@ -2,52 +2,82 @@ import path from 'node:path';
22import fs from 'node:fs/promises' ;
33import { promisify } from 'node:util' ;
44import { exec } from 'node:child_process' ;
5+ import * as oxfmt from 'oxfmt' ;
56import * as git from '@changesets/git' ;
7+ import getChangesets from '@changesets/read' ;
68import writeChangeset from '@changesets/write' ;
79import { type Changeset } from '@changesets/types' ;
810import { shouldSkipPackage } from '@changesets/should-skip-package' ;
11+ import oxfmtConfig from '../../.oxfmtrc.json' with { type : 'json' } ;
912import changesetConfig from '../../.changeset/config.json' with { type : 'json' } ;
1013
1114const execAsync = promisify ( exec ) ;
1215
1316const BASE_BRANCH = 'origin/' + changesetConfig . baseBranch ;
1417const ROOT_DIR = path . resolve ( import . meta. dirname , '../../' ) ;
1518
19+ const changedPackages = await getVersionableChangedPackages ( ) ;
20+
21+ const newChangeset : Changeset = {
22+ summary : 'Update dependencies' ,
23+ releases : changedPackages . map ( ( pkg ) => ( {
24+ name : pkg . packageJson . name ,
25+ type : 'patch' ,
26+ } ) ) ,
27+ } ;
28+
1629const existingChangeset = await getExistingChangeset ( ) ;
1730
1831if ( existingChangeset ) {
19- await fs . rm ( existingChangeset ) ;
20- }
32+ if ( changesetsAreEqual ( existingChangeset , newChangeset ) ) {
33+ console . log ( 'Changeset is already up to date' ) ;
34+ process . exit ( 0 ) ;
35+ }
2136
22- const changedPackages = await getVersionableChangedPackages ( ) ;
37+ await removeChangeset ( existingChangeset . id ) ;
38+
39+ console . log ( 'Removed outdated changeset' ) ;
40+ }
2341
2442if ( changedPackages . length === 0 ) {
2543 console . log ( 'No changed packages found' ) ;
2644 process . exit ( 0 ) ;
2745}
2846
29- const changeset : Changeset = {
30- summary : 'Update dependencies' ,
31- releases : changedPackages . map ( ( pkg ) => ( {
32- name : pkg . packageJson . name ,
33- type : 'patch' ,
34- } ) ) ,
35- } ;
47+ const changesetId = await writeChangeset ( newChangeset , ROOT_DIR ) ;
48+
49+ await formatChangeset ( changesetId ) ;
3650
37- await writeChangeset ( changeset , ROOT_DIR ) ;
51+ console . log ( 'Added new changeset' ) ;
3852
3953await git . add ( '-A' , ROOT_DIR ) ;
4054await git . commit ( 'chore: update changeset' , ROOT_DIR ) ;
4155
4256await execAsync ( 'git push' , { cwd : ROOT_DIR } ) ;
4357
4458async function getExistingChangeset ( ) {
45- const changedFiles = await git . getChangedFilesSince ( {
46- cwd : ROOT_DIR ,
47- ref : BASE_BRANCH ,
48- } ) ;
59+ return ( await getChangesets ( ROOT_DIR , BASE_BRANCH ) ) . find (
60+ ( changeset ) => changeset . summary === newChangeset . summary ,
61+ ) ;
62+ }
4963
50- return changedFiles . find ( ( file ) => file . startsWith ( '.changeset/' ) && file . endsWith ( '.md' ) ) ;
64+ async function removeChangeset ( changesetId : string ) {
65+ const changesetPath = getChangesetPath ( changesetId ) ;
66+
67+ await fs . rm ( changesetPath ) ;
68+ }
69+
70+ async function formatChangeset ( changesetId : string ) {
71+ const changesetPath = getChangesetPath ( changesetId ) ;
72+
73+ const changeset = await fs . readFile ( changesetPath , 'utf8' ) ;
74+ const formattedChangeset = await oxfmt . format ( changesetPath , changeset , oxfmtConfig as oxfmt . FormatOptions ) ;
75+
76+ await fs . writeFile ( changesetPath , formattedChangeset . code ) ;
77+ }
78+
79+ function getChangesetPath ( changesetId : string ) {
80+ return path . resolve ( ROOT_DIR , '.changeset' , changesetId + '.md' ) ;
5181}
5282
5383// Inspired by:
@@ -66,3 +96,10 @@ async function getVersionableChangedPackages() {
6696 } ) ,
6797 ) ;
6898}
99+
100+ function changesetsAreEqual ( changeset1 : Changeset , changeset2 : Changeset ) {
101+ return (
102+ changeset1 . summary === changeset2 . summary &&
103+ JSON . stringify ( changeset1 . releases ) === JSON . stringify ( changeset2 . releases )
104+ ) ;
105+ }
0 commit comments