codify-plugin-lib 1.0.180 → 1.0.182-beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/bin/build.d.ts +1 -0
  2. package/dist/bin/build.js +80 -0
  3. package/dist/bin/deploy-plugin.d.ts +2 -0
  4. package/dist/bin/deploy-plugin.js +8 -0
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/plugin/plugin.js +5 -2
  8. package/dist/pty/background-pty.d.ts +1 -0
  9. package/dist/pty/background-pty.js +17 -3
  10. package/dist/pty/index.d.ts +17 -1
  11. package/dist/pty/seqeuntial-pty.d.ts +16 -0
  12. package/dist/pty/seqeuntial-pty.js +84 -0
  13. package/dist/resource/parsed-resource-settings.d.ts +3 -1
  14. package/dist/resource/parsed-resource-settings.js +4 -6
  15. package/dist/resource/resource-settings.d.ts +5 -1
  16. package/dist/resource/resource-settings.js +4 -3
  17. package/dist/scripts/deploy.d.ts +1 -0
  18. package/dist/scripts/deploy.js +2 -0
  19. package/dist/utils/codify-spawn.d.ts +29 -0
  20. package/dist/utils/codify-spawn.js +136 -0
  21. package/dist/utils/index.d.ts +25 -0
  22. package/dist/utils/index.js +108 -0
  23. package/dist/utils/internal-utils.d.ts +18 -0
  24. package/dist/utils/internal-utils.js +86 -0
  25. package/dist/utils/pty-local-storage.d.ts +0 -1
  26. package/package.json +17 -12
  27. package/src/index.ts +1 -1
  28. package/src/plan/plan.test.ts +6 -1
  29. package/src/plugin/plugin.test.ts +11 -2
  30. package/src/plugin/plugin.ts +5 -2
  31. package/src/pty/background-pty.ts +20 -3
  32. package/src/pty/index.test.ts +7 -4
  33. package/src/pty/index.ts +17 -1
  34. package/src/pty/seqeuntial-pty.ts +105 -0
  35. package/src/pty/sequential-pty.test.ts +61 -0
  36. package/src/resource/parsed-resource-settings.ts +8 -7
  37. package/src/resource/resource-controller-stateful-mode.test.ts +2 -1
  38. package/src/resource/resource-controller.test.ts +22 -4
  39. package/src/resource/resource-settings.test.ts +29 -2
  40. package/src/resource/resource-settings.ts +16 -4
  41. package/src/utils/index.ts +140 -0
  42. package/src/utils/{utils.test.ts → internal-utils.test.ts} +1 -1
  43. package/src/utils/test-utils.test.ts +5 -2
  44. /package/src/utils/{utils.ts → internal-utils.ts} +0 -0
