codify-plugin-lib 1.0.182-beta7 → 1.0.182-beta71

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 (53) hide show
  1. package/bin/build.js +189 -0
  2. package/dist/bin/build.js +0 -0
  3. package/dist/index.d.ts +3 -0
  4. package/dist/index.js +3 -0
  5. package/dist/messages/handlers.js +10 -2
  6. package/dist/plan/plan.js +45 -3
  7. package/dist/plugin/plugin.d.ts +2 -1
  8. package/dist/plugin/plugin.js +6 -1
  9. package/dist/pty/background-pty.d.ts +3 -2
  10. package/dist/pty/background-pty.js +7 -14
  11. package/dist/pty/index.d.ts +8 -2
  12. package/dist/pty/seqeuntial-pty.d.ts +3 -2
  13. package/dist/pty/seqeuntial-pty.js +47 -12
  14. package/dist/resource/parsed-resource-settings.d.ts +5 -2
  15. package/dist/resource/parsed-resource-settings.js +16 -2
  16. package/dist/resource/resource-controller.js +5 -5
  17. package/dist/resource/resource-settings.d.ts +13 -3
  18. package/dist/resource/resource-settings.js +2 -2
  19. package/dist/test.d.ts +1 -0
  20. package/dist/test.js +5 -0
  21. package/dist/utils/file-utils.d.ts +14 -7
  22. package/dist/utils/file-utils.js +65 -51
  23. package/dist/utils/functions.js +2 -2
  24. package/dist/utils/index.d.ts +21 -1
  25. package/dist/utils/index.js +160 -0
  26. package/dist/utils/load-resources.d.ts +1 -0
  27. package/dist/utils/load-resources.js +46 -0
  28. package/dist/utils/package-json-utils.d.ts +12 -0
  29. package/dist/utils/package-json-utils.js +34 -0
  30. package/package.json +5 -4
  31. package/rollup.config.js +24 -0
  32. package/src/index.ts +3 -0
  33. package/src/messages/handlers.test.ts +23 -0
  34. package/src/messages/handlers.ts +11 -2
  35. package/src/plan/plan.test.ts +46 -0
  36. package/src/plan/plan.ts +65 -4
  37. package/src/plugin/plugin.test.ts +31 -0
  38. package/src/plugin/plugin.ts +8 -2
  39. package/src/pty/background-pty.ts +10 -18
  40. package/src/pty/index.ts +10 -4
  41. package/src/pty/seqeuntial-pty.ts +62 -16
  42. package/src/pty/sequential-pty.test.ts +137 -4
  43. package/src/resource/parsed-resource-settings.test.ts +24 -0
  44. package/src/resource/parsed-resource-settings.ts +26 -8
  45. package/src/resource/resource-controller.test.ts +126 -0
  46. package/src/resource/resource-controller.ts +5 -6
  47. package/src/resource/resource-settings.test.ts +36 -0
  48. package/src/resource/resource-settings.ts +17 -5
  49. package/src/utils/file-utils.test.ts +7 -0
  50. package/src/utils/file-utils.ts +70 -55
  51. package/src/utils/functions.ts +3 -3
  52. package/src/utils/index.ts +197 -1
  53. package/src/utils/internal-utils.test.ts +1 -0
@@ -25,7 +25,7 @@ export class FileUtils {
25
25
  console.log(`Finished downloading to ${destination}`);
26
26
  }
27
27
 
