@naesofteak/naeso 0.0.0-stage → 1.0.0

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.
@@ -0,0 +1,225 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
4
+
5
+ function expandHome(value) {
6
+ if (!value) {
7
+ return value;
8
+ }
9
+
10
+ if (value === "~") {
11
+ return process.env.HOME;
12
+ }
13
+
14
+ if (value.startsWith("~/")) {
15
+ return path.join(
16
+ process.env.HOME,
17
+ value.slice(2)
18
+ );
19
+ }
20
+
21
+ return value;
22
+ }
23
+
24
+
25
+ function getShellConfigFile() {
26
+ const shell =
27
+ process.env.SHELL || "";
28
+
29
+ if (
30
+ shell.endsWith("/zsh")
31
+ ) {
32
+ return path.join(
33
+ process.env.HOME,
34
+ ".zprofile"
35
+ );
36
+ }
37
+
38
+ if (
39
+ shell.endsWith("/bash")
40
+ ) {
41
+ return path.join(
42
+ process.env.HOME,
43
+ ".bash_profile"
44
+ );
45
+ }
46
+
47
+ return path.join(
48
+ process.env.HOME,
49
+ ".profile"
50
+ );
51
+ }
52
+
53
+
54
+ function configurePath(paths) {
55
+ if (
56
+ !Array.isArray(paths)
57
+ ) {
58
+ return {
59
+ shellConfig:
60
+ getShellConfigFile(),
61
+ changed: false
62
+ };
63
+ }
64
+
65
+ const shellConfig =
66
+ getShellConfigFile();
67
+
68
+ let content = "";
69
+
70
+ if (
71
+ fs.existsSync(
72
+ shellConfig
73
+ )
74
+ ) {
75
+ content =
76
+ fs.readFileSync(
77
+ shellConfig,
78
+ "utf8"
79
+ );
80
+ }
81
+
82
+ let changed = false;
83
+
84
+ for (
85
+ const configuredPath of paths
86
+ ) {
87
+ const pathLine =
88
+ `export PATH="${configuredPath}:$PATH"`;
89
+
90
+ if (
91
+ content.includes(
92
+ pathLine
93
+ )
94
+ ) {
95
+ continue;
96
+ }
97
+
98
+ if (
99
+ !content.endsWith("\n")
100
+ ) {
101
+ content += "\n";
102
+ }
103
+
104
+ content +=
105
+ `# Naeso PATH\n${pathLine}\n`;
106
+
107
+ changed = true;
108
+ }
109
+
110
+ if (
111
+ changed
112
+ ) {
113
+ fs.writeFileSync(
114
+ shellConfig,
115
+ content
116
+ );
117
+ }
118
+
119
+ return {
120
+ shellConfig,
121
+ changed
122
+ };
123
+ }
124
+
125
+
126
+ function removePath(paths) {
127
+ if (
128
+ !Array.isArray(paths)
129
+ ) {
130
+ return {
131
+ shellConfig:
132
+ getShellConfigFile(),
133
+ changed: false
134
+ };
135
+ }
136
+
137
+ const shellConfig =
138
+ getShellConfigFile();
139
+
140
+ if (
141
+ !fs.existsSync(
142
+ shellConfig
143
+ )
144
+ ) {
145
+ return {
146
+ shellConfig,
147
+ changed: false
148
+ };
149
+ }
150
+
151
+ let content =
152
+ fs.readFileSync(
153
+ shellConfig,
154
+ "utf8"
155
+ );
156
+
157
+ let changed = false;
158
+
159
+ for (
160
+ const configuredPath of paths
161
+ ) {
162
+ const pathLine =
163
+ `export PATH="${configuredPath}:$PATH"`;
164
+
165
+ const block =
166
+ `# Naeso PATH\n${pathLine}\n`;
167
+
168
+ if (
169
+ content.includes(
170
+ block
171
+ )
172
+ ) {
173
+ content =
174
+ content.replace(
175
+ block,
176
+ ""
177
+ );
178
+
179
+ changed = true;
180
+ continue;
181
+ }
182
+
183
+ if (
184
+ content.includes(
185
+ pathLine
186
+ )
187
+ ) {
188
+ content =
189
+ content.replace(
190
+ `${pathLine}\n`,
191
+ ""
192
+ );
193
+
194
+ content =
195
+ content.replace(
196
+ pathLine,
197
+ ""
198
+ );
199
+
200
+ changed = true;
201
+ }
202
+ }
203
+
204
+ if (
205
+ changed
206
+ ) {
207
+ fs.writeFileSync(
208
+ shellConfig,
209
+ content
210
+ );
211
+ }
212
+
213
+ return {
214
+ shellConfig,
215
+ changed
216
+ };
217
+ }
218
+
219
+
220
+ module.exports = {
221
+ configurePath,
222
+ removePath,
223
+ getShellConfigFile,
224
+ expandHome
225
+ };
@@ -0,0 +1,235 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const { spawn } = require("child_process");
4
+
5
+ function runCommand(command, args) {
6
+ return new Promise((resolve, reject) => {
7
+ const process = spawn(command, args);
8
+
9
+ let stderr = "";
10
+
11
+ process.stderr.on("data", (data) => {
12
+ stderr += data.toString();
13
+ });
14
+
15
+ process.on("error", reject);
16
+
17
+ process.on("close", (code) => {
18
+ if (code !== 0) {
19
+ return reject(
20
+ new Error(
21
+ `${command} failed: ${stderr.trim()}`
22
+ )
23
+ );
24
+ }
25
+
26
+ resolve();
27
+ });
28
+ });
29
+ }
30
+
31
+ async function extractTarball(
32
+ archivePath,
33
+ destination
34
+ ) {
35
+ fs.mkdirSync(destination, {
36
+ recursive: true
37
+ });
38
+
39
+ await runCommand(
40
+ "tar",
41
+ [
42
+ "-xzf",
43
+ archivePath,
44
+ "-C",
45
+ destination
46
+ ]
47
+ );
48
+
49
+ return destination;
50
+ }
51
+
52
+ async function extractZip(
53
+ archivePath,
54
+ destination
55
+ ) {
56
+ fs.mkdirSync(destination, {
57
+ recursive: true
58
+ });
59
+
60
+ /*
61
+ * macOS provides /usr/bin/ditto.
62
+ * Using ditto avoids requiring Homebrew
63
+ * or an npm ZIP extraction dependency.
64
+ */
65
+ await runCommand(
66
+ "/usr/bin/ditto",
67
+ [
68
+ "-x",
69
+ "-k",
70
+ archivePath,
71
+ destination
72
+ ]
73
+ );
74
+
75
+ return destination;
76
+ }
77
+
78
+ async function extractArchive(
79
+ archivePath,
80
+ type,
81
+ destination
82
+ ) {
83
+ if (!fs.existsSync(archivePath)) {
84
+ throw new Error(
85
+ `Archive not found: ${archivePath}`
86
+ );
87
+ }
88
+
89
+ switch (type) {
90
+ case "tarball":
91
+ case "tgz":
92
+ return extractTarball(
93
+ archivePath,
94
+ destination
95
+ );
96
+
97
+ case "zip":
98
+ return extractZip(
99
+ archivePath,
100
+ destination
101
+ );
102
+
103
+ default:
104
+ throw new Error(
105
+ `Unsupported archive type: ${type}`
106
+ );
107
+ }
108
+ }
109
+
110
+ function findDirectory(
111
+ directory,
112
+ predicate
113
+ ) {
114
+ const entries = fs.readdirSync(
115
+ directory,
116
+ {
117
+ withFileTypes: true
118
+ }
119
+ );
120
+
121
+ return entries.find(
122
+ predicate
123
+ );
124
+ }
125
+
126
+ function findMongoRoot(
127
+ extractDirectory
128
+ ) {
129
+ const directory =
130
+ findDirectory(
131
+ extractDirectory,
132
+ (entry) =>
133
+ entry.isDirectory() &&
134
+ entry.name.startsWith("mongodb-")
135
+ );
136
+
137
+ if (!directory) {
138
+ throw new Error(
139
+ "MongoDB server directory not found after extraction."
140
+ );
141
+ }
142
+
143
+ return path.join(
144
+ extractDirectory,
145
+ directory.name
146
+ );
147
+ }
148
+
149
+ function findMongoshRoot(
150
+ extractDirectory
151
+ ) {
152
+ const directory =
153
+ findDirectory(
154
+ extractDirectory,
155
+ (entry) =>
156
+ entry.isDirectory() &&
157
+ entry.name.startsWith("mongosh-")
158
+ );
159
+
160
+ if (!directory) {
161
+ throw new Error(
162
+ "mongosh directory not found after extraction."
163
+ );
164
+ }
165
+
166
+ return path.join(
167
+ extractDirectory,
168
+ directory.name
169
+ );
170
+ }
171
+
172
+ function findDatabaseToolsRoot(
173
+ extractDirectory
174
+ ) {
175
+ const directory =
176
+ findDirectory(
177
+ extractDirectory,
178
+ (entry) =>
179
+ entry.isDirectory() &&
180
+ entry.name.startsWith(
181
+ "mongodb-database-tools-"
182
+ )
183
+ );
184
+
185
+ if (!directory) {
186
+ throw new Error(
187
+ "MongoDB Database Tools directory not found after extraction."
188
+ );
189
+ }
190
+
191
+ return path.join(
192
+ extractDirectory,
193
+ directory.name
194
+ );
195
+ }
196
+
197
+ function findBinary(
198
+ rootDirectory,
199
+ binaryName
200
+ ) {
201
+ const directPath =
202
+ path.join(
203
+ rootDirectory,
204
+ "bin",
205
+ binaryName
206
+ );
207
+
208
+ if (fs.existsSync(directPath)) {
209
+ return directPath;
210
+ }
211
+
212
+ const alternativePath =
213
+ path.join(
214
+ rootDirectory,
215
+ binaryName
216
+ );
217
+
218
+ if (fs.existsSync(alternativePath)) {
219
+ return alternativePath;
220
+ }
221
+
222
+ throw new Error(
223
+ `Binary '${binaryName}' not found in ${rootDirectory}.`
224
+ );
225
+ }
226
+
227
+ module.exports = {
228
+ extractTarball,
229
+ extractZip,
230
+ extractArchive,
231
+ findMongoRoot,
232
+ findMongoshRoot,
233
+ findDatabaseToolsRoot,
234
+ findBinary
235
+ };