@journeyapps/cloudcode-build-agent 0.0.0-dev.9188dcc → 0.0.0-dev.9aa2c43

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/run.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runCommand = void 0;
4
+ const spawn_stream_1 = require("./spawn-stream");
5
+ async function* runCommand(command, args, options) {
6
+ const { childProcess, stream } = (0, spawn_stream_1.spawnStream)(command, args, { cwd: options?.cwd, splitLines: true, ...options });
7
+ let exitCode = null;
8
+ for await (let event of stream) {
9
+ yield event;
10
+ if (event.exitCode != null) {
11
+ exitCode = event.exitCode;
12
+ }
13
+ }
14
+ if (exitCode != 0) {
15
+ throw new Error(`Command failed with code ${exitCode}`);
16
+ }
17
+ }
18
+ exports.runCommand = runCommand;
19
+ //# sourceMappingURL=run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":";;;AAAA,iDAA+D;AAGxD,KAAK,SAAS,CAAC,CAAC,UAAU,CAC/B,OAAe,EACf,IAAc,EACd,OAA+B;IAE/B,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,IAAA,0BAAW,EAAC,OAAO,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAEjH,IAAI,QAAQ,GAAkB,IAAI,CAAC;IAEnC,IAAI,KAAK,EAAE,IAAI,KAAK,IAAI,MAAM,EAAE;QAC9B,MAAM,KAAK,CAAC;QACZ,IAAI,KAAK,CAAC,QAAQ,IAAI,IAAI,EAAE;YAC1B,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;SAC3B;KACF;IAED,IAAI,QAAQ,IAAI,CAAC,EAAE;QACjB,MAAM,IAAI,KAAK,CAAC,4BAA4B,QAAQ,EAAE,CAAC,CAAC;KACzD;AACH,CAAC;AAnBD,gCAmBC"}
@@ -0,0 +1,15 @@
1
+ /// <reference types="node" />
2
+ import { SpawnOptions } from 'child_process';
3
+ export interface StreamOptions {
4
+ splitLines?: boolean;
5
+ }
6
+ export declare function spawnStream(command: string, args: string[], options: SpawnOptions & StreamOptions): {
7
+ childProcess: import("child_process").ChildProcessWithoutNullStreams;
8
+ stream: AsyncIterable<SpawnStreamEvent>;
9
+ };
10
+ export interface SpawnStreamEvent {
11
+ event: 'stdout' | 'stderr' | 'exit';
12
+ stdout?: string;
13
+ stderr?: string;
14
+ exitCode?: number;
15
+ }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.spawnStream = void 0;
4
+ const child_process_1 = require("child_process");
5
+ const stream_1 = require("stream");
6
+ function spawnStream(command, args, options) {
7
+ const process = (0, child_process_1.spawn)(command, args, {
8
+ ...options,
9
+ stdio: 'pipe'
10
+ });
11
+ return {
12
+ childProcess: process,
13
+ stream: processToStream(process, options)
14
+ };
15
+ }
16
+ exports.spawnStream = spawnStream;
17
+ function processToStream(process, options) {
18
+ const combinedStream = new stream_1.PassThrough({ objectMode: true });
19
+ async function* transform(stream, key) {
20
+ stream.setEncoding('utf-8');
21
+ let iterable = stream;
22
+ if (options.splitLines) {
23
+ iterable = splitLines(iterable);
24
+ }
25
+ for await (const chunk of iterable) {
26
+ yield {
27
+ event: key,
28
+ [key]: chunk
29
+ };
30
+ }
31
+ }
32
+ if (process.stdout) {
33
+ stream_1.Readable.from(transform(process.stdout, 'stdout')).pipe(combinedStream, { end: false });
34
+ }
35
+ if (process.stderr) {
36
+ stream_1.Readable.from(transform(process.stderr, 'stderr')).pipe(combinedStream, { end: false });
37
+ }
38
+ process.on('exit', (code) => {
39
+ combinedStream.write({ exitCode: code }, () => {
40
+ combinedStream.end();
41
+ });
42
+ });
43
+ return combinedStream;
44
+ }
45
+ async function* splitLines(stream) {
46
+ let buffer = '';
47
+ for await (const chunk of stream) {
48
+ buffer += chunk;
49
+ const lines = buffer.split('\n');
50
+ buffer = lines.pop();
51
+ for (let line of lines) {
52
+ yield `${line}\n`;
53
+ }
54
+ }
55
+ if (buffer) {
56
+ yield buffer;
57
+ }
58
+ }
59
+ //# sourceMappingURL=spawn-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spawn-stream.js","sourceRoot":"","sources":["../src/spawn-stream.ts"],"names":[],"mappings":";;;AAAA,iDAAkE;AAClE,mCAA+C;AAM/C,SAAgB,WAAW,CAAC,OAAe,EAAE,IAAc,EAAE,OAAqC;IAChG,MAAM,OAAO,GAAG,IAAA,qBAAK,EAAC,OAAO,EAAE,IAAI,EAAE;QACnC,GAAG,OAAO;QACV,KAAK,EAAE,MAAM;KACd,CAAC,CAAC;IAEH,OAAO;QACL,YAAY,EAAE,OAAO;QACrB,MAAM,EAAE,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC;KAC1C,CAAC;AACJ,CAAC;AAVD,kCAUC;AASD,SAAS,eAAe,CAAC,OAAqB,EAAE,OAAsB;IACpE,MAAM,cAAc,GAAG,IAAI,oBAAW,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;IAE7D,KAAK,SAAS,CAAC,CAAC,SAAS,CAAC,MAAgB,EAAE,GAAwB;QAClE,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QAC5B,IAAI,QAAQ,GAA0B,MAAM,CAAC;QAC7C,IAAI,OAAO,CAAC,UAAU,EAAE;YACtB,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;SACjC;QACD,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,QAAQ,EAAE;YAClC,MAAM;gBACJ,KAAK,EAAE,GAAG;gBACV,CAAC,GAAG,CAAC,EAAE,KAAK;aACb,CAAC;SACH;IACH,CAAC;IAED,IAAI,OAAO,CAAC,MAAM,EAAE;QAClB,iBAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,cAAc,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;KACzF;IACD,IAAI,OAAO,CAAC,MAAM,EAAE;QAClB,iBAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,cAAc,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;KACzF;IAED,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAmB,EAAE,EAAE;QACzC,cAAc,CAAC,KAAK,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE;YAC5C,cAAc,CAAC,GAAG,EAAE,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,cAAc,CAAC;AACxB,CAAC;AAED,KAAK,SAAS,CAAC,CAAC,UAAU,CAAC,MAA6B;IACtD,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE;QAChC,MAAM,IAAI,KAAK,CAAC;QAChB,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACjC,MAAM,GAAG,KAAK,CAAC,GAAG,EAAG,CAAC;QACtB,KAAK,IAAI,IAAI,IAAI,KAAK,EAAE;YACtB,MAAM,GAAG,IAAI,IAAI,CAAC;SACnB;KACF;IACD,IAAI,MAAM,EAAE;QACV,MAAM,MAAM,CAAC;KACd;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@journeyapps/cloudcode-build-agent",
3
- "version": "0.0.0-dev.9188dcc",
3
+ "version": "0.0.0-dev.9aa2c43",
4
4
  "description": "",
