@kernelminds/create-enclave 0.0.1

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.
package/src/package.ts ADDED
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env node
2
+
3
+ import child_process = require("child_process");
4
+ import path = require("path");
5
+ import prompt = require('@inquirer/prompts');
6
+ import YAML = require('yaml');
7
+ import fs = require("fs");
8
+ import semver = require("semver");
9
+ import AdmZip = require("adm-zip");
10
+ import favicons = require("favicons");
11
+
12
+ async function loadManifest() {
13
+ const file = fs.readFileSync('MANIFEST.yaml', 'utf8')
14
+ return YAML.parse(file);
15
+ }
16
+
17
+ async function acceptUserInputs({ existingVersion }: { existingVersion: string }) {
18
+ let nextVersion = semver.inc(existingVersion, "patch") || "0.0.1";
19
+ const userEnteredVersion = (await prompt.input({
20
+ message: "Current version: " + existingVersion + ". Enter the Next Version (Semver Format): ",
21
+ default: nextVersion,
22
+ required: true,
23
+ validate: input => input.length > 0
24
+ })).trim();
25
+
26
+ if (!semver.valid(userEnteredVersion)) {
27
+ console.log("Invalid semver: " + userEnteredVersion);
28
+ process.exit(1);
29
+ }
30
+
31
+ if (semver.compare(userEnteredVersion, existingVersion) <= 0) {
32
+ console.log(`Version should be greater than ${existingVersion}`);
33
+ process.exit(1);
34
+ } else {
35
+
36
+ }
37
+
38
+ return userEnteredVersion;
39
+ }
40
+
41
+ function spawnChildProcess(command: string, args: string[] = [], options = {}) {
42
+ return new Promise((resolve, reject) => {
43
+ const child = child_process.spawn(command, args, { ...options, shell: process.platform === "win32" ? true : undefined });
44
+
45
+ // Optional: Log stdout and stderr for debugging
46
+ child.stdout.on('data', (data) => {
47
+ console.log(`${data}`);
48
+ });
49
+
50
+ // child.stderr.on('data', (data) => {
51
+ // console.error(`stderr: ${data}`);
52
+ // });
53
+
54
+ child.on('close', (code) => {
55
+ if (code === 0) {
56
+ resolve(`Child process exited with code ${code}`);
57
+ } else {
58
+ reject(new Error(`Child process exited with code ${code}`));
59
+ }
60
+ });
61
+
62
+ child.on('error', (err) => {
63
+ reject(err);
64
+ });
65
+ });
66
+ }
67
+
68
+ async function generateSimpleFavicon({ sourceFile, outputDir, fileName }: { sourceFile: string, outputDir: string, fileName: string }) {
69
+ try {
70
+ // 1. Configure to only generate the standard favicon
71
+ console.log('Generating favicon.ico...');
72
+ const response = await favicons.favicons(sourceFile, {
73
+ icons: {
74
+ android: false,
75
+ appleIcon: false,
76
+ appleStartup: false,
77
+ favicons: true, // This enables the .ico file
78
+ windows: false,
79
+ yandex: false
80
+ }
81
+ });
82
+
83
+ // 2. Find the specific .ico file in the response images array
84
+ const favicon = response.images.find(img => img.name === "favicon.ico");
85
+
86
+ if (favicon) {
87
+ await fs.writeFileSync(path.join(outputDir, fileName), favicon.contents);
88
+ console.log(`Success! Created: ${path.join(outputDir, fileName)}`);
89
+ } else {
90
+ console.error('Could not find favicon.ico in the generated output.');
91
+ }
92
+
93
+ } catch (error) {
94
+ console.error('Generation failed:', error);
95
+ }
96
+ }
97
+
98
+ interface MANIFEST_ENV {
99
+ /**The name of the environment variable */
100
+ name: string;
101
+ /**The value of the environment variable */
102
+ value: string;
103
+ /**Stores if the environment variable is a secret */
104
+ is_secret: boolean;
105
+ }
106
+
107
+ interface MANIFEST {
108
+ manifest_version: string;
109
+ enclave_runtime: "node" | "golang" | "python";
110
+ app_version: string;
111
+ app_name: string;
112
+ enclave_name: string;
113
+ app_unique_identifier: string;
114
+ start_exec: string;
115
+ entry_point_management: "platform_redirect" | "direct_url";
116
+ env_variables: MANIFEST_ENV[];
117
+ resources: {
118
+ logos: string[];
119
+ folders: string[];
120
+ files: string[];
121
+ }
122
+ }
123
+
124
+
125
+ async function main() {
126
+ let manifest: MANIFEST = await loadManifest();
127
+ let userEnteredVersion = await acceptUserInputs({ existingVersion: manifest.app_version });
128
+ if (manifest.entry_point_management !== "platform_redirect" && manifest.entry_point_management !== "direct_url") {
129
+ console.log("Invalid entry point: " + manifest.entry_point_management + ". Should be either 'platform_redirect' or 'direct_url'");
130
+ process.exit(1);
131
+ }
132
+
133
+ manifest.app_version = userEnteredVersion;
134
+ fs.writeFileSync('MANIFEST.yaml', YAML.stringify(manifest, { indent: 4 }));
135
+
136
+ // Create the favicon here
137
+ await generateSimpleFavicon({ sourceFile: path.join("resources", "dist", "img", "logo.png"), outputDir: path.join("resources", "dist", "img"), fileName: "favicon.ico" });
138
+
139
+ await spawnChildProcess("npm", ["run", "css:build"]);
140
+ await spawnChildProcess("npm", ["run", "ui:build"]);
141
+ fs.mkdirSync(path.join("artifacts", "resources"), { recursive: true });
142
+ fs.copyFileSync("MANIFEST.yaml", path.join("artifacts", "MANIFEST.yaml"));
143
+
144
+ fs.cpSync(path.join("resources", "dist"), path.join("artifacts", "resources", "dist"), { recursive: true });
145
+
146
+ fs.copyFileSync("package.json", path.join("artifacts", "package.json"));
147
+ fs.copyFileSync("package-lock.json", path.join("artifacts", "package-lock.json"));
148
+
149
+ let foldersToCopy = manifest.resources.folders;
150
+ if (foldersToCopy && foldersToCopy.length > 0) {
151
+ for (const folder of foldersToCopy) {
152
+ fs.mkdirSync(path.join("artifacts", folder), { recursive: true });
153
+ fs.cpSync(path.join(folder), path.join("artifacts", folder), { recursive: true });
154
+ }
155
+ }
156
+
157
+ let filesToCopy = manifest.resources.files;
158
+ if (filesToCopy && filesToCopy.length > 0) {
159
+ for (const file of filesToCopy) {
160
+ fs.copyFileSync(file, path.join("artifacts", file));
161
+ }
162
+ }
163
+
164
+ process.chdir("artifacts");
165
+ // Zip the folder
166
+ const zip = new AdmZip();
167
+
168
+ zip.addLocalFolder(process.cwd());
169
+ process.chdir("..");
170
+
171
+ const outputName = `${manifest.app_name}.enc`;
172
+
173
+ zip.writeZip(outputName);
174
+ fs.rmSync(path.join("artifacts"), { recursive: true });
175
+
176
+ console.log(`Successfully package: ${outputName} (${userEnteredVersion})`);
177
+ }
178
+
179
+ main();