11#!/usr/bin/env node
2- import addStream from 'add-stream' ;
3- import chalk from 'chalk' ;
4- import conventionalChangelog from 'conventional-changelog' ;
52import fs from 'node:fs' ;
6- import path from 'node:path ' ;
7- import tempfile from 'tempfile ' ;
3+ import { Package } from './changelog/Package.mjs ' ;
4+ import { generateChangelog } from './changelog/generate.mjs ' ;
85
96const PRESET = 'angular' ;
107
118/**
12- * @typedef {{
13- * path: string;
14- * excluded?: true;
15- * sameAs?: string;
16- * name?: string;
17- * changelogPath?: string;
18- * version?: string;
19- * gitPath?: string;
20- * }} Package
21- *
22- * @type {Package[] }
9+ * @type {import('./changelog/Package.mjs').PackageConfiguration[] }
2310 */
2411const PACKAGES = [
2512 { path : './packages/astro' } ,
2613 { path : './packages/cli' } ,
2714 { path : './packages/components/react' } ,
2815 { path : './packages/runtime' } ,
16+ { path : './packages/theme' } ,
17+ { path : './packages/types' } ,
2918
3019 // we do not include this one because it is not published
3120 { path : './packages/template' , excluded : true } ,
@@ -37,98 +26,56 @@ const PACKAGES = [
3726processPackages ( ) ;
3827
3928async function processPackages ( ) {
29+ /** @type {Map<string, Package> } */
30+ const packages = new Map ( ) ;
31+
4032 // infer extra properties
41- for ( const pkg of PACKAGES ) {
42- pkg . path = path . normalize ( pkg . path ) ;
33+ for ( const pkgDef of PACKAGES ) {
34+ const pkg = new Package ( pkgDef ) ;
35+ packages . set ( pkg . name , pkg ) ;
36+ }
4337
44- const pkgJSON = JSON . parse ( fs . readFileSync ( path . join ( pkg . path , 'package.json' ) ) ) ;
38+ // overwrites temporarily the version on the `tutorialkit` package as it's released separately later
39+ const tutorialkit = packages . get ( 'tutorialkit' ) ;
40+ const tutorialkitAstro = packages . get ( '@tutorialkit/astro' ) ;
4541
46- pkg . name = pkgJSON . name ;
47- pkg . version = pkgJSON . version ;
48- pkg . changelogPath = path . join ( pkg . path , 'CHANGELOG.md' ) ;
49- }
42+ const originalVersion = tutorialkit . version ;
43+ tutorialkit . version = tutorialkitAstro . version ;
44+
45+ tutorialkit . write ( ) ;
5046
5147 // generate change logs
5248 await Promise . all (
53- PACKAGES . map ( ( pkg ) => {
49+ [ ... packages . values ( ) ] . map ( ( pkg ) => {
5450 if ( pkg . excluded ) {
55- return ;
51+ return Promise . resolve ( ) ;
5652 }
5753
58- return generateChangelog ( pkg ) ;
54+ return generateChangelog ( pkg , PRESET ) ;
5955 } ) ,
6056 ) ;
6157
6258 // copy changelogs that are identical
63- for ( const pkg of PACKAGES ) {
59+ for ( const pkg of packages . values ( ) ) {
6460 if ( pkg . sameAs ) {
65- const otherPkg = PACKAGES . find ( ( otherPkg ) => otherPkg . name === pkg . sameAs ) ;
61+ const otherPkg = packages . get ( pkg . sameAs ) ;
6662
6763 fs . copyFileSync ( otherPkg . changelogPath , pkg . changelogPath ) ;
6864 }
6965 }
7066
7167 // generate root changelog
72- const tutorialkit = PACKAGES . find ( ( pkg ) => pkg . name === 'tutorialkit' ) ;
73-
74- await generateChangelog ( {
75- version : tutorialkit . version ,
76- path : tutorialkit . path ,
77- gitPath : '.' ,
78- changelogPath : 'CHANGELOG.md' ,
79- } ) ;
80- }
81-
82- /**
83- * Generate a changelog for the provided package and aggregate the data
84- * for the root changelog.
85- *
86- * @param {Package } pkg the package
87- */
88- function generateChangelog ( pkg ) {
89- const options = {
90- preset : PRESET ,
91- pkg : {
92- path : pkg . path ,
68+ await generateChangelog (
69+ {
70+ version : tutorialkit . version ,
71+ path : tutorialkit . path ,
72+ gitPath : '.' ,
73+ changelogPath : 'CHANGELOG.md' ,
9374 } ,
94- append : undefined ,
95- releaseCount : undefined ,
96- skipUnstable : undefined ,
97- outputUnreleased : undefined ,
98- tagPrefix : undefined ,
99- } ;
100-
101- const context = {
102- version : pkg . version ,
103- title : pkg . name ,
104- } ;
105-
106- const gitRawCommitsOpts = {
107- path : pkg . gitPath ?? pkg . path ,
108- } ;
109-
110- const changelogStream = conventionalChangelog ( options , context , gitRawCommitsOpts ) . on ( 'error' , ( error ) => {
111- console . error ( error . stack ) ;
112- process . exit ( 1 ) ;
113- } ) ;
114-
115- const CHANGELOG_FILE = pkg . changelogPath ;
116-
117- return new Promise ( ( resolve ) => {
118- const readStream = fs . createReadStream ( CHANGELOG_FILE ) . on ( 'error' , ( ) => {
119- // if there was no changelog we create it here
120- changelogStream . pipe ( fs . createWriteStream ( CHANGELOG_FILE ) ) . on ( 'finish' , resolve ) ;
121- } ) ;
122-
123- const tmp = tempfile ( ) ;
124-
125- changelogStream
126- . pipe ( addStream ( readStream ) )
127- . pipe ( fs . createWriteStream ( tmp ) )
128- . on ( 'finish' , ( ) => {
129- fs . createReadStream ( tmp ) . pipe ( fs . createWriteStream ( CHANGELOG_FILE ) ) . on ( 'finish' , resolve ) ;
130- } ) ;
131- } ) . then ( ( ) => {
132- console . log ( `${ chalk . green ( 'UPDATED' ) } ${ CHANGELOG_FILE } ${ chalk . gray ( pkg . version ) } ` ) ;
133- } ) ;
75+ PRESET ,
76+ ) ;
77+
78+ // reset the version of the CLI:
79+ tutorialkit . version = originalVersion ;
80+ tutorialkit . write ( ) ;
13481}
0 commit comments