codify-plugin-lib 1.0.181 → 1.0.182-beta10
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 +4 -1
- package/dist/index.js +4 -1
- package/dist/plugin/plugin.js +5 -2
- package/dist/pty/background-pty.d.ts +1 -0
- package/dist/pty/background-pty.js +17 -6
- package/dist/pty/index.d.ts +19 -1
- package/dist/pty/seqeuntial-pty.d.ts +17 -0
- package/dist/pty/seqeuntial-pty.js +117 -0
- package/dist/resource/parsed-resource-settings.d.ts +3 -1
- package/dist/resource/parsed-resource-settings.js +4 -6
- package/dist/resource/resource-settings.d.ts +5 -1
- package/dist/resource/resource-settings.js +1 -1
- package/dist/test.d.ts +1 -0
- package/dist/test.js +5 -0
- package/dist/utils/file-utils.d.ts +16 -0
- package/dist/utils/file-utils.js +172 -0
- package/dist/utils/functions.d.ts +12 -0
- package/dist/utils/functions.js +74 -0
- package/dist/utils/index.d.ts +26 -0
- package/dist/utils/index.js +111 -0
- package/dist/utils/internal-utils.d.ts +12 -0
- package/dist/utils/internal-utils.js +74 -0
- package/dist/utils/verbosity-level.d.ts +5 -0
- package/dist/utils/verbosity-level.js +9 -0
- package/package.json +2 -2
- package/src/index.ts +4 -1
- package/src/plan/plan.test.ts +6 -1
- package/src/plugin/plugin.test.ts +11 -2
- package/src/plugin/plugin.ts +5 -2
- package/src/pty/background-pty.ts +18 -6
- package/src/pty/index.test.ts +7 -4
- package/src/pty/index.ts +20 -2
- package/src/pty/seqeuntial-pty.ts +148 -0
- package/src/pty/sequential-pty.test.ts +179 -0
- package/src/resource/parsed-resource-settings.ts +8 -7
- package/src/resource/resource-controller-stateful-mode.test.ts +2 -1
- package/src/resource/resource-controller.test.ts +22 -4
- package/src/resource/resource-settings.test.ts +29 -2
- package/src/resource/resource-settings.ts +13 -2
- package/src/utils/file-utils.test.ts +7 -0
- package/src/utils/file-utils.ts +216 -0
- package/src/utils/{utils.ts → functions.ts} +0 -16
- package/src/utils/index.ts +144 -0
- package/src/utils/{utils.test.ts → internal-utils.test.ts} +1 -1
- package/src/utils/test-utils.test.ts +5 -2
- package/src/utils/verbosity-level.ts +11 -0
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/utils.js';
|
|
13
|
+
export * from './utils/file-utils.js';
|
|
14
|
+
export * from './utils/functions.js';
|
|
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/utils.js';
|
|
13
|
+
export * from './utils/file-utils.js';
|
|
14
|
+
export * from './utils/functions.js';
|
|
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));
|
package/dist/plugin/plugin.js
CHANGED
|
@@ -2,9 +2,10 @@ import { ApplyValidationError } from '../common/errors.js';
|
|
|
2
2
|
import { Plan } from '../plan/plan.js';
|
|
3
3
|
import { BackgroundPty } from '../pty/background-pty.js';
|
|
4
4
|
import { getPty } from '../pty/index.js';
|
|
5
|
+
import { SequentialPty } from '../pty/seqeuntial-pty.js';
|
|
5
6
|
import { ResourceController } from '../resource/resource-controller.js';
|
|
6
7
|
import { ptyLocalStorage } from '../utils/pty-local-storage.js';
|
|
7
|
-
import { VerbosityLevel } from '../utils/
|
|
8
|
+
import { VerbosityLevel } from '../utils/verbosity-level.js';
|
|
8
9
|
export class Plugin {
|
|
9
10
|
name;
|
|
10
11
|
resourceControllers;
|
|
@@ -44,6 +45,7 @@ export class Plugin {
|
|
|
44
45
|
dependencies: r.dependencies,
|
|
45
46
|
type: r.typeId,
|
|
46
47
|
sensitiveParameters,
|
|
48
|
+
operatingSystems: r.settings.operatingSystems,
|
|
47
49
|
};
|
|
48
50
|
})
|
|
49
51
|
};
|
|
@@ -81,6 +83,7 @@ export class Plugin {
|
|
|
81
83
|
import: {
|
|
82
84
|
requiredParameters: requiredPropertyNames,
|
|
83
85
|
},
|
|
86
|
+
operatingSystems: resource.settings.operatingSystems,
|
|
84
87
|
sensitiveParameters,
|
|
85
88
|
allowMultiple
|
|
86
89
|
};
|
|
@@ -158,7 +161,7 @@ export class Plugin {
|
|
|
158
161
|
if (!resource) {
|
|
159
162
|
throw new Error('Malformed plan with resource that cannot be found');
|
|
160
163
|
}
|
|
161
|
-
await resource.apply(plan);
|
|
164
|
+
await ptyLocalStorage.run(new SequentialPty(), async () => resource.apply(plan));
|
|
162
165
|
// Validate using desired/desired. If the apply was successful, no changes should be reported back.
|
|
163
166
|
// Default back desired back to current if it is not defined (for destroys only)
|
|
164
167
|
const validationPlan = await ptyLocalStorage.run(new BackgroundPty(), async () => {
|
|
@@ -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();
|
|
@@ -5,7 +5,8 @@ import { EventEmitter } from 'node:events';
|
|
|
5
5
|
import * as fs from 'node:fs/promises';
|
|
6
6
|
import stripAnsi from 'strip-ansi';
|
|
7
7
|
import { debugLog } from '../utils/debug.js';
|
|
8
|
-
import {
|
|
8
|
+
import { Shell, Utils } from '../utils/index.js';
|
|
9
|
+
import { VerbosityLevel } from '../utils/verbosity-level.js';
|
|
9
10
|
import { SpawnError } from './index.js';
|
|
10
11
|
import { PromiseQueue } from './promise-queue.js';
|
|
11
12
|
EventEmitter.defaultMaxListeners = 1000;
|
|
@@ -16,8 +17,10 @@ EventEmitter.defaultMaxListeners = 1000;
|
|
|
16
17
|
* without a tty (or even a stdin) attached so interactive commands will not work.
|
|
17
18
|
*/
|
|
18
19
|
export class BackgroundPty {
|
|
20
|
+
historyIgnore = Utils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' };
|
|
19
21
|
basePty = pty.spawn(this.getDefaultShell(), ['-i'], {
|
|
20
|
-
env: process.env,
|
|
22
|
+
env: { ...process.env, ...this.historyIgnore },
|
|
23
|
+
name: nanoid(6),
|
|
21
24
|
handleFlowControl: true
|
|
22
25
|
});
|
|
23
26
|
promiseQueue = new PromiseQueue();
|
|
@@ -104,9 +107,17 @@ export class BackgroundPty {
|
|
|
104
107
|
let outputBuffer = '';
|
|
105
108
|
return new Promise(resolve => {
|
|
106
109
|
// zsh-specific commands
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
+
// switch (Utils.getShell()) {
|
|
111
|
+
// case Shell.ZSH: {
|
|
112
|
+
// this.basePty.write('setopt HIST_NO_STORE;\n');
|
|
113
|
+
// break;
|
|
114
|
+
// }
|
|
115
|
+
//
|
|
116
|
+
// default: {
|
|
117
|
+
// this.basePty.write('export HISTIGNORE=\'history*\';\n');
|
|
118
|
+
// break;
|
|
119
|
+
// }
|
|
120
|
+
// }
|
|
110
121
|
this.basePty.write(' unset PS1;\n');
|
|
111
122
|
this.basePty.write(' unset PS0;\n');
|
|
112
123
|
this.basePty.write(' echo setup complete\\"\n');
|
|
@@ -121,6 +132,6 @@ export class BackgroundPty {
|
|
|
121
132
|
});
|
|
122
133
|
}
|
|
123
134
|
getDefaultShell() {
|
|
124
|
-
return process.
|
|
135
|
+
return process.env.SHELL;
|
|
125
136
|
}
|
|
126
137
|
}
|
package/dist/pty/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export interface SpawnResult {
|
|
2
|
-
status: '
|
|
2
|
+
status: 'error' | 'success';
|
|
3
3
|
exitCode: number;
|
|
4
4
|
data: string;
|
|
5
5
|
}
|
|
@@ -7,9 +7,27 @@ export declare enum SpawnStatus {
|
|
|
7
7
|
SUCCESS = "success",
|
|
8
8
|
ERROR = "error"
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Represents the configuration options for spawning a child process.
|
|
12
|
+
*
|
|
13
|
+
* @interface SpawnOptions
|
|
14
|
+
*
|
|
15
|
+
* @property {string} [cwd] - Specifies the working directory of the child process.
|
|
16
|
+
* If not provided, the current working directory of the parent process is used.
|
|
17
|
+
*
|
|
18
|
+
* @property {Record<string, unknown>} [env] - Defines environment key-value pairs
|
|
19
|
+
* that will be available to the child process. If not specified, the child process
|
|
20
|
+
* will inherit the environment variables of the parent process.
|
|
21
|
+
*
|
|
22
|
+
* @property {boolean} [interactive] - Indicates whether the spawned process needs
|
|
23
|
+
* to be interactive. Only works within apply (not plan). Defaults to true.
|
|
24
|
+
*/
|
|
10
25
|
export interface SpawnOptions {
|
|
11
26
|
cwd?: string;
|
|
12
27
|
env?: Record<string, unknown>;
|
|
28
|
+
interactive?: boolean;
|
|
29
|
+
requiresRoot?: boolean;
|
|
30
|
+
stdin?: boolean;
|
|
13
31
|
}
|
|
14
32
|
export declare class SpawnError extends Error {
|
|
15
33
|
data: string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { IPty, SpawnOptions, SpawnResult } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* The background pty is a specialized pty designed for speed. It can launch multiple tasks
|
|
4
|
+
* in parallel by moving them to the background. It attaches unix FIFO pipes to each process
|
|
5
|
+
* to listen to stdout and stderr. One limitation of the BackgroundPty is that the tasks run
|
|
6
|
+
* without a tty (or even a stdin) attached so interactive commands will not work.
|
|
7
|
+
*/
|
|
8
|
+
export declare class SequentialPty implements IPty {
|
|
9
|
+
spawn(cmd: string, options?: SpawnOptions): Promise<SpawnResult>;
|
|
10
|
+
spawnSafe(cmd: string, options?: SpawnOptions): Promise<SpawnResult>;
|
|
11
|
+
kill(): Promise<{
|
|
12
|
+
exitCode: number;
|
|
13
|
+
signal?: number | undefined;
|
|
14
|
+
}>;
|
|
15
|
+
externalSpawn(cmd: string, opts: SpawnOptions): Promise<SpawnResult>;
|
|
16
|
+
private getDefaultShell;
|
|
17
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
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';
|
|
5
|
+
import { EventEmitter } from 'node:events';
|
|
6
|
+
import stripAnsi from 'strip-ansi';
|
|
7
|
+
import { Shell, Utils } from '../utils/index.js';
|
|
8
|
+
import { VerbosityLevel } from '../utils/verbosity-level.js';
|
|
9
|
+
import { SpawnError, SpawnStatus } from './index.js';
|
|
10
|
+
EventEmitter.defaultMaxListeners = 1000;
|
|
11
|
+
const ajv = new Ajv({
|
|
12
|
+
strict: true,
|
|
13
|
+
});
|
|
14
|
+
const validateSudoRequestResponse = ajv.compile(CommandRequestResponseDataSchema);
|
|
15
|
+
/**
|
|
16
|
+
* The background pty is a specialized pty designed for speed. It can launch multiple tasks
|
|
17
|
+
* in parallel by moving them to the background. It attaches unix FIFO pipes to each process
|
|
18
|
+
* to listen to stdout and stderr. One limitation of the BackgroundPty is that the tasks run
|
|
19
|
+
* without a tty (or even a stdin) attached so interactive commands will not work.
|
|
20
|
+
*/
|
|
21
|
+
export class SequentialPty {
|
|
22
|
+
async spawn(cmd, options) {
|
|
23
|
+
const spawnResult = await this.spawnSafe(cmd, options);
|
|
24
|
+
if (spawnResult.status !== 'success') {
|
|
25
|
+
throw new SpawnError(cmd, spawnResult.exitCode, spawnResult.data);
|
|
26
|
+
}
|
|
27
|
+
return spawnResult;
|
|
28
|
+
}
|
|
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
|
+
}
|
|
37
|
+
console.log(`Running command: ${cmd}` + (options?.cwd ? `(${options?.cwd})` : ''));
|
|
38
|
+
return new Promise((resolve) => {
|
|
39
|
+
const output = [];
|
|
40
|
+
const historyIgnore = Utils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' };
|
|
41
|
+
// If TERM_PROGRAM=Apple_Terminal is set then ANSI escape characters may be included
|
|
42
|
+
// in the response.
|
|
43
|
+
const env = {
|
|
44
|
+
...process.env, ...options?.env,
|
|
45
|
+
TERM_PROGRAM: 'codify',
|
|
46
|
+
COMMAND_MODE: 'unix2003',
|
|
47
|
+
COLORTERM: 'truecolor',
|
|
48
|
+
...historyIgnore
|
|
49
|
+
};
|
|
50
|
+
// Initial terminal dimensions
|
|
51
|
+
const initialCols = process.stdout.columns ?? 80;
|
|
52
|
+
const initialRows = process.stdout.rows ?? 24;
|
|
53
|
+
const args = options?.interactive ? ['-i', '-c', cmd] : ['-c', cmd];
|
|
54
|
+
// Run the command in a pty for interactivity
|
|
55
|
+
const mPty = pty.spawn(this.getDefaultShell(), args, {
|
|
56
|
+
...options,
|
|
57
|
+
cols: initialCols,
|
|
58
|
+
rows: initialRows,
|
|
59
|
+
env
|
|
60
|
+
});
|
|
61
|
+
mPty.onData((data) => {
|
|
62
|
+
if (VerbosityLevel.get() > 0) {
|
|
63
|
+
process.stdout.write(data);
|
|
64
|
+
}
|
|
65
|
+
output.push(data.toString());
|
|
66
|
+
});
|
|
67
|
+
const resizeListener = () => {
|
|
68
|
+
const { columns, rows } = process.stdout;
|
|
69
|
+
mPty.resize(columns, rows);
|
|
70
|
+
};
|
|
71
|
+
// Listen to resize events for the terminal window;
|
|
72
|
+
process.stdout.on('resize', resizeListener);
|
|
73
|
+
mPty.onExit((result) => {
|
|
74
|
+
process.stdout.off('resize', resizeListener);
|
|
75
|
+
resolve({
|
|
76
|
+
status: result.exitCode === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR,
|
|
77
|
+
exitCode: result.exitCode,
|
|
78
|
+
data: stripAnsi(output.join('\n').trim()),
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
async kill() {
|
|
84
|
+
// No-op here. Each pty instance is stand alone and tied to the parent process. Everything should be killed as expected.
|
|
85
|
+
return {
|
|
86
|
+
exitCode: 0,
|
|
87
|
+
signal: 0,
|
|
88
|
+
};
|
|
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
|
+
}
|
|
114
|
+
getDefaultShell() {
|
|
115
|
+
return process.env.SHELL;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JSONSchemaType } from 'ajv';
|
|
2
|
-
import { StringIndexedObject } from 'codify-schemas';
|
|
2
|
+
import { OS, StringIndexedObject } from 'codify-schemas';
|
|
3
3
|
import { StatefulParameterController } from '../stateful-parameter/stateful-parameter-controller.js';
|
|
4
4
|
import { ArrayParameterSetting, DefaultParameterSetting, InputTransformation, ResourceSettings } from './resource-settings.js';
|
|
5
5
|
export interface ParsedStatefulParameterSetting extends DefaultParameterSetting {
|
|
@@ -26,6 +26,8 @@ export declare class ParsedResourceSettings<T extends StringIndexedObject> imple
|
|
|
26
26
|
removeStatefulParametersBeforeDestroy?: boolean | undefined;
|
|
27
27
|
dependencies?: string[] | undefined;
|
|
28
28
|
transformation?: InputTransformation;
|
|
29
|
+
operatingSystems: Array<OS>;
|
|
30
|
+
isSensitive?: boolean;
|
|
29
31
|
private settings;
|
|
30
32
|
constructor(settings: ResourceSettings<T>);
|
|
31
33
|
get typeId(): string;
|
|
@@ -8,15 +8,13 @@ export class ParsedResourceSettings {
|
|
|
8
8
|
removeStatefulParametersBeforeDestroy;
|
|
9
9
|
dependencies;
|
|
10
10
|
transformation;
|
|
11
|
+
operatingSystems;
|
|
12
|
+
isSensitive;
|
|
11
13
|
settings;
|
|
12
14
|
constructor(settings) {
|
|
13
15
|
this.settings = settings;
|
|
14
|
-
|
|
15
|
-
this
|
|
16
|
-
this.allowMultiple = settings.allowMultiple;
|
|
17
|
-
this.removeStatefulParametersBeforeDestroy = settings.removeStatefulParametersBeforeDestroy;
|
|
18
|
-
this.dependencies = settings.dependencies;
|
|
19
|
-
this.transformation = settings.transformation;
|
|
16
|
+
const { parameterSettings, ...rest } = settings;
|
|
17
|
+
Object.assign(this, rest);
|
|
20
18
|
this.validateSettings();
|
|
21
19
|
}
|
|
22
20
|
get typeId() {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JSONSchemaType } from 'ajv';
|
|
2
|
-
import { StringIndexedObject } from 'codify-schemas';
|
|
2
|
+
import { OS, StringIndexedObject } from 'codify-schemas';
|
|
3
3
|
import { ArrayStatefulParameter, StatefulParameter } from '../stateful-parameter/stateful-parameter.js';
|
|
4
4
|
import { RefreshContext } from './resource.js';
|
|
5
5
|
export interface InputTransformation {
|
|
@@ -14,6 +14,10 @@ export interface ResourceSettings<T extends StringIndexedObject> {
|
|
|
14
14
|
* The typeId of the resource.
|
|
15
15
|
*/
|
|
16
16
|
id: string;
|
|
17
|
+
/**
|
|
18
|
+
* List of supported operating systems
|
|
19
|
+
*/
|
|
20
|
+
operatingSystems: Array<OS>;
|
|
17
21
|
/**
|
|
18
22
|
* Schema to validate user configs with. Must be in the format JSON Schema draft07
|
|
19
23
|
*/
|
|
@@ -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/
|
|
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,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
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { OS } from 'codify-schemas';
|
|
2
|
+
export declare function isDebug(): boolean;
|
|
3
|
+
export declare enum Shell {
|
|
4
|
+
ZSH = "zsh",
|
|
5
|
+
BASH = "bash",
|
|
6
|
+
SH = "sh",
|
|
7
|
+
KSH = "ksh",
|
|
8
|
+
CSH = "csh",
|
|
9
|
+
FISH = "fish"
|
|
10
|
+
}
|
|
11
|
+
export interface SystemInfo {
|
|
12
|
+
os: OS;
|
|
13
|
+
shell: Shell;
|
|
14
|
+
}
|
|
15
|
+
export declare const Utils: {
|
|
16
|
+
getUser(): string;
|
|
17
|
+
getSystemInfo(): {
|
|
18
|
+
os: string;
|
|
19
|
+
shell: Shell | undefined;
|
|
20
|
+
};
|
|
21
|
+
isMacOS(): boolean;
|
|
22
|
+
isLinux(): boolean;
|
|
23
|
+
getShell(): Shell | undefined;
|
|
24
|
+
getPrimaryShellRc(): string;
|
|
25
|
+
getShellRcFiles(): string[];
|
|
26
|
+
};
|