link-stack/packages/zammad-addon-common/build.ts

84 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

2023-05-10 09:34:18 +00:00
#!/usr/bin/env node
2023-05-09 12:26:23 +00:00
import { promises as fs } from "fs";
2023-05-17 08:16:41 +00:00
import { promisify } from "util";
import glob from "glob";
2023-05-10 09:34:18 +00:00
import path from "path";
import os from "os";
const packageFile = async (actualPath: string): Promise<any> => {
console.log(`Packaging: ${actualPath}`);
const packagePath = actualPath.slice(4);
const data = await fs.readFile(actualPath, "utf-8");
const content = Buffer.from(data, "utf-8").toString("base64");
const fileStats = await fs.stat(actualPath);
const permission = parseInt((fileStats.mode & 0o777).toString(8).slice(-3), 10);
return {
location: packagePath,
permission,
encode: "base64",
content,
};
2023-05-17 08:16:41 +00:00
};
2023-05-10 09:34:18 +00:00
const packageFiles = async () => {
const packagedFiles: any[] = [];
const ignoredPatterns = [/\.gitkeep/, /Gemfile/, /Gemfile.lock/, /\.ruby-version/];
const processDir = async (dir: string) => {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const entryPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
await processDir(entryPath);
} else if (entry.isFile()) {
if (!ignoredPatterns.some((pattern) => pattern.test(entry.name))) {
packagedFiles.push(await packageFile(entryPath));
}
}
}
};
await processDir("./src/");
return packagedFiles;
2023-05-17 08:16:41 +00:00
};
2023-05-10 09:34:18 +00:00
export const createZPM = async ({ name, displayName, version }: Record<string, string>) => {
const files = await packageFiles();
const skeleton = {
name: displayName,
version,
vendor: "Center for Digital Resilience",
license: "AGPL-v3+",
url: `https://gitlab.com/digiresilience/link/link-stack/packages/${name}`,
buildhost: os.hostname(),
builddate: new Date().toISOString(),
files
2023-05-17 08:16:41 +00:00
};
2023-05-10 09:34:18 +00:00
const pkg = JSON.stringify(skeleton, null, 2);
2023-05-17 08:16:41 +00:00
try {
2024-02-14 12:13:00 +01:00
// @ts-ignore
2023-05-17 08:16:41 +00:00
const gs = promisify(glob);
const files = await gs(`../../docker/zammad/addons/${name}-v*.zpm`);
for (const file of files) {
await fs.unlink(file);
console.log(`${file} was deleted`);
}
} catch (err) {
console.error(err);
}
2023-05-16 10:34:05 +00:00
await fs.writeFile(`../../docker/zammad/addons/${name}-v${version}.zpm`, pkg, 'utf-8');
2023-05-17 08:16:41 +00:00
};
2023-05-09 12:26:23 +00:00
const main = async () => {
const packageJSON = JSON.parse(await fs.readFile("./package.json", "utf-8"));
2024-06-05 08:52:41 +02:00
const { name: fullName, displayName, version } = packageJSON;
2023-05-10 11:10:32 +00:00
console.log(`Building addon ${displayName} v${version}`);
2024-06-05 08:52:41 +02:00
const name = fullName.split("/").pop();
2023-05-10 09:34:18 +00:00
await createZPM({ name, displayName, version });
2023-05-17 08:16:41 +00:00
};
2023-05-09 12:26:23 +00:00
main();