Skip to content

Commit

Permalink
Start on autoupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Tatarintsev committed Apr 29, 2024
1 parent 3d2d757 commit 321ad6e
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 2,423 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/update-client-and-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Update client and publish a new version
on:
pull_request:
# TODO: cron
branches:
- main

concurrency:
group: 'publish'

jobs:
check-update:
runs-on: ubuntu-latest
outputs:
latest: ${{ steps.check-update.outputs.latest }}
dev: ${{ steps.check-update.outputs.dev }}
integration: ${{ steps.check-update.outputs.dev }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup

- name: Check for updates
id: check-update
run: yarn check-update

test-publish:
runs-on: ubuntu-latest

needs:
- check-update
steps:
- name: Update and publish latest
if: ${{ needs.check-update.outputs.latest != ''}}
run: echo "Latest"
- name: Update and publish dev
if: ${{ needs.check-update.outputs.dev != ''}}
run: echo "Latest"
- name: Update and publish integration
if: ${{ needs.check-update.outputs.integration != ''}}
run: echo "Latest"
27 changes: 5 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
"scripts": {
"example": "yarn workspace react-native-prisma-example",
"test": "jest",
"check-updates": "tsx scripts/check-updates.ts",
"bump-client": "tsx scripts/bump-client.ts",
"download-engine": "tsx scripts/download-engine.ts",
"typecheck": "tsc --noEmit",
"lint": "eslint \"**/*.{js,ts,tsx}\"",
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
"prepare": "tsx scripts/download-engine.ts && bob build",
"release": "release-it",
"prepare": "yarn download-engine && bob build",
"ios:full": "yarn qe:sim && cd example && yarn ios",
"clang": "clang-format -i ./cpp/*.cpp ./cpp/*.h && git add .",
"ios": "cd example && yarn ios",
Expand Down Expand Up @@ -68,7 +70,6 @@
"@evilmartians/lefthook": "^1.5.0",
"@prisma/client": "5.14.0-dev.22",
"@react-native/eslint-config": "^0.72.2",
"@release-it/conventional-changelog": "^5.0.0",
"@tsconfig/react-native": "^3.0.5",
"@types/jest": "^28.1.2",
"@types/react": "~17.0.21",
Expand All @@ -82,14 +83,14 @@
"expo-module-scripts": "^3.4.1",
"jest": "^28.1.1",
"p-retry": "^6.2.0",
"pod-install": "0.2.0",
"prettier": "^2.0.5",
"prisma": "5.14.0-dev.22",
"react": "18.2.0",
"react-native": "0.73.2",
"react-native-builder-bob": "0.23.2",
"react-native-quick-base64": "2.0.8",
"react-native-url-polyfill": "2.0.0",
"release-it": "^15.0.0",
"tsx": "^4.7.3",
"typescript": "^5.4.5",
"unzipper": "^0.11.4",
Expand Down Expand Up @@ -119,23 +120,6 @@
"<rootDir>/lib/"
]
},
"release-it": {
"git": {
"commitMessage": "chore: release ${version}",
"tagName": "v${version}"
},
"npm": {
"publish": true
},
"github": {
"release": true
},
"plugins": {
"@release-it/conventional-changelog": {
"preset": "angular"
}
}
},
"eslintConfig": {
"root": true,
"extends": [
Expand Down Expand Up @@ -192,7 +176,6 @@
}
},
"dependencies": {
"pod-install": "^0.2.0",
"react-native-quick-base64": "^2.0.8",
"react-native-url-polyfill": "^2.0.0"
}
Expand Down
21 changes: 9 additions & 12 deletions scripts/bump-client.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { $ } from 'zx';

Check warning on line 1 in scripts/bump-client.ts

View workflow job for this annotation

GitHub Actions / Lint

There should be at least one empty line between import groups
import pRetry from 'p-retry';
import { downloadEngine, ensureNpmTag, writeVersionFile } from './utils';

async function main() {
const version = process.argv[2];
const tag = process.argv[2];
ensureNpmTag(tag);
const version = process.argv[3];
if (!version) {
console.error(`Usage: bump-engines <version>`);
console.error(`Usage: bump-engines <tag> <version>`);
process.exitCode = 1;
return;
}
await $`yarn up prisma@${version} @prisma/client@${version}`;
const enginesVersion = require('@prisma/engines-version').enginesVersion;

await fs.writeFile(
path.resolve(__dirname, '..', '.versions', 'engine'),
enginesVersion
);

// await $`git add -A`;
// await $`git commit -m "Bump client to ${version}"`;
await writeVersionFile('engine', enginesVersion);
await writeVersionFile(`prisma-${tag}`, version);
// downloading updated engine
await downloadEngine();
}

