Addon build working

This commit is contained in:
Darren Clarke 2023-05-10 09:34:18 +00:00
parent 6fca9c704f
commit 60e590f75c
22 changed files with 141 additions and 475 deletions

View file

@ -1,11 +1,67 @@
import { createZPM } from "./lib";
#!/usr/bin/env node
import { promises as fs } from "fs";
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,
};
}
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;
}
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
}
const pkg = JSON.stringify(skeleton, null, 2);
await fs.writeFile(`../../docker/zammad/auto_install/${name}-v${version}.zpm`, pkg, 'utf-8');
}
const main = async () => {
const packageJSON = JSON.parse(await fs.readFile("./package.json", "utf-8"));
const { name, version } = packageJSON;
console.log(`Building ${name} v${version}`);
await createZPM({ name, version });
const { name, displayName, version } = packageJSON;
console.log(`Building ${displayName} v${version}`);
await createZPM({ name, displayName, version });
}
main();

View file

@ -1 +0,0 @@
export {};

View file

@ -1,12 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lib_1 = require("./lib");
const fs_1 = require("fs");
const main = async () => {
const packageJSON = JSON.parse(await fs_1.promises.readFile("./package.json", "utf-8"));
const { name, version } = packageJSON;
console.log(`Building ${name} v${version}`);
await (0, lib_1.createZPM)({ name, version });
};
main();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9idWlsZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLCtCQUFrQztBQUNsQywyQkFBb0M7QUFFcEMsTUFBTSxJQUFJLEdBQUcsS0FBSyxJQUFJLEVBQUU7SUFDdEIsTUFBTSxXQUFXLEdBQUcsSUFBSSxDQUFDLEtBQUssQ0FBQyxNQUFNLGFBQUUsQ0FBQyxRQUFRLENBQUMsZ0JBQWdCLEVBQUUsT0FBTyxDQUFDLENBQUMsQ0FBQztJQUM3RSxNQUFNLEVBQUUsSUFBSSxFQUFFLE9BQU8sRUFBRSxHQUFHLFdBQVcsQ0FBQztJQUN0QyxPQUFPLENBQUMsR0FBRyxDQUFDLFlBQVksSUFBSSxLQUFLLE9BQU8sRUFBRSxDQUFDLENBQUM7SUFDNUMsTUFBTSxJQUFBLGVBQVMsRUFBQyxFQUFFLElBQUksRUFBRSxPQUFPLEVBQUUsQ0FBQyxDQUFDO0FBQ3JDLENBQUMsQ0FBQTtBQUVELElBQUksRUFBRSxDQUFDIn0=

View file

@ -1,2 +0,0 @@
export declare const createZPM: () => Promise<void>;
export declare const createMigration: () => Promise<void>;

File diff suppressed because one or more lines are too long

View file

@ -1,2 +0,0 @@
export declare const createZPM: ({ name, version }: Record<string, string>) => Promise<void>;
export declare const createMigration: ({ packageName }: Record<string, string>) => Promise<void>;

File diff suppressed because one or more lines are too long

View file

@ -1 +0,0 @@
export {};

View file

@ -1,11 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lib_1 = require("./lib");
const fs_1 = require("fs");
const main = async () => {
const packageJSON = JSON.parse(await fs_1.promises.readFile("./package.json", "utf-8"));
const { name, } = packageJSON;
await (0, lib_1.createMigration)({ packageName: name });
};
main();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWlncmF0ZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL21pZ3JhdGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwrQkFBd0M7QUFDeEMsMkJBQW9DO0FBRXBDLE1BQU0sSUFBSSxHQUFHLEtBQUssSUFBSSxFQUFFO0lBQ3RCLE1BQU0sV0FBVyxHQUFHLElBQUksQ0FBQyxLQUFLLENBQUMsTUFBTSxhQUFFLENBQUMsUUFBUSxDQUFDLGdCQUFnQixFQUFFLE9BQU8sQ0FBQyxDQUFDLENBQUM7SUFDN0UsTUFBTSxFQUFFLElBQUksR0FBRyxHQUFHLFdBQVcsQ0FBQztJQUM5QixNQUFNLElBQUEscUJBQWUsRUFBQyxFQUFFLFdBQVcsRUFBRSxJQUFJLEVBQUUsQ0FBQyxDQUFDO0FBQy9DLENBQUMsQ0FBQTtBQUVELElBQUksRUFBRSxDQUFDIn0=

File diff suppressed because one or more lines are too long

View file