5
5
  "main": "./dist/index",
6
6
  "types": "./dist/index",
@@ -9,11 +9,21 @@
9
9
  "cloudcode-build-agent": "./bin.js"
10
10
  },
11
11
  "dependencies": {
12
+ "child-process-promise": "^2.2.1",
12
13
  "fs-jetpack": "5.1.0",
14
+ "lodash": "4.17.21",
15
+ "node-fetch": "<3",
16
+ "semver": "7.3.8",
17
+ "tar": "6.1.13",
13
18
  "yargs": "17.6.2"
14
19
  },
15
20
  "devDependencies": {
16
- "@types/node": "^18.6.3",
21
+ "@types/child-process-promise": "2.2.2",
22
+ "@types/lodash": "4.14.191",
23
+ "@types/node": "14.18.36",
24
+ "@types/node-fetch": "2.6.2",
25
+ "@types/semver": "7.3.13",
26
+ "@types/tar": "6.1.4",
17
27
  "@types/yargs": "17.0.22",
18
28
  "typescript": "4.9.5"
19
29
  },
package/src/builder.ts CHANGED
@@ -1,7 +1,122 @@
1
- import { spawnSync } from 'child_process';
1
+ import * as path from 'node:path';
2
+ import * as jetpack from 'fs-jetpack';
3
+ import * as semver from 'semver';
2
4
 
