@alwaysai/device-agent 0.0.3 → 0.0.5

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 (210) hide show
  1. package/lib/application-control/backup.d.ts +8 -0
  2. package/lib/application-control/backup.d.ts.map +1 -0
  3. package/lib/application-control/backup.js +34 -0
  4. package/lib/application-control/backup.js.map +1 -0
  5. package/lib/application-control/environment-variables.d.ts +9 -0
  6. package/lib/application-control/environment-variables.d.ts.map +1 -0
  7. package/lib/application-control/environment-variables.js +82 -0
  8. package/lib/application-control/environment-variables.js.map +1 -0
  9. package/lib/application-control/index.d.ts +9 -0
  10. package/lib/application-control/index.d.ts.map +1 -0
  11. package/lib/application-control/index.js +27 -0
  12. package/lib/application-control/index.js.map +1 -0
  13. package/lib/application-control/install.d.ts +16 -0
  14. package/lib/application-control/install.d.ts.map +1 -0
  15. package/lib/application-control/install.js +117 -0
  16. package/lib/application-control/install.js.map +1 -0
  17. package/lib/application-control/models.d.ts +8 -0
  18. package/lib/application-control/models.d.ts.map +1 -1
  19. package/lib/application-control/models.js +44 -11
  20. package/lib/application-control/models.js.map +1 -1
  21. package/lib/application-control/status.d.ts +26 -0
  22. package/lib/application-control/status.d.ts.map +1 -0
  23. package/lib/application-control/status.js +138 -0
  24. package/lib/application-control/status.js.map +1 -0
  25. package/lib/application-control/types.d.ts +5 -0
  26. package/lib/application-control/types.d.ts.map +1 -0
  27. package/lib/{util/spawner → application-control}/types.js +0 -0
  28. package/lib/{util/spawner → application-control}/types.js.map +1 -1
  29. package/lib/application-control/utils.d.ts +2 -9
  30. package/lib/application-control/utils.d.ts.map +1 -1
  31. package/lib/application-control/utils.js +14 -29
  32. package/lib/application-control/utils.js.map +1 -1
  33. package/lib/cloud-connection/device-agent-cloud-connection.d.ts +16 -1
  34. package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
  35. package/lib/cloud-connection/device-agent-cloud-connection.js +239 -9
  36. package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
  37. package/lib/docker/docker-cmd.js +2 -2
  38. package/lib/docker/docker-cmd.js.map +1 -1
  39. package/lib/docker/docker-compose-cmd.js +2 -2
  40. package/lib/docker/docker-compose-cmd.js.map +1 -1
  41. package/lib/environment.d.ts +2 -0
  42. package/lib/environment.d.ts.map +1 -1
  43. package/lib/environment.js +3 -1
  44. package/lib/environment.js.map +1 -1
  45. package/lib/index.js +10 -8
  46. package/lib/index.js.map +1 -1
  47. package/lib/infrastructure/agent-config.d.ts +73 -0
  48. package/lib/infrastructure/agent-config.d.ts.map +1 -0
  49. package/lib/infrastructure/agent-config.js +186 -0
  50. package/lib/infrastructure/agent-config.js.map +1 -0
  51. package/lib/infrastructure/agent-config.test.d.ts +2 -0
  52. package/lib/infrastructure/agent-config.test.d.ts.map +1 -0
  53. package/lib/infrastructure/agent-config.test.js +135 -0
  54. package/lib/infrastructure/agent-config.test.js.map +1 -0
  55. package/lib/infrastructure/certificates-and-tokens.d.ts +6 -0
  56. package/lib/infrastructure/certificates-and-tokens.d.ts.map +1 -0
  57. package/lib/infrastructure/certificates-and-tokens.js +69 -0
  58. package/lib/infrastructure/certificates-and-tokens.js.map +1 -0
  59. package/lib/{util → infrastructure}/urls.d.ts +0 -0
  60. package/lib/infrastructure/urls.d.ts.map +1 -0
  61. package/lib/{util → infrastructure}/urls.js +3 -3
  62. package/lib/infrastructure/urls.js.map +1 -0
  63. package/lib/root.js +3 -3
  64. package/lib/root.js.map +1 -1
  65. package/lib/subcommands/app/app.d.ts +31 -14
  66. package/lib/subcommands/app/app.d.ts.map +1 -1
  67. package/lib/subcommands/app/app.js +117 -62
  68. package/lib/subcommands/app/app.js.map +1 -1
  69. package/lib/subcommands/app/index.d.ts.map +1 -1
  70. package/lib/subcommands/app/index.js +6 -1
  71. package/lib/subcommands/app/index.js.map +1 -1
  72. package/lib/subcommands/device/device.d.ts +6 -0
  73. package/lib/subcommands/device/device.d.ts.map +1 -0
  74. package/lib/subcommands/device/device.js +62 -0
  75. package/lib/subcommands/device/device.js.map +1 -0
  76. package/lib/subcommands/device/index.d.ts +2 -0
  77. package/lib/subcommands/device/index.d.ts.map +1 -0
  78. package/lib/subcommands/device/index.js +11 -0
  79. package/lib/subcommands/device/index.js.map +1 -0
  80. package/lib/subcommands/get-model-package.d.ts +5 -0
  81. package/lib/subcommands/get-model-package.d.ts.map +1 -0
  82. package/lib/subcommands/get-model-package.js +51 -0
  83. package/lib/subcommands/get-model-package.js.map +1 -0
  84. package/lib/subcommands/index.d.ts +8 -1
  85. package/lib/subcommands/index.d.ts.map +1 -1
  86. package/lib/subcommands/index.js +6 -6
  87. package/lib/subcommands/index.js.map +1 -1
  88. package/lib/subcommands/login.d.ts +3 -2
  89. package/lib/subcommands/login.d.ts.map +1 -1
  90. package/lib/subcommands/login.js +11 -4
  91. package/lib/subcommands/login.js.map +1 -1
  92. package/lib/util/copy-dir.js +3 -3
  93. package/lib/util/copy-dir.js.map +1 -1
  94. package/lib/util/directories.d.ts +1 -1
  95. package/lib/util/directories.d.ts.map +1 -1
  96. package/lib/util/directories.js +9 -14
  97. package/lib/util/directories.js.map +1 -1
  98. package/lib/util/get-device-id.d.ts +2 -0
  99. package/lib/util/get-device-id.d.ts.map +1 -0
  100. package/lib/util/get-device-id.js +24 -0
  101. package/lib/util/get-device-id.js.map +1 -0
  102. package/package.json +19 -14
  103. package/readme.md +176 -72
  104. package/src/application-control/backup.ts +32 -0
  105. package/src/application-control/environment-variables.ts +81 -0
  106. package/src/application-control/index.ts +40 -0
  107. package/src/application-control/install.ts +126 -0
  108. package/src/application-control/models.ts +51 -11
  109. package/src/application-control/status.ts +156 -0
  110. package/src/application-control/types.ts +1 -0
  111. package/src/application-control/utils.ts +12 -27
  112. package/src/cloud-connection/device-agent-cloud-connection.ts +280 -13
  113. package/src/docker/docker-cmd.ts +1 -1
  114. package/src/docker/docker-compose-cmd.ts +1 -1
  115. package/src/environment.ts +2 -0
  116. package/src/index.ts +10 -7
  117. package/src/infrastructure/agent-config.test.ts +143 -0
  118. package/src/infrastructure/agent-config.ts +217 -0
  119. package/src/infrastructure/certificates-and-tokens.ts +71 -0
  120. package/src/{util → infrastructure}/urls.ts +1 -1
  121. package/src/root.ts +3 -3
  122. package/src/subcommands/app/app.ts +135 -62
  123. package/src/subcommands/app/index.ts +11 -1
  124. package/src/subcommands/device/device.ts +63 -0
  125. package/src/subcommands/device/index.ts +8 -0
  126. package/src/subcommands/get-model-package.ts +60 -0
  127. package/src/subcommands/index.ts +5 -5
  128. package/src/subcommands/login.ts +11 -4
  129. package/src/util/copy-dir.ts +1 -1
  130. package/src/util/directories.ts +12 -17
  131. package/src/util/get-device-id.ts +22 -0
  132. package/lib/application-control/application-control.d.ts +0 -46
  133. package/lib/application-control/application-control.d.ts.map +0 -1
  134. package/lib/application-control/application-control.js +0 -234
  135. package/lib/application-control/application-control.js.map +0 -1
  136. package/lib/constants.d.ts +0 -17
  137. package/lib/constants.d.ts.map +0 -1
  138. package/lib/constants.js +0 -24
  139. package/lib/constants.js.map +0 -1
  140. package/lib/subcommands/device-control.d.ts +0 -2
  141. package/lib/subcommands/device-control.d.ts.map +0 -1
  142. package/lib/subcommands/device-control.js +0 -19
  143. package/lib/subcommands/device-control.js.map +0 -1
  144. package/lib/subcommands/test-app.d.ts +0 -2
  145. package/lib/subcommands/test-app.d.ts.map +0 -1
  146. package/lib/subcommands/test-app.js +0 -29
  147. package/lib/subcommands/test-app.js.map +0 -1
  148. package/lib/util/spawner/gnu-spawner.d.ts +0 -9
  149. package/lib/util/spawner/gnu-spawner.d.ts.map +0 -1
  150. package/lib/util/spawner/gnu-spawner.js +0 -102
  151. package/lib/util/spawner/gnu-spawner.js.map +0 -1
  152. package/lib/util/spawner/js-spawner.d.ts +0 -5
  153. package/lib/util/spawner/js-spawner.d.ts.map +0 -1
  154. package/lib/util/spawner/js-spawner.js +0 -89
  155. package/lib/util/spawner/js-spawner.js.map +0 -1
  156. package/lib/util/spawner/types.d.ts +0 -28
  157. package/lib/util/spawner/types.d.ts.map +0 -1
  158. package/lib/util/spawner-base/index.d.ts +0 -17
  159. package/lib/util/spawner-base/index.d.ts.map +0 -1
  160. package/lib/util/spawner-base/index.js +0 -30
  161. package/lib/util/spawner-base/index.js.map +0 -1
  162. package/lib/util/spawner-base/run-foreground-sync.d.ts +0 -3
  163. package/lib/util/spawner-base/run-foreground-sync.d.ts.map +0 -1
  164. package/lib/util/spawner-base/run-foreground-sync.js +0 -18
  165. package/lib/util/spawner-base/run-foreground-sync.js.map +0 -1
  166. package/lib/util/spawner-base/run-foreground.d.ts +0 -3
  167. package/lib/util/spawner-base/run-foreground.d.ts.map +0 -1
  168. package/lib/util/spawner-base/run-foreground.js +0 -49
  169. package/lib/util/spawner-base/run-foreground.js.map +0 -1
  170. package/lib/util/spawner-base/run-streaming.d.ts +0 -4
  171. package/lib/util/spawner-base/run-streaming.d.ts.map +0 -1
  172. package/lib/util/spawner-base/run-streaming.js +0 -35
  173. package/lib/util/spawner-base/run-streaming.js.map +0 -1
  174. package/lib/util/spawner-base/run.d.ts +0 -4
  175. package/lib/util/spawner-base/run.d.ts.map +0 -1
  176. package/lib/util/spawner-base/run.js +0 -56
  177. package/lib/util/spawner-base/run.js.map +0 -1
  178. package/lib/util/urls.d.ts.map +0 -1
  179. package/lib/util/urls.js.map +0 -1
  180. package/lib/web/index.html +0 -229
  181. package/lib/web/static/Karla.css +0 -18
  182. package/lib/web/static/bootstrap-4.3.1.min.css +0 -7
  183. package/lib/web/static/bootstrap-4.3.1.min.js +0 -7
  184. package/lib/web/static/favicon.ico +0 -0
  185. package/lib/web/static/jquery-3.3.1.slim.min.js +0 -2
  186. package/lib/web/static/popper-1.14.7.min.js +0 -5
  187. package/lib/web/web-interface.d.ts +0 -2
  188. package/lib/web/web-interface.d.ts.map +0 -1
  189. package/lib/web/web-interface.js +0 -74
  190. package/lib/web/web-interface.js.map +0 -1
  191. package/src/application-control/application-control.ts +0 -273
  192. package/src/constants.ts +0 -32
  193. package/src/subcommands/device-control.ts +0 -16
  194. package/src/subcommands/test-app.ts +0 -30
  195. package/src/util/spawner/gnu-spawner.ts +0 -114
  196. package/src/util/spawner/js-spawner.ts +0 -110
  197. package/src/util/spawner/types.ts +0 -28
  198. package/src/util/spawner-base/index.ts +0 -28
  199. package/src/util/spawner-base/run-foreground-sync.ts +0 -16
  200. package/src/util/spawner-base/run-foreground.ts +0 -49
  201. package/src/util/spawner-base/run-streaming.ts +0 -40
  202. package/src/util/spawner-base/run.ts +0 -60
  203. package/src/web/index.html +0 -229
  204. package/src/web/static/Karla.css +0 -18
  205. package/src/web/static/bootstrap-4.3.1.min.css +0 -7
  206. package/src/web/static/bootstrap-4.3.1.min.js +0 -7
  207. package/src/web/static/favicon.ico +0 -0
  208. package/src/web/static/jquery-3.3.1.slim.min.js +0 -2
  209. package/src/web/static/popper-1.14.7.min.js +0 -5
  210. package/src/web/web-interface.ts +0 -89
