@codifycli/plugin-core 1.0.0-beta1

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 (152) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc.json +30 -0
  3. package/.github/workflows/release.yaml +19 -0
  4. package/.github/workflows/unit-test-ci.yaml +18 -0
  5. package/.prettierrc.json +1 -0
  6. package/bin/build.js +189 -0
  7. package/dist/bin/build.d.ts +1 -0
  8. package/dist/bin/build.js +80 -0
  9. package/dist/bin/deploy-plugin.d.ts +2 -0
  10. package/dist/bin/deploy-plugin.js +8 -0
  11. package/dist/common/errors.d.ts +8 -0
  12. package/dist/common/errors.js +24 -0
  13. package/dist/entities/change-set.d.ts +24 -0
  14. package/dist/entities/change-set.js +152 -0
  15. package/dist/entities/errors.d.ts +4 -0
  16. package/dist/entities/errors.js +7 -0
  17. package/dist/entities/plan-types.d.ts +25 -0
  18. package/dist/entities/plan-types.js +1 -0
  19. package/dist/entities/plan.d.ts +15 -0
  20. package/dist/entities/plan.js +127 -0
  21. package/dist/entities/plugin.d.ts +16 -0
  22. package/dist/entities/plugin.js +80 -0
  23. package/dist/entities/resource-options.d.ts +31 -0
  24. package/dist/entities/resource-options.js +76 -0
  25. package/dist/entities/resource-types.d.ts +11 -0
  26. package/dist/entities/resource-types.js +1 -0
  27. package/dist/entities/resource.d.ts +42 -0
  28. package/dist/entities/resource.js +303 -0
  29. package/dist/entities/stateful-parameter.d.ts +29 -0
  30. package/dist/entities/stateful-parameter.js +46 -0
  31. package/dist/entities/transform-parameter.d.ts +4 -0
  32. package/dist/entities/transform-parameter.js +2 -0
  33. package/dist/errors.d.ts +4 -0
  34. package/dist/errors.js +7 -0
  35. package/dist/index.d.ts +20 -0
  36. package/dist/index.js +26 -0
  37. package/dist/messages/handlers.d.ts +14 -0
  38. package/dist/messages/handlers.js +134 -0
  39. package/dist/messages/sender.d.ts +11 -0
  40. package/dist/messages/sender.js +57 -0
  41. package/dist/plan/change-set.d.ts +53 -0
  42. package/dist/plan/change-set.js +153 -0
  43. package/dist/plan/plan-types.d.ts +23 -0
  44. package/dist/plan/plan-types.js +1 -0
  45. package/dist/plan/plan.d.ts +66 -0
  46. package/dist/plan/plan.js +328 -0
  47. package/dist/plugin/plugin.d.ts +24 -0
  48. package/dist/plugin/plugin.js +200 -0
  49. package/dist/pty/background-pty.d.ts +21 -0
  50. package/dist/pty/background-pty.js +127 -0
  51. package/dist/pty/index.d.ts +50 -0
  52. package/dist/pty/index.js +20 -0
  53. package/dist/pty/promise-queue.d.ts +5 -0
  54. package/dist/pty/promise-queue.js +26 -0
  55. package/dist/pty/seqeuntial-pty.d.ts +17 -0
  56. package/dist/pty/seqeuntial-pty.js +119 -0
  57. package/dist/pty/vitest.config.d.ts +2 -0
  58. package/dist/pty/vitest.config.js +11 -0
  59. package/dist/resource/config-parser.d.ts +11 -0
  60. package/dist/resource/config-parser.js +21 -0
  61. package/dist/resource/parsed-resource-settings.d.ts +47 -0
  62. package/dist/resource/parsed-resource-settings.js +196 -0
  63. package/dist/resource/resource-controller.d.ts +36 -0
  64. package/dist/resource/resource-controller.js +402 -0
  65. package/dist/resource/resource-settings.d.ts +303 -0
  66. package/dist/resource/resource-settings.js +147 -0
  67. package/dist/resource/resource.d.ts +144 -0
  68. package/dist/resource/resource.js +44 -0
  69. package/dist/resource/stateful-parameter.d.ts +165 -0
  70. package/dist/resource/stateful-parameter.js +94 -0
  71. package/dist/scripts/deploy.d.ts +1 -0
  72. package/dist/scripts/deploy.js +2 -0
  73. package/dist/stateful-parameter/stateful-parameter-controller.d.ts +21 -0
  74. package/dist/stateful-parameter/stateful-parameter-controller.js +81 -0
  75. package/dist/stateful-parameter/stateful-parameter.d.ts +144 -0
  76. package/dist/stateful-parameter/stateful-parameter.js +43 -0
  77. package/dist/test.d.ts +1 -0
  78. package/dist/test.js +5 -0
  79. package/dist/utils/codify-spawn.d.ts +29 -0
  80. package/dist/utils/codify-spawn.js +136 -0
  81. package/dist/utils/debug.d.ts +2 -0
  82. package/dist/utils/debug.js +10 -0
  83. package/dist/utils/file-utils.d.ts +23 -0
  84. package/dist/utils/file-utils.js +186 -0
  85. package/dist/utils/functions.d.ts +12 -0
  86. package/dist/utils/functions.js +74 -0
  87. package/dist/utils/index.d.ts +46 -0
  88. package/dist/utils/index.js +271 -0
  89. package/dist/utils/internal-utils.d.ts +12 -0
  90. package/dist/utils/internal-utils.js +74 -0
  91. package/dist/utils/load-resources.d.ts +1 -0
  92. package/dist/utils/load-resources.js +46 -0
  93. package/dist/utils/package-json-utils.d.ts +12 -0
  94. package/dist/utils/package-json-utils.js +34 -0
  95. package/dist/utils/pty-local-storage.d.ts +2 -0
  96. package/dist/utils/pty-local-storage.js +2 -0
  97. package/dist/utils/spawn-2.d.ts +5 -0
  98. package/dist/utils/spawn-2.js +7 -0
  99. package/dist/utils/spawn.d.ts +29 -0
  100. package/dist/utils/spawn.js +124 -0
  101. package/dist/utils/utils.d.ts +18 -0
  102. package/dist/utils/utils.js +86 -0
  103. package/dist/utils/verbosity-level.d.ts +5 -0
  104. package/dist/utils/verbosity-level.js +9 -0
  105. package/package.json +59 -0
  106. package/rollup.config.js +24 -0
  107. package/src/common/errors.test.ts +43 -0
  108. package/src/common/errors.ts +31 -0
  109. package/src/errors.ts +8 -0
  110. package/src/index.test.ts +6 -0
  111. package/src/index.ts +30 -0
  112. package/src/messages/handlers.test.ts +329 -0
  113. package/src/messages/handlers.ts +181 -0
  114. package/src/messages/sender.ts +69 -0
  115. package/src/plan/change-set.test.ts +280 -0
  116. package/src/plan/change-set.ts +236 -0
  117. package/src/plan/plan-types.ts +27 -0
  118. package/src/plan/plan.test.ts +413 -0
  119. package/src/plan/plan.ts +499 -0
  120. package/src/plugin/plugin.test.ts +533 -0
  121. package/src/plugin/plugin.ts +291 -0
  122. package/src/pty/background-pty.test.ts +69 -0
  123. package/src/pty/background-pty.ts +154 -0
  124. package/src/pty/index.test.ts +129 -0
  125. package/src/pty/index.ts +66 -0
  126. package/src/pty/promise-queue.ts +33 -0
  127. package/src/pty/seqeuntial-pty.ts +151 -0
  128. package/src/pty/sequential-pty.test.ts +194 -0
  129. package/src/resource/config-parser.ts +42 -0
  130. package/src/resource/parsed-resource-settings.test.ts +186 -0
  131. package/src/resource/parsed-resource-settings.ts +307 -0
  132. package/src/resource/resource-controller-stateful-mode.test.ts +253 -0
  133. package/src/resource/resource-controller.test.ts +1081 -0
  134. package/src/resource/resource-controller.ts +563 -0
  135. package/src/resource/resource-settings.test.ts +1213 -0
  136. package/src/resource/resource-settings.ts +545 -0
  137. package/src/resource/resource.ts +157 -0
  138. package/src/stateful-parameter/stateful-parameter-controller.test.ts +244 -0
  139. package/src/stateful-parameter/stateful-parameter-controller.ts +111 -0
  140. package/src/stateful-parameter/stateful-parameter.ts +160 -0
  141. package/src/utils/debug.ts +11 -0
  142. package/src/utils/file-utils.test.ts +7 -0
  143. package/src/utils/file-utils.ts +231 -0
  144. package/src/utils/functions.ts +103 -0
  145. package/src/utils/index.ts +340 -0
  146. package/src/utils/internal-utils.test.ts +52 -0
  147. package/src/utils/pty-local-storage.ts +3 -0
  148. package/src/utils/test-utils.test.ts +96 -0
  149. package/src/utils/verbosity-level.ts +11 -0
  150. package/tsconfig.json +26 -0
  151. package/tsconfig.test.json +9 -0
  152. package/vitest.config.ts +10 -0