3
- // Detect tasks in given path and build them.
4
- export async function buildTasks(src: string, dest: string, only?: string) {}
5
+ import { ensureCustomNodeVersion } from './installer';
6
+ import { detectTasks, DetectedTask } from './detect_tasks';
7
+ import { runCommand } from './run';
5
8
 
6
- // Build a single task in the specified subdirectory.
7
- async function buildTask(src: string, dest: string, config: any) {}
9
+ import * as _ from 'lodash';
10
+
11
+ export type GeneralTaskConfig = {
12
+ app_id: string;
13
+ env: string;
14
+ backend_id: string;
15
+ backend_url: string;
16
+ };
17
+
18
+ export type BuildOptions = {
19
+ only?: string;
20
+ custom_node_installation_path?: string;
21
+ };
22
+
23
+ type ToolPaths = { bin_path: string; node_bin: string; npm_bin: string };
24
+
25
+ export async function buildTasks(
26
+ project_path: string,
27
+ dest: string,
28
+ config: GeneralTaskConfig,
29
+ options?: BuildOptions
30
+ ) {
31
+ const tasks = await detectTasks(project_path, options?.only);
32
+
33
+ // Use the version of nodejs that's running this script as the default.
34
+ const default_tool_paths = {
35
+ bin_path: path.dirname(process.execPath),
36
+ node_bin: process.execPath,
37
+ npm_bin: path.join(path.dirname(process.execPath), 'npm')
38
+ };
39
+
40
+ for (const task of tasks) {
41
+ let custom_tool_paths;
42
+ if (task.required_node_version) {
43
+ // FIXME: when defaulting to project_path, the custom node installation is copied into the builder's workdir as well.
44
+ const custom_node_installation_path = options?.custom_node_installation_path ?? project_path;
45
+ const custom_node_path = path.resolve(
46
+ custom_node_installation_path,
47
+ `node-${semver.major(task.required_node_version)}`
48
+ );
49
+ custom_tool_paths = await ensureCustomNodeVersion(task.required_node_version, custom_node_path);
50
+ }
51
+ const node_version = task.required_node_version ?? process.version;
52
+
53
+ await buildTask(task, project_path, dest, config, {
54
+ node_version,
55
+ ...(custom_tool_paths ?? default_tool_paths)
56
+ });
57
+ }
58
+ }
59
+
60
+ export async function buildTask(
61
+ task: DetectedTask,
62
+ project_path: string,
63
+ dest: string,
64
+ config: GeneralTaskConfig,
65
+ node_context: { node_version: string } & ToolPaths
66
+ ) {
67
+ const builder_package = task.builder_package;
68
+ const builder_script = task.builder_script;
69
+ const { bin_path, node_bin, npm_bin } = node_context;
70
+ const builder_bin = path.resolve(bin_path, builder_script);
71
+
72
+ if (!jetpack.exists(node_bin)) {
73
+ throw new Error(`Node binary not found: ${node_bin}`);
74
+ }
75
+
76
+ console.debug(
77
+ `[${task.task_name}] Installing builder script "${builder_package}" for node ${node_context.node_version}`
78
+ );
79
+ // debug(`Installing builder script "${builder_package}" for node ${node_version}`);
80
+
81
+ const stream1 = runCommand(node_bin, [npm_bin, '--global', 'install', builder_package], {
82
+ cwd: project_path,
83
+ env: process.env
84
+ });
85
+ for await (let event of stream1) {
86
+ const log = event.stdout ?? event.stderr;
87
+ if (log) {
88
+ console.log(`[${task.task_name} - install]`, log.trimRight());
89
+ }
90
+ }
91
+
92
+ if (!jetpack.exists(builder_bin)) {
93
+ console.error(`[${task.task_name}] ${builder_bin} not found`);
94
+ throw new Error(`Builder script not found for task ${task.task_name}`);
95
+ }
96
+
97
+ const builder_args: string[] = [];
98
+ // --src ./ --out ./dist/echo --task echo --appId 'appid' --env 'testing' --backendId 'backendid' --backendUrl 'http://run.journeyapps.test'
99
+ builder_args.push(builder_bin);
100
+ builder_args.push('--src', project_path);
101
+ builder_args.push(`--out`, path.join(dest, task.task_name));
102
+ builder_args.push(`--zip`, path.join(dest, `${task.task_name}.zip`));
103
+ builder_args.push(`--task`, `${task.task_name}`);
104
+ builder_args.push(`--appId`, config.app_id);
105
+ builder_args.push(`--env`, config.env);
106
+ builder_args.push(`--backendId`, config.backend_id);
107
+ builder_args.push(`--backendUrl`, config.backend_url);
108
+ // builder_args.push(`--runInstallScripts`, 'true'); // TODO: handle this from feature flags somehow
109
+
110
+ console.debug(`[${task.task_name}] Trying: ${node_bin} ${builder_args.join(' ')}`);
111
+ // debug(`Trying: ${node_bin} ${builder_args.join(' ')}`);
112
+
113
+ const stream2 = runCommand(node_bin, builder_args, { cwd: project_path, env: process.env });
114
+
115
+ for await (let event of stream2) {
116
+ const log = event.stdout ?? event.stderr;
117
+ if (log) {
118
+ console.log(`[${task.task_name} - build]`, log.trimRight());
119
+ }
120
+ }
121
+ // console.debug('----');
122
+ }
package/src/cli.ts CHANGED
@@ -2,40 +2,41 @@
2
2
 
