107 lines
3.4 KiB
JavaScript
107 lines
3.4 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { spawnSync } from "node:child_process";
|
|
import { mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import bcrypt from "bcryptjs";
|
|
import { PrismaClient } from "@prisma/client";
|
|
import { createApp } from "../dist/main.js";
|
|
import { signIdentity } from "../dist/auth.js";
|
|
|
|
const prisma = new PrismaClient();
|
|
const packageKeys = ["acheng-campus-food", "linjing-editorial"];
|
|
|
|
function run(command, args, options = {}) {
|
|
const result = spawnSync(command, args, { stdio: "inherit", ...options });
|
|
if (result.error) throw result.error;
|
|
if (result.status !== 0) throw new Error(`${command} exited with ${result.status}`);
|
|
}
|
|
|
|
function archiveFor(packageKey) {
|
|
const output = mkdtempSync(path.join(os.tmpdir(), "pb-template-integration-"));
|
|
const repositoryRoot = path.resolve(process.cwd(), "../..");
|
|
run(
|
|
"node",
|
|
[
|
|
path.join(repositoryRoot, "scripts/template-pack.cjs"),
|
|
"--template",
|
|
packageKey,
|
|
"--out",
|
|
output,
|
|
],
|
|
{ cwd: repositoryRoot },
|
|
);
|
|
const filename = readdirSync(output).find((item) => item.endsWith(".tar.gz"));
|
|
assert.ok(filename, `template pack did not produce an archive for ${packageKey}`);
|
|
return {
|
|
directory: output,
|
|
filename,
|
|
content: readFileSync(path.join(output, filename)),
|
|
};
|
|
}
|
|
|
|
async function importPackage(app, token, archive) {
|
|
const boundary = `----pb-integration-${Date.now()}`;
|
|
const payload = Buffer.concat([
|
|
Buffer.from(
|
|
`--${boundary}\r\nContent-Disposition: form-data; name="archive"; filename="${archive.filename}"\r\nContent-Type: application/gzip\r\n\r\n`,
|
|
),
|
|
archive.content,
|
|
Buffer.from(`\r\n--${boundary}--\r\n`),
|
|
]);
|
|
const response = await app.inject({
|
|
method: "POST",
|
|
url: "/admin/templates/import",
|
|
headers: {
|
|
authorization: `Bearer ${token}`,
|
|
"content-type": `multipart/form-data; boundary=${boundary}`,
|
|
},
|
|
payload,
|
|
});
|
|
assert.equal(response.statusCode, 201, response.body);
|
|
const imported = response.json().version;
|
|
const publish = await app.inject({
|
|
method: "POST",
|
|
url: `/admin/templates/${imported.id}/publish`,
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(publish.statusCode, 201, publish.body);
|
|
}
|
|
|
|
async function main() {
|
|
const actor = await prisma.adminUser.upsert({
|
|
where: { username: "integration-template-admin" },
|
|
update: {
|
|
isEnabled: true,
|
|
mustChangePassword: false,
|
|
platformTemplateRole: "template_admin",
|
|
},
|
|
create: {
|
|
username: "integration-template-admin",
|
|
passwordHash: await bcrypt.hash("integration-only-password", 4),
|
|
displayName: "Integration Template Admin",
|
|
platformTemplateRole: "template_admin",
|
|
},
|
|
});
|
|
const token = signIdentity({ subject: actor.id, brandId: "integration", role: "admin" });
|
|
const app = await createApp();
|
|
const archives = [];
|
|
try {
|
|
await app.init();
|
|
for (const packageKey of packageKeys) {
|
|
const archive = archiveFor(packageKey);
|
|
archives.push(archive.directory);
|
|
await importPackage(app, token, archive);
|
|
}
|
|
} finally {
|
|
await app.close();
|
|
for (const directory of archives) rmSync(directory, { recursive: true, force: true });
|
|
await prisma.$disconnect();
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|