codify-plugin-lib 1.0.182-beta1 → 1.0.182-beta11

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/dist/index.d.ts CHANGED
@@ -10,5 +10,8 @@ export * from './resource/parsed-resource-settings.js';
10
10
  export * from './resource/resource.js';
11
11
  export * from './resource/resource-settings.js';
12
12
  export * from './stateful-parameter/stateful-parameter.js';
13
+ export * from './utils/file-utils.js';
14
+ export * from './utils/functions.js';
13
15
  export * from './utils/index.js';
16
+ export * from './utils/verbosity-level.js';
14
17
  export declare function runPlugin(plugin: Plugin): Promise<void>;
package/dist/index.js CHANGED
@@ -10,7 +10,10 @@ export * from './resource/parsed-resource-settings.js';
10
10
  export * from './resource/resource.js';
11
11
  export * from './resource/resource-settings.js';
12
12
  export * from './stateful-parameter/stateful-parameter.js';
13
+ export * from './utils/file-utils.js';
14
+ export * from './utils/functions.js';
13
15
  export * from './utils/index.js';
16
+ export * from './utils/verbosity-level.js';
14
17
  export async function runPlugin(plugin) {
15
18
  const messageHandler = new MessageHandler(plugin);
16
19
  process.on('message', (message) => messageHandler.onMessage(message));
@@ -4,8 +4,8 @@ import { BackgroundPty } from '../pty/background-pty.js';
4
4
  import { getPty } from '../pty/index.js';
5
5
  import { SequentialPty } from '../pty/seqeuntial-pty.js';
6
6
  import { ResourceController } from '../resource/resource-controller.js';
7
- import { VerbosityLevel } from '../utils/internal-utils.js';
8
7
  import { ptyLocalStorage } from '../utils/pty-local-storage.js';
8
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
9
9
  export class Plugin {
10
10
  name;
11
11
  resourceControllers;
@@ -6,6 +6,7 @@ import { IPty, SpawnOptions, SpawnResult } from './index.js';
6
6
  * without a tty (or even a stdin) attached so interactive commands will not work.
7
7
  */
8
8
  export declare class BackgroundPty implements IPty {
9
+ private historyIgnore;
9
10
  private basePty;
10
11
  private promiseQueue;
11
12
  constructor();
@@ -6,7 +6,6 @@ import * as fs from 'node:fs/promises';
6
6
  import stripAnsi from 'strip-ansi';
7
7
  import { debugLog } from '../utils/debug.js';
8
8
  import { Shell, Utils } from '../utils/index.js';
9
- import { VerbosityLevel } from '../utils/internal-utils.js';
10
9
  import { SpawnError } from './index.js';
11
10
  import { PromiseQueue } from './promise-queue.js';
12
11
  EventEmitter.defaultMaxListeners = 1000;
@@ -17,8 +16,10 @@ EventEmitter.defaultMaxListeners = 1000;
17
16
  * without a tty (or even a stdin) attached so interactive commands will not work.
18
17
  */
19
18
  export class BackgroundPty {
19
+ historyIgnore = Utils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' };
20
20
  basePty = pty.spawn(this.getDefaultShell(), ['-i'], {
21
- env: process.env, name: nanoid(6),
21
+ env: { ...process.env, ...this.historyIgnore },
22
+ name: nanoid(6),
22
23
  handleFlowControl: true
23
24
  });
24
25
  promiseQueue = new PromiseQueue();
@@ -65,9 +66,9 @@ export class BackgroundPty {
65
66
  }
66
67
  else {
67
68
  // Print to stdout if the verbosity level is above 0
68
- if (VerbosityLevel.get() > 0) {
69
- process.stdout.write(data);
70
- }
69
+ // if (VerbosityLevel.get() > 0) {
70
+ // process.stdout.write(data);
71
+ // }
71
72
  }
72
73
  });
73
74
  this.promiseQueue.run(async () => new Promise((resolve) => {
@@ -105,16 +106,17 @@ export class BackgroundPty {
105
106
  let outputBuffer = '';
106
107
  return new Promise(resolve => {
107
108
  // zsh-specific commands
108
- switch (Utils.getShell()) {
109
- case Shell.ZSH: {
110
- this.basePty.write('setopt HIST_NO_STORE;\n');
111
- break;
112
- }
113
- default: {
114
- this.basePty.write('export HISTIGNORE=\'history*\';\n');
115
- break;
116
- }
117
- }
109
+ // switch (Utils.getShell()) {
110
+ // case Shell.ZSH: {
111
+ // this.basePty.write('setopt HIST_NO_STORE;\n');
112
+ // break;
113
+ // }
114
+ //
115
+ // default: {
116
+ // this.basePty.write('export HISTIGNORE=\'history*\';\n');
117
+ // break;
118
+ // }
119
+ // }
118
120
  this.basePty.write(' unset PS1;\n');
119
121
  this.basePty.write(' unset PS0;\n');
120
122
  this.basePty.write(' echo setup complete\\"\n');
@@ -26,6 +26,8 @@ export interface SpawnOptions {
26
26
  cwd?: string;
27
27
  env?: Record<string, unknown>;
28
28
  interactive?: boolean;
29
+ requiresRoot?: boolean;
30
+ stdin?: boolean;
29
31
  }
30
32
  export declare class SpawnError extends Error {
31
33
  data: string;
@@ -12,5 +12,6 @@ export declare class SequentialPty implements IPty {
12
12
  exitCode: number;
13
13
  signal?: number | undefined;
14
14
  }>;
15
+ externalSpawn(cmd: string, opts: SpawnOptions): Promise<SpawnResult>;
15
16
  private getDefaultShell;
16
17
  }
@@ -1,10 +1,17 @@
1
1
  import pty from '@homebridge/node-pty-prebuilt-multiarch';
2
+ import { Ajv } from 'ajv';
3
+ import { CommandRequestResponseDataSchema, MessageCmd } from 'codify-schemas';
4
+ import { nanoid } from 'nanoid';
2
5
  import { EventEmitter } from 'node:events';
3
6
  import stripAnsi from 'strip-ansi';
4
7
  import { Shell, Utils } from '../utils/index.js';
5
- import { VerbosityLevel } from '../utils/internal-utils.js';
8
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
6
9
  import { SpawnError, SpawnStatus } from './index.js';
7
10
  EventEmitter.defaultMaxListeners = 1000;
11
+ const ajv = new Ajv({
12
+ strict: true,
13
+ });
14
+ const validateSudoRequestResponse = ajv.compile(CommandRequestResponseDataSchema);
8
15
  /**
9
16
  * The background pty is a specialized pty designed for speed. It can launch multiple tasks
10
17
  * in parallel by moving them to the background. It attaches unix FIFO pipes to each process
@@ -20,6 +27,13 @@ export class SequentialPty {
20
27
  return spawnResult;
21
28
  }
22
29
  async spawnSafe(cmd, options) {
30
+ if (cmd.includes('sudo')) {
31
+ throw new Error('Do not directly use sudo. Use the option { requiresRoot: true } instead');
32
+ }
33
+ // If sudo is required, we must delegate to the main codify process.
34
+ if (options?.stdin || options?.requiresRoot) {
35
+ return this.externalSpawn(cmd, options);
36
+ }
23
37
  console.log(`Running command: ${cmd}` + (options?.cwd ? `(${options?.cwd})` : ''));
24
38
  return new Promise((resolve) => {
25
39
  const output = [];
@@ -30,12 +44,13 @@ export class SequentialPty {
30
44
  ...process.env, ...options?.env,
31
45
  TERM_PROGRAM: 'codify',
32
46
  COMMAND_MODE: 'unix2003',
33
- COLORTERM: 'truecolor', ...historyIgnore
47
+ COLORTERM: 'truecolor',
48
+ ...historyIgnore
34
49
  };
35
50
  // Initial terminal dimensions
36
51
  const initialCols = process.stdout.columns ?? 80;
37
52
  const initialRows = process.stdout.rows ?? 24;
38
- const args = (options?.interactive ?? true) ? ['-i', '-c', `"${cmd}"`] : ['-c', `"${cmd}"`];
53
+ const args = options?.interactive ? ['-i', '-c', cmd] : ['-c', cmd];
39
54
  // Run the command in a pty for interactivity
40
55
  const mPty = pty.spawn(this.getDefaultShell(), args, {
41
56
  ...options,
@@ -49,20 +64,14 @@ export class SequentialPty {
49
64
  }
50
65
  output.push(data.toString());
51
66
  });
52
- const stdinListener = (data) => {
53
- mPty.write(data.toString());
54
- };
55
67
  const resizeListener = () => {
56
68
  const { columns, rows } = process.stdout;
57
69
  mPty.resize(columns, rows);
58
70
  };
59
71
  // Listen to resize events for the terminal window;
60
72
  process.stdout.on('resize', resizeListener);
61
- // Listen for user input
62
- process.stdin.on('data', stdinListener);
63
73
  mPty.onExit((result) => {
64
74
  process.stdout.off('resize', resizeListener);
65
- process.stdin.off('data', stdinListener);
66
75
  resolve({
67
76
  status: result.exitCode === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR,
68
77
  exitCode: result.exitCode,
@@ -78,6 +87,30 @@ export class SequentialPty {
78
87
  signal: 0,
79
88
  };
80
89
  }
90
+ // For safety reasons, requests that require sudo or are interactive must be run via the main client
91
+ async externalSpawn(cmd, opts) {
92
+ return new Promise((resolve) => {
93
+ const requestId = nanoid(8);
94
+ const listener = (data) => {
95
+ if (data.requestId === requestId) {
96
+ process.removeListener('message', listener);
97
+ if (!validateSudoRequestResponse(data.data)) {
98
+ throw new Error(`Invalid response for sudo request: ${JSON.stringify(validateSudoRequestResponse.errors, null, 2)}`);
99
+ }
100
+ resolve(data.data);
101
+ }
102
+ };
103
+ process.on('message', listener);
104
+ process.send({
105
+ cmd: MessageCmd.COMMAND_REQUEST,
106
+ data: {
107
+ command: cmd,
108
+ options: opts ?? {},
109
+ },
110
+ requestId
111
+ });
112
+ });
113
+ }
81
114
  getDefaultShell() {
82
115
  return process.env.SHELL;
83
116
  }
@@ -1,6 +1,6 @@
1
1
  import isObjectsEqual from 'lodash.isequal';
2
2
  import path from 'node:path';
3
- import { addVariablesToPath, areArraysEqual, resolvePathWithVariables, tildify, untildify } from '../utils/internal-utils.js';
3
+ import { addVariablesToPath, areArraysEqual, resolvePathWithVariables, tildify, untildify } from '../utils/functions.js';
4
4
  const ParameterEqualsDefaults = {
5
5
  'boolean': (a, b) => Boolean(a) === Boolean(b),
6
6
  'directory': (a, b) => {
package/dist/test.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/test.js ADDED
@@ -0,0 +1,5 @@
1
+ import { SequentialPty } from './pty/seqeuntial-pty.js';
2
+ import { VerbosityLevel } from './utils/verbosity-level.js';
3
+ VerbosityLevel.set(1);
4
+ const $ = new SequentialPty();
5
+ await $.spawn('sudo ls', { interactive: true });
@@ -0,0 +1,16 @@
1
+ export declare class FileUtils {
2
+ static downloadFile(url: string, destination: string): Promise<void>;
3
+ static addToStartupFile(line: string): Promise<void>;
4
+ static addAllToStartupFile(lines: string[]): Promise<void>;
5
+ static addPathToPrimaryShellRc(value: string, prepend: boolean): Promise<void>;
6
+ static dirExists(path: string): Promise<boolean>;
7
+ static fileExists(path: string): Promise<boolean>;
8
+ static exists(path: string): Promise<boolean>;
9
+ static checkDirExistsOrThrowIfFile(path: string): Promise<boolean>;
10
+ static createDirIfNotExists(path: string): Promise<void>;
11
+ static removeFromFile(filePath: string, search: string): Promise<void>;
12
+ static removeLineFromFile(filePath: string, search: RegExp | string): Promise<void>;
13
+ static removeLineFromPrimaryShellRc(search: RegExp | string): Promise<void>;
14
+ static appendToFileWithSpacing(file: string, textToInsert: string): string;
15
+ private static calculateEndingNewLines;
16
+ }
@@ -0,0 +1,172 @@
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
+ import { Utils } from './index.js';
7
+ const SPACE_REGEX = /^\s*$/;
8
+ export class FileUtils {
9
+ static async downloadFile(url, destination) {
10
+ console.log(`Downloading file from ${url} to ${destination}`);
11
+ const { body } = await fetch(url);
12
+ const dirname = path.dirname(destination);
13
+ if (!await fs.stat(dirname).then((s) => s.isDirectory()).catch(() => false)) {
14
+ await fs.mkdir(dirname, { recursive: true });
15
+ }
16
+ const ws = fsSync.createWriteStream(destination);
17
+ // Different type definitions here for readable stream (NodeJS vs DOM). Small hack to fix that
18
+ await finished(Readable.fromWeb(body).pipe(ws));
19
+ console.log(`Finished downloading to ${destination}`);
20
+ }
21
+ static async addToStartupFile(line) {
22
+ const lineToInsert = addLeadingSpacer(addTrailingSpacer(line));
23
+ await fs.appendFile(Utils.getPrimaryShellRc(), lineToInsert);
24
+ function addLeadingSpacer(line) {
25
+ return line.startsWith('\n')
26
+ ? line
27
+ : '\n' + line;
28
+ }
29
+ function addTrailingSpacer(line) {
30
+ return line.endsWith('\n')
31
+ ? line
32
+ : line + '\n';
33
+ }
34
+ }
35
+ static async addAllToStartupFile(lines) {
36
+ const formattedLines = '\n' + lines.join('\n') + '\n';
37
+ const shellRc = Utils.getPrimaryShellRc();
38
+ console.log(`Adding to ${path.basename(shellRc)}:
39
+ ${lines.join('\n')}`);
40
+ await fs.appendFile(shellRc, formattedLines);
41
+ }
42
+ static async addPathToPrimaryShellRc(value, prepend) {
43
+ const shellRc = Utils.getPrimaryShellRc();
44
+ console.log(`Saving path: ${value} to ${shellRc}`);
45
+ if (prepend) {
46
+ await fs.appendFile(shellRc, `\nexport PATH=$PATH:${value};`, { encoding: 'utf8' });
47
+ return;
48
+ }
49
+ await fs.appendFile(shellRc, `\nexport PATH=${value}:$PATH;`, { encoding: 'utf8' });
50
+ }
51
+ static async dirExists(path) {
52
+ let stat;
53
+ try {
54
+ stat = await fs.stat(path);
55
+ return stat.isDirectory();
56
+ }
57
+ catch {
58
+ return false;
59
+ }
60
+ }
61
+ static async fileExists(path) {
62
+ let stat;
63
+ try {
64
+ stat = await fs.stat(path);
65
+ return stat.isFile();
66
+ }
67
+ catch {
68
+ return false;
69
+ }
70
+ }
71
+ static async exists(path) {
72
+ try {
73
+ await fs.stat(path);
74
+ return true;
75
+ }
76
+ catch {
77
+ return false;
78
+ }
79
+ }
80
+ static async checkDirExistsOrThrowIfFile(path) {
81
+ let stat;
82
+ try {
83
+ stat = await fs.stat(path);
84
+ }
85
+ catch {
86
+ return false;
87
+ }
88
+ if (stat.isDirectory()) {
89
+ return true;
90
+ }
91
+ throw new Error(`Directory ${path} already exists and is a file`);
92
+ }
93
+ static async createDirIfNotExists(path) {
94
+ if (!fsSync.existsSync(path)) {
95
+ await fs.mkdir(path, { recursive: true });
96
+ }
97
+ }
98
+ static async removeFromFile(filePath, search) {
99
+ const contents = await fs.readFile(filePath, 'utf8');
100
+ const newContents = contents.replaceAll(search, '');
101
+ await fs.writeFile(filePath, newContents, 'utf8');
102
+ }
103
+ static async removeLineFromFile(filePath, search) {
104
+ const file = await fs.readFile(filePath, 'utf8');
105
+ const lines = file.split('\n');
106
+ let searchRegex;
107
+ let searchString;
108
+ if (typeof search === 'object') {
109
+ const startRegex = /^([\t ]*)?/;
110
+ const endRegex = /([\t ]*)?/;
111
+ // Augment regex with spaces criteria to make sure this function is not deleting lines that are comments or has other content.
112
+ searchRegex = search
113
+ ? new RegExp(startRegex.source + search.source + endRegex.source, search.flags)
114
+ : search;
115
+ }
116
+ if (typeof search === 'string') {
117
+ searchString = search;
118
+ }
119
+ for (let counter = lines.length; counter >= 0; counter--) {
120
+ if (!lines[counter]) {
121
+ continue;
122
+ }
123
+ if (searchString && lines[counter].includes(searchString)) {
124
+ lines.splice(counter, 1);
125
+ continue;
126
+ }
127
+ if (searchRegex && lines[counter].search(searchRegex) !== -1) {
128
+ lines.splice(counter, 1);
129
+ }
130
+ }
131
+ await fs.writeFile(filePath, lines.join('\n'));
132
+ console.log(`Removed line: ${search} from ${filePath}`);
133
+ }
134
+ static async removeLineFromPrimaryShellRc(search) {
135
+ return FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
136
+ }
137
+ // Append the string to the end of a file ensuring at least 1 lines of space between.
138
+ // Ex result:
139
+ // something something;
140
+ //
141
+ // newline;
142
+ static appendToFileWithSpacing(file, textToInsert) {
143
+ const lines = file.trimEnd().split(/\n/);
144
+ if (lines.length === 0) {
145
+ return textToInsert;
146
+ }
147
+ const endingNewLines = FileUtils.calculateEndingNewLines(lines);
148
+ const numNewLines = endingNewLines === -1
149
+ ? 0
150
+ : Math.max(0, 2 - endingNewLines);
151
+ return lines.join('\n') + '\n'.repeat(numNewLines) + textToInsert;
152
+ }
153
+ // This is overly complicated but it can be used to insert into any
154
+ // position in the future
155
+ static calculateEndingNewLines(lines) {
156
+ let counter = 0;
157
+ while (true) {
158
+ const line = lines.at(-counter - 1);
159
+ if (!line) {
160
+ return -1;
161
+ }
162
+ if (!SPACE_REGEX.test(line)) {
163
+ return counter;
164
+ }
165
+ counter++;
166
+ // Short circuit here because we don't need to check over 2;
167
+ if (counter > 2) {
168
+ return counter;
169
+ }
170
+ }
171
+ }
172
+ }
@@ -0,0 +1,12 @@
1
+ import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
2
+ export declare function splitUserConfig<T extends StringIndexedObject>(config: ResourceConfig & T): {
3
+ parameters: T;
4
+ coreParameters: ResourceConfig;
5
+ };
6
+ export declare function setsEqual(set1: Set<unknown>, set2: Set<unknown>): boolean;
7
+ export declare function untildify(pathWithTilde: string): string;
8
+ export declare function tildify(pathWithTilde: string): string;
9
+ export declare function resolvePathWithVariables(pathWithVariables: string): string;
10
+ export declare function addVariablesToPath(pathWithoutVariables: string): string;
11
+ export declare function unhome(pathWithHome: string): string;
12
+ export declare function areArraysEqual(isElementEqual: ((desired: unknown, current: unknown) => boolean) | undefined, desired: unknown, current: unknown): boolean;
@@ -0,0 +1,74 @@
1
+ import os from 'node:os';
2
+ import path from 'node:path';
3
+ export function splitUserConfig(config) {
4
+ const coreParameters = {
5
+ type: config.type,
6
+ ...(config.name ? { name: config.name } : {}),
7
+ ...(config.dependsOn ? { dependsOn: config.dependsOn } : {}),
8
+ };
9
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
10
+ const { type, name, dependsOn, ...parameters } = config;
11
+ return {
12
+ parameters: parameters,
13
+ coreParameters,
14
+ };
15
+ }
16
+ export function setsEqual(set1, set2) {
17
+ return set1.size === set2.size && [...set1].every((v) => set2.has(v));
18
+ }
19
+ const homeDirectory = os.homedir();
20
+ export function untildify(pathWithTilde) {
21
+ return homeDirectory ? pathWithTilde.replace(/^~(?=$|\/|\\)/, homeDirectory) : pathWithTilde;
22
+ }
23
+ export function tildify(pathWithTilde) {
24
+ return homeDirectory ? pathWithTilde.replace(homeDirectory, '~') : pathWithTilde;
25
+ }
26
+ export function resolvePathWithVariables(pathWithVariables) {
27
+ // @ts-expect-error Ignore this for now
28
+ return pathWithVariables.replace(/\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/ig, (_, a, b) => process.env[a || b]);
29
+ }
30
+ export function addVariablesToPath(pathWithoutVariables) {
31
+ let result = pathWithoutVariables;
32
+ for (const [key, value] of Object.entries(process.env)) {
33
+ if (!value || !path.isAbsolute(value) || value === '/' || key === 'HOME' || key === 'PATH' || key === 'SHELL' || key === 'PWD') {
34
+ continue;
35
+ }
36
+ result = result.replaceAll(value, `$${key}`);
37
+ }
38
+ return result;
39
+ }
40
+ export function unhome(pathWithHome) {
41
+ return pathWithHome.includes('$HOME') ? pathWithHome.replaceAll('$HOME', os.homedir()) : pathWithHome;
42
+ }
43
+ export function areArraysEqual(isElementEqual, desired, current) {
44
+ if (!desired || !current) {
45
+ return false;
46
+ }
47
+ if (!Array.isArray(desired) || !Array.isArray(current)) {
48
+ throw new Error(`A non-array value:
49
+
50
+ Desired: ${JSON.stringify(desired, null, 2)}
51
+
52
+ Current: ${JSON.stringify(desired, null, 2)}
53
+
54
+ Was provided even though type array was specified.
55
+ `);
56
+ }
57
+ if (desired.length !== current.length) {
58
+ return false;
59
+ }
60
+ const desiredCopy = [...desired];
61
+ const currentCopy = [...current];
62
+ // Algorithm for to check equality between two un-ordered; un-hashable arrays using
63
+ // an isElementEqual method. Time: O(n^2)
64
+ for (let counter = desiredCopy.length - 1; counter >= 0; counter--) {
65
+ const idx = currentCopy.findIndex((e2) => (isElementEqual
66
+ ?? ((a, b) => a === b))(desiredCopy[counter], e2));
67
+ if (idx === -1) {
68
+ return false;
69
+ }
70
+ desiredCopy.splice(counter, 1);
71
+ currentCopy.splice(idx, 1);
72
+ }
73
+ return currentCopy.length === 0;
74
+ }
@@ -1,4 +1,5 @@
1
1
  import { OS } from 'codify-schemas';
2
+ export declare function isDebug(): boolean;
2
3
  export declare enum Shell {
3
4
  ZSH = "zsh",
4
5
  BASH = "bash",
@@ -1,5 +1,8 @@
1
1
  import os from 'node:os';
2
2
  import path from 'node:path';
3
+ export function isDebug() {
4
+ return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
5
+ }
3
6
  export var Shell;
4
7
  (function (Shell) {
5
8
  Shell["ZSH"] = "zsh";
@@ -1,10 +1,4 @@
1
1
  import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
2
- export declare const VerbosityLevel: {
3
- level: number;
4
- get(): number;
5
- set(level: number): void;
6
- };
7
- export declare function isDebug(): boolean;
8
2
  export declare function splitUserConfig<T extends StringIndexedObject>(config: ResourceConfig & T): {
9
3
  parameters: T;
10
4
  coreParameters: ResourceConfig;
@@ -1,17 +1,5 @@
1
1
  import os from 'node:os';
2
2
  import path from 'node:path';
3
- export const VerbosityLevel = new class {
4
- level = 0;
5
- get() {
6
- return this.level;
7
- }
8
- set(level) {
9
- this.level = level;
10
- }
11
- };
12
- export function isDebug() {
13
- return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
14
- }
15
3
  export function splitUserConfig(config) {
16
4
  const coreParameters = {
17
5
  type: config.type,
@@ -0,0 +1,5 @@
1
+ export declare const VerbosityLevel: {
2
+ level: number;
3
+ get(): number;
4
+ set(level: number): void;
5
+ };
@@ -0,0 +1,9 @@
1
+ export const VerbosityLevel = new class {
2
+ level = 0;
3
+ get() {
4
+ return this.level;
5
+ }
6
+ set(level) {
7
+ this.level = level;
8
+ }
9
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.182-beta1",
3
+ "version": "1.0.182-beta11",
4
4
  "description": "Library plugin library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -22,7 +22,7 @@
22
22
  "ajv": "^8.12.0",
23
23
  "ajv-formats": "^2.1.1",
24
24
  "clean-deep": "^3.4.0",
25
- "codify-schemas": "1.0.86",
25
+ "codify-schemas": "1.0.86-beta5",
26
26
  "lodash.isequal": "^4.5.0",
27
27
  "nanoid": "^5.0.9",
28
28
  "strip-ansi": "^7.1.0",
package/src/index.ts CHANGED
@@ -12,7 +12,10 @@ export * from './resource/parsed-resource-settings.js';
12
12
  export * from './resource/resource.js'
13
13
  export * from './resource/resource-settings.js'
14
14
  export * from './stateful-parameter/stateful-parameter.js'
15
+ export * from './utils/file-utils.js'
16
+ export * from './utils/functions.js'
15
17
  export * from './utils/index.js'
18
+ export * from './utils/verbosity-level.js'
16
19
 
17
20
  export async function runPlugin(plugin: Plugin) {
18
21
  const messageHandler = new MessageHandler(plugin);
@@ -24,8 +24,8 @@ import { getPty } from '../pty/index.js';
24
24
  import { SequentialPty } from '../pty/seqeuntial-pty.js';
25
25
  import { Resource } from '../resource/resource.js';
26
26
  import { ResourceController } from '../resource/resource-controller.js';
27
- import { VerbosityLevel } from '../utils/internal-utils.js';
28
27
  import { ptyLocalStorage } from '../utils/pty-local-storage.js';
28
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
29
29
 
30
30
  export class Plugin {
31
31
  planStorage: Map<string, Plan<any>>;
@@ -7,7 +7,7 @@ import stripAnsi from 'strip-ansi';
7
7
 
8
8
  import { debugLog } from '../utils/debug.js';
9
9
  import { Shell, Utils } from '../utils/index.js';
10
- import { VerbosityLevel } from '../utils/internal-utils.js';
10
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
11
11
  import { IPty, SpawnError, SpawnOptions, SpawnResult } from './index.js';
12
12
  import { PromiseQueue } from './promise-queue.js';
13
13
 
@@ -20,8 +20,10 @@ EventEmitter.defaultMaxListeners = 1000;
20
20
  * without a tty (or even a stdin) attached so interactive commands will not work.
21
21
  */
22
22
  export class BackgroundPty implements IPty {
23
+ private historyIgnore = Utils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' };
23
24
  private basePty = pty.spawn(this.getDefaultShell(), ['-i'], {
24
- env: process.env, name: nanoid(6),
25
+ env: { ...process.env, ...this.historyIgnore },
26
+ name: nanoid(6),
25
27
  handleFlowControl: true
26
28
  });
27
29
 
@@ -79,9 +81,9 @@ export class BackgroundPty implements IPty {
79
81
  });
80
82
  } else {
81
83
  // Print to stdout if the verbosity level is above 0
82
- if (VerbosityLevel.get() > 0) {
83
- process.stdout.write(data);
84
- }
84
+ // if (VerbosityLevel.get() > 0) {
85
+ // process.stdout.write(data);
86
+ // }
85
87
  }
86
88
  })
87
89
 
@@ -129,17 +131,17 @@ export class BackgroundPty implements IPty {
129
131
 
130
132
  return new Promise(resolve => {
131
133
  // zsh-specific commands
132
- switch (Utils.getShell()) {
133
- case Shell.ZSH: {
134
- this.basePty.write('setopt HIST_NO_STORE;\n');
135
- break;
136
- }
137
-
138
- default: {
139
- this.basePty.write('export HISTIGNORE=\'history*\';\n');
140
- break;
141
- }
142
- }
134
+ // switch (Utils.getShell()) {
135
+ // case Shell.ZSH: {
136
+ // this.basePty.write('setopt HIST_NO_STORE;\n');
137
+ // break;
138
+ // }
139
+ //
140
+ // default: {
141
+ // this.basePty.write('export HISTIGNORE=\'history*\';\n');
142
+ // break;
143
+ // }
144
+ // }
143
145
 
144
146
  this.basePty.write(' unset PS1;\n');
145
147
  this.basePty.write(' unset PS0;\n')
package/src/pty/index.ts CHANGED
@@ -28,8 +28,10 @@ export enum SpawnStatus {
28
28
  */
29
29
  export interface SpawnOptions {
30
30
  cwd?: string;
31
- env?: Record<string, unknown>,
32
- interactive?: boolean,
31
+ env?: Record<string, unknown>;
32
+ interactive?: boolean;
33
+ requiresRoot?: boolean;
34
+ stdin?: boolean;
33
35
  }
34
36
 
35
37
  export class SpawnError extends Error {
@@ -1,13 +1,21 @@
1
1
  import pty from '@homebridge/node-pty-prebuilt-multiarch';
2
+ import { Ajv } from 'ajv';
3
+ import { CommandRequestResponseData, CommandRequestResponseDataSchema, IpcMessageV2, MessageCmd } from 'codify-schemas';
4
+ import { nanoid } from 'nanoid';
2
5
  import { EventEmitter } from 'node:events';
3
6
  import stripAnsi from 'strip-ansi';
4
7
 
5
8
  import { Shell, Utils } from '../utils/index.js';
6
- import { VerbosityLevel } from '../utils/internal-utils.js';
9
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
7
10
  import { IPty, SpawnError, SpawnOptions, SpawnResult, SpawnStatus } from './index.js';
8
11
 
9
12
  EventEmitter.defaultMaxListeners = 1000;
10
13
 
14
+ const ajv = new Ajv({
15
+ strict: true,
16
+ });
17
+ const validateSudoRequestResponse = ajv.compile(CommandRequestResponseDataSchema);
18
+
11
19
  /**
12
20
  * The background pty is a specialized pty designed for speed. It can launch multiple tasks
13
21
  * in parallel by moving them to the background. It attaches unix FIFO pipes to each process
@@ -26,11 +34,19 @@ export class SequentialPty implements IPty {
26
34
  }
27
35
 
28
36
  async spawnSafe(cmd: string, options?: SpawnOptions): Promise<SpawnResult> {
37
+ if (cmd.includes('sudo')) {
38
+ throw new Error('Do not directly use sudo. Use the option { requiresRoot: true } instead')
39
+ }
40
+
41
+ // If sudo is required, we must delegate to the main codify process.
42
+ if (options?.stdin || options?.requiresRoot) {
43
+ return this.externalSpawn(cmd, options);
44
+ }
45
+
29
46
  console.log(`Running command: ${cmd}` + (options?.cwd ? `(${options?.cwd})` : ''))
30
47
 
31
48
  return new Promise((resolve) => {
32
49
  const output: string[] = [];
33
-
34
50
  const historyIgnore = Utils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' };
35
51
 
36
52
  // If TERM_PROGRAM=Apple_Terminal is set then ANSI escape characters may be included
@@ -39,14 +55,15 @@ export class SequentialPty implements IPty {
39
55
  ...process.env, ...options?.env,
40
56
  TERM_PROGRAM: 'codify',
41
57
  COMMAND_MODE: 'unix2003',
42
- COLORTERM: 'truecolor', ...historyIgnore
58
+ COLORTERM: 'truecolor',
59
+ ...historyIgnore
43
60
  }
44
61
 
45
62
  // Initial terminal dimensions
46
63
  const initialCols = process.stdout.columns ?? 80;
47
64
  const initialRows = process.stdout.rows ?? 24;
48
65
 
49
- const args = (options?.interactive ?? true) ? ['-i', '-c', `"${cmd}"`] : ['-c', `"${cmd}"`]
66
+ const args = options?.interactive ? ['-i', '-c', cmd] : ['-c', cmd]
50
67
 
51
68
  // Run the command in a pty for interactivity
52
69
  const mPty = pty.spawn(this.getDefaultShell(), args, {
@@ -64,10 +81,6 @@ export class SequentialPty implements IPty {
64
81
  output.push(data.toString());
65
82
  })
66
83
 
67
- const stdinListener = (data: any) => {
68
- mPty.write(data.toString());
69
- };
70
-
71
84
  const resizeListener = () => {
72
85
  const { columns, rows } = process.stdout;
73
86
  mPty.resize(columns, rows);
@@ -75,12 +88,9 @@ export class SequentialPty implements IPty {
75
88
 
76
89
  // Listen to resize events for the terminal window;
77
90
  process.stdout.on('resize', resizeListener);
78
- // Listen for user input
79
- process.stdin.on('data', stdinListener);
80
91
 
81
92
  mPty.onExit((result) => {
82
93
  process.stdout.off('resize', resizeListener);
83
- process.stdin.off('data', stdinListener);
84
94
 
85
95
  resolve({
86
96
  status: result.exitCode === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR,
@@ -99,6 +109,39 @@ export class SequentialPty implements IPty {
99
109
  }
100
110
  }
101
111
 
112
+ // For safety reasons, requests that require sudo or are interactive must be run via the main client
113
+ async externalSpawn(
114
+ cmd: string,
115
+ opts: SpawnOptions
116
+ ): Promise<SpawnResult> {
117
+ return new Promise((resolve) => {
118
+ const requestId = nanoid(8);
119
+
120
+ const listener = (data: IpcMessageV2) => {
121
+ if (data.requestId === requestId) {
122
+ process.removeListener('message', listener);
123
+
124
+ if (!validateSudoRequestResponse(data.data)) {
125
+ throw new Error(`Invalid response for sudo request: ${JSON.stringify(validateSudoRequestResponse.errors, null, 2)}`);
126
+ }
127
+
128
+ resolve(data.data as unknown as CommandRequestResponseData);
129
+ }
130
+ }
131
+
132
+ process.on('message', listener);
133
+
134
+ process.send!(<IpcMessageV2>{
135
+ cmd: MessageCmd.COMMAND_REQUEST,
136
+ data: {
137
+ command: cmd,
138
+ options: opts ?? {},
139
+ },
140
+ requestId
141
+ })
142
+ });
143
+ }
144
+
102
145
  private getDefaultShell(): string {
103
146
  return process.env.SHELL!;
104
147
  }
@@ -1,6 +1,8 @@
1
1
  import { describe, expect, it } from 'vitest';
2
2
  import { SequentialPty } from './seqeuntial-pty.js';
3
- import { VerbosityLevel } from '../utils/internal-utils.js';
3
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
4
+ import { MessageStatus, SpawnStatus } from 'codify-schemas/src/types/index.js';
5
+ import { IpcMessageV2, MessageCmd } from 'codify-schemas';
4
6
 
5
7
  describe('SequentialPty tests', () => {
6
8
  it('Can launch a simple command', async () => {
@@ -33,8 +35,8 @@ describe('SequentialPty tests', () => {
33
35
  const resultFailed = await pty.spawnSafe('which sjkdhsakjdhjkash');
34
36
  expect(resultFailed).toMatchObject({
35
37
  status: 'error',
36
- exitCode: 127,
37
- data: 'zsh:1: command not found: which sjkdhsakjdhjkash' // This might change on different os or shells. Keep for now.
38
+ exitCode: 1,
39
+ data: 'sjkdhsakjdhjkash not found' // This might change on different os or shells. Keep for now.
38
40
  })
39
41
  });
40
42
 
@@ -50,12 +52,128 @@ describe('SequentialPty tests', () => {
50
52
  });
51
53
 
52
54
  it('It can launch a command in interactive mode', async () => {
53
- const pty = new SequentialPty();
55
+ const originalSend = process.send;
56
+ process.send = (req: IpcMessageV2) => {
57
+ expect(req).toMatchObject({
58
+ cmd: MessageCmd.COMMAND_REQUEST,
59
+ requestId: expect.any(String),
60
+ data: {
61
+ command: 'ls',
62
+ options: {
63
+ cwd: '/tmp',
64
+ interactive: true,
65
+ }
66
+ }
67
+ })
68
+
69
+ // This may look confusing but what we're doing here is directly finding the process listener and calling it without going through serialization
70
+ const listeners = process.listeners('message');
71
+ listeners[2](({
72
+ cmd: MessageCmd.COMMAND_REQUEST,
73
+ requestId: req.requestId,
74
+ status: MessageStatus.SUCCESS,
75
+ data: {
76
+ status: SpawnStatus.SUCCESS,
77
+ exitCode: 0,
78
+ data: 'My data',
79
+ }
80
+ }))
81
+
82
+ return true;
83
+ }
84
+
85
+ const $ = new SequentialPty();
86
+ const resultSuccess = await $.spawnSafe('ls', { interactive: true, cwd: '/tmp' });
54
87
 
55
- const resultSuccess = await pty.spawnSafe('ls', { interactive: false });
56
88
  expect(resultSuccess).toMatchObject({
57
89
  status: 'success',
58
90
  exitCode: 0,
59
- })
91
+ });
92
+
93
+ process.send = originalSend;
60
94
  });
95
+
96
+ it('It can work with root (sudo)', async () => {
97
+ const originalSend = process.send;
98
+ process.send = (req: IpcMessageV2) => {
99
+ expect(req).toMatchObject({
100
+ cmd: MessageCmd.COMMAND_REQUEST,
101
+ requestId: expect.any(String),
102
+ data: {
103
+ command: 'ls',
104
+ options: {
105
+ interactive: true,
106
+ requiresRoot: true,
107
+ }
108
+ }
109
+ })
110
+
111
+ // This may look confusing but what we're doing here is directly finding the process listener and calling it without going through serialization
112
+ const listeners = process.listeners('message');
113
+ listeners[2](({
114
+ cmd: MessageCmd.COMMAND_REQUEST,
115
+ requestId: req.requestId,
116
+ status: MessageStatus.SUCCESS,
117
+ data: {
118
+ status: SpawnStatus.SUCCESS,
119
+ exitCode: 0,
120
+ data: 'My data',
121
+ }
122
+ }))
123
+
124
+ return true;
125
+ }
126
+
127
+ const $ = new SequentialPty();
128
+ const resultSuccess = await $.spawn('ls', { interactive: true, requiresRoot: true });
129
+
130
+ expect(resultSuccess).toMatchObject({
131
+ status: 'success',
132
+ exitCode: 0,
133
+ });
134
+
135
+ process.send = originalSend;
136
+ })
137
+
138
+ it('It can handle errors when in sudo', async () => {
139
+ const originalSend = process.send;
140
+ process.send = (req: IpcMessageV2) => {
141
+ expect(req).toMatchObject({
142
+ cmd: MessageCmd.COMMAND_REQUEST,
143
+ requestId: expect.any(String),
144
+ data: {
145
+ command: 'ls',
146
+ options: {
147
+ requiresRoot: true,
148
+ interactive: true,
149
+ }
150
+ }
151
+ })
152
+
153
+ // This may look confusing but what we're doing here is directly finding the process listener and calling it without going through serialization
154
+ const listeners = process.listeners('message');
155
+ listeners[2](({
156
+ cmd: MessageCmd.COMMAND_REQUEST,
157
+ requestId: req.requestId,
158
+ status: MessageStatus.SUCCESS,
159
+ data: {
160
+ status: SpawnStatus.ERROR,
161
+ exitCode: 127,
162
+ data: 'My data',
163
+ }
164
+ }))
165
+
166
+ return true;
167
+ }
168
+
169
+ const $ = new SequentialPty();
170
+ const resultSuccess = await $.spawnSafe('ls', { interactive: true, requiresRoot: true });
171
+
172
+ expect(resultSuccess).toMatchObject({
173
+ status: SpawnStatus.ERROR,
174
+ exitCode: 127,
175
+ });
176
+
177
+ process.send = originalSend;
178
+ })
61
179
  })
@@ -7,7 +7,7 @@ import { CreatePlan, DestroyPlan, ModifyPlan } from '../plan/plan-types.js';
7
7
  import { ParameterChange } from '../plan/change-set.js';
8
8
  import { ResourceController } from './resource-controller.js';
9
9
  import { TestConfig, testPlan, TestResource, TestStatefulParameter } from '../utils/test-utils.test.js';
10
- import { tildify, untildify } from '../utils/internal-utils.js';
10
+ import { tildify, untildify } from '../utils/functions.js';
11
11
  import { ArrayStatefulParameter, StatefulParameter } from '../stateful-parameter/stateful-parameter.js';
12
12
  import { Plan } from '../plan/plan.js';
13
13
  import os from 'node:os';
@@ -10,7 +10,7 @@ import {
10
10
  resolvePathWithVariables,
11
11
  tildify,
12
12
  untildify
13
- } from '../utils/internal-utils.js';
13
+ } from '../utils/functions.js';
14
14
  import { RefreshContext } from './resource.js';
15
15
 
16
16
  export interface InputTransformation {
@@ -0,0 +1,7 @@
1
+ import { describe, it } from 'vitest';
2
+
3
+ describe('File utils tests', { timeout: 100_000_000 }, () => {
4
+ it('Can download a file', async () => {
5
+ // await FileUtils.downloadFile('https://download.jetbrains.com/webstorm/WebStorm-2025.3.1-aarch64.dmg?_gl=1*1huoi7o*_gcl_aw*R0NMLjE3NjU3NDAwMTcuQ2p3S0NBaUEzZm5KQmhBZ0Vpd0F5cW1ZNVhLVENlbHJOcTk2YXdjZVlfMS1wdE91MXc0WDk2bFJkVDM3QURhUFNJMUtwNVVSVUhxWTJob0NuZ0FRQXZEX0J3RQ..*_gcl_au*MjA0MDQ0MjE2My4xNzYzNjQzNzMz*FPAU*MjA0MDQ0MjE2My4xNzYzNjQzNzMz*_ga*MTYxMDg4MTkzMi4xNzYzNjQzNzMz*_ga_9J976DJZ68*czE3NjYzNjI5ODAkbzEyJGcxJHQxNzY2MzYzMDQwJGo2MCRsMCRoMA..', path.join(process.cwd(), 'google.html'));
6
+ })
7
+ })
@@ -0,0 +1,216 @@
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 addToStartupFile(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 addAllToStartupFile(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
+ static async addPathToPrimaryShellRc(value: string, prepend: boolean): Promise<void> {
59
+ const shellRc = Utils.getPrimaryShellRc();
60
+ console.log(`Saving path: ${value} to ${shellRc}`);
61
+
62
+ if (prepend) {
63
+ await fs.appendFile(shellRc, `\nexport PATH=$PATH:${value};`, { encoding: 'utf8' });
64
+ return;
65
+ }
66
+
67
+ await fs.appendFile(shellRc, `\nexport PATH=${value}:$PATH;`, { encoding: 'utf8' });
68
+ }
69
+
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
+ static async removeFromFile(filePath: string, search: string): Promise<void> {
121
+ const contents = await fs.readFile(filePath, 'utf8');
122
+ const newContents = contents.replaceAll(search, '');
123
+
124
+ await fs.writeFile(filePath, newContents, 'utf8');
125
+ }
126
+
127
+
128
+ static async removeLineFromFile(filePath: string, search: RegExp | string): Promise<void> {
129
+ const file = await fs.readFile(filePath, 'utf8')
130
+ const lines = file.split('\n');
131
+
132
+ let searchRegex;
133
+ let searchString;
134
+
135
+ if (typeof search === 'object') {
136
+ const startRegex = /^([\t ]*)?/;
137
+ const endRegex = /([\t ]*)?/;
138
+
139
+ // Augment regex with spaces criteria to make sure this function is not deleting lines that are comments or has other content.
140
+ searchRegex = search
141
+ ? new RegExp(
142
+ startRegex.source + search.source + endRegex.source,
143
+ search.flags
144
+ )
145
+ : search;
146
+ }
147
+
148
+ if (typeof search === 'string') {
149
+ searchString = search;
150
+ }
151
+
152
+ for (let counter = lines.length; counter >= 0; counter--) {
153
+ if (!lines[counter]) {
154
+ continue;
155
+ }
156
+
157
+ if (searchString && lines[counter].includes(searchString)) {
158
+ lines.splice(counter, 1);
159
+ continue;
160
+ }
161
+
162
+ if (searchRegex && lines[counter].search(searchRegex) !== -1) {
163
+ lines.splice(counter, 1);
164
+ }
165
+ }
166
+
167
+ await fs.writeFile(filePath, lines.join('\n'));
168
+ console.log(`Removed line: ${search} from ${filePath}`)
169
+ }
170
+
171
+ static async removeLineFromPrimaryShellRc(search: RegExp | string): Promise<void> {
172
+ return FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
173
+ }
174
+
175
+ // Append the string to the end of a file ensuring at least 1 lines of space between.
176
+ // Ex result:
177
+ // something something;
178
+ //
179
+ // newline;
180
+ static appendToFileWithSpacing(file: string, textToInsert: string): string {
181
+ const lines = file.trimEnd().split(/\n/);
182
+ if (lines.length === 0) {
183
+ return textToInsert;
184
+ }
185
+
186
+ const endingNewLines = FileUtils.calculateEndingNewLines(lines);
187
+ const numNewLines = endingNewLines === -1
188
+ ? 0
189
+ : Math.max(0, 2 - endingNewLines);
190
+ return lines.join('\n') + '\n'.repeat(numNewLines) + textToInsert
191
+ }
192
+
193
+ // This is overly complicated but it can be used to insert into any
194
+ // position in the future
195
+ private static calculateEndingNewLines(lines: string[]): number {
196
+ let counter = 0;
197
+ while (true) {
198
+ const line = lines.at(-counter - 1);
199
+
200
+ if (!line) {
201
+ return -1
202
+ }
203
+
204
+ if (!SPACE_REGEX.test(line)) {
205
+ return counter;
206
+ }
207
+
208
+ counter++;
209
+
210
+ // Short circuit here because we don't need to check over 2;
211
+ if (counter > 2) {
212
+ return counter;
213
+ }
214
+ }
215
+ }
216
+ }
@@ -2,22 +2,6 @@ import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
2
2
  import os from 'node:os';
3
3
  import path from 'node:path';
4
4
 
5
- export const VerbosityLevel = new class {
6
- level = 0;
7
-
8
- get() {
9
- return this.level;
10
- }
11
-
12
- set(level: number) {
13
- this.level = level;
14
- }
15
- }
16
-
17
- export function isDebug(): boolean {
18
- return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
19
- }
20
-
21
5
  export function splitUserConfig<T extends StringIndexedObject>(
22
6
  config: ResourceConfig & T
23
7
  ): { parameters: T; coreParameters: ResourceConfig } {
@@ -2,6 +2,10 @@ import { OS } from 'codify-schemas';
2
2
  import os from 'node:os';
3
3
  import path from 'node:path';
4
4
 
5
+ export function isDebug(): boolean {
6
+ return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
7
+ }
8
+
5
9
  export enum Shell {
6
10
  ZSH = 'zsh',
7
11
  BASH = 'bash',
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { addVariablesToPath, resolvePathWithVariables, splitUserConfig } from './internal-utils.js';
2
+ import { addVariablesToPath, resolvePathWithVariables, splitUserConfig } from './functions.js';
3
3
  import os from 'node:os';
4
4
 
5
5
  describe('Utils tests', () => {
@@ -0,0 +1,11 @@
1
+ export const VerbosityLevel = new class {
2
+ level = 0;
3
+
4
+ get() {
5
+ return this.level;
6
+ }
7
+
8
+ set(level: number) {
9
+ this.level = level;
10
+ }
11
+ }