@codifycli/plugin-core 1.0.0-beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.eslintrc.json +30 -0
- package/.github/workflows/release.yaml +19 -0
- package/.github/workflows/unit-test-ci.yaml +18 -0
- package/.prettierrc.json +1 -0
- package/bin/build.js +189 -0
- package/dist/bin/build.d.ts +1 -0
- package/dist/bin/build.js +80 -0
- package/dist/bin/deploy-plugin.d.ts +2 -0
- package/dist/bin/deploy-plugin.js +8 -0
- package/dist/common/errors.d.ts +8 -0
- package/dist/common/errors.js +24 -0
- package/dist/entities/change-set.d.ts +24 -0
- package/dist/entities/change-set.js +152 -0
- package/dist/entities/errors.d.ts +4 -0
- package/dist/entities/errors.js +7 -0
- package/dist/entities/plan-types.d.ts +25 -0
- package/dist/entities/plan-types.js +1 -0
- package/dist/entities/plan.d.ts +15 -0
- package/dist/entities/plan.js +127 -0
- package/dist/entities/plugin.d.ts +16 -0
- package/dist/entities/plugin.js +80 -0
- package/dist/entities/resource-options.d.ts +31 -0
- package/dist/entities/resource-options.js +76 -0
- package/dist/entities/resource-types.d.ts +11 -0
- package/dist/entities/resource-types.js +1 -0
- package/dist/entities/resource.d.ts +42 -0
- package/dist/entities/resource.js +303 -0
- package/dist/entities/stateful-parameter.d.ts +29 -0
- package/dist/entities/stateful-parameter.js +46 -0
- package/dist/entities/transform-parameter.d.ts +4 -0
- package/dist/entities/transform-parameter.js +2 -0
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js +7 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +26 -0
- package/dist/messages/handlers.d.ts +14 -0
- package/dist/messages/handlers.js +134 -0
- package/dist/messages/sender.d.ts +11 -0
- package/dist/messages/sender.js +57 -0
- package/dist/plan/change-set.d.ts +53 -0
- package/dist/plan/change-set.js +153 -0
- package/dist/plan/plan-types.d.ts +23 -0
- package/dist/plan/plan-types.js +1 -0
- package/dist/plan/plan.d.ts +66 -0
- package/dist/plan/plan.js +328 -0
- package/dist/plugin/plugin.d.ts +24 -0
- package/dist/plugin/plugin.js +200 -0
- package/dist/pty/background-pty.d.ts +21 -0
- package/dist/pty/background-pty.js +127 -0
- package/dist/pty/index.d.ts +50 -0
- package/dist/pty/index.js +20 -0
- package/dist/pty/promise-queue.d.ts +5 -0
- package/dist/pty/promise-queue.js +26 -0
- package/dist/pty/seqeuntial-pty.d.ts +17 -0
- package/dist/pty/seqeuntial-pty.js +119 -0
- package/dist/pty/vitest.config.d.ts +2 -0
- package/dist/pty/vitest.config.js +11 -0
- package/dist/resource/config-parser.d.ts +11 -0
- package/dist/resource/config-parser.js +21 -0
- package/dist/resource/parsed-resource-settings.d.ts +47 -0
- package/dist/resource/parsed-resource-settings.js +196 -0
- package/dist/resource/resource-controller.d.ts +36 -0
- package/dist/resource/resource-controller.js +402 -0
- package/dist/resource/resource-settings.d.ts +303 -0
- package/dist/resource/resource-settings.js +147 -0
- package/dist/resource/resource.d.ts +144 -0
- package/dist/resource/resource.js +44 -0
- package/dist/resource/stateful-parameter.d.ts +165 -0
- package/dist/resource/stateful-parameter.js +94 -0
- package/dist/scripts/deploy.d.ts +1 -0
- package/dist/scripts/deploy.js +2 -0
- package/dist/stateful-parameter/stateful-parameter-controller.d.ts +21 -0
- package/dist/stateful-parameter/stateful-parameter-controller.js +81 -0
- package/dist/stateful-parameter/stateful-parameter.d.ts +144 -0
- package/dist/stateful-parameter/stateful-parameter.js +43 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +5 -0
- package/dist/utils/codify-spawn.d.ts +29 -0
- package/dist/utils/codify-spawn.js +136 -0
- package/dist/utils/debug.d.ts +2 -0
- package/dist/utils/debug.js +10 -0
- package/dist/utils/file-utils.d.ts +23 -0
- package/dist/utils/file-utils.js +186 -0
- package/dist/utils/functions.d.ts +12 -0
- package/dist/utils/functions.js +74 -0
- package/dist/utils/index.d.ts +46 -0
- package/dist/utils/index.js +271 -0
- package/dist/utils/internal-utils.d.ts +12 -0
- package/dist/utils/internal-utils.js +74 -0
- package/dist/utils/load-resources.d.ts +1 -0
- package/dist/utils/load-resources.js +46 -0
- package/dist/utils/package-json-utils.d.ts +12 -0
- package/dist/utils/package-json-utils.js +34 -0
- package/dist/utils/pty-local-storage.d.ts +2 -0
- package/dist/utils/pty-local-storage.js +2 -0
- package/dist/utils/spawn-2.d.ts +5 -0
- package/dist/utils/spawn-2.js +7 -0
- package/dist/utils/spawn.d.ts +29 -0
- package/dist/utils/spawn.js +124 -0
- package/dist/utils/utils.d.ts +18 -0
- package/dist/utils/utils.js +86 -0
- package/dist/utils/verbosity-level.d.ts +5 -0
- package/dist/utils/verbosity-level.js +9 -0
- package/package.json +59 -0
- package/rollup.config.js +24 -0
- package/src/common/errors.test.ts +43 -0
- package/src/common/errors.ts +31 -0
- package/src/errors.ts +8 -0
- package/src/index.test.ts +6 -0
- package/src/index.ts +30 -0
- package/src/messages/handlers.test.ts +329 -0
- package/src/messages/handlers.ts +181 -0
- package/src/messages/sender.ts +69 -0
- package/src/plan/change-set.test.ts +280 -0
- package/src/plan/change-set.ts +236 -0
- package/src/plan/plan-types.ts +27 -0
- package/src/plan/plan.test.ts +413 -0
- package/src/plan/plan.ts +499 -0
- package/src/plugin/plugin.test.ts +533 -0
- package/src/plugin/plugin.ts +291 -0
- package/src/pty/background-pty.test.ts +69 -0
- package/src/pty/background-pty.ts +154 -0
- package/src/pty/index.test.ts +129 -0
- package/src/pty/index.ts +66 -0
- package/src/pty/promise-queue.ts +33 -0
- package/src/pty/seqeuntial-pty.ts +151 -0
- package/src/pty/sequential-pty.test.ts +194 -0
- package/src/resource/config-parser.ts +42 -0
- package/src/resource/parsed-resource-settings.test.ts +186 -0
- package/src/resource/parsed-resource-settings.ts +307 -0
- package/src/resource/resource-controller-stateful-mode.test.ts +253 -0
- package/src/resource/resource-controller.test.ts +1081 -0
- package/src/resource/resource-controller.ts +563 -0
- package/src/resource/resource-settings.test.ts +1213 -0
- package/src/resource/resource-settings.ts +545 -0
- package/src/resource/resource.ts +157 -0
- package/src/stateful-parameter/stateful-parameter-controller.test.ts +244 -0
- package/src/stateful-parameter/stateful-parameter-controller.ts +111 -0
- package/src/stateful-parameter/stateful-parameter.ts +160 -0
- package/src/utils/debug.ts +11 -0
- package/src/utils/file-utils.test.ts +7 -0
- package/src/utils/file-utils.ts +231 -0
- package/src/utils/functions.ts +103 -0
- package/src/utils/index.ts +340 -0
- package/src/utils/internal-utils.test.ts +52 -0
- package/src/utils/pty-local-storage.ts +3 -0
- package/src/utils/test-utils.test.ts +96 -0
- package/src/utils/verbosity-level.ts +11 -0
- package/tsconfig.json +26 -0
- package/tsconfig.test.json +9 -0
- package/vitest.config.ts +10 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { Ajv } from 'ajv';
|
|
2
|
+
import { MessageCmd, SudoRequestResponseDataSchema } from 'codify-schemas';
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
|
+
import stripAnsi from 'strip-ansi';
|
|
6
|
+
import { VerbosityLevel } from './utils.js';
|
|
7
|
+
import { SudoError } from '../errors.js';
|
|
8
|
+
const ajv = new Ajv({
|
|
9
|
+
strict: true,
|
|
10
|
+
});
|
|
11
|
+
const validateSudoRequestResponse = ajv.compile(SudoRequestResponseDataSchema);
|
|
12
|
+
export var SpawnStatus;
|
|
13
|
+
(function (SpawnStatus) {
|
|
14
|
+
SpawnStatus["SUCCESS"] = "success";
|
|
15
|
+
SpawnStatus["ERROR"] = "error";
|
|
16
|
+
})(SpawnStatus || (SpawnStatus = {}));
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param cmd Command to run. Ex: `rm -rf`
|
|
20
|
+
* @param opts Standard options for node spawn. Additional argument:
|
|
21
|
+
* throws determines if a shell will throw a JS error. Defaults to true
|
|
22
|
+
*
|
|
23
|
+
* @see promiseSpawn
|
|
24
|
+
* @see spawn
|
|
25
|
+
*
|
|
26
|
+
* @returns SpawnResult { status: SUCCESS | ERROR; data: string }
|
|
27
|
+
*/
|
|
28
|
+
export async function codifySpawn(cmd, opts) {
|
|
29
|
+
const throws = opts?.throws ?? true;
|
|
30
|
+
console.log(`Running command: ${cmd}` + (opts?.cwd ? `(${opts?.cwd})` : ''));
|
|
31
|
+
try {
|
|
32
|
+
// TODO: Need to benchmark the effects of using sh vs zsh for shell.
|
|
33
|
+
// Seems like zsh shells run slower
|
|
34
|
+
const result = await (opts?.requiresRoot
|
|
35
|
+
? externalSpawnWithSudo(cmd, opts)
|
|
36
|
+
: internalSpawn(cmd, opts ?? {}));
|
|
37
|
+
if (result.status !== SpawnStatus.SUCCESS) {
|
|
38
|
+
throw new Error(result.data);
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (isDebug()) {
|
|
44
|
+
console.error(`CodifySpawn error for command ${cmd}`, error);
|
|
45
|
+
}
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
if (error.message?.startsWith('sudo:')) {
|
|
48
|
+
throw new SudoError(cmd);
|
|
49
|
+
}
|
|
50
|
+
if (throws) {
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
if (error instanceof Error) {
|
|
54
|
+
return {
|
|
55
|
+
status: SpawnStatus.ERROR,
|
|
56
|
+
data: error.message,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
status: SpawnStatus.ERROR,
|
|
61
|
+
data: String(error),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function internalSpawn(cmd, opts) {
|
|
66
|
+
return new Promise((resolve) => {
|
|
67
|
+
const output = [];
|
|
68
|
+
// If TERM_PROGRAM=Apple_Terminal is set then ANSI escape characters may be included
|
|
69
|
+
// in the response.
|
|
70
|
+
const env = { ...process.env, ...opts.env, TERM_PROGRAM: 'codify', COMMAND_MODE: 'unix2003', COLORTERM: 'truecolor' };
|
|
71
|
+
const shell = getDefaultShell();
|
|
72
|
+
const rcFile = shell === 'zsh' ? '~/.zshrc' : '~/.bashrc';
|
|
73
|
+
// Source start up shells to emulate a users environment vs. a non-interactive non-login shell script
|
|
74
|
+
// Ignore all stdin
|
|
75
|
+
// If tty is requested then we'll need to sleep 1 to avoid race conditions. This is because if the terminal updates async after the tty message is
|
|
76
|
+
// displayed then it'll disappear. By adding sleep 1 it'll allow ink.js to finish all the updates before the tty message is shown
|
|
77
|
+
const _process = spawn(`source ${rcFile}; ${opts.requestsTTY ? 'sleep 1;' : ''}${cmd}`, [], {
|
|
78
|
+
...opts,
|
|
79
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
80
|
+
shell,
|
|
81
|
+
env
|
|
82
|
+
});
|
|
83
|
+
const { stdout, stderr, stdin } = _process;
|
|
84
|
+
stdout.setEncoding('utf8');
|
|
85
|
+
stderr.setEncoding('utf8');
|
|
86
|
+
stdout.on('data', (data) => {
|
|
87
|
+
output.push(data.toString());
|
|
88
|
+
});
|
|
89
|
+
stderr.on('data', (data) => {
|
|
90
|
+
output.push(data.toString());
|
|
91
|
+
});
|
|
92
|
+
_process.on('error', (data) => { });
|
|
93
|
+
// please node that this is not a full replacement for 'inherit'
|
|
94
|
+
// the child process can and will detect if stdout is a pty and change output based on it
|
|
95
|
+
// the terminal context is lost & ansi information (coloring) etc will be lost
|
|
96
|
+
if (stdout && stderr && VerbosityLevel.get() > 0) {
|
|
97
|
+
stdout.pipe(process.stdout);
|
|
98
|
+
stderr.pipe(process.stderr);
|
|
99
|
+
}
|
|
100
|
+
_process.on('close', (code) => {
|
|
101
|
+
resolve({
|
|
102
|
+
status: code === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR,
|
|
103
|
+
data: stripAnsi(output.join('\n')),
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
async function externalSpawnWithSudo(cmd, opts) {
|
|
109
|
+
return new Promise((resolve) => {
|
|
110
|
+
const requestId = nanoid(8);
|
|
111
|
+
const listener = (data) => {
|
|
112
|
+
if (data.requestId === requestId) {
|
|
113
|
+
process.removeListener('message', listener);
|
|
114
|
+
if (!validateSudoRequestResponse(data.data)) {
|
|
115
|
+
throw new Error(`Invalid response for sudo request: ${JSON.stringify(validateSudoRequestResponse.errors, null, 2)}`);
|
|
116
|
+
}
|
|
117
|
+
resolve(data.data);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
process.on('message', listener);
|
|
121
|
+
process.send({
|
|
122
|
+
cmd: MessageCmd.SUDO_REQUEST,
|
|
123
|
+
data: {
|
|
124
|
+
command: cmd,
|
|
125
|
+
options: opts ?? {},
|
|
126
|
+
},
|
|
127
|
+
requestId
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
export function isDebug() {
|
|
132
|
+
return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
|
|
133
|
+
}
|
|
134
|
+
function getDefaultShell() {
|
|
135
|
+
return process.platform === 'darwin' ? 'zsh' : 'bash';
|
|
136
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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 removeFromFile(filePath: string, search: string): Promise<void>;
|
|
13
|
+
static removeLineFromFile(filePath: string, search: RegExp | string): Promise<void>;
|
|
14
|
+
static removeLineFromShellRc(search: RegExp | string): Promise<void>;
|
|
15
|
+
static removeAllLinesFromShellRc(searches: Array<RegExp | string>): Promise<void>;
|
|
16
|
+
static appendToFileWithSpacing(file: string, textToInsert: string): string;
|
|
17
|
+
static dirExists(path: string): Promise<boolean>;
|
|
18
|
+
static fileExists(path: string): Promise<boolean>;
|
|
19
|
+
static exists(path: string): Promise<boolean>;
|
|
20
|
+
static checkDirExistsOrThrowIfFile(path: string): Promise<boolean>;
|
|
21
|
+
static createDirIfNotExists(path: string): Promise<void>;
|
|
22
|
+
private static calculateEndingNewLines;
|
|
23
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
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 removeFromFile(filePath, search) {
|
|
61
|
+
const contents = await fs.readFile(filePath, 'utf8');
|
|
62
|
+
const newContents = contents.replaceAll(search, '');
|
|
63
|
+
await fs.writeFile(filePath, newContents, 'utf8');
|
|
64
|
+
}
|
|
65
|
+
static async removeLineFromFile(filePath, search) {
|
|
66
|
+
const file = await fs.readFile(filePath, 'utf8');
|
|
67
|
+
const lines = file.split('\n');
|
|
68
|
+
let searchRegex;
|
|
69
|
+
let searchString;
|
|
70
|
+
if (typeof search === 'object') {
|
|
71
|
+
const startRegex = /^([\t ]*)?/;
|
|
72
|
+
const endRegex = /([\t ]*)?/;
|
|
73
|
+
// Augment regex with spaces criteria to make sure this function is not deleting lines that are comments or has other content.
|
|
74
|
+
searchRegex = search
|
|
75
|
+
? new RegExp(startRegex.source + search.source + endRegex.source, search.flags)
|
|
76
|
+
: search;
|
|
77
|
+
}
|
|
78
|
+
if (typeof search === 'string') {
|
|
79
|
+
searchString = search;
|
|
80
|
+
}
|
|
81
|
+
for (let counter = lines.length; counter >= 0; counter--) {
|
|
82
|
+
if (!lines[counter]) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (searchString && lines[counter].includes(searchString)) {
|
|
86
|
+
lines.splice(counter, 1);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (searchRegex && lines[counter].search(searchRegex) !== -1) {
|
|
90
|
+
lines.splice(counter, 1);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
await fs.writeFile(filePath, lines.join('\n'));
|
|
94
|
+
console.log(`Removed line: ${search} from ${filePath}`);
|
|
95
|
+
}
|
|
96
|
+
static async removeLineFromShellRc(search) {
|
|
97
|
+
return FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
|
|
98
|
+
}
|
|
99
|
+
static async removeAllLinesFromShellRc(searches) {
|
|
100
|
+
for (const search of searches) {
|
|
101
|
+
await FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Append the string to the end of a file ensuring at least 1 lines of space between.
|
|
105
|
+
// Ex result:
|
|
106
|
+
// something something;
|
|
107
|
+
//
|
|
108
|
+
// newline;
|
|
109
|
+
static appendToFileWithSpacing(file, textToInsert) {
|
|
110
|
+
const lines = file.trimEnd().split(/\n/);
|
|
111
|
+
if (lines.length === 0) {
|
|
112
|
+
return textToInsert;
|
|
113
|
+
}
|
|
114
|
+
const endingNewLines = FileUtils.calculateEndingNewLines(lines);
|
|
115
|
+
const numNewLines = endingNewLines === -1
|
|
116
|
+
? 0
|
|
117
|
+
: Math.max(0, 2 - endingNewLines);
|
|
118
|
+
return lines.join('\n') + '\n'.repeat(numNewLines) + textToInsert;
|
|
119
|
+
}
|
|
120
|
+
static async dirExists(path) {
|
|
121
|
+
let stat;
|
|
122
|
+
try {
|
|
123
|
+
stat = await fs.stat(path);
|
|
124
|
+
return stat.isDirectory();
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
static async fileExists(path) {
|
|
131
|
+
let stat;
|
|
132
|
+
try {
|
|
133
|
+
stat = await fs.stat(path);
|
|
134
|
+
return stat.isFile();
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
static async exists(path) {
|
|
141
|
+
try {
|
|
142
|
+
await fs.stat(path);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
static async checkDirExistsOrThrowIfFile(path) {
|
|
150
|
+
let stat;
|
|
151
|
+
try {
|
|
152
|
+
stat = await fs.stat(path);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (stat.isDirectory()) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
throw new Error(`Directory ${path} already exists and is a file`);
|
|
161
|
+
}
|
|
162
|
+
static async createDirIfNotExists(path) {
|
|
163
|
+
if (!fsSync.existsSync(path)) {
|
|
164
|
+
await fs.mkdir(path, { recursive: true });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// This is overly complicated but it can be used to insert into any
|
|
168
|
+
// position in the future
|
|
169
|
+
static calculateEndingNewLines(lines) {
|
|
170
|
+
let counter = 0;
|
|
171
|
+
while (true) {
|
|
172
|
+
const line = lines.at(-counter - 1);
|
|
173
|
+
if (!line) {
|
|
174
|
+
return -1;
|
|
175
|
+
}
|
|
176
|
+
if (!SPACE_REGEX.test(line)) {
|
|
177
|
+
return counter;
|
|
178
|
+
}
|
|
179
|
+
counter++;
|
|
180
|
+
// Short circuit here because we don't need to check over 2;
|
|
181
|
+
if (counter > 2) {
|
|
182
|
+
return counter;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -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
|
+
...(config.os ? { os: config.os } : {}),
|
|
9
|
+
};
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
11
|
+
const { type, name, dependsOn, os, ...parameters } = config;
|
|
12
|
+
return {
|
|
13
|
+
parameters: parameters,
|
|
14
|
+
coreParameters,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export function setsEqual(set1, set2) {
|
|
18
|
+
return set1.size === set2.size && [...set1].every((v) => set2.has(v));
|
|
19
|
+
}
|
|
20
|
+
const homeDirectory = os.homedir();
|
|
21
|
+
export function untildify(pathWithTilde) {
|
|
22
|
+
return homeDirectory ? pathWithTilde.replace(/^~(?=$|\/|\\)/, homeDirectory) : pathWithTilde;
|
|
23
|
+
}
|
|
24
|
+
export function tildify(pathWithTilde) {
|
|
25
|
+
return homeDirectory ? pathWithTilde.replace(homeDirectory, '~') : pathWithTilde;
|
|
26
|
+
}
|
|
27
|
+
export function resolvePathWithVariables(pathWithVariables) {
|
|
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,46 @@
|
|
|
1
|
+
import { LinuxDistro, 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
|
+
isArmArch(): Promise<boolean>;
|
|
24
|
+
isHomebrewInstalled(): Promise<boolean>;
|
|
25
|
+
isRosetta2Installed(): Promise<boolean>;
|
|
26
|
+
getShell(): Shell | undefined;
|
|
27
|
+
getPrimaryShellRc(): string;
|
|
28
|
+
getShellRcFiles(): string[];
|
|
29
|
+
isDirectoryOnPath(directory: string): Promise<boolean>;
|
|
30
|
+
assertBrewInstalled(): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Installs a package via the system package manager. This will use Homebrew on macOS and apt on Ubuntu/Debian or dnf on Fedora.
|
|
33
|
+
* @param packageName
|
|
34
|
+
*/
|
|
35
|
+
installViaPkgMgr(packageName: string): Promise<void>;
|
|
36
|
+
uninstallViaPkgMgr(packageName: string): Promise<boolean>;
|
|
37
|
+
getLinuxDistro(): Promise<LinuxDistro | undefined>;
|
|
38
|
+
isUbuntu(): Promise<boolean>;
|
|
39
|
+
isDebian(): Promise<boolean>;
|
|
40
|
+
isArch(): Promise<boolean>;
|
|
41
|
+
isCentOS(): Promise<boolean>;
|
|
42
|
+
isFedora(): Promise<boolean>;
|
|
43
|
+
isRHEL(): Promise<boolean>;
|
|
44
|
+
isDebianBased(): boolean;
|
|
45
|
+
isRedhatBased(): boolean;
|
|
46
|
+
};
|