package/src/constants.ts DELETED
@@ -1,32 +0,0 @@
1
- import { join, posix } from 'path';
2
- import { homedir } from 'os';
3
- const os = require('os');
4
- const path = require('path');
5
-
6
- export const ALWAYSAI_CLI_EXECUTABLE_NAME = 'aai-agent';
7
- export const DOT_ALWAYSAI_DIR = join(homedir(), '.alwaysai');
8
- export const AAI_DIR = path.join(os.homedir(), '.alwaysai');
9
-
10
- export const DEVICE_TOKEN_FILE_NAME = 'alwaysai.credentials.json';
11
- export const DEVICE_CONFIG_FILE_NAME = 'alwaysai.config.json';
12
-
13
- export const DEV_HOST_TOKEN_DIR = join(homedir(), '.config', 'alwaysai');
14
- export const DEVICE_TOKEN_DIR_LINUX = posix.join('~', '.config', 'alwaysai');
15
-
16
- export const DEV_HOST_CERT_AND_KEY_DIR = join(DEV_HOST_TOKEN_DIR, 'certificates');
17
- export const DEVICE_CERT_AND_KEY_DIR_LINUX = posix.join(
18
- DEVICE_TOKEN_DIR_LINUX,
19
- 'certificates',
20
- );
21
-
22
- export const DEVICE_CERTIFICATE_FILE_NAME = 'alwaysai.certificate.pem.crt';
23
- export const DEVICE_PRIVATE_KEY_FILE_NAME = 'alwaysai.private.pem.key';
24
- export const DEVICE_PUBLIC_KEY_FILE_NAME = 'alwaysai.public.pem.key';
25
- export const DEVICE_ROOT_CERT_FILE_NAME = 'alwaysai.root.pem';
26
-
27
- export const SUPPORTED_JETPACK_VERSION = 4.4;
28
- export const JETPACK_IMAGE_LINK =
29
- 'https://developer.nvidia.com/jetson-nano-sd-card-image-44';
30
-
31
- export const PLEASE_REPORT_THIS_ERROR_MESSAGE =
32
- 'Please report this error message to support@alwaysai.co';
@@ -1,16 +0,0 @@
1
- import { CliLeaf } from '@alwaysai/alwayscli';
2
- import { getCpuUtil, getDiskUtil, getMemUtil } from '../device-control/device-control';
3
-
4
- export const getDeviceInfoCliLeaf = CliLeaf({
5
- name: 'get-device-info',
6
- description: 'Get device info',
7
- namedInputs: {},
8
- async action(_, opts) {
9
- const deviceInfo = [
10
- ['CPU Utilization', `${String(await getCpuUtil())} %`],
11
- ['Disk Utilization', `${String(await getDiskUtil())} %`],
12
- ['Memory Utilization', `${String(await getMemUtil())} %`],
13
- ];
14
- console.table(deviceInfo);
15
- },
16
- });
@@ -1,30 +0,0 @@
1
- import { CliLeaf, CliStringInput, CliTerseError } from '@alwaysai/alwayscli';
2
- const path = require('path');
3
- import { installApp } from '../application-control/application-control';
4
- import { JsSpawner } from '../util/spawner/js-spawner';
5
-
6
- // export const installTestAppCliLeaf = CliLeaf({
7
- // name: 'install-test-app',
8
- // description: 'Install a test application',
9
- // namedInputs: {
10
- // project: CliStringInput({
11
- // description: 'Project Id',
12
- // }),
13
- // version: CliStringInput({
14
- // description: 'Version',
15
- // }),
16
- // },
17
- // async action(_, opts) {
18
- // const { project, version } = opts;
19
- // const postfix = Math.random()
20
- // .toString()
21
- // .slice(2, 14);
22
- // const projectId = project ? project : `16ad11bb-8c3a-4a15-923e-${postfix}`;
23
- // const releaseVersion = version ? version : 'f43e816b30aa612f103fe73314f9716a436ce4f5';
24
- // // Package test app
25
- // const appDir = path.join('test', 'test-app');
26
- // const spawner = JsSpawner({ path: appDir });
27
- // const appPkg = await spawner.tar(...(await spawner.readdir()));
28
- // await installApp({ projectId, appPkg, version: releaseVersion });
29
- // },
30
- // });
@@ -1,114 +0,0 @@
1
- import { Spawner } from './types';
2
- import { CodedError } from '@carnesen/coded-error';
3
- const { Readable } = require('stream');
4
-
5
- export function GnuSpawner(context: {
6
- resolvePath: Spawner['resolvePath'];
7
- run: Spawner['run'];
8
- runForegroundSync: Spawner['runForegroundSync'];
9
- runForeground: Spawner['runForeground'];
10
- runStreaming: Spawner['runStreaming'];
11
- }): Spawner {
12
- const { resolvePath, run, runForegroundSync, runForeground, runStreaming } = context;
13
-
14
- return {
15
- run,
16
- runForegroundSync,
17
- runForeground,
18
- runStreaming,
19
- resolvePath,
20
- readdir,
21
- async readFile(path) {
22
- const output = await run({
23
- exe: 'cat',
24
- args: [resolvePath(path)],
25
- });
26
- return output;
27
- },
28
- async writeFile(path, data) {
29
- await run({
30
- exe: 'dd',
31
- args: [`of=${resolvePath(path)}`],
32
- input: Readable.from(data),
33
- });
34
- },
35
- async rename(oldPath, newPath) {
36
- await run({
37
- exe: 'mv',
38
- args: [resolvePath(oldPath), resolvePath(newPath)],
39
- });
40
- },
41
- mkdirp,
42
- rimraf,
43
- sanatizedrimraf,
44
- tar,
45
- untar,
46
- exists,
47
- };
48
-
49
- async function mkdirp(path?: string) {
50
- await run({ exe: 'mkdir', args: ['-p', resolvePath(path)] });
51
- }
52
-
53
- async function readdir(path?: string) {
54
- let output: string;
55
- const resolvedPath = resolvePath(path);
56
- try {
57
- output = await run({ exe: 'ls', args: ['-A1', resolvedPath] });
58
- // ^^ The output looks like '/foo/bar.txt' if path is an existing file
59
- // Else it looks like 'a b c' if the path is a directory with files/subdirs a, b, c.
60
- } catch (ex) {
61
- if (
62
- ex &&
63
- typeof ex.message === 'string' &&
64
- ex.message.includes('No such file or directory')
65
- ) {
66
- ex.code = 'ENOENT';
67
- }
68
- throw ex;
69
- }
70
- if (output.startsWith('/')) {
71
- throw new CodedError(`ENOTDIR: not a directory "${resolvedPath}"`, 'ENOTDIR');
72
- }
73
- return output.length > 0 ? output.split('\n') : [];
74
- }
75
-
76
- async function rimraf(path?: string) {
77
- await run({ exe: 'rm', args: ['-rf', resolvePath(path)] });
78
- }
79
-
80
- async function sanatizedrimraf(path?: string) {
81
- const sanatizedPath = resolvePath(path).replace('*', '');
82
-
83
- await run({ exe: 'rm', args: ['-rf', sanatizedPath] });
84
- }
85
-
86
- async function tar(...paths: string[]) {
87
- return await runStreaming({
88
- exe: 'tar',
89
- args: ['-cz', ...paths],
90
- cwd: resolvePath(),
91
- });
92
- }
93
-
94
- async function untar(input: NodeJS.ReadableStream, cwd = '.') {
95
- await run({
96
- exe: 'tar',
97
- args: ['-xz'],
98
- cwd,
99
- input,
100
- });
101
- }
102
-
103
- async function exists(path: string) {
104
- if (!path) {
105
- throw new Error('"path" is required');
106
- }
107
- try {
108
- await run({ exe: 'stat', args: [resolvePath(path)] });
109
- return true;
110
- } catch (ex) {
111
- return false;
112
- }
113
- }
114
- }
@@ -1,110 +0,0 @@
1
- import {
2
- readdir as fsReaddir,
3
- access as fsAccess,
4
- readFile as fsReadFile,
5
- writeFile as fsWriteFile,
6
- rename as fsRename,
7
- } from 'fs';
8
-
9
- import { promisify } from 'util';
10
- import { isAbsolute, resolve } from 'path';
11
-
12
- import * as tarJs from 'tar';
13
- import * as mkdirpJs from 'mkdirp';
14
- import * as rimrafJs from 'rimraf';
15
-
16
- import { Spawner, Cmd } from './types';
17
- import { SpawnerBase } from '../spawner-base';
18
- import { GnuSpawner } from './gnu-spawner';
19
- import pump = require('pump');
20
-
21
- export function JsSpawner(context: { path?: string } = {}): Spawner {
22
- const gnuSpawner = GnuSpawner({ resolvePath, ...SpawnerBase(translate) });
23
- return {
24
- ...gnuSpawner,
25
- resolvePath,
26
- readdir,
27
- async readFile(path) {
28
- return await promisify(fsReadFile)(resolvePath(path), { encoding: 'utf8' });
29
- },
30
- async writeFile(path, data) {
31
- await promisify(fsWriteFile)(resolvePath(path), data, { encoding: 'utf8' });
32
- },
33
- mkdirp,
34
- rimraf,
35
- tar,
36
- async rename(oldPath, newPath) {
37
- await promisify(fsRename)(resolvePath(oldPath), resolvePath(newPath));
38
- },
39
- async untar(input, cwd) {
40
- await new Promise<string | void>((resolve, reject) => {
41
- const writable = tarJs.extract({ cwd: resolvePath(cwd) });
42
- pump(input, writable, err => {
43
- if (err) {
44
- reject(err);
45
- } else {
46
- resolve();
47
- }
48
- });
49
- });
50
- },
51
- exists,
52
- };
53
-
54
- function resolvePath(...paths: (string | undefined)[]) {
55
- return resolve(context.path || '', ...paths.map(path => path || ''));
56
- }
57
-
58
- function translate(cmd: Cmd) {
59
- if (cmd.superuser) {
60
- throw new Error(`${JsSpawner.name} does not support cmd option "superuser"`);
61
- }
62
- const translated: Cmd = {
63
- ...cmd,
64
- };
65
-
66
- if (cmd.cwd) {
67
- translated.cwd = resolvePath(cmd.cwd);
68
- }
69
-
70
- return translated;
71
- }
72
-
73
- async function mkdirp(path = '') {
74
- await mkdirpJs(resolvePath(path));
75
- }
76
-
77
- async function rimraf(path = '') {
78
- await promisify(rimrafJs)(resolvePath(path));
79
- }
80
-
81
- function readdir(path = '') {
82
- return promisify(fsReaddir)(resolvePath(path));
83
- }
84
-
85
- async function tar(...paths: string[]) {
86
- for (const path in paths) {
87
- if (isAbsolute(path)) {
88
- throw new Error('Paths passed to spawner.tar must not be absolute');
89
- }
90
- }
91
- const packageStream = (tarJs.create(
92
- { sync: true, gzip: true, cwd: resolvePath() },
93
- paths,
94
- ) as unknown) as NodeJS.ReadableStream;
95
- // ^^ The @types for tar.create are not correct
96
- return packageStream;
97
- }
98
-
99
- async function exists(path: string) {
100
- if (!path) {
101
- throw new Error('path is mandatory');
102
- }
103
- try {
104
- await promisify(fsAccess)(resolvePath(path));
105
- return true;
106
- } catch (ex) {
107
- return false;
108
- }
109
- }
110
- }
@@ -1,28 +0,0 @@
1
- export type Cmd = {
2
- exe: string;
3
- args?: string[];
4
- cwd?: string;
5
- tty?: boolean;
6
- input?: NodeJS.ReadableStream;
7
- superuser?: boolean;
8
- };
9
-
10
- export type Translate = (cmd: Cmd) => Cmd;
11
-
12
- export type Spawner = {
13
- run: (cmd: Cmd) => Promise<string>;
14
- runForegroundSync: (cmd: Cmd) => void;
15
- runForeground: (cmd: Cmd) => Promise<number | undefined | void>;
16
- runStreaming: (cmd: Cmd) => Promise<NodeJS.ReadableStream>;
17
- resolvePath: (...paths: (string | undefined)[]) => string;
18
- readdir: (path?: string) => Promise<string[]>;
19
- readFile: (path: string) => Promise<string>;
20
- writeFile: (path: string, data: string) => Promise<void>;
21
- mkdirp: (path?: string) => Promise<void>;
22
- rename: (oldPath: string, newPath: string) => Promise<void>;
23
- rimraf: (path?: string) => Promise<void>;
24
- sanatizedrimraf: (path?: string) => Promise<void>;
25
- tar: (...paths: string[]) => Promise<NodeJS.ReadableStream>;
26
- untar: (input: NodeJS.ReadableStream, cwd?: string) => Promise<void>;
27
- exists: (path: string) => Promise<boolean>;
28
- };
@@ -1,28 +0,0 @@
1
- import { run } from './run';
2
- import { runForegroundSync } from './run-foreground-sync';
3
- import { runForeground } from './run-foreground';
4
- import { runStreaming } from './run-streaming';
5
- import { Cmd } from '../spawner/types';
6
-
7
- export function SpawnerBase(translate: (cmd: Cmd) => Cmd) {
8
- return {
9
- run(cmd: Cmd) {
10
- return run(translate(cmd));
11
- },
12
- runForeground(cmd: Cmd) {
13
- return runForeground(translate(cmd));
14
- },
15
- runForegroundSync(cmd: Cmd) {
16
- return runForegroundSync(translate(cmd));
17
- },
18
- runStreaming(cmd: Cmd) {
19
- return runStreaming(translate(cmd));
20
- },
21
- };
22
- }
23
-
24
- export const spawnerBase = {
25
- run,
26
- runForegroundSync,
27
- runStreaming,
28
- };
@@ -1,16 +0,0 @@
1
- import { spawnSync } from 'child_process';
2
- const winston = require('winston');
3
-
4
- import { Cmd } from '../spawner/types';
5
-
6
- export function runForegroundSync(cmd: Cmd) {
7
- const optString = cmd.args ? cmd.args.join(' ') : '';
8
- winston.debug(`${cmd.exe} ${optString}`);
9
- const out = spawnSync(cmd.exe, cmd.args || [], {
10
- cwd: cmd.cwd,
11
- stdio: 'inherit',
12
- });
13
- if (out.error) {
14
- throw out.error;
15
- }
16
- }
@@ -1,49 +0,0 @@
1
- import { spawn } from 'child_process';
2
- import { Cmd } from '../spawner/types';
3
- import signalExit = require('signal-exit');
4
- import rKill = require('tree-kill');
5
- import { CliTerseError } from '@alwaysai/alwayscli';
6
- import { ALWAYSAI_OS_PLATFORM } from '../../environment';
7
-
8
- export async function runForeground(cmd: Cmd) {
9
- const childProcess = spawn(cmd.exe, cmd.args || [], {
10
- cwd: cmd.cwd,
11
- stdio: 'inherit',
12
- });
13
-
14
- // Kill child when parent exits
15
- signalExit(() => {
16
- if (ALWAYSAI_OS_PLATFORM === 'darwin') {
17
- childProcess.kill('SIGINT');
18
- // ensures the main process gets killed by the correct signal - this will only activate with a ctrl + c
19
- // when it hits the siginit it will then activate the on exit signal and finish
20
- process.on('SIGINT', () => {
21
- if (childProcess.pid !== undefined) {
22
- rKill(childProcess.pid, 'SIGINT', (error?: Error | undefined) => {
23
- if (error) {
24
- throw new CliTerseError(
25
- `Failed to kill child process {error.name} {error.message}`,
26
- );
27
- }
28
- });
29
- }
30
- });
31
- } else {
32
- childProcess.kill();
33
- }
34
- });
35
-
36
- return await new Promise<number | undefined | void>((resolve, reject) => {
37
- childProcess.on('error', err => {
38
- reject(err);
39
- });
40
-
41
- childProcess.on('exit', code => {
42
- if (code) {
43
- resolve(code);
44
- } else {
45
- resolve();
46
- }
47
- });
48
- });
49
- }
@@ -1,40 +0,0 @@
1
- import { spawn } from 'child_process';
2
-
3
- import signalExit = require('signal-exit');
4
-
5
- import { Cmd } from '../spawner/types';
6
-
7
- export function runStreaming(cmd: Cmd) {
8
- return new Promise<NodeJS.ReadableStream>((resolve, reject) => {
9
- const child = spawn(cmd.exe, cmd.args || [], {
10
- cwd: cmd.cwd,
11
- });
12
-
13
- signalExit(() => {
14
- child.kill();
15
- });
16
-
17
- if (cmd.input) {
18
- cmd.input.pipe(child.stdin);
19
- }
20
-
21
- const errChunks: Buffer[] = [];
22
- function stderrChunkHandler(chunk: Buffer) {
23
- errChunks.push(chunk);
24
- }
25
-
26
- child.stderr.on('data', stderrChunkHandler);
27
-
28
- child.on('error', (err: any) => {
29
- if (err && !err.stderr) {
30
- err.stderr = Buffer.concat(errChunks).toString();
31
- }
32
- reject(err);
33
- });
34
-
35
- child.stdout.once('readable', () => {
36
- resolve(child.stdout);
37
- child.stderr.removeListener('data', stderrChunkHandler);
38
- });
39
- });
40
- }
@@ -1,60 +0,0 @@
1
- import { spawn } from 'child_process';
2
- import signalExit = require('signal-exit');
3
-
4
- import { CliTerseError } from '@alwaysai/alwayscli';
5
-
6
- import { Cmd } from '../spawner/types';
7
-
8
- export const PROCESS_EXITED_WITH_NON_ZERO_STATUS_CODE =
9
- 'Process exited with non-zero status code';
10
-
11
- export async function run(cmd: Cmd) {
12
- return new Promise<string>((resolve, reject) => {
13
- const optString = cmd.args ? cmd.args.join(' ') : '';
14
- const child = spawn(cmd.exe, cmd.args || [], { cwd: cmd.cwd });
15
- signalExit(() => {
16
- child.kill();
17
- });
18
-
19
- if (cmd.input) {
20
- cmd.input.pipe(child.stdin);
21
- }
22
-
23
- // TODO: Limit buffer size
24
- const outChunks: Buffer[] = [];
25
- const allChunks: Buffer[] = []; // stderr and stdout
26
-
27
- child.stdout.on('data', chunk => {
28
- outChunks.push(chunk);
29
- allChunks.push(chunk);
30
- });
31
-
32
- child.stderr.on('data', chunk => {
33
- allChunks.push(chunk);
34
- });
35
-
36
- child.on('error', err => {
37
- reject(err);
38
- });
39
-
40
- child.on('close', code => {
41
- if (code === 0) {
42
- const str = Buffer.concat(outChunks).toString('utf8');
43
- if (str.endsWith('\n')) {
44
- resolve(str.slice(0, -1));
45
- } else {
46
- resolve(str);
47
- }
48
- } else if (code !== null) {
49
- let message = `${PROCESS_EXITED_WITH_NON_ZERO_STATUS_CODE} ${code.toString()}`;
50
- message += '\n\n';
51
- message += `$ ${cmd.exe} ${cmd.args ? cmd.args.join(' ') : ''}`;
52
- message += '\n\n';
53
- message += Buffer.concat(allChunks).toString('utf8');
54
- reject(new CliTerseError(message));
55
- } else {
56
- reject(new CliTerseError('Close code is undefined!'));
57
- }
58
- });
59
- });
60
- }