3
3
  import * as yargs from 'yargs';
4
4
 
5
- import { ProcessError, buildTasks, prepareBuildEnvForTasks } from './';
5
+ import { buildTasks } from './';
6
6
 
7
7
  const DEFAULT_SRC_PATH = './';
8
- const DEFAULT_OUT_PATH = '';
8
+ const DEFAULT_OUT_PATH = './dist/cloudcode';
9
+ const DEFAULT_TMP_PATH = '/tmp/cloudcode-build-agent/node';
9
10
 
10
11
  yargs
11
12
  .command(
12
13
  ['build', '$0'], // $0 means this is the default command
13
14
  'build tasks',
14
15
  (yargs) => {
15
- return yargs
16
- .option('only', { string: true })
17
- .option('path', { default: DEFAULT_SRC_PATH })
18
- .option('out', { default: DEFAULT_OUT_PATH });
16
+ return (
17
+ yargs
18
+ .option('only', { string: true })
19
+ .option('path', { default: DEFAULT_SRC_PATH })
20
+ .option('out', { default: DEFAULT_OUT_PATH })
21
+ .option('node_work_dir', { default: DEFAULT_TMP_PATH })
22
+ // TODO: investigate yargs.config() for reading task config blob from a file. But how to ensure required args?
23
+ .option('appId', { string: true, demandOption: true, describe: "CloudCode's reference id" })
24
+ .option('env', { string: true, demandOption: true })
25
+ .option('backendId', { string: true, demandOption: true })
26
+ .option('backendUrl', { string: true, demandOption: true })
27
+ );
19
28
  },
20
29
  handled(async (argv) => {
21
- // iterate over task directories and run: 'yarn cloudcode-build'.
22
- // optionally filtered by `argv.only` to build a single task.
23
-
24
- await buildTasks(argv.path, argv.out, argv.only);
25
- })
26
- )
27
- .command(
28
- 'prepare-env',
29
- 'Install node environments and packages for each task',
30
- (yargs) => {
31
- return yargs.option('only', { string: true }).option('path', { default: DEFAULT_SRC_PATH });
32
- },
33
- handled(async (argv) => {
34
- // iterate over task directories:
35
- // ensure required node version for task CC version is installed - What if too old - error?
36
- // run yarn install
37
-
38
- await prepareBuildEnvForTasks(argv.path, argv.only);
30
+ const config = {
31
+ app_id: argv.appId,
32
+ env: argv.env,
33
+ backend_id: argv.backendId,
34
+ backend_url: argv.backendUrl
35
+ };
36
+ await buildTasks(argv.path, argv.out, config, {
37
+ only: argv.only,
38
+ custom_node_installation_path: argv.node_work_dir
39
+ });
39
40
  })
40
41
  )