28
- static async addToStartupFile(line: string): Promise<void> {
28
+ static async addToShellRc(line: string): Promise<void> {
29
29
  const lineToInsert = addLeadingSpacer(
30
30
  addTrailingSpacer(line)
31
31
  );
@@ -45,7 +45,7 @@ export class FileUtils {
45
45
  }
46
46
  }
47
47
 
48
- static async addAllToStartupFile(lines: string[]): Promise<void> {
48
+ static async addAllToShellRc(lines: string[]): Promise<void> {
49
49
  const formattedLines = '\n' + lines.join('\n') + '\n';
50
50
  const shellRc = Utils.getPrimaryShellRc();
51
51
 
@@ -55,7 +55,17 @@ ${lines.join('\n')}`)
55
55
  await fs.appendFile(shellRc, formattedLines)
56
56
  }
57
57
 
58
- static async addPathToPrimaryShellRc(value: string, prepend: boolean): Promise<void> {
58
+ /**
59
+ * This method adds a directory path to the shell rc file if it doesn't already exist.
60
+ *
61
+ * @param value - The directory path to add.
62
+ * @param prepend - Whether to prepend the path to the existing PATH variable.
63
+ */
64
+ static async addPathToShellRc(value: string, prepend: boolean): Promise<void> {
65
+ if (await Utils.isDirectoryOnPath(value)) {
66
+ return;
67
+ }
68
+
59
69
  const shellRc = Utils.getPrimaryShellRc();
60
70
  console.log(`Saving path: ${value} to ${shellRc}`);
61
71
 
@@ -67,56 +77,6 @@ ${lines.join('\n')}`)
67
77
  await fs.appendFile(shellRc, `\nexport PATH=${value}:$PATH;`, { encoding: 'utf8' });
68
78
  }
69
79
 
70
- static async dirExists(path: string): Promise<boolean> {
71
- let stat;
72
- try {
73
- stat = await fs.stat(path);
74
- return stat.isDirectory();
75
- } catch {
76
- return false;
77
- }
78
- }
79
-
80
- static async fileExists(path: string): Promise<boolean> {
81
- let stat;
82
- try {
83
- stat = await fs.stat(path);
84
- return stat.isFile();
85
- } catch {
86
- return false;
87
- }
88
- }
89
-
90
- static async exists(path: string): Promise<boolean> {
91
- try {
92
- await fs.stat(path);
93
- return true;
94
- } catch {
95
- return false;
96
- }
97
- }
98
-
99
- static async checkDirExistsOrThrowIfFile(path: string): Promise<boolean> {
100
- let stat;
101
- try {
102
- stat = await fs.stat(path);
103
- } catch {
104
- return false;
105
- }
106
-
107
- if (stat.isDirectory()) {
108
- return true;
109
- }
110
-
111
- throw new Error(`Directory ${path} already exists and is a file`);
112
- }
113
-
114
- static async createDirIfNotExists(path: string): Promise<void> {
115
- if (!fsSync.existsSync(path)) {
116
- await fs.mkdir(path, { recursive: true });
117
- }
118
- }
119
-
120
80
  static async removeFromFile(filePath: string, search: string): Promise<void> {
121
81
  const contents = await fs.readFile(filePath, 'utf8');
122
82
  const newContents = contents.replaceAll(search, '');
@@ -124,7 +84,6 @@ ${lines.join('\n')}`)
124
84
  await fs.writeFile(filePath, newContents, 'utf8');
125
85
  }
126
86
 
127
-
128
87
  static async removeLineFromFile(filePath: string, search: RegExp | string): Promise<void> {
129
88
  const file = await fs.readFile(filePath, 'utf8')
130
89
  const lines = file.split('\n');
@@ -168,10 +127,16 @@ ${lines.join('\n')}`)
168
127
  console.log(`Removed line: ${search} from ${filePath}`)
169
128
  }
170
129
 