@@ -0,0 +1,231 @@
1
+ import * as fsSync from 'node:fs';
2
+ import * as fs from 'node:fs/promises';
3
+ import path from 'node:path';
4
+ import { Readable } from 'node:stream';
5
+ import { finished } from 'node:stream/promises';
6
+
7
+ import { Utils } from './index.js';
8
+
9
+ const SPACE_REGEX = /^\s*$/
10
+
11
+ export class FileUtils {
12
+ static async downloadFile(url: string, destination: string): Promise<void> {
13
+ console.log(`Downloading file from ${url} to ${destination}`);
14
+ const { body } = await fetch(url)
15
+
16
+ const dirname = path.dirname(destination);
17
+ if (!await fs.stat(dirname).then((s) => s.isDirectory()).catch(() => false)) {
18
+ await fs.mkdir(dirname, { recursive: true });
19
+ }
20
+
21
+ const ws = fsSync.createWriteStream(destination)
22
+ // Different type definitions here for readable stream (NodeJS vs DOM). Small hack to fix that
23
+ await finished(Readable.fromWeb(body as never).pipe(ws));
24
+
25
+ console.log(`Finished downloading to ${destination}`);
26
+ }
27
+
28
+ static async addToShellRc(line: string): Promise<void> {
29
+ const lineToInsert = addLeadingSpacer(
30
+ addTrailingSpacer(line)
31
+ );
32
+
33
+ await fs.appendFile(Utils.getPrimaryShellRc(), lineToInsert)
34
+
35
+ function addLeadingSpacer(line: string): string {
36
+ return line.startsWith('\n')
37
+ ? line
38
+ : '\n' + line;
39
+ }
40
+
41
+ function addTrailingSpacer(line: string): string {
42
+ return line.endsWith('\n')
43
+ ? line
44
+ : line + '\n';
45
+ }
46
+ }
47
+
48
+ static async addAllToShellRc(lines: string[]): Promise<void> {
49
+ const formattedLines = '\n' + lines.join('\n') + '\n';
50
+ const shellRc = Utils.getPrimaryShellRc();
51
+
52
+ console.log(`Adding to ${path.basename(shellRc)}:
53
+ ${lines.join('\n')}`)
54
+
55
+ await fs.appendFile(shellRc, formattedLines)
56
+ }
57
+
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
+
69
+ const shellRc = Utils.getPrimaryShellRc();
70
+ console.log(`Saving path: ${value} to ${shellRc}`);
71
+
72
+ if (prepend) {
73
+ await fs.appendFile(shellRc, `\nexport PATH=$PATH:${value};`, { encoding: 'utf8' });
74
+ return;
75
+ }
76
+
77
+ await fs.appendFile(shellRc, `\nexport PATH=${value}:$PATH;`, { encoding: 'utf8' });
78
+ }
79
+
80
+ static async removeFromFile(filePath: string, search: string): Promise<void> {
81
+ const contents = await fs.readFile(filePath, 'utf8');
82
+ const newContents = contents.replaceAll(search, '');
83
+
84
+ await fs.writeFile(filePath, newContents, 'utf8');
85
+ }
86
+
87
+ static async removeLineFromFile(filePath: string, search: RegExp | string): Promise<void> {
88
+ const file = await fs.readFile(filePath, 'utf8')
89
+ const lines = file.split('\n');
90
+
91
+ let searchRegex;
92
+ let searchString;
93
+
94
+ if (typeof search === 'object') {
95
+ const startRegex = /^([\t ]*)?/;
96
+ const endRegex = /([\t ]*)?/;
97
+
98
+ // Augment regex with spaces criteria to make sure this function is not deleting lines that are comments or has other content.
99
+ searchRegex = search
100
+ ? new RegExp(
101
+ startRegex.source + search.source + endRegex.source,
102
+ search.flags
103
+ )
104
+ : search;
105
+ }
106
+
107
+ if (typeof search === 'string') {
108
+ searchString = search;
109
+ }
110
+
111
+ for (let counter = lines.length; counter >= 0; counter--) {
112
+ if (!lines[counter]) {
113
+ continue;
114
+ }
115
+
116
+ if (searchString && lines[counter].includes(searchString)) {
117
+ lines.splice(counter, 1);
118
+ continue;
119
+ }
120
+
121
+ if (searchRegex && lines[counter].search(searchRegex) !== -1) {
122
+ lines.splice(counter, 1);
123
+ }
124
+ }
125
+
126
+ await fs.writeFile(filePath, lines.join('\n'));
127
+ console.log(`Removed line: ${search} from ${filePath}`)
128
+ }
129
+
130
+ static async removeLineFromShellRc(search: RegExp | string): Promise<void> {
131
+ return FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
132
+ }
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
+
140
+ // Append the string to the end of a file ensuring at least 1 lines of space between.
141
+ // Ex result:
142
+ // something something;
143
+ //
144
+ // newline;
145
+ static appendToFileWithSpacing(file: string, textToInsert: string): string {
146
+ const lines = file.trimEnd().split(/\n/);
147
+ if (lines.length === 0) {
148
+ return textToInsert;
149
+ }
150
+
151
+ const endingNewLines = FileUtils.calculateEndingNewLines(lines);
152
+ const numNewLines = endingNewLines === -1
153
+ ? 0
154
+ : Math.max(0, 2 - endingNewLines);
155
+ return lines.join('\n') + '\n'.repeat(numNewLines) + textToInsert
156
+ }
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
+
208
+ // This is overly complicated but it can be used to insert into any
209
+ // position in the future
210
+ private static calculateEndingNewLines(lines: string[]): number {
211
+ let counter = 0;
212
+ while (true) {
213
+ const line = lines.at(-counter - 1);
214
+
215
+ if (!line) {
216
+ return -1
217
+ }
218
+
219
+ if (!SPACE_REGEX.test(line)) {
220
+ return counter;
221
+ }
222
+
223
+ counter++;
224
+
225
+ // Short circuit here because we don't need to check over 2;
226
+ if (counter > 2) {
227
+ return counter;
228
+ }
229
+ }
230
+ }
231
+ }
@@ -0,0 +1,103 @@
1
+ import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+
5
+ export function splitUserConfig<T extends StringIndexedObject>(
6
+ config: ResourceConfig & T
7
+ ): { parameters: T; coreParameters: ResourceConfig } {
8
+ const coreParameters = {
9
+ type: config.type,
10
+ ...(config.name ? { name: config.name } : {}),
11
+ ...(config.dependsOn ? { dependsOn: config.dependsOn } : {}),
12
+ ...(config.os ? { os: config.os } : {}),
13
+ };
14
+
15
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
16
+ const { type, name, dependsOn, os, ...parameters } = config;
17
+
18
+ return {
19
+ parameters: parameters as T,
20
+ coreParameters,
21
+ };
22
+ }
23
+
24
+ export function setsEqual(set1: Set<unknown>, set2: Set<unknown>): boolean {
25
+ return set1.size === set2.size && [...set1].every((v) => set2.has(v));
26
+ }
27
+
28
+ const homeDirectory = os.homedir();
29
+
30
+ export function untildify(pathWithTilde: string) {
31
+ return homeDirectory ? pathWithTilde.replace(/^~(?=$|\/|\\)/, homeDirectory) : pathWithTilde;
32
+ }
33
+
34
+ export function tildify(pathWithTilde: string) {
35
+ return homeDirectory ? pathWithTilde.replace(homeDirectory, '~') : pathWithTilde;
36
+ }
37
+
38
+ export function resolvePathWithVariables(pathWithVariables: string) {
39
+ return pathWithVariables.replace(/\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/ig, (_, a, b) => process.env[a || b]!)
40
+ }
41
+
42
+ export function addVariablesToPath(pathWithoutVariables: string) {
43
+ let result = pathWithoutVariables;
44
+ for (const [key, value] of Object.entries(process.env)) {
45
+ if (!value || !path.isAbsolute(value) || value === '/' || key === 'HOME' || key === 'PATH' || key === 'SHELL' || key === 'PWD') {
46
+ continue;
47
+ }
48
+
49
+ result = result.replaceAll(value, `$${key}`)
50
+ }
51
+
52
+ return result;
53
+ }
54
+
55
+ export function unhome(pathWithHome: string): string {
56
+ return pathWithHome.includes('$HOME') ? pathWithHome.replaceAll('$HOME', os.homedir()) : pathWithHome;
57
+ }
58
+
59
+ export function areArraysEqual(
60
+ isElementEqual: ((desired: unknown, current: unknown) => boolean) | undefined,
61
+ desired: unknown,
62
+ current: unknown
63
+ ): boolean {
64
+ if (!desired || !current) {
65
+ return false;
66
+ }
67
+
68
+ if (!Array.isArray(desired) || !Array.isArray(current)) {
69
+ throw new Error(`A non-array value:
70
+
71
+ Desired: ${JSON.stringify(desired, null, 2)}
72
+
73
+ Current: ${JSON.stringify(desired, null, 2)}
74
+
75
+ Was provided even though type array was specified.
76
+ `)
77
+ }
78
+
79
+ if (desired.length !== current.length) {
80
+ return false;
81
+ }
82
+
83
+ const desiredCopy = [...desired];
84
+ const currentCopy = [...current];
85
+
86
+ // Algorithm for to check equality between two un-ordered; un-hashable arrays using
87
+ // an isElementEqual method. Time: O(n^2)
88
+ for (let counter = desiredCopy.length - 1; counter >= 0; counter--) {
89
+ const idx = currentCopy.findIndex((e2) => (
90
+ isElementEqual
91
+ ?? ((a, b) => a === b))(desiredCopy[counter], e2
92
+ ))
93
+
94
+ if (idx === -1) {
95
+ return false;
96
+ }
97
+
98
+ desiredCopy.splice(counter, 1)
99
+ currentCopy.splice(idx, 1)
100
+ }
101
+
102
+ return currentCopy.length === 0;
103
+ }
@@ -0,0 +1,340 @@
1
+ import { LinuxDistro, OS } from 'codify-schemas';
2
+ import * as fsSync from 'node:fs';
3
+ import * as fs from 'node:fs/promises';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+
7
+ import { SpawnStatus, getPty } from '../pty/index.js';
8
+
9
+ export function isDebug(): boolean {
10
+ return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
11
+ }
12
+
13
+ export enum Shell {
14
+ ZSH = 'zsh',
15
+ BASH = 'bash',
16
+ SH = 'sh',
17
+ KSH = 'ksh',
18
+ CSH = 'csh',
19
+ FISH = 'fish',
20
+ }
21
+
22
+ export interface SystemInfo {
23
+ os: OS;
24
+ shell: Shell;
25
+ }
26
+
27
+ export const Utils = {
28
+ getUser(): string {
29
+ return os.userInfo().username;
30
+ },
31
+
32
+ getSystemInfo() {
33
+ return {
34
+ os: os.type(),
35
+ shell: this.getShell(),
36
+ }
37
+ },
38
+
39
+ isMacOS(): boolean {
40
+ return os.platform() === 'darwin';
41
+ },
42
+
43
+ isLinux(): boolean {
44
+ return os.platform() === 'linux';
45
+ },
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
+
75
+ getShell(): Shell | undefined {
76
+ const shell = process.env.SHELL || '';
77
+
78
+ if (shell.endsWith('bash')) {
79
+ return Shell.BASH
80
+ }
81
+
82
+ if (shell.endsWith('zsh')) {
83
+ return Shell.ZSH
84
+ }
85
+
86
+ if (shell.endsWith('sh')) {
87
+ return Shell.SH
88
+ }
89
+
90
+ if (shell.endsWith('csh')) {
91
+ return Shell.CSH
92
+ }
93
+
94
+ if (shell.endsWith('ksh')) {
95
+ return Shell.KSH
96
+ }
97
+
98
+ if (shell.endsWith('fish')) {
99
+ return Shell.FISH
100
+ }
101
+
102
+ return undefined;
103
+ },
104
+
105
+
106
+ getPrimaryShellRc(): string {
107
+ return this.getShellRcFiles()[0];
108
+ },
109
+
110
+ getShellRcFiles(): string[] {
111
+ const shell = process.env.SHELL || '';
112
+ const homeDir = os.homedir();
113
+
114
+ if (shell.endsWith('bash')) {
115
+ // Linux typically uses .bashrc, macOS uses .bash_profile
116
+ if (Utils.isLinux()) {
117
+ return [
118
+ path.join(homeDir, '.bashrc'),
119
+ path.join(homeDir, '.bash_profile'),
120
+ path.join(homeDir, '.profile'),
121
+ ];
122
+ }
123
+
124
+ return [
125
+ path.join(homeDir, '.bash_profile'),
126
+ path.join(homeDir, '.bashrc'),
127
+ path.join(homeDir, '.profile'),
128
+ ];
129
+ }
130
+
131
+ if (shell.endsWith('zsh')) {
132
+ return [
133
+ path.join(homeDir, '.zshrc'),
134
+ path.join(homeDir, '.zprofile'),
135
+ path.join(homeDir, '.zshenv'),
136
+ ];
137
+ }
138
+
139
+ if (shell.endsWith('sh')) {
140
+ return [
141
+ path.join(homeDir, '.profile'),
142
+ ]
143
+ }
144
+
145
+ if (shell.endsWith('ksh')) {
146
+ return [
147
+ path.join(homeDir, '.profile'),
148
+ path.join(homeDir, '.kshrc'),
149
+ ]
150
+ }
151
+
152
+ if (shell.endsWith('csh')) {
153
+ return [
154
+ path.join(homeDir, '.cshrc'),
155
+ path.join(homeDir, '.login'),
156
+ path.join(homeDir, '.logout'),
157
+ ]
158
+ }
159
+
160
+ if (shell.endsWith('fish')) {
161
+ return [
162
+ path.join(homeDir, '.config/fish/config.fish'),
163
+ ]
164
+ }
165
+
166
+ // Default to bash-style files
167
+ return [
168
+ path.join(homeDir, '.bashrc'),
169
+ path.join(homeDir, '.bash_profile'),
170
+ path.join(homeDir, '.profile'),
171
+ ];
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', NEEDRESTART_MODE: 'a' }
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', NEEDRESTART_MODE: 'a' }
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', NEEDRESTART_MODE: 'a' }
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
+ }
337
+ };
338
+
339
+
340
+