@@ -0,0 +1 @@
1
+ export declare function build(): Promise<void>;
@@ -0,0 +1,80 @@
1
+ import { Ajv } from 'ajv';
2
+ import { IpcMessageSchema, MessageStatus, ResourceSchema } from 'codify-schemas';
3
+ // @ts-ignore
4
+ import mergeJsonSchemas from 'merge-json-schemas';
5
+ import { fork } from 'node:child_process';
6
+ import fs from 'node:fs/promises';
7
+ import path from 'node:path';
8
+ import * as url from 'node:url';
9
+ import { codifySpawn } from '../utils/codify-spawn.js';
10
+ const ajv = new Ajv({
11
+ strict: true
12
+ });
13
+ const ipcMessageValidator = ajv.compile(IpcMessageSchema);
14
+ function sendMessageAndAwaitResponse(process, message) {
15
+ return new Promise((resolve, reject) => {
16
+ process.on('message', (response) => {
17
+ if (!ipcMessageValidator(response)) {
18
+ throw new Error(`Invalid message from plugin. ${JSON.stringify(message, null, 2)}`);
19
+ }
20
+ // Wait for the message response. Other messages such as sudoRequest may be sent before the response returns
21
+ if (response.cmd === message.cmd + '_Response') {
22
+ if (response.status === MessageStatus.SUCCESS) {
23
+ resolve(response.data);
24
+ }
25
+ else {
26
+ reject(new Error(String(response.data)));
27
+ }
28
+ }
29
+ });
30
+ // Send message last to ensure listeners are all registered
31
+ process.send(message);
32
+ });
33
+ }
34
+ export async function build() {
35
+ await fs.rm('./dist', { force: true, recursive: true });
36
+ await codifySpawn('npm run rollup -- -f es');
37
+ const plugin = fork('./dist/index.js', [], {
38
+ // Use default true to test plugins in secure mode (un-able to request sudo directly)
39
+ detached: true,
40
+ env: { ...process.env },
41
+ execArgv: ['--import', 'tsx/esm'],
42
+ });
43
+ const initializeResult = await sendMessageAndAwaitResponse(plugin, {
44
+ cmd: 'initialize',
45
+ data: {}
46
+ });
47
+ const { resourceDefinitions } = initializeResult;
48
+ const resourceTypes = resourceDefinitions.map((i) => i.type);
49
+ const schemasMap = new Map();
50
+ for (const type of resourceTypes) {
51
+ const resourceInfo = await sendMessageAndAwaitResponse(plugin, {
52
+ cmd: 'getResourceInfo',
53
+ data: { type }
54
+ });
55
+ schemasMap.set(type, resourceInfo.schema);
56
+ }
57
+ const mergedSchemas = [...schemasMap.entries()].map(([type, schema]) => {
58
+ // const resolvedSchema = await $RefParser.dereference(schema)
59
+ const resourceSchema = JSON.parse(JSON.stringify(ResourceSchema));
60
+ delete resourceSchema.$id;
61
+ delete resourceSchema.$schema;
62
+ delete resourceSchema.title;
63
+ delete resourceSchema.oneOf;
64
+ delete resourceSchema.properties.type;
65
+ if (schema) {
66
+ delete schema.$id;
67
+ delete schema.$schema;
68
+ delete schema.title;
69
+ delete schema.oneOf;
70
+ }
71
+ return mergeJsonSchemas([schema ?? {}, resourceSchema, { properties: { type: { const: type, type: 'string' } } }]);
72
+ });
73
+ await fs.rm('./dist', { force: true, recursive: true });
74
+ await codifySpawn('npm run rollup'); // re-run rollup without building for es
75
+ console.log('Generated JSON Schemas for all resources');
76
+ const distFolder = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '..', 'dist');
77
+ const schemaOutputPath = path.resolve(distFolder, 'schemas.json');
78
+ await fs.writeFile(schemaOutputPath, JSON.stringify(mergedSchemas, null, 2));
79
+ console.log('Successfully wrote schema to ./dist/schemas.json');
80
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ import path from 'node:path';
3
+ import * as fs from 'node:fs';
4
+ import { build } from './build.js';
5
+ const packageJson = fs.readFileSync(path.join(process.env['npm_config_local_prefix'], 'package.json'), 'utf8');
6
+ const { name: libraryName, version: libraryVersion } = JSON.parse(packageJson);
7
+ console.log(libraryName, libraryVersion);
8
+ await build();
package/dist/index.d.ts CHANGED
@@ -10,5 +10,5 @@ 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/index.js';
14
14
  export declare function runPlugin(plugin: Plugin): Promise<void>;
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ 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/index.js';
14
14
  export async function runPlugin(plugin) {
15
15
  const messageHandler = new MessageHandler(plugin);
16
16
  process.on('message', (message) => messageHandler.onMessage(message));
@@ -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';
7
+ import { VerbosityLevel } from '../utils/internal-utils.js';
6
8
  import { ptyLocalStorage } from '../utils/pty-local-storage.js';
7
- import { VerbosityLevel } from '../utils/utils.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 () => {
@@ -16,4 +16,5 @@ export declare class BackgroundPty implements IPty {
16
16
  signal?: number | undefined;
17
17
  }>;
18
18
  private initialize;
19
+ private getDefaultShell;
19
20
  }
@@ -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 { VerbosityLevel } from '../utils/utils.js';
8
+ import { Shell, Utils } from '../utils/index.js';
9
+ import { VerbosityLevel } from '../utils/internal-utils.js';
9
10
  import { SpawnError } from './index.js';
10
11
  import { PromiseQueue } from './promise-queue.js';
11
12
  EventEmitter.defaultMaxListeners = 1000;
