tutanota/buildSrc/generateMigration.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-06-07 09:57:44 +02:00
import fs from "fs-extra"
2022-12-27 15:37:40 +01:00
import { Argument, InvalidArgumentError, program } from "commander"
import path from "node:path"
2022-12-27 15:37:40 +01:00
import { fileExists } from "./buildUtils.js"
2022-06-07 09:57:44 +02:00
await program
.addArgument(
2023-11-03 17:01:47 +01:00
new Argument("app", "Which tuta application needs a migration").choices(["accounting", "base", "gossip", "monitor", "storage", "sys", "tutanota"]),
2022-06-07 09:57:44 +02:00
)
2022-12-27 15:37:40 +01:00
.addArgument(new Argument("version", "Which version of the given app needs a migration").argParser(validateNumberArg))
2022-06-07 09:57:44 +02:00
.action(run)
.parseAsync(process.argv)
function validateNumberArg(value) {
2022-12-27 15:37:40 +01:00
const parsedValue = parseInt(value, 10)
2022-06-07 09:57:44 +02:00
if (isNaN(parsedValue)) {
2022-12-27 15:37:40 +01:00
throw new InvalidArgumentError("Not a number.")
2022-06-07 09:57:44 +02:00
}
2022-12-27 15:37:40 +01:00
return parsedValue
2022-06-07 09:57:44 +02:00
}
async function run(app, version) {
const template = `
import {OfflineMigration} from "../OfflineStorageMigrator.js"
import {OfflineStorage} from "../OfflineStorage.js"
export const ${app}${version}: OfflineMigration = {
\tapp: "${app}",
\tversion: ${version},
\tasync migrate(storage: OfflineStorage) {
\t throw new Error("TODO")
\t}
}
`.trimStart()
const outputName = path.resolve(`./src/common/api/worker/offline/migrations/${app}-v${version}.ts`)
2022-06-07 09:57:44 +02:00
if (await fileExists(outputName)) {
console.error("That migration already exists!")
process.exit(1)
}
await fs.writeFile(outputName, template, "utf-8")
console.log(`Wrote to: ${outputName}`)
2022-12-27 15:37:40 +01:00
}