Skip to content
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

feat: compatibility endpoint #1212

Merged
merged 40 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
aa57b45
feat: compatibility endpoint
eemmiillyy Oct 6, 2023
f026cbb
clean
eemmiillyy Oct 6, 2023
73ba214
name
eemmiillyy Oct 6, 2023
3baed08
clean
eemmiillyy Oct 6, 2023
04daef8
hook
eemmiillyy Oct 9, 2023
8e4db20
change location of compatibility.json
eemmiillyy Oct 9, 2023
8369405
Merge branch 'main' into feat/compatibility-endpoint
eemmiillyy Oct 9, 2023
dc09a70
correct branch
eemmiillyy Oct 9, 2023
9e7ce3a
clean
eemmiillyy Oct 9, 2023
1732a7f
pr changes
eemmiillyy Oct 10, 2023
dc85c30
Merge branch 'main' into feat/compatibility-endpoint
eemmiillyy Oct 11, 2023
e7a60cf
Merge branch 'main' into feat/compatibility-endpoint
eemmiillyy Oct 16, 2023
ec08a66
Merge branch 'main' into feat/compatibility-endpoint
eemmiillyy Oct 23, 2023
a9158ef
pr changes
eemmiillyy Oct 24, 2023
d625612
release
eemmiillyy Oct 24, 2023
cad3fdb
script
eemmiillyy Oct 24, 2023
aba9747
tests
eemmiillyy Oct 27, 2023
b34de0c
fix tests
eemmiillyy Oct 27, 2023
16558a0
clean
eemmiillyy Oct 27, 2023
05c9fe4
clean
eemmiillyy Oct 27, 2023
d1d5b6e
simplify
eemmiillyy Oct 27, 2023
d54e0ef
Merge branch 'main' into feat/compatibility-endpoint
eemmiillyy Oct 27, 2023
de55186
alpha versions
eemmiillyy Oct 30, 2023
2d8e561
ensure alpha versions work
eemmiillyy Oct 30, 2023
faa0c82
clean
eemmiillyy Oct 30, 2023
d99ba2e
correct url
eemmiillyy Oct 30, 2023
b2d0820
fix
eemmiillyy Oct 30, 2023
878bfdf
feat: add total count to search responses (#1232)
eemmiillyy Nov 1, 2023
4a7b9ca
fix: excessive depth on summarize (#1235)
eemmiillyy Nov 2, 2023
b12b9eb
fix: column renaming errors (#1231)
eemmiillyy Nov 2, 2023
5ef6047
Partial update
SferaDev Nov 6, 2023
c81dfed
Merge branch 'main' into feat/compatibility-endpoint
SferaDev Nov 6, 2023
b253347
Apply suggestions from code review
SferaDev Nov 6, 2023
d862ebd
Merge remote-tracking branch 'origin/main' into feat/compatibility-en…
SferaDev Nov 27, 2023
27f8007
Update action
SferaDev Nov 27, 2023
7ee6c38
Update script
SferaDev Nov 27, 2023
fa503e1
Update tests
SferaDev Nov 27, 2023
8bda935
Create hot-bulldogs-dream.md
SferaDev Nov 27, 2023
6b9a2b6
Remove uppercase
SferaDev Nov 27, 2023
91d1e30
Update message
SferaDev Nov 27, 2023
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
8 changes: 8 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ jobs:
run: pnpm build

- name: Create Release Pull Request or Publish to npm
id: release
SferaDev marked this conversation as resolved.
Show resolved Hide resolved
uses: changesets/action@v1
with:
title: Release tracking
publish: npx changeset publish
SferaDev marked this conversation as resolved.
Show resolved Hide resolved
env:
GITHUB_TOKEN: ${{ secrets.GIT_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Versions
if: ${{ steps.release.outputs.published == 'true' }}
run: |
touch .env
echo PUBLISHED_PACKAGES=${{ steps.versions.outputs.publishedPackages }} >> .env
pnpm changeset-version
7 changes: 6 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@rollup/plugin-virtual": "^3.0.2",
"@types/ini": "^1.3.32",
"@types/prompts": "^2.4.7",
"@types/semver": "^7.5.3",
"@xata.io/client": "workspace:*",
"@xata.io/codegen": "workspace:*",
"@xata.io/importer": "workspace:*",
Expand Down Expand Up @@ -60,6 +61,7 @@
"rollup-plugin-hypothetical": "^2.1.1",
"rollup-plugin-import-cdn": "^0.2.2",
"rollup-plugin-virtual-fs": "^4.0.1-alpha.0",
"semver": "^7.5.4",
"text-table": "^0.2.0",
"tmp": "^0.2.1",
"which": "^4.0.0",
Expand All @@ -86,6 +88,9 @@
},
"oclif": {
"bin": "xata",
"hooks": {
"init": "./dist/hooks/init/compatibility"
},
"dirname": "xata",
"commands": "./dist/commands",
"plugins": [
Expand Down Expand Up @@ -130,7 +135,7 @@
"postpack": "shx rm -f oclif.manifest.json",
"posttest": "npm run lint",
"prepack": "oclif manifest && oclif readme",
"version": "oclif readme && git add README.md",
"changeset-version": "oclif readme && git add README.md && node ../scripts/changeset-version.mjs && git add ../scripts/changeset-version.mjs",
"start": "node ./bin/run.js"
},
"engines": {
Expand Down
94 changes: 94 additions & 0 deletions cli/src/hooks/init/compatibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Hook } from '@oclif/core';
import { readFile, stat, writeFile } from 'fs/promises';
import semver from 'semver';
import { mkdir } from 'fs';
import path from 'path';
import fetch from 'node-fetch';

export type Packages = 'cli' | 'sdk';
export type Compatibility = {
[key in Packages]: {
latest: string;
compatibility: { range: string; compatible: boolean; error?: string }[];
};
};
export type Latest = { latest: { [key in Packages]: string } };
export type PackageJson = { dependencies: Record<string, string> };

const hook: Hook<'init'> = async function (_options) {
const ONE_DAY = 1000 * 60 * 60 * 24 * 1;
const dir = path.join(process.cwd(), '.xata', 'version');
const latestFile = `${dir}/version.json`;
const compatibilityFile = `${dir}/compatibility.json`;
const compatibilityUri = 'https://raw.githubusercontent.com/xataio/client-ts/main/compatibility.json';

const displayWarning = async () => {
await check('cli', this.config.version);
const sdkVersion = await getSdkVersion();
sdkVersion && (await check('sdk', sdkVersion));
};

const check = async (pkg: 'cli' | 'sdk', currentVersion: string) => {
// Latest version check
const tag: Latest = JSON.parse(await readFile(latestFile, 'utf-8'));
const updateAvailable = semver.lt(this.config.version, tag.latest[pkg]);
if (updateAvailable)
this.log(
`✨ A newer version of the Xata ${pkg.toUpperCase()} is now available: ${
tag.latest[pkg]
}. You are currently using version: ${currentVersion}`
);
// Compatibility check
const compatibility: Compatibility = JSON.parse(await readFile(compatibilityFile, 'utf-8'));
const compatibleRange = compatibility[pkg].compatibility
.filter((v) => v.compatible)
.map((v) => v.range)
.join('||');
SferaDev marked this conversation as resolved.
Show resolved Hide resolved
const semverCompatible = semver.satisfies(currentVersion, compatibleRange);
if (!semverCompatible)
this.error(
`Incompatible version of ${pkg.toUpperCase()}: ${currentVersion}. Please upgrade to a version that satisfies: ${compatibleRange}.`
);
};

const getSdkVersion = async (): Promise<null | string> => {
const packageJson: PackageJson = JSON.parse(await readFile(`${path.join(process.cwd())}/package.json`, 'utf-8'));
return packageJson && packageJson.dependencies && packageJson.dependencies['@xata.io/client']
? packageJson.dependencies['@xata.io/client']
: null;
};

const fetchInfo = async () => {
let shouldRefresh = true;
try {
// Latest time of one of the files should be enough
const statResult = await stat(latestFile);
const lastModified = new Date(statResult.mtime);
// Last param is the number of days - we fetch new package info if the file is older than 1 day
const staleAt = new Date(lastModified.valueOf() + ONE_DAY);
shouldRefresh = new Date() > staleAt;
} catch (_e) {
// Do nothing
}
if (shouldRefresh) {
try {
mkdir(dir, { recursive: true }, () => {});
const latestCompatibilityResponse = await fetch(compatibilityUri);
const body = latestCompatibilityResponse.body;
await writeFile(compatibilityFile, body as NodeJS.ReadStream);
const compatibility: Compatibility = JSON.parse(await readFile(compatibilityFile, 'utf-8'));
await writeFile(
latestFile,
JSON.stringify({ latest: { cli: compatibility.cli.latest, sdk: compatibility.sdk.latest } }, null, 2)
);
} catch (_e) {
// Do nothing
}
}
};

await fetchInfo();
await displayWarning();
};

export default hook;
30 changes: 30 additions & 0 deletions compatibility.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"cli": {
"latest": "0.14.2",
"compatibility": [
{
"range": ">0.0.1",
"compatible": true
},
{
"range": "<0.0.1",
"compatbile": false,
"error": "Please update to v0.0.1"
}
]
},
"sdk": {
"latest": "0.26.9",
"compatibility": [
{
"range": ">0.0.1",
"compatible": true
},
{
"range": "<0.0.1",
"compatbile": false,
"error": "Please update to v0.0.1"
}
]
}
}
3 changes: 2 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"scripts": {
"build": "rimraf dist && rollup -c",
"tsc": "tsc --noEmit",
"add-version": "node ../../scripts/add-version-file.mjs"
"add-version": "node ../../scripts/add-version-file.mjs",
"changeset-version": "node ../../scripts/changeset-version.mjs && git add ../../scripts/changeset-version.mjs"
},
"peerDependencies": {
"typescript": ">=4.5"
Expand Down
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions scripts/changeset-version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'fs';
import path from 'path';

//[{"name": "@xx/xx", "version": "1.2.0"}, {"name": "@xx/xy", "version": "0.8.9"}]
const packageNames = ["@xata.io/client", "@xata.io/cli"]
const packages = JSON.parse(process.env.PUBLISHED_PACKAGES).filter(pkg => packageNames.includes(pkg.name));

const compatibilityPath = path.join(process.cwd(), 'compatibility.json');
const compatibility = fs.readFileSync(compatibilityPath, 'utf8');

for (const pkg of packages) {
const oldVersion = JSON.parse(compatibility)[name].latest
const newVersion = pkg.version
const name = pkg.name
compatibility.replace(oldVersion, newVersion)
}

fs.writeFileSync(compatibilityPath, compatibility);