171
- static async removeLineFromPrimaryShellRc(search: RegExp | string): Promise<void> {
130
+ static async removeLineFromShellRc(search: RegExp | string): Promise<void> {
172
131
  return FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
173
132
  }
174
133
 
134
+ static async removeAllLinesFromShellRc(searches: Array<RegExp | string>): Promise<void> {
135
+ for (const search of searches) {
136
+ await FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
137
+ }
138
+ }
139
+
175
140
  // Append the string to the end of a file ensuring at least 1 lines of space between.
176
141
  // Ex result:
177
142
  // something something;
@@ -190,6 +155,56 @@ ${lines.join('\n')}`)
190
155
  return lines.join('\n') + '\n'.repeat(numNewLines) + textToInsert
191
156
  }
192
157
 
158
+ static async dirExists(path: string): Promise<boolean> {
159
+ let stat;
160
+ try {
161
+ stat = await fs.stat(path);
162
+ return stat.isDirectory();
163
+ } catch {
164
+ return false;
165
+ }
166
+ }
167
+
168
+ static async fileExists(path: string): Promise<boolean> {
169
+ let stat;
170
+ try {
171
+ stat = await fs.stat(path);
172
+ return stat.isFile();
173
+ } catch {
174
+ return false;
175
+ }
176
+ }
177
+
178
+ static async exists(path: string): Promise<boolean> {
179
+ try {
180
+ await fs.stat(path);
181
+ return true;
182
+ } catch {
183
+ return false;
184
+ }
185
+ }
186
+
187
+ static async checkDirExistsOrThrowIfFile(path: string): Promise<boolean> {
188
+ let stat;
189
+ try {
190
+ stat = await fs.stat(path);
191
+ } catch {
192
+ return false;
193
+ }
194
+
195
+ if (stat.isDirectory()) {
196
+ return true;
197
+ }
198
+
199
+ throw new Error(`Directory ${path} already exists and is a file`);
200
+ }
201
+
202
+ static async createDirIfNotExists(path: string): Promise<void> {
203
+ if (!fsSync.existsSync(path)) {
204
+ await fs.mkdir(path, { recursive: true });
205
+ }
206
+ }
207
+
193
208
  // This is overly complicated but it can be used to insert into any
194
209
  // position in the future
195
210
  private static calculateEndingNewLines(lines: string[]): number {
@@ -9,10 +9,11 @@ export function splitUserConfig<T extends StringIndexedObject>(
9
9
  type: config.type,
10
10
  ...(config.name ? { name: config.name } : {}),
11
11
  ...(config.dependsOn ? { dependsOn: config.dependsOn } : {}),
12
+ ...(config.os ? { os: config.os } : {}),
12
13
  };
13
14
 
14
15
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
- const { type, name, dependsOn, ...parameters } = config;
16
+ const { type, name, dependsOn, os, ...parameters } = config;
16
17
 
17
18
  return {
18
19
  parameters: parameters as T,
@@ -35,8 +36,7 @@ export function tildify(pathWithTilde: string) {
35
36
  }
36
37
 
37
38
  export function resolvePathWithVariables(pathWithVariables: string) {
38
- // @ts-expect-error Ignore this for now
39
- return pathWithVariables.replace(/\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/ig, (_, a, b) => process.env[a || b])
39
+ return pathWithVariables.replace(/\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/ig, (_, a, b) => process.env[a || b]!)
40
40
  }
41
41
 
42
42
  export function addVariablesToPath(pathWithoutVariables: string) {
@@ -1,7 +1,11 @@
1
- import { OS } from 'codify-schemas';
1
+ import { LinuxDistro, OS } from 'codify-schemas';
2
+ import * as fsSync from 'node:fs';
3
+ import * as fs from 'node:fs/promises';
2
4
  import os from 'node:os';
3
5
  import path from 'node:path';
4
6
 
7
+ import { SpawnStatus, getPty } from '../pty/index.js';
8
+
5
9
  export function isDebug(): boolean {
6
10
  return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
7
11
  }
@@ -40,6 +44,34 @@ export const Utils = {
40
44
  return os.platform() === 'linux';
41
45
  },
42
46
 
47
+ async isArmArch(): Promise<boolean> {
48
+ const $ = getPty();
49
+ if (!Utils.isMacOS()) {
50
+ // On Linux, check uname -m
51
+ const query = await $.spawn('uname -m');
52
+ return query.data.trim() === 'aarch64' || query.data.trim() === 'arm64';
53
+ }
54
+
55
+ const query = await $.spawn('sysctl -n machdep.cpu.brand_string');
56
+ return /M(\d)/.test(query.data);
57
+ },
58
+
59
+ async isHomebrewInstalled(): Promise<boolean> {
60
+ const $ = getPty();
61
+ const query = await $.spawnSafe('which brew', { interactive: true });
62
+ return query.status === SpawnStatus.SUCCESS;
63
+ },
64
+
65
+ async isRosetta2Installed(): Promise<boolean> {
66
+ if (!Utils.isMacOS()) {
67
+ return false;
68
+ }
69
+
70
+ const $ = getPty();
71
+ const query = await $.spawnSafe('arch -x86_64 /usr/bin/true 2> /dev/null', { interactive: true });
72
+ return query.status === SpawnStatus.SUCCESS;
73
+ },
74
+
43
75
  getShell(): Shell | undefined {
44
76
  const shell = process.env.SHELL || '';
45
77
 
@@ -138,6 +170,170 @@ export const Utils = {
138
170
  path.join(homeDir, '.profile'),
139
171
  ];
140
172
  },
173
+
174
+ async isDirectoryOnPath(directory: string): Promise<boolean> {
175
+ const $ = getPty();
176
+ const { data: pathQuery } = await $.spawn('echo $PATH', { interactive: true });
177
+ const lines = pathQuery.split(':');
178
+ return lines.includes(directory);
179
+ },
180
+
181
+ async assertBrewInstalled(): Promise<void> {
182
+ const $ = getPty();
183
+ const brewCheck = await $.spawnSafe('which brew', { interactive: true });
184
+ if (brewCheck.status === SpawnStatus.ERROR) {
185
+ throw new Error(
186
+ `Homebrew is not installed. Cannot install git-lfs without Homebrew installed.
187
+
188
+ Brew can be installed using Codify:
189
+ {
190
+ "type": "homebrew",
191
+ }`
192
+ );
193
+ }
194
+ },
195
+
196
+ /**
197
+ * Installs a package via the system package manager. This will use Homebrew on macOS and apt on Ubuntu/Debian or dnf on Fedora.
198
+ * @param packageName
199
+ */
200
+ async installViaPkgMgr(packageName: string): Promise<void> {
201
+ const $ = getPty();
202
+
203
+ if (Utils.isMacOS()) {
204
+ await this.assertBrewInstalled();
205
+ await $.spawn(`brew install ${packageName}`, { interactive: true, env: { HOMEBREW_NO_AUTO_UPDATE: 1 } });
206
+ }
207
+
208
+ if (Utils.isLinux()) {
209
+ const isAptInstalled = await $.spawnSafe('which apt');
210
+ if (isAptInstalled.status === SpawnStatus.SUCCESS) {
211
+ await $.spawn('apt-get update', { requiresRoot: true });
212
+ const { status, data } = await $.spawnSafe(`apt-get -y install ${packageName}`, {
213
+ requiresRoot: true,
214
+ env: { DEBIAN_FRONTEND: 'noninteractive' }
215
+ });
216
+
217
+ if (status === SpawnStatus.ERROR && data.includes('E: dpkg was interrupted, you must manually run \'sudo dpkg --configure -a\' to correct the problem.')) {
218
+ await $.spawn('dpkg --configure -a', { requiresRoot: true });
219
+ await $.spawn(`apt-get -y install ${packageName}`, {
220
+ requiresRoot: true,
221
+ env: { DEBIAN_FRONTEND: 'noninteractive' }
222
+ });
223
+
224
+ return;
225
+ }
226
+
227
+ if (status === SpawnStatus.ERROR) {
228
+ throw new Error(`Failed to install package ${packageName} via apt: ${data}`);
229
+ }
230
+ }
231
+
232
+ const isDnfInstalled = await $.spawnSafe('which dnf');
233
+ if (isDnfInstalled.status === SpawnStatus.SUCCESS) {
234
+ await $.spawn('dnf update', { requiresRoot: true });
235
+ await $.spawn(`dnf install ${packageName} -y`, { requiresRoot: true });
236
+ }
237
+
238
+ const isYumInstalled = await $.spawnSafe('which yum');
239
+ if (isYumInstalled.status === SpawnStatus.SUCCESS) {
240
+ await $.spawn('yum update', { requiresRoot: true });
241
+ await $.spawn(`yum install ${packageName} -y`, { requiresRoot: true });
242
+ }
243
+
244
+ const isPacmanInstalled = await $.spawnSafe('which pacman');
245
+ if (isPacmanInstalled.status === SpawnStatus.SUCCESS) {
246
+ await $.spawn('pacman -Syu', { requiresRoot: true });
247
+ await $.spawn(`pacman -S ${packageName} --noconfirm`, { requiresRoot: true });
248
+ }
249
+
250
+ }
251
+ },
252
+
253
+ async uninstallViaPkgMgr(packageName: string): Promise<boolean> {
254
+ const $ = getPty();
255
+
256
+ if (Utils.isMacOS()) {
257
+ await this.assertBrewInstalled();
258
+ const { status } = await $.spawnSafe(`brew uninstall --zap ${packageName}`, {
259
+ interactive: true,
260
+ env: { HOMEBREW_NO_AUTO_UPDATE: 1 }
261
+ });
262
+ return status === SpawnStatus.SUCCESS;
263
+ }
264
+
265
+ if (Utils.isLinux()) {
266
+ const isAptInstalled = await $.spawnSafe('which apt');
267
+ if (isAptInstalled.status === SpawnStatus.SUCCESS) {
268
+ const { status } = await $.spawnSafe(`apt-get autoremove -y --purge ${packageName}`, {
269
+ requiresRoot: true,
270
+ env: { DEBIAN_FRONTEND: 'noninteractive' }
271
+ });
272
+ return status === SpawnStatus.SUCCESS;
273
+ }
274
+
275
+ const isDnfInstalled = await $.spawnSafe('which dnf');
276
+ if (isDnfInstalled.status === SpawnStatus.SUCCESS) {
277
+ const { status } = await $.spawnSafe(`dnf autoremove ${packageName} -y`, { requiresRoot: true });
278
+ return status === SpawnStatus.SUCCESS;
279
+ }
280
+
281
+ const isYumInstalled = await $.spawnSafe('which yum');
282
+ if (isYumInstalled.status === SpawnStatus.SUCCESS) {
283
+ const { status } = await $.spawnSafe(`yum autoremove ${packageName} -y`, { requiresRoot: true });
284
+ return status === SpawnStatus.SUCCESS;
285
+ }
286
+
287
+ return false;
288
+ }
289
+
290
+ return false;
291
+ },
292
+
293
+ async getLinuxDistro(): Promise<LinuxDistro | undefined> {
294
+ const osRelease = await fs.readFile('/etc/os-release', 'utf8');
295
+ const lines = osRelease.split('\n');
296
+ for (const line of lines) {
297
+ if (line.startsWith('ID=')) {
298
+ const distroId = line.slice(3).trim().replaceAll('"', '');
299
+ return Object.values(LinuxDistro).includes(distroId as LinuxDistro) ? distroId as LinuxDistro : undefined;
300
+ }
301
+ }
302
+
303
+ return undefined;
304
+ },
305
+
306
+ async isUbuntu(): Promise<boolean> {
307
+ return (await this.getLinuxDistro()) === LinuxDistro.UBUNTU;
308
+ },
309
+
310
+ async isDebian(): Promise<boolean> {
311
+ return (await this.getLinuxDistro()) === LinuxDistro.DEBIAN;
312
+ },
313
+
314
+ async isArch(): Promise<boolean> {
315
+ return (await this.getLinuxDistro()) === LinuxDistro.ARCH;
316
+ },
317
+
318
+ async isCentOS(): Promise<boolean> {
319
+ return (await this.getLinuxDistro()) === LinuxDistro.CENTOS;
320
+ },
321
+
322
+ async isFedora(): Promise<boolean> {
323
+ return (await this.getLinuxDistro()) === LinuxDistro.FEDORA;
324
+ },
325
+
326
+ async isRHEL(): Promise<boolean> {
327
+ return (await this.getLinuxDistro()) === LinuxDistro.RHEL;
328
+ },
329
+
330
+ isDebianBased(): boolean {
331
+ return fsSync.existsSync('/etc/debian_version');
332
+ },
333
+
334
+ isRedhatBased(): boolean {
335
+ return fsSync.existsSync('/etc/redhat-release');
336
+ }
141
337
  };
142
338
 
143
339
 
@@ -8,6 +8,7 @@ describe('Utils tests', () => {
8
8
  type: 'type',
9
9
  name: 'name',
10
10
  dependsOn: ['a', 'b', 'c'],
11
+ os: ['linux'],
11
12
  propA: 'propA',
12
13
  propB: 'propB',
13
14
  propC: 'propC',