Skip to content

fix(scripts): dart and js releases [skip-bc] #4104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clients/algoliasearch-client-javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"scripts": {
"build": "lerna run build --scope '@algolia/requester-testing' --scope '@algolia/logger-console' --scope 'algoliasearch' --scope '@algolia/client-composition' --include-dependencies",
"clean": "lerna run clean",
"release:bump": "lerna version ${0:-patch} --no-changelog --no-git-tag-version --no-push --exact --force-publish --yes",
"release:publish": "tsc --project scripts/tsconfig.json && node scripts/dist/scripts/publish.js",
"test": "lerna run test $*",
"test:size": "bundlesize",
Expand Down
2 changes: 1 addition & 1 deletion scripts/release/createReleasePR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const fetchedUsers: Record<string, string> = {};

export function getVersionChangesText(versions: Versions): string {
return LANGUAGES.map((lang) => {
if (!versions[lang]) {
if (!versions[lang]?.next) {
return `- ~${lang}: ${getPackageVersionDefault(lang)} (no commit)~`;
}

Expand Down
90 changes: 90 additions & 0 deletions scripts/release/dart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import fsp from 'fs/promises';

import yaml from 'js-yaml';

import clientsConfig from '../../config/clients.config.json' assert { type: 'json' };
import { GENERATORS, toAbsolutePath } from '../common.js';

import { writeJsonFile } from './common.js';
import { updateChangelog } from './updateAPIVersions.js';

/**
* Updates packages versions and generates the changelog.
*/
export async function updateDartPackages(changelog: string, nextVersion: string): Promise<void> {
for (const gen of Object.values(GENERATORS)) {
if (gen.language !== 'dart') {
continue;
}

if (!nextVersion) {
throw new Error(`Failed to bump '${gen.packageName}'.`);
}

let currentVersion = await getPubspecField(gen.output, 'version');

// if there's no version then it mostly means it's a new client.
if (!currentVersion) {
currentVersion = '0.0.1';
}

await updateChangelog('dart', changelog, currentVersion, nextVersion, toAbsolutePath(`${gen.output}/CHANGELOG.md`));
}

// Version is sync'd on every clients so we set it once.
clientsConfig.dart.packageVersion = nextVersion;

// update `clients.config.json` file for the utils version.
await writeJsonFile(toAbsolutePath('config/clients.config.json'), clientsConfig);

// Core client package path
const corePackagePath = 'clients/algoliasearch-client-dart/packages/client_core';

// fetch the version from the pubspec file of the core package
let currentCoreVersion = await getPubspecField(corePackagePath, 'version');
if (!currentCoreVersion) {
currentCoreVersion = '0.0.1';
}

// update the changelog for core package
await updateChangelog(
'dart',
changelog,
currentCoreVersion,
nextVersion,
toAbsolutePath(`${corePackagePath}/CHANGELOG.md`),
);

// we've bumped generated clients but still need to do the manual ones.
await bumpPubspecVersion(toAbsolutePath(`${corePackagePath}/pubspec.yaml`), nextVersion);
}

/**
* Get 'version' from pubspec.yaml file.
*/
async function getPubspecField(filePath: string, field: string): Promise<string | undefined> {
try {
const fileContent = await fsp.readFile(toAbsolutePath(`${filePath}/pubspec.yaml`), 'utf8');
const pubspec = yaml.load(fileContent) as Record<string, any>;

return pubspec[field];
} catch (error) {
throw new Error(`Error reading file ${filePath}: ${error}`);
}
}

/**
* Bump 'version' of the given pubspec.yaml file path.
*/
async function bumpPubspecVersion(filePath: string, nextVersion: string): Promise<void> {
try {
const fileContent = await fsp.readFile(toAbsolutePath(filePath), 'utf8');
const pubspec = yaml.load(fileContent) as Record<string, any>;

pubspec.version = nextVersion;

await fsp.writeFile(filePath, yaml.dump(pubspec));
} catch (error) {
throw new Error(`Error writing file ${filePath}: ${error}`);
}
}
16 changes: 16 additions & 0 deletions scripts/release/javascript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ReleaseType } from 'semver';
import { run } from '../common.js';
import { getLanguageFolder } from '../config.js';
import { isPreRelease } from './versionsHistory.js';

export async function updateJavaScriptPackages(releaseType: ReleaseType) {
const cwd = getLanguageFolder('javascript');
const packages = JSON.parse(await run('yarn lerna ls --json --all --loglevel silent', { cwd })) as Array<{
version: string;
location: string;
}>;

for (const pkg of packages) {
await run(`yarn version ${isPreRelease(pkg.version) ? 'prerelease' : releaseType}`, { cwd: pkg.location });
}
}
95 changes: 6 additions & 89 deletions scripts/release/updateAPIVersions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fsp from 'fs/promises';

import yaml from 'js-yaml';

import clientsConfig from '../../config/clients.config.json' assert { type: 'json' };
import { CI, exists, GENERATORS, run, setVerbose, toAbsolutePath } from '../common.js';
import { CI, exists, setVerbose, toAbsolutePath } from '../common.js';
import { getGitHubUrl, getLanguageFolder } from '../config.js';
import type { Language } from '../types.js';

import { writeJsonFile } from './common.js';
import { updateDartPackages } from './dart.js';
import { updateJavaScriptPackages } from './javascript.js';
import type { Changelog, Versions } from './types.js';

async function updateConfigFiles(versionsToRelease: Versions): Promise<void> {
Expand All @@ -21,7 +21,7 @@ async function updateConfigFiles(versionsToRelease: Versions): Promise<void> {
await writeJsonFile(toAbsolutePath('config/clients.config.json'), clientsConfig);
}

async function updateChangelog(
export async function updateChangelog(
lang: Language,
changelog: string,
current: string,
Expand Down Expand Up @@ -58,11 +58,9 @@ export async function updateAPIVersions(versions: Versions, changelog: Changelog
continue;
}

if (lang === 'javascript') {
if (lang === 'javascript' && releaseType) {
setVerbose(CI);
await run(`yarn install && yarn release:bump ${releaseType}`, {
cwd: getLanguageFolder(lang),
});
await updateJavaScriptPackages(releaseType);
}
Comment on lines +61 to 64
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except the code move, this was the only change


await updateChangelog(
Expand All @@ -74,84 +72,3 @@ export async function updateAPIVersions(versions: Versions, changelog: Changelog
);
}
}

/**
* Updates packages versions and generates the changelog.
*/
async function updateDartPackages(changelog: string, nextVersion: string): Promise<void> {
for (const gen of Object.values(GENERATORS)) {
if (gen.language !== 'dart') {
continue;
}

if (!nextVersion) {
throw new Error(`Failed to bump '${gen.packageName}'.`);
}

let currentVersion = await getPubspecField(gen.output, 'version');

// if there's no version then it mostly means it's a new client.
if (!currentVersion) {
currentVersion = '0.0.1';
}

await updateChangelog('dart', changelog, currentVersion, nextVersion, toAbsolutePath(`${gen.output}/CHANGELOG.md`));
}

// Version is sync'd on every clients so we set it once.
clientsConfig.dart.packageVersion = nextVersion;

// update `clients.config.json` file for the utils version.
await writeJsonFile(toAbsolutePath('config/clients.config.json'), clientsConfig);

// Core client package path
const corePackagePath = 'clients/algoliasearch-client-dart/packages/client_core';

// fetch the version from the pubspec file of the core package
let currentCoreVersion = await getPubspecField(corePackagePath, 'version');
if (!currentCoreVersion) {
currentCoreVersion = '0.0.1';
}

// update the changelog for core package
await updateChangelog(
'dart',
changelog,
currentCoreVersion,
nextVersion,
toAbsolutePath(`${corePackagePath}/CHANGELOG.md`),
);

// we've bumped generated clients but still need to do the manual ones.
await bumpPubspecVersion(toAbsolutePath(`${corePackagePath}/pubspec.yaml`), nextVersion);
}

/**
* Get 'version' from pubspec.yaml file.
*/
async function getPubspecField(filePath: string, field: string): Promise<string | undefined> {
try {
const fileContent = await fsp.readFile(toAbsolutePath(`${filePath}/pubspec.yaml`), 'utf8');
const pubspec = yaml.load(fileContent) as Record<string, any>;

return pubspec[field];
} catch (error) {
throw new Error(`Error reading file ${filePath}: ${error}`);
}
}

/**
* Bump 'version' of the given pubspec.yaml file path.
*/
async function bumpPubspecVersion(filePath: string, nextVersion: string): Promise<void> {
try {
const fileContent = await fsp.readFile(toAbsolutePath(filePath), 'utf8');
const pubspec = yaml.load(fileContent) as Record<string, any>;

pubspec.version = nextVersion;

await fsp.writeFile(filePath, yaml.dump(pubspec));
} catch (error) {
throw new Error(`Error writing file ${filePath}: ${error}`);
}
}
Loading