Skip to content

Commit

Permalink
Fix incorrect version increment on publish
Browse files Browse the repository at this point in the history
  • Loading branch information
xentobias committed Sep 29, 2024
1 parent 4dae3c3 commit e570506
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xentom/cli",
"version": "0.0.15",
"version": "0.0.16",
"type": "module",
"scripts": {
"format": "prettier --check .",
Expand Down
50 changes: 18 additions & 32 deletions src/commands/publish/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { createCommand } from '@/commands/utils';
import { env } from '@/env';
import { createRequestHeaders } from '@/lib/trpc';
import { createZipInMemory } from '@/lib/zip';
import { createZipInMemory, type File } from '@/lib/zip';
import { ActionError, actionErrorHandler } from '@/utils/action';
import { getIntegrationMetadata } from '@/utils/metadata';
import { cmd } from '@/utils/output';
import ora from 'ora';
import { cyan, red } from 'yoctocolors';
import { ZodError } from 'zod';
import { IntegrationMetadata } from '@xentom/integration/schema';
import { getNextVersion, setVersion } from './version';

export function createPublishCommand() {
return createCommand()
Expand Down Expand Up @@ -78,19 +79,17 @@ export async function publish(options: PublishOptions) {
);
}

const previousVersion = metadata.version;
if (options.increment) {
metadata = await incrementVersion(metadata);
metadata.version = await getNextVersion(metadata.version);
}

try {
await upload(await pack(metadata), options.tag);
} catch (error) {
// Revert the version back to the previous one if the upload fails

if (options.increment) {
metadata = await setVersion(metadata, previousVersion);
await setVersion(metadata.version);
}

} catch (error) {
if (options.ignoreDuplicates) {
if (
error instanceof ActionError &&
Expand All @@ -117,35 +116,22 @@ export async function publish(options: PublishOptions) {
);
}

async function incrementVersion(metadata: IntegrationMetadata) {
const version = metadata.version.split('.');
version[version.length - 1] = String(Number(version[version.length - 1]) + 1);
return await setVersion(metadata, version.join('.'));
}

async function setVersion(metadata: IntegrationMetadata, version: string) {
const copy = { ...metadata };
copy.version = version;
await Bun.write('./integration.json', JSON.stringify(copy, null, 2));
return copy;
}

async function pack(metadata: IntegrationMetadata) {
const files = [
'./integration.json',
'./CHANGELOG.md',
'./LICENSE.txt',
'./README.md',
'./dist/browser.js',
'./dist/browser.css',
'./dist/declarations.json',
'./dist/definition.json',
'./dist/server.js',
'./dist/index.d.ts',
const files: File[] = [
{ path: './integration.json', content: JSON.stringify(metadata, null, 2) },
{ path: './CHANGELOG.md' },
{ path: './LICENSE.txt' },
{ path: './README.md' },
{ path: './dist/browser.js' },
{ path: './dist/browser.css' },
{ path: './dist/declarations.json' },
{ path: './dist/definition.json' },
{ path: './dist/server.js' },
{ path: './dist/index.d.ts' },
];

if (metadata.logo) {
files.push(metadata.logo);
files.push({ path: metadata.logo });
}

return await createZipInMemory(files);
Expand Down
33 changes: 33 additions & 0 deletions src/commands/publish/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ActionError } from '@/utils/action';

export async function getNextVersion(version: string) {
const parts = version.split('.');
parts[parts.length - 1] = String(Number(parts[parts.length - 1]) + 1);
return parts.join('.');
}

export async function setVersion(version: string) {
const file = await getVersionSource();
file.content.version = version;
await Bun.write(file.source, JSON.stringify(file.content, null, 2));
}

async function getVersionSource() {
const metadata = await Bun.file('./integration.json').json();
if (metadata.version) {
return {
source: 'integration.json',
content: metadata as { version: string },
};
}

const packageJson = await Bun.file('./package.json').json();
if (packageJson.version) {
return {
source: 'package.json',
content: packageJson as { version: string },
};
}

throw new ActionError('No version found in integration.json or package.json');
}
13 changes: 9 additions & 4 deletions src/lib/zip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import * as path from 'node:path';
import { PassThrough } from 'node:stream';
import archiver from 'archiver';

export function createZipInMemory(files: string[]): Promise<Buffer> {
export interface File {
path: string;
content?: string;
}

export function createZipInMemory(files: File[]): Promise<Buffer> {
return new Promise((resolve, reject) => {
const archive = archiver('zip', {
zlib: { level: 9 },
Expand All @@ -19,10 +24,10 @@ export function createZipInMemory(files: string[]): Promise<Buffer> {
archive.pipe(passthrough);

files.forEach((file) => {
const prefix = path.normalize(path.dirname(file));
const prefix = path.normalize(path.dirname(file.path));
try {
archive.append(fs.createReadStream(file), {
name: path.basename(file),
archive.append(file.content ?? fs.createReadStream(file.path), {
name: path.basename(file.path),
prefix: prefix === '.' ? undefined : prefix,
});
} catch {}
Expand Down

0 comments on commit e570506

Please sign in to comment.