codify-plugin-lib 1.0.182-beta2 → 1.0.182-beta20
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 +3 -0
- package/dist/index.js +3 -0
- package/dist/plugin/plugin.js +1 -1
- package/dist/pty/background-pty.d.ts +1 -0
- package/dist/pty/background-pty.js +5 -14
- package/dist/pty/index.d.ts +2 -0
- package/dist/pty/seqeuntial-pty.d.ts +1 -0
- package/dist/pty/seqeuntial-pty.js +42 -9
- 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 +22 -0
- package/dist/utils/file-utils.js +181 -0
- package/dist/utils/functions.d.ts +12 -0
- package/dist/utils/functions.js +74 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +10 -0
- package/dist/utils/internal-utils.d.ts +0 -6
- package/dist/utils/internal-utils.js +0 -12
- 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 +3 -0
- package/src/plugin/plugin.ts +1 -1
- package/src/pty/background-pty.ts +5 -16
- package/src/pty/index.ts +4 -2
- package/src/pty/seqeuntial-pty.ts +54 -11
- package/src/pty/sequential-pty.test.ts +124 -6
- package/src/resource/resource-controller.test.ts +1 -1
- package/src/resource/resource-settings.ts +1 -1
- package/src/utils/file-utils.test.ts +7 -0
- package/src/utils/file-utils.ts +225 -0
- package/src/utils/{internal-utils.ts → functions.ts} +0 -16
- package/src/utils/index.ts +12 -0
- package/src/utils/internal-utils.test.ts +1 -1
- 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/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));
|
package/dist/plugin/plugin.js
CHANGED
|
@@ -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,7 @@ 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/
|
|
9
|
+
import { VerbosityLevel } from '../utils/verbosity-level.js';
|
|
10
10
|
import { SpawnError } from './index.js';
|
|
11
11
|
import { PromiseQueue } from './promise-queue.js';
|
|
12
12
|
EventEmitter.defaultMaxListeners = 1000;
|
|
@@ -17,8 +17,10 @@ EventEmitter.defaultMaxListeners = 1000;
|
|
|
17
17
|
* without a tty (or even a stdin) attached so interactive commands will not work.
|
|
18
18
|
*/
|
|
19
19
|
export class BackgroundPty {
|
|
20
|
+
historyIgnore = Utils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' };
|
|
20
21
|
basePty = pty.spawn(this.getDefaultShell(), ['-i'], {
|
|
21
|
-
env: process.env,
|
|
22
|
+
env: { ...process.env, ...this.historyIgnore },
|
|
23
|
+
name: nanoid(6),
|
|
22
24
|
handleFlowControl: true
|
|
23
25
|
});
|
|
24
26
|
promiseQueue = new PromiseQueue();
|
|
@@ -84,7 +86,7 @@ export class BackgroundPty {
|
|
|
84
86
|
resolve(null);
|
|
85
87
|
}
|
|
86
88
|
});
|
|
87
|
-
console.log(`Running command ${cmd}${options?.cwd ? ` (cwd: ${options.cwd})` : ''}`);
|
|
89
|
+
console.log(`Running command: ${cmd}${options?.cwd ? ` (cwd: ${options.cwd})` : ''}`);
|
|
88
90
|
this.basePty.write(`${command}\r`);
|
|
89
91
|
}));
|
|
90
92
|
}).finally(async () => {
|
|
@@ -104,17 +106,6 @@ export class BackgroundPty {
|
|
|
104
106
|
await this.promiseQueue.run(async () => {
|
|
105
107
|
let outputBuffer = '';
|
|
106
108
|
return new Promise(resolve => {
|
|
107
|
-
// 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
|
-
}
|
|
118
109
|
this.basePty.write(' unset PS1;\n');
|
|
119
110
|
this.basePty.write(' unset PS0;\n');
|
|
120
111
|
this.basePty.write(' echo setup complete\\"\n');
|
package/dist/pty/index.d.ts
CHANGED
|
@@ -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/
|
|
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',
|
|
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 =
|
|
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/
|
|
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,22 @@
|
|
|
1
|
+
export declare class FileUtils {
|
|
2
|
+
static downloadFile(url: string, destination: string): Promise<void>;
|
|
3
|
+
static addToShellRc(line: string): Promise<void>;
|
|
4
|
+
static addAllToShellRc(lines: string[]): Promise<void>;
|
|
5
|
+
/**
|
|
6
|
+
* This method adds a directory path to the shell rc file if it doesn't already exist.
|
|
7
|
+
*
|
|
8
|
+
* @param value - The directory path to add.
|
|
9
|
+
* @param prepend - Whether to prepend the path to the existing PATH variable.
|
|
10
|
+
*/
|
|
11
|
+
static addPathToShellRc(value: string, prepend: boolean): Promise<void>;
|
|
12
|
+
static dirExists(path: string): Promise<boolean>;
|
|
13
|
+
static fileExists(path: string): Promise<boolean>;
|
|
14
|
+
static exists(path: string): Promise<boolean>;
|
|
15
|
+
static checkDirExistsOrThrowIfFile(path: string): Promise<boolean>;
|
|
16
|
+
static createDirIfNotExists(path: string): Promise<void>;
|
|
17
|
+
static removeFromFile(filePath: string, search: string): Promise<void>;
|
|
18
|
+
static removeLineFromFile(filePath: string, search: RegExp | string): Promise<void>;
|
|
19
|
+
static removeLineFromPrimaryShellRc(search: RegExp | string): Promise<void>;
|
|
20
|
+
static appendToFileWithSpacing(file: string, textToInsert: string): string;
|
|
21
|
+
private static calculateEndingNewLines;
|
|
22
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
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 addToShellRc(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 addAllToShellRc(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
|
+
/**
|
|
43
|
+
* This method adds a directory path to the shell rc file if it doesn't already exist.
|
|
44
|
+
*
|
|
45
|
+
* @param value - The directory path to add.
|
|
46
|
+
* @param prepend - Whether to prepend the path to the existing PATH variable.
|
|
47
|
+
*/
|
|
48
|
+
static async addPathToShellRc(value, prepend) {
|
|
49
|
+
if (await Utils.isDirectoryOnPath(value)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const shellRc = Utils.getPrimaryShellRc();
|
|
53
|
+
console.log(`Saving path: ${value} to ${shellRc}`);
|
|
54
|
+
if (prepend) {
|
|
55
|
+
await fs.appendFile(shellRc, `\nexport PATH=$PATH:${value};`, { encoding: 'utf8' });
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
await fs.appendFile(shellRc, `\nexport PATH=${value}:$PATH;`, { encoding: 'utf8' });
|
|
59
|
+
}
|
|
60
|
+
static async dirExists(path) {
|
|
61
|
+
let stat;
|
|
62
|
+
try {
|
|
63
|
+
stat = await fs.stat(path);
|
|
64
|
+
return stat.isDirectory();
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
static async fileExists(path) {
|
|
71
|
+
let stat;
|
|
72
|
+
try {
|
|
73
|
+
stat = await fs.stat(path);
|
|
74
|
+
return stat.isFile();
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
static async exists(path) {
|
|
81
|
+
try {
|
|
82
|
+
await fs.stat(path);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
static async checkDirExistsOrThrowIfFile(path) {
|
|
90
|
+
let stat;
|
|
91
|
+
try {
|
|
92
|
+
stat = await fs.stat(path);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
if (stat.isDirectory()) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
throw new Error(`Directory ${path} already exists and is a file`);
|
|
101
|
+
}
|
|
102
|
+
static async createDirIfNotExists(path) {
|
|
103
|
+
if (!fsSync.existsSync(path)) {
|
|
104
|
+
await fs.mkdir(path, { recursive: true });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
static async removeFromFile(filePath, search) {
|
|
108
|
+
const contents = await fs.readFile(filePath, 'utf8');
|
|
109
|
+
const newContents = contents.replaceAll(search, '');
|
|
110
|
+
await fs.writeFile(filePath, newContents, 'utf8');
|
|
111
|
+
}
|
|
112
|
+
static async removeLineFromFile(filePath, search) {
|
|
113
|
+
const file = await fs.readFile(filePath, 'utf8');
|
|
114
|
+
const lines = file.split('\n');
|
|
115
|
+
let searchRegex;
|
|
116
|
+
let searchString;
|
|
117
|
+
if (typeof search === 'object') {
|
|
118
|
+
const startRegex = /^([\t ]*)?/;
|
|
119
|
+
const endRegex = /([\t ]*)?/;
|
|
120
|
+
// Augment regex with spaces criteria to make sure this function is not deleting lines that are comments or has other content.
|
|
121
|
+
searchRegex = search
|
|
122
|
+
? new RegExp(startRegex.source + search.source + endRegex.source, search.flags)
|
|
123
|
+
: search;
|
|
124
|
+
}
|
|
125
|
+
if (typeof search === 'string') {
|
|
126
|
+
searchString = search;
|
|
127
|
+
}
|
|
128
|
+
for (let counter = lines.length; counter >= 0; counter--) {
|
|
129
|
+
if (!lines[counter]) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (searchString && lines[counter].includes(searchString)) {
|
|
133
|
+
lines.splice(counter, 1);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (searchRegex && lines[counter].search(searchRegex) !== -1) {
|
|
137
|
+
lines.splice(counter, 1);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
await fs.writeFile(filePath, lines.join('\n'));
|
|
141
|
+
console.log(`Removed line: ${search} from ${filePath}`);
|
|
142
|
+
}
|
|
143
|
+
static async removeLineFromPrimaryShellRc(search) {
|
|
144
|
+
return FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
|
|
145
|
+
}
|
|
146
|
+
// Append the string to the end of a file ensuring at least 1 lines of space between.
|
|
147
|
+
// Ex result:
|
|
148
|
+
// something something;
|
|
149
|
+
//
|
|
150
|
+
// newline;
|
|
151
|
+
static appendToFileWithSpacing(file, textToInsert) {
|
|
152
|
+
const lines = file.trimEnd().split(/\n/);
|
|
153
|
+
if (lines.length === 0) {
|
|
154
|
+
return textToInsert;
|
|
155
|
+
}
|
|
156
|
+
const endingNewLines = FileUtils.calculateEndingNewLines(lines);
|
|
157
|
+
const numNewLines = endingNewLines === -1
|
|
158
|
+
? 0
|
|
159
|
+
: Math.max(0, 2 - endingNewLines);
|
|
160
|
+
return lines.join('\n') + '\n'.repeat(numNewLines) + textToInsert;
|
|
161
|
+
}
|
|
162
|
+
// This is overly complicated but it can be used to insert into any
|
|
163
|
+
// position in the future
|
|
164
|
+
static calculateEndingNewLines(lines) {
|
|
165
|
+
let counter = 0;
|
|
166
|
+
while (true) {
|
|
167
|
+
const line = lines.at(-counter - 1);
|
|
168
|
+
if (!line) {
|
|
169
|
+
return -1;
|
|
170
|
+
}
|
|
171
|
+
if (!SPACE_REGEX.test(line)) {
|
|
172
|
+
return counter;
|
|
173
|
+
}
|
|
174
|
+
counter++;
|
|
175
|
+
// Short circuit here because we don't need to check over 2;
|
|
176
|
+
if (counter > 2) {
|
|
177
|
+
return counter;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -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
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -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",
|
|
@@ -22,4 +23,5 @@ export declare const Utils: {
|
|
|
22
23
|
getShell(): Shell | undefined;
|
|
23
24
|
getPrimaryShellRc(): string;
|
|
24
25
|
getShellRcFiles(): string[];
|
|
26
|
+
isDirectoryOnPath(directory: string): Promise<boolean>;
|
|
25
27
|
};
|
package/dist/utils/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import os from 'node:os';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import { getPty } from '../pty/index.js';
|
|
4
|
+
export function isDebug() {
|
|
5
|
+
return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
|
|
6
|
+
}
|
|
3
7
|
export var Shell;
|
|
4
8
|
(function (Shell) {
|
|
5
9
|
Shell["ZSH"] = "zsh";
|
|
@@ -105,4 +109,10 @@ export const Utils = {
|
|
|
105
109
|
path.join(homeDir, '.profile'),
|
|
106
110
|
];
|
|
107
111
|
},
|
|
112
|
+
async isDirectoryOnPath(directory) {
|
|
113
|
+
const $ = getPty();
|
|
114
|
+
const { data: pathQuery } = await $.spawn('echo $PATH', { interactive: true });
|
|
115
|
+
const lines = pathQuery.split(':');
|
|
116
|
+
return lines.includes(directory);
|
|
117
|
+
},
|
|
108
118
|
};
|
|
@@ -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,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codify-plugin-lib",
|
|
3
|
-
"version": "1.0.182-
|
|
3
|
+
"version": "1.0.182-beta20",
|
|
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);
|
package/src/plugin/plugin.ts
CHANGED
|
@@ -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>>;
|