@ -1,115 +0,0 @@
import { promises as fs } from "fs";
import path from "path";
import os from "os";
const skeleton: Record<string, any> = {
"name": "PackageName",
"version": "0.1.0",
"vendor": "Center for Digital Resilience",
"license": "AGPL-v3+",
"url": "https://gitlab.com/digiresilience/link/zammad-addon-package-name",
"buildhost": "",
"builddate": "",
"change_log": [
{
"version": "0.0.1",
"date": "2020-02-11",
"log": "Some changes"
}
],
"description": [
{
"language": "en",
"text": "Change me"
}
],
"files": []
}
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,
};
}
const packageFiles = async () => {
const packagedFiles: any[] = [];
const ignoredPatterns = [/\.gitkeep/];
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;
}
const underscore = (str: string) => {
return str
.replace(/([a-z\d])([A-Z])/g, "$1_$2")
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, "$1_$2")
.toLowerCase();
}
const camelize = (str: string): string => {
const camelizedStr = str.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
return camelizedStr.charAt(0).toUpperCase() + camelizedStr.slice(1);
}
export const createZPM = async ({ name, version }: Record<string, string>) => {
const files = await packageFiles();
skeleton.files = files;
skeleton.builddate = new Date().toISOString();
skeleton.buildhost = os.hostname();
const pkg = JSON.stringify(skeleton, null, 2);
await fs.writeFile(`../../docker/zammad/auto_install/${name}-v${version}.zpm`, pkg, 'utf-8');
}
export const createMigration = async ({ packageName}: Record<string, string>) => {
const rawName: string = await new Promise((resolve) => {
process.stdin.setEncoding("utf-8");
process.stdout.write("Enter migration name: ");
process.stdin.once("data", (data: string) => {
resolve(data.trim());
});
});
const migrationBaseName = `${packageName}_${underscore(rawName)}`;
const migrationName = camelize(migrationBaseName);
const migrationTemplate = `class MIGRATION_NAME < ActiveRecord::Migration[5.2]
def self.up
# add your code here
end
def self.down
# add your code here
end
end`;
const contents = migrationTemplate.replace("MIGRATION_NAME", migrationName);
const time = new Date().toISOString().replace(/[-:.]/g, "").slice(0, 14);
const migrationFileName = `${time}_${migrationBaseName}.rb`;
const addonDir = path.join("src", "db", "addon", skeleton["name"]);
await fs.mkdir(addonDir, { recursive: true });
await fs.writeFile(path.join(addonDir, migrationFileName), contents);
}

View file

@ -1,10 +1,53 @@
import { createMigration } from "./lib";
#!/usr/bin/env node
import { promises as fs } from "fs";
import path from "path";
const underscore = (str: string) => {
return str
.replace(/([a-z\d])([A-Z])/g, "$1_$2")
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, "$1_$2")
.toLowerCase();
}
const camelize = (str: string): string => {
const camelizedStr = str.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
return camelizedStr.charAt(0).toUpperCase() + camelizedStr.slice(1);
}
export const createMigration = async ({ displayName }: Record<string, string>) => {
const rawName: string = await new Promise((resolve) => {
process.stdin.setEncoding("utf-8");
process.stdout.write("Enter migration name: ");
process.stdin.once("data", (data: string) => {
resolve(data.trim());
});
});
const migrationBaseName = `${displayName}_${underscore(rawName)}`;
const migrationName = camelize(migrationBaseName);
const migrationTemplate = `class MIGRATION_NAME < ActiveRecord::Migration[5.2]
def self.up
# add your code here
end
def self.down
# add your code here
end
end`;
const contents = migrationTemplate.replace("MIGRATION_NAME", migrationName);
const time = new Date().toISOString().replace(/[-:.]/g, "").slice(0, 14);
const migrationFileName = `${time}_${migrationBaseName}.rb`;
const addonDir = path.join("src", "db", "addon", displayName);
await fs.mkdir(addonDir, { recursive: true });
await fs.writeFile(path.join(addonDir, migrationFileName), contents);
}
const main = async () => {
const packageJSON = JSON.parse(await fs.readFile("./package.json", "utf-8"));
const { name, } = packageJSON;
await createMigration({ packageName: name });
const { displayName } = packageJSON;
await createMigration({ displayName });
}
main();

View file

@ -6,9 +6,6 @@
"zpm-build": "./dist/build.js",
"zpm-migrate": "./dist/migrate.js"
},
"dependencies": {
"ts-node": "^10.9.1"
},
"scripts": {
"build": "tsc"
},

View file

@ -1,3 +0,0 @@
const { createZPM } = require("./dist/index.js");
console.log({ createZPM });