41
42
  .option('verbose', {
@@ -48,13 +49,8 @@ function handled<T>(fn: (argv: T) => Promise<void>): (argv: T) => Promise<void>
48
49
  try {
49
50
  await fn(argv);
50
51
  } catch (err) {
51
- if (err instanceof ProcessError) {
52
- console.error(err.message);
53
- process.exit(err.status);
54
- } else {
55
- console.error(err.stack);
56
- process.exit(1);
57
- }
52
+ console.error(err.stack);
53
+ process.exit(1);
58
54
  }
59
55
  };
60
56
  }
package/src/defs.ts ADDED
@@ -0,0 +1,34 @@
1
+ // TODO: maybe publish (some of) this from the CC service?
2
+ export const SUPPORTED_VERSIONS = [
3
+ // {
4
+ // // proposed
5
+ // version: '1.13.0',
6
+ // node: '16.19.1',
7
+ // builder_package: '@journeyapps/cloudcode-build@1.13.0',
8
+ // builder_script: 'cloudcode-build'
9
+ // },
10
+ {
11
+ version: '1.12.0',
12
+ node: '16.19.1', // FIXME: maybe the very specific node version here is too brittle? Should we just specify the major version?
13
+ builder_package: '@journeyapps/cloudcode-build-legacy@1.12.0',
14
+ builder_script: 'cloudcode-build-legacy'
15
+ },
16
+ {
17
+ version: '1.11.2',
18
+ node: '14.21.3',
19
+ builder_package: '@journeyapps/cloudcode-build-legacy@1.12.0',
20
+ builder_script: 'cloudcode-build-legacy'
21
+ },
22
+ {
23
+ version: '1.11.1',
24
+ node: '14.21.3',
25
+ builder_package: '@journeyapps/cloudcode-build-legacy@1.12.0',
26
+ builder_script: 'cloudcode-build-legacy'
27
+ },
28
+ {
29
+ version: '1.11.0',
30
+ node: '14.21.3',
31
+ builder_package: '@journeyapps/cloudcode-build-legacy@1.12.0',
32
+ builder_script: 'cloudcode-build-legacy'
33
+ }
34
+ ];
@@ -0,0 +1,83 @@
1
+ import * as fs from 'node:fs';
2
+ import * as path from 'node:path';
3
+ import * as jetpack from 'fs-jetpack';
4
+ import * as semver from 'semver';
5
+ import * as defs from './defs';
6
+
7
+ // TODO: validations for cloudcode specific keys and structure?
8
+ async function readPackage(path: string) {
9
+ try {
10
+ const content = await fs.promises.readFile(path, { encoding: 'utf-8' });
11
+ return JSON.parse(content);
12
+ } catch (err) {
13
+ console.error(`ERROR: ${err}`);
14
+ throw err;
15
+ // todo: check for enoent and skip?
16
+ // todo: if json error, throw a CC build error?
17
+ }
18
+ }
19
+
20
+ export type DetectedTask = {
21
+ pkg_path: string;
22
+ task_name: string;
23
+ task_version: string;
24
+ required_node_version?: string;
25
+ builder_package: string;
26
+ builder_script: string;
27
+ };
28
+
29
+ export async function detectTasks(project_path: string, only?: string): Promise<DetectedTask[]> {
30
+ const package_files = await jetpack.findAsync(path.join(project_path, 'cloudcode'), { matching: '**/package.json' });
31
+ const filtered_package_files = package_files.filter((pkg_path) => {
32
+ // FIXME: this is kinda clunky.
33
+ const pm = /^\/?cloudcode\/([^\/]+)\/package\.json$/.exec(pkg_path);
34
+ if (!pm) {
35
+ return false;
36
+ }
37
+ const taskName = pm[1];
38
+
39
+ if (only != null && only != taskName) {
40
+ // !(only.indexOf(taskName) >= 0) // TODO: support a specific list of tasks to build?
41
+ return false;
42
+ }
43
+ return true;
44
+ });
45
+
46
+ return Promise.all(
47
+ filtered_package_files.map(async (pkg_path) => {
48
+ const task_package = await readPackage(pkg_path);
49
+
50
+ const task_name = task_package.name; // CAVEAT: the pkg name _must_ match the dir.
51
+ const task_version = task_package?.cloudcode?.runtime;
52
+ // FIXME: Do we want to filter out disabled tasks from the build process?
53
+
54
+ console.debug(`Detected task version ${task_version}`);
55
+
56
+ const matching = defs.SUPPORTED_VERSIONS.find((v) => {
57
+ return semver.satisfies(v.version, task_version);
58
+ });
59
+ if (!matching) {
60
+ throw new Error('FIXME: unsupported version');
61
+ }
62
+
63
+ console.debug(`Matching versions: ${JSON.stringify(matching)}`);
64
+
65
+ const running_node_version = process.versions.node;
66
+ let required_node_version;
67
+ // if (matching?.node && semver.major(matching.node) !== semver.major(running_node_version)) {
68
+ if (matching?.node && semver.parse(matching.node) !== semver.parse(running_node_version)) {
69
+ console.debug(`Task requires different node version: v${matching.node}`);
70
+
71
+ required_node_version = matching.node;
72
+ }
73
+ return {
74
+ pkg_path,
75
+ task_name,
76
+ task_version,
77
+ required_node_version,
78
+ builder_package: matching.builder_package,
79
+ builder_script: matching.builder_script
80
+ };
81
+ })
82
+ );
83
+ }
package/src/index.ts CHANGED
@@ -1,3 +1 @@
1
- export * from './errors';
2
1
  export * from './builder';