@@ -16,7 +17,7 @@ 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 {
19
- basePty = pty.spawn('zsh', ['-i'], {
20
+ basePty = pty.spawn(this.getDefaultShell(), ['-i'], {
20
21
  env: process.env, name: nanoid(6),
21
22
  handleFlowControl: true
22
23
  });
@@ -103,7 +104,17 @@ export class BackgroundPty {
103
104
  await this.promiseQueue.run(async () => {
104
105
  let outputBuffer = '';
105
106
  return new Promise(resolve => {
106
- this.basePty.write('setopt hist_ignore_space;\n');
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
+ }
107
118
  this.basePty.write(' unset PS1;\n');
108
119
  this.basePty.write(' unset PS0;\n');
109
120
  this.basePty.write(' echo setup complete\\"\n');
@@ -117,4 +128,7 @@ export class BackgroundPty {
117
128
  });
118
129
  });
119
130
  }
131
+ getDefaultShell() {
132
+ return process.env.SHELL;
133
+ }
120
134
  }
@@ -1,5 +1,5 @@
1
1
  export interface SpawnResult {
2
- status: 'success' | 'error';
2
+ status: 'error' | 'success';
3
3
  exitCode: number;
4
4
  data: string;
5
5
  }
@@ -7,9 +7,25 @@ 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;
13
29
  }
14
30
  export declare class SpawnError extends Error {
15
31
  data: string;
@@ -0,0 +1,16 @@
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
+ private getDefaultShell;
16
+ }
@@ -0,0 +1,84 @@
1
+ import pty from '@homebridge/node-pty-prebuilt-multiarch';
2
+ import { EventEmitter } from 'node:events';
3
+ import stripAnsi from 'strip-ansi';
4
+ import { Shell, Utils } from '../utils/index.js';
5
+ import { VerbosityLevel } from '../utils/internal-utils.js';
6
+ import { SpawnError, SpawnStatus } from './index.js';
7
+ EventEmitter.defaultMaxListeners = 1000;
8
+ /**
9
+ * The background pty is a specialized pty designed for speed. It can launch multiple tasks
10
+ * in parallel by moving them to the background. It attaches unix FIFO pipes to each process
11
+ * to listen to stdout and stderr. One limitation of the BackgroundPty is that the tasks run
12
+ * without a tty (or even a stdin) attached so interactive commands will not work.
13
+ */
14
+ export class SequentialPty {
15
+ async spawn(cmd, options) {
16
+ const spawnResult = await this.spawnSafe(cmd, options);
17
+ if (spawnResult.status !== 'success') {
18
+ throw new SpawnError(cmd, spawnResult.exitCode, spawnResult.data);
19
+ }
20
+ return spawnResult;
21
+ }
22
+ async spawnSafe(cmd, options) {
23
+ console.log(`Running command: ${cmd}` + (options?.cwd ? `(${options?.cwd})` : ''));
24
+ return new Promise((resolve) => {
25
+ const output = [];
26
+ const historyIgnore = Utils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' };
27
+ // If TERM_PROGRAM=Apple_Terminal is set then ANSI escape characters may be included
28
+ // in the response.
29
+ const env = {
30
+ ...process.env, ...options?.env,
31
+ TERM_PROGRAM: 'codify',
32
+ COMMAND_MODE: 'unix2003',
33
+ COLORTERM: 'truecolor', ...historyIgnore
34
+ };
35
+ // Initial terminal dimensions
36
+ const initialCols = process.stdout.columns ?? 80;
37
+ const initialRows = process.stdout.rows ?? 24;
38
+ const args = (options?.interactive ?? true) ? ['-i', '-c', `"${cmd}"`] : ['-c', `"${cmd}"`];
39
+ // Run the command in a pty for interactivity
40
+ const mPty = pty.spawn(this.getDefaultShell(), args, {
41
+ ...options,
42
+ cols: initialCols,
43
+ rows: initialRows,
44
+ env
45
+ });
46
+ mPty.onData((data) => {
47
+ if (VerbosityLevel.get() > 0) {
48
+ process.stdout.write(data);
49
+ }
50
+ output.push(data.toString());
51
+ });
52
+ const stdinListener = (data) => {
53
+ mPty.write(data.toString());
54
+ };
55
+ const resizeListener = () => {
56
+ const { columns, rows } = process.stdout;
57
+ mPty.resize(columns, rows);
58
+ };
59
+ // Listen to resize events for the terminal window;
60
+ process.stdout.on('resize', resizeListener);
61
+ // Listen for user input
62
+ process.stdin.on('data', stdinListener);
63
+ mPty.onExit((result) => {
64
+ process.stdout.off('resize', resizeListener);
65
+ process.stdin.off('data', stdinListener);
66
+ resolve({
67
+ status: result.exitCode === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR,
68
+ exitCode: result.exitCode,
69
+ data: stripAnsi(output.join('\n').trim()),
70
+ });
71
+ });
72
+ });
73
+ }
74
+ async kill() {
75
+ // No-op here. Each pty instance is stand alone and tied to the parent process. Everything should be killed as expected.
76
+ return {
77
+ exitCode: 0,
78
+ signal: 0,
79
+ };
80
+ }
81
+ getDefaultShell() {
82
+ return process.env.SHELL;
83
+ }
84
+ }
@@ -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
- this.id = settings.id;
15
- this.schema = settings.schema;
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/utils.js';
3
+ import { addVariablesToPath, areArraysEqual, resolvePathWithVariables, tildify, untildify } from '../utils/internal-utils.js';
4
4
  const ParameterEqualsDefaults = {
5
5
  'boolean': (a, b) => Boolean(a) === Boolean(b),
6
6
  'directory': (a, b) => {
@@ -12,8 +12,9 @@ const ParameterEqualsDefaults = {
12
12
  if (transformedB.startsWith('.')) { // Only relative paths start with '.'
13
13
  transformedB = path.resolve(transformedB);
14
14
  }
15
- const notCaseSensitive = process.platform === 'darwin';
16
- if (notCaseSensitive) {
15
+ // macOS has case-insensitive filesystem by default, Linux is case-sensitive
16
+ const isCaseSensitive = process.platform === 'linux';
17
+ if (!isCaseSensitive) {
17
18
  transformedA = transformedA.toLowerCase();
18
19
  transformedB = transformedB.toLowerCase();
19
20
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ console.log('Deploying!!');
2
+ export {};
@@ -0,0 +1,29 @@
1
+ import { SpawnOptions } from 'node:child_process';
2
+ export declare enum SpawnStatus {
3
+ SUCCESS = "success",
4
+ ERROR = "error"
5
+ }
6
+ export interface SpawnResult {
7
+ status: SpawnStatus;
8
+ data: string;
9
+ }
10
+ type CodifySpawnOptions = {
11
+ cwd?: string;
12
+ throws?: boolean;
13
+ requiresRoot?: boolean;
14
+ requestsTTY?: boolean;
15
+ } & Omit<SpawnOptions, 'detached' | 'shell' | 'stdio'>;
16
+ /**
17
+ *
18
+ * @param cmd Command to run. Ex: `rm -rf`
19
+ * @param opts Standard options for node spawn. Additional argument:
20
+ * throws determines if a shell will throw a JS error. Defaults to true
21
+ *
22
+ * @see promiseSpawn
23
+ * @see spawn
24
+ *
25
+ * @returns SpawnResult { status: SUCCESS | ERROR; data: string }
26
+ */
27
+ export declare function codifySpawn(cmd: string, opts?: CodifySpawnOptions): Promise<SpawnResult>;
28
+ export declare function isDebug(): boolean;
29
+ export {};
@@ -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,25 @@
1
+ import { OS } from 'codify-schemas';
2
+ export declare enum Shell {
3
+ ZSH = "zsh",
4
+ BASH = "bash",
5
+ SH = "sh",
6
+ KSH = "ksh",
7
+ CSH = "csh",
8
+ FISH = "fish"
9
+ }
10
+ export interface SystemInfo {
11
+ os: OS;
12
+ shell: Shell;
13
+ }
14
+ export declare const Utils: {
15
+ getUser(): string;
16
+ getSystemInfo(): {
17
+ os: string;
18
+ shell: Shell | undefined;
19
+ };
20
+ isMacOS(): boolean;
21
+ isLinux(): boolean;
22
+ getShell(): Shell | undefined;
23
+ getPrimaryShellRc(): string;
24
+ getShellRcFiles(): string[];
25
+ };