pgdesign 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/bin/pgdesign +14 -0
  2. package/install.js +133 -0
  3. package/package.json +24 -0
package/bin/pgdesign ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env node
2
+ const { execFileSync } = require("child_process");
3
+ const path = require("path");
4
+
5
+ const ext = process.platform === "win32" ? ".exe" : "";
6
+ const bin = path.join(__dirname, "..", "pgdesign" + ext);
7
+
8
+ try {
9
+ execFileSync(bin, process.argv.slice(2), { stdio: "inherit" });
10
+ } catch (e) {
11
+ if (e.status !== undefined) process.exit(e.status);
12
+ console.error("pgdesign not found. Try reinstalling: npm install pgdesign");
13
+ process.exit(1);
14
+ }
package/install.js ADDED
@@ -0,0 +1,133 @@
1
+ const https = require("https");
2
+ const http = require("http");
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { execSync } = require("child_process");
6
+
7
+ const pkg = require("./package.json");
8
+ const version = pkg.version;
9
+
10
+ const PLATFORM_MAP = {
11
+ darwin: "darwin",
12
+ linux: "linux",
13
+ win32: "windows",
14
+ };
15
+
16
+ const ARCH_MAP = {
17
+ x64: "amd64",
18
+ arm64: "arm64",
19
+ };
20
+
21
+ function getPlatformArch() {
22
+ const os = PLATFORM_MAP[process.platform];
23
+ const arch = ARCH_MAP[process.arch];
24
+ if (!os) {
25
+ throw new Error(`Unsupported platform: ${process.platform}`);
26
+ }
27
+ if (!arch) {
28
+ throw new Error(`Unsupported architecture: ${process.arch}`);
29
+ }
30
+ return { os, arch };
31
+ }
32
+
33
+ function downloadUrl() {
34
+ const { os, arch } = getPlatformArch();
35
+ const ext = os === "windows" ? "zip" : "tar.gz";
36
+ return `https://github.com/smm-h/pgdesign/releases/download/v${version}/pgdesign_${version}_${os}_${arch}.${ext}`;
37
+ }
38
+
39
+ function follow(url, redirects) {
40
+ if (redirects > 5) {
41
+ return Promise.reject(new Error("Too many redirects"));
42
+ }
43
+ return new Promise((resolve, reject) => {
44
+ const proto = url.startsWith("https") ? https : http;
45
+ proto.get(url, { headers: { "User-Agent": "pgdesign-npm" } }, (res) => {
46
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
47
+ resolve(follow(res.headers.location, redirects + 1));
48
+ return;
49
+ }
50
+ if (res.statusCode !== 200) {
51
+ reject(new Error(`Download failed: HTTP ${res.statusCode} from ${url}`));
52
+ return;
53
+ }
54
+ resolve(res);
55
+ }).on("error", reject);
56
+ });
57
+ }
58
+
59
+ async function download(url, dest) {
60
+ const res = await follow(url, 0);
61
+ return new Promise((resolve, reject) => {
62
+ const file = fs.createWriteStream(dest);
63
+ res.pipe(file);
64
+ file.on("finish", () => file.close(resolve));
65
+ file.on("error", reject);
66
+ });
67
+ }
68
+
69
+ function extract(archive, destDir) {
70
+ const isZip = archive.endsWith(".zip");
71
+ if (isZip) {
72
+ if (process.platform === "win32") {
73
+ execSync(
74
+ `powershell -Command "Expand-Archive -Path '${archive}' -DestinationPath '${destDir}' -Force"`,
75
+ );
76
+ } else {
77
+ execSync(`unzip -o "${archive}" -d "${destDir}"`);
78
+ }
79
+ } else {
80
+ execSync(`tar xzf "${archive}" -C "${destDir}"`);
81
+ }
82
+ }
83
+
84
+ function findBinary(dir) {
85
+ const name = process.platform === "win32" ? "pgdesign.exe" : "pgdesign";
86
+ // Check top-level first
87
+ const top = path.join(dir, name);
88
+ if (fs.existsSync(top)) return top;
89
+ // Check subdirectories
90
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
91
+ if (entry.isDirectory()) {
92
+ const nested = path.join(dir, entry.name, name);
93
+ if (fs.existsSync(nested)) return nested;
94
+ }
95
+ }
96
+ throw new Error(`Could not find ${name} in extracted archive`);
97
+ }
98
+
99
+ async function main() {
100
+ const url = downloadUrl();
101
+ const isZip = url.endsWith(".zip");
102
+ const archiveName = `pgdesign-download${isZip ? ".zip" : ".tar.gz"}`;
103
+ const archivePath = path.join(__dirname, archiveName);
104
+ const extractDir = path.join(__dirname, "pgdesign-extract");
105
+ const binaryName = process.platform === "win32" ? "pgdesign.exe" : "pgdesign";
106
+ const binaryDest = path.join(__dirname, binaryName);
107
+
108
+ console.log(`Downloading pgdesign v${version}...`);
109
+ console.log(` ${url}`);
110
+
111
+ await download(url, archivePath);
112
+
113
+ fs.mkdirSync(extractDir, { recursive: true });
114
+ extract(archivePath, extractDir);
115
+
116
+ const binarySrc = findBinary(extractDir);
117
+ fs.copyFileSync(binarySrc, binaryDest);
118
+
119
+ if (process.platform !== "win32") {
120
+ fs.chmodSync(binaryDest, 0o755);
121
+ }
122
+
123
+ // Clean up
124
+ fs.unlinkSync(archivePath);
125
+ fs.rmSync(extractDir, { recursive: true, force: true });
126
+
127
+ console.log(`pgdesign v${version} installed successfully.`);
128
+ }
129
+
130
+ main().catch((err) => {
131
+ console.error(`Failed to install pgdesign: ${err.message}`);
132
+ process.exit(1);
133
+ });
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "pgdesign",
3
+ "version": "0.1.2",
4
+ "description": "PostgreSQL schema compiler with strict enforcement, NF auditing, and declarative migrations",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/smm-h/pgdesign.git"
9
+ },
10
+ "homepage": "https://github.com/smm-h/pgdesign",
11
+ "scripts": {
12
+ "postinstall": "node install.js"
13
+ },
14
+ "bin": {
15
+ "pgdesign": "bin/pgdesign"
16
+ },
17
+ "files": [
18
+ "install.js",
19
+ "bin/"
20
+ ],
21
+ "keywords": [
22
+ "rlsbl"
23
+ ]
24
+ }