3
- export * from './installer';
package/src/installer.ts CHANGED
@@ -1,68 +1,117 @@
1
- import { spawnSync } from 'child_process';
2
- import * as fs from 'node:fs/promises';
3
- import * as path from 'node:path';
4
1
  import * as jetpack from 'fs-jetpack';
2
+ import * as path from 'node:path';
3
+ import * as URL from 'node:url';
4
+ import fetch from 'node-fetch';
5
+ import * as fs from 'node:fs';
6
+ import * as os from 'node:os';
7
+ import * as tar from 'tar';
5
8
 
6
- const SUPPORTED_VERSIONS = [
7
- {
8
- version: '1.12.0',
9
- node: '16'
10
- },
11
- {
12
- version: '1.11.2',
13
- node: '14'
14
- },
15
- {
16
- version: '1.11.1',
17
- node: '14'
18
- },
19
- {
20
- version: '1.11.0',
21
- node: '14'
9
+ export async function ensureCustomNodeVersion(node_version: string, install_path: string) {
10
+ if (!jetpack.exists(path.join(install_path, 'bin/node'))) {
11
+ console.debug(`[node ${node_version}] Installing to ${install_path}`);
12
+ await jetpack.dirAsync(install_path);
13
+ await downloadAndInstallNode(node_version, install_path);
14
+ } else {
15
+ console.debug(`[node ${node_version}] Already installed in ${install_path}`);
22
16
  }
23
- ];
24
-
25
- export async function prepareBuildEnvForTasks(project_path: string, only?: string) {
26
- // const subdirs = (await fs.readdir(project_path, { withFileTypes: true }))
27
- // .filter((dirent) => dirent.isDirectory())
28
- // .map((d) => d.name);
29
-
30
- // for (const dir of subdirs) {
31
- // try {
32
- // } catch (err) {
33
- // console.error(`ERROR: ${err}`)
34
- // // todo: check for enoent and skip
35
- // // todo: if json error, throw a CC build error
36
- // }
37
- // }
38
-
39
- const package_files = await jetpack.findAsync(path.join(project_path, 'cloudcode'), { matching: '**/package.json' });
40
-
41
- for (const pkg_path of package_files) {
42
- console.log(pkg_path);
43
- const pm = /^\/?cloudcode\/([^\/]+)\/package\.json$/.exec(pkg_path);
44
- if (!pm) {
45
- continue;
46
- }
47
- const taskName = pm[1];
17
+ return nodePaths(install_path);
18
+ }
48
19
 
49
- if (only != null && only != taskName) {
50
- // !(only.indexOf(taskName) >= 0)
51
- continue;
52
- }
20
+ /* Basically this, but for different dirs.
21
+ curl https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${PLATFORM}64.tar.gz -L | tar -xzC /node-dedicated && \
22
+ mv /node-dedicated/node-v${NODE_VERSION}-linux-${PLATFORM}64/bin/node /node-dedicated/node
23
+ */
24
+ // TODO: feature to find the latest minor and patch for a given major version?
25
+ export async function downloadAndInstallNode(node_version: string, destination: string) {
26
+ // eg. https://nodejs.org/dist/v18.14.2/node-v18.14.2-linux-x64.tar.xz
27
+ const ARCH = os.arch();
28
+ const PLATFORM = os.platform();
29
+ const node_download_url = `https://nodejs.org/dist/v${node_version}/node-v${node_version}-${PLATFORM}-${ARCH}.tar.gz`;
53
30
 
54
- try {
55
- const content = await fs.readFile(pkg_path, { encoding: 'utf-8' });
56
- const task_package = JSON.parse(content);
57
- const task_version = task_package?.cloudcode?.runtime;
58
-
59
- // check task version against supported versions, install relevant node version and yarn
60
- // cwd into directory and run the correct node's yarn.
61
- console.log(`Detected task version ${task_version}`);
62
- } catch (err) {
63
- console.error(`ERROR: ${err}`);
64
- // todo: check for enoent and skip
65
- // todo: if json error, throw a CC build error
66
- }
31
+ await downloadAndExtractTarball(node_download_url, destination);
32
+ return nodePaths(destination);
33
+ }
34
+
35
+ export function nodePaths(destination: string) {
36
+ return {
37
+ bin_path: path.resolve(destination, 'bin'),
38
+ node_bin: path.resolve(destination, 'bin', 'node'),
39
+ npm_bin: path.resolve(destination, 'bin', 'npm')
40
+ };
41
+ }
42
+
43
+ export async function downloadAndExtractTarball(url: string, dest: string) {
44
+ const tmpdir = os.tmpdir();
45
+
46
+ const filename = path.basename(URL.parse(url).pathname as string);
47
+ const base = path.basename(filename, '.tar.gz');
48
+ const download = path.join(tmpdir, filename);
49
+
50
+ if (fs.existsSync(download)) {
51
+ console.debug(`deleting old ${download}`);
52
+ fs.unlinkSync(download);
67
53
  }
54
+
55
+ await new Promise(async (resolve, reject) => {
56
+ console.log(`fetching ${url} into ${download}`);
57
+
58
+ const response = await fetch(url);
59
+ if (!response.ok) {
60
+ const errorBody = await response.statusText;
61
+ throw new Error(`Failed to download: ${errorBody}`);
62
+ }
63
+
64
+ const file = fs.createWriteStream(download); // Sink the download stream into a file, so it can be extracted.
65
+ response.body.pipe(file);
66
+
67
+ file.on('finish', async () => {
68
+ try {
69
+ console.debug('Extracting...', download);
70
+
71
+ tar.extract({
72
+ cwd: tmpdir,
73
+ file: download,
74
+ sync: true
75
+ });
76
+
77
+ const uncomp = path.join(tmpdir, base);
78
+
79
+ const dist = path.resolve(dest);
80
+ // FIXME: dangerous. Add protection or replace.
81
+ // if (fs.existsSync(dist)) {
82
+ // fs.rmSync(dist, { recursive: true });
83
+ // }
84
+ if (fs.existsSync(uncomp)) {
85
+ console.debug(`Moving extracted files from ${uncomp} to ${dist}`);
86
+ fs.renameSync(uncomp, dist);
87
+ } else {
88
+ console.debug(`Can't find extract dir ${uncomp}`);
89
+ }
90
+
91
+ /*
92
+ FIXME: this seems to sometimes cause errors: eg.
93
+ ```
94
+ node:events:505
95
+ throw er; // Unhandled 'error' event
96
+ ^
97
+
98
+ Error: ENOENT: no such file or directory, open '/var/folders/kc/h6m4zpmd23v13s63mygvkslm0000gn/T/node-v16.19.1-darwin-arm64.tar.gz'
99
+ Emitted 'error' event on ReadStream instance at:
100
+ at emitErrorNT (node:internal/streams/destroy:157:8)
101
+ at emitErrorCloseNT (node:internal/streams/destroy:122:3)
102
+ at processTicksAndRejections (node:internal/process/task_queues:83:21) {
103
+ errno: -2,
104
+ code: 'ENOENT',
105
+ syscall: 'open',
106
+ path: '/var/folders/kc/h6m4zpmd23v13s63mygvkslm0000gn/T/node-v16.19.1-darwin-arm64.tar.gz'
107
+ ```
108
+ */
109
+ // fs.unlinkSync(file.path);
110
+
111
+ resolve(null);
112
+ } catch (err) {
113
+ reject(err);
114
+ }
115
+ });
116
+ });
68
117
  }
package/src/run.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { spawnStream, SpawnStreamEvent } from './spawn-stream';
2
+ import { SpawnOptions } from 'child_process';
3
+
4
+ export async function* runCommand(
5
+ command: string,
6
+ args: string[],
7
+ options?: Partial<SpawnOptions>
8
+ ): AsyncIterable<SpawnStreamEvent> {
9
+ const { childProcess, stream } = spawnStream(command, args, { cwd: options?.cwd, splitLines: true, ...options });
10
+
11
+ let exitCode: number | null = null;
12
+
13
+ for await (let event of stream) {
14
+ yield event;
15
+ if (event.exitCode != null) {
16
+ exitCode = event.exitCode;
17
+ }
18
+ }
19
+
20
+ if (exitCode != 0) {
21
+ throw new Error(`Command failed with code ${exitCode}`);
22
+ }
23
+ }