Skip to content

Commit 81ae3c7

Browse files
authored
feat(dart): rely on common changelog (#1788)
1 parent 7dfd3ea commit 81ae3c7

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
lines changed

scripts/release/createReleasePR.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,7 @@ async function createReleasePR(): Promise<void> {
404404
const versionChanges = getVersionChangesText(versions);
405405

406406
console.log('Creating changelogs for all languages...');
407-
const changelog: Changelog = LANGUAGES.filter(
408-
(lang) => lang !== 'dart'
409-
).reduce((newChangelog, lang) => {
407+
const changelog: Changelog = LANGUAGES.reduce((newChangelog, lang) => {
410408
if (versions[lang].noCommit) {
411409
return newChangelog;
412410
}

scripts/release/updateAPIVersions.ts

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fsp from 'fs/promises';
22

33
import dotenv from 'dotenv';
44
import yaml from 'js-yaml';
5+
import type { ReleaseType } from 'semver';
56

67
import clientsConfig from '../../config/clients.config.json' assert { type: 'json' };
78
import openapiConfig from '../../config/openapitools.json' assert { type: 'json' };
@@ -45,12 +46,10 @@ async function updateChangelog(
4546
lang: Language,
4647
changelog: string,
4748
current: string,
48-
next: string
49+
next: string,
50+
changelogPath: string
4951
): Promise<void> {
5052
let content = '';
51-
const changelogPath = toAbsolutePath(
52-
`${getLanguageFolder(lang)}/CHANGELOG.md`
53-
);
5453
const changelogHeader = `## [${next}](${getGitHubUrl(
5554
lang
5655
)}/compare/${current}...${next})`;
@@ -112,51 +111,73 @@ export async function updateAPIVersions(
112111
versionsToRelease
113112
)) {
114113
if (lang === 'dart') {
115-
await updateDartPackages();
114+
await updateDartPackages(changelog[lang]!, releaseType);
116115

117116
continue;
118117
}
119118

120119
if (lang === 'javascript') {
121-
const cwd = getLanguageFolder(lang);
122-
123120
setVerbose(CI);
124-
await run(`yarn install && yarn release:bump ${releaseType}`, { cwd });
121+
await run(`yarn install && yarn release:bump ${releaseType}`, {
122+
cwd: getLanguageFolder(lang),
123+
});
125124
}
126125

127-
await updateChangelog(lang as Language, changelog[lang], current, next);
126+
await updateChangelog(
127+
lang as Language,
128+
changelog[lang],
129+
current,
130+
next,
131+
toAbsolutePath(`${getLanguageFolder(lang as Language)}/CHANGELOG.md`)
132+
);
128133
}
129134
}
130135

131136
/**
132137
* Updates packages versions and generates the changelog.
133138
* Documentation: {@link https://melos.invertase.dev/commands/version | melos version}.
134139
*/
135-
async function updateDartPackages(): Promise<void> {
136-
const cwd = getLanguageFolder('dart');
137-
138-
// Generate dart packages versions and changelogs
139-
await run(
140-
`(cd ${cwd} && dart pub get && melos version --no-git-tag-version --yes --diff ${RELEASED_TAG})`
141-
);
140+
async function updateDartPackages(
141+
changelog: string,
142+
releaseType: ReleaseType
143+
): Promise<void> {
144+
await run('dart pub get', { cwd: getLanguageFolder('dart') });
142145

143146
// Update packages configs based on generated versions
144147
for (const gen of Object.values(GENERATORS)) {
145-
if (gen.language === 'dart') {
146-
const { additionalProperties } = gen;
147-
const newVersion = await getPubspecVersion(
148-
`../${gen.output}/pubspec.yaml`
149-
);
150-
if (!newVersion) {
151-
throw new Error(`Failed to bump '${gen.packageName}'.`);
152-
}
153-
additionalProperties.packageVersion = newVersion;
154-
additionalProperties.packageName = undefined;
148+
if (gen.language !== 'dart') {
149+
continue;
150+
}
155151

156-
if (gen.client === 'algoliasearch') {
157-
clientsConfig.dart.packageVersion = newVersion;
158-
}
152+
const currentVersion = gen.additionalProperties.packageVersion;
153+
const packageName = await getPubspecField(gen.output, 'name');
154+
if (!packageName) {
155+
throw new Error(`Unable to find packageName for '${gen.packageName}'.`);
156+
}
157+
158+
await run(
159+
`melos version --manual-version=${packageName}:${releaseType} --no-changelog --no-git-tag-version --yes --diff ${RELEASED_TAG}`,
160+
{ cwd: gen.output }
161+
);
162+
163+
const newVersion = await getPubspecField(gen.output, 'version');
164+
if (!newVersion) {
165+
throw new Error(`Failed to bump '${gen.packageName}'.`);
159166
}
167+
gen.additionalProperties.packageVersion = newVersion;
168+
gen.additionalProperties.packageName = undefined;
169+
170+
if (gen.client === 'algoliasearch') {
171+
clientsConfig.dart.packageVersion = newVersion;
172+
}
173+
174+
await updateChangelog(
175+
'dart',
176+
changelog,
177+
currentVersion,
178+
newVersion,
179+
toAbsolutePath(`${gen.output}/CHANGELOG.md`)
180+
);
160181
}
161182

162183
// update `openapitools.json` config file
@@ -175,13 +196,18 @@ async function updateDartPackages(): Promise<void> {
175196
/**
176197
* Get 'version' from pubspec.yaml file.
177198
*/
178-
async function getPubspecVersion(
179-
filePath: string
199+
async function getPubspecField(
200+
filePath: string,
201+
field: string
180202
): Promise<string | undefined> {
181203
try {
182-
const fileContent = await fsp.readFile(filePath, 'utf8');
183-
const data = yaml.load(fileContent) as { version?: string };
184-
return data.version;
204+
const fileContent = await fsp.readFile(
205+
toAbsolutePath(`${filePath}/pubspec.yaml`),
206+
'utf8'
207+
);
208+
const pubspec = yaml.load(fileContent) as Record<string, any>;
209+
210+
return pubspec[field];
185211
} catch (error) {
186212
throw new Error(`Error reading the file: ${error}`);
187213
}

0 commit comments

Comments
 (0)