main();
44 changes: 44 additions & 0 deletions scripts/check-updates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fs from 'node:fs/promises';
import { $ } from 'zx';
import pRetry from 'p-retry';

Check warning on line 3 in scripts/check-updates.ts

View workflow job for this annotation

GitHub Actions / Lint

There should be at least one empty line between import groups

Check warning on line 3 in scripts/check-updates.ts

View workflow job for this annotation

GitHub Actions / Lint

`p-retry` import should occur before import of `zx`
import { NPM_TAGS, type NpmTag, readVersionFile } from './utils';

async function main() {
for (let tag of NPM_TAGS) {

Check warning on line 7 in scripts/check-updates.ts

View workflow job for this annotation

GitHub Actions / Lint

'tag' is never reassigned. Use 'const' instead
await checkUpdate(tag);
}
}

async function checkUpdate(tag: NpmTag) {
console.log(`Checking update for npm tag: ${tag}`);
const currentVersion = await readVersionFile(`prisma-${tag}`);
const npmVersion = await getNpmVersion(tag);
console.log(`Current version ${currentVersion}`);
console.log(`npm version ${npmVersion}`);
if (currentVersion !== npmVersion) {
console.log('Update needed');
const ghOut = process.env.GITHUB_OUT;
if (ghOut) {
await fs.appendFile(ghOut, `${tag}=${npmVersion}\n`);
}
} else {
console.log('Up to date');
}
}

async function getNpmVersion(tag: NpmTag) {
const content = await pRetry(
() => $`yarn npm info prisma@${tag} --json -f version`,
{
retries: 3,
}
);

const parsed = JSON.parse(content.stdout);
if (typeof parsed?.version !== 'string') {
throw new Error(`Can not get npm version for prisma@${tag}`);
}
return parsed.version;
}

main();
31 changes: 2 additions & 29 deletions scripts/download-engine.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { pipeline } from 'node:stream/promises';
import { Extract } from 'unzipper';
import { downloadEngine } from './utils';

async function main() {
const version = await fs.readFile(
path.resolve(__dirname, '..', '.versions', 'engine'),
'utf8'
);

console.log(`Downloading engine ${version}`);

const url = `https://binaries.prisma.sh/all_commits/${version}/react-native/binaries.zip`;

console.log(url);

const resp = await fetch(url);
if (!resp.body) {
throw new Error('Failed to read the response from binaries server');
}
await pipeline(
resp.body,
Extract({ path: path.resolve(__dirname, '..', 'engines') })
);

console.log('Download complete');
}

main();
downloadEngine();
53 changes: 53 additions & 0 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { pipeline } from 'node:stream/promises';
import { Extract } from 'unzipper';

export const NPM_TAGS = ['dev', 'latest', 'integration'] as const;

type VersionFile =
| 'prisma-dev'
| 'prisma-integration'
| 'prisma-latest'
| 'engine';

export type NpmTag = (typeof NPM_TAGS)[number];

export function ensureNpmTag(str: string): asserts str is NpmTag {
if (!(NPM_TAGS as readonly string[]).includes(str)) {
throw new Error(`${str} is not supported npm tag`);
}
}

export function readVersionFile(file: VersionFile): Promise<string> {
return fs.readFile(versionFilePath(file), 'utf8');
}

export function writeVersionFile(
file: VersionFile,
version: string
): Promise<void> {
return fs.writeFile(versionFilePath(file), version);
}

export async function downloadEngine() {
const version = await readVersionFile('engine');

console.log(`Downloading engine ${version}`);

const url = `https://binaries.prisma.sh/all_commits/${version}/react-native/binaries.zip`;

const resp = await fetch(url);
if (!resp.body) {
throw new Error('Failed to read the response from binaries server');
}
await pipeline(
resp.body,
Extract({ path: path.resolve(__dirname, '..', 'engines') })
);

console.log('Download complete');
}
function versionFilePath(file: VersionFile): string {
return path.resolve(__dirname, '..', '.versions', file);
}
Loading

0 comments on commit 321ad6e

Please sign in to comment.