Skip to content

Commit

Permalink
postupdate scripts engine
Browse files Browse the repository at this point in the history
  • Loading branch information
cardo-podcast committed Sep 5, 2024
1 parent 346133a commit d28ccfc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cardo",
"private": true,
"version": "1.2.0",
"version": "1.3.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"package": {
"productName": "Cardo",
"version": "1.2.0"
"version": "1.3.0"
},
"tauri": {
"updater": {
Expand Down
12 changes: 9 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import ReactDOM from "react-dom/client";
import App from "./App";
import "./main.css"
import "./engines/translations"
import { postupdate } from "./postupdate";


ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<App />
);
postupdate().then(
() => {
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<App />
);
}
)

38 changes: 38 additions & 0 deletions src/postupdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { appConfigDir, join } from "@tauri-apps/api/path";
import config from '../src-tauri/tauri.conf.json'
import { exists, readTextFile, writeTextFile } from "@tauri-apps/api/fs";


// migrations in ascending order
const migrations: { [version: string]: () => Promise<void> } = {
'1.3.0': async function () {
console.log('Hey you just updated to v1.3.0!')
}
}


export async function postupdate() {
const updatesFile = await join(await appConfigDir(), 'updates.json')
const version = config.package.version

const appliedUpdates: string[] = await exists(updatesFile) ?
JSON.parse(await readTextFile(updatesFile)) : []

if (appliedUpdates.includes(version)) return // this is not the first time this version runs, just open the app

// check thought all previous versions to apply
for (const migration of Object.keys(migrations)) {
if (!appliedUpdates.includes(migration)) {
await migrations[migration]()
appliedUpdates.push(migration)
}
}

if (!appliedUpdates.includes(version)) {
// append this version to the array even when it didn't apply nothing
appliedUpdates.push(version)
}

await writeTextFile(updatesFile, JSON.stringify(appliedUpdates))

}

0 comments on commit d28ccfc

Please sign in to comment.