@enspirit/emb 0.2.0 → 0.3.1

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 (27) hide show
  1. package/README.md +1 -1
  2. package/dist/src/cli/commands/components/shell.js +3 -3
  3. package/dist/src/cli/commands/tasks/index.js +21 -9
  4. package/dist/src/docker/compose/operations/ComposeExecOperation.d.ts +13 -0
  5. package/dist/src/docker/compose/operations/ComposeExecOperation.js +59 -0
  6. package/dist/src/docker/{operations/containers/ExecShellOperation.d.ts → compose/operations/ComposeExecShellOperation.d.ts} +2 -2
  7. package/dist/src/docker/{operations/containers/ExecShellOperation.js → compose/operations/ComposeExecShellOperation.js} +9 -23
  8. package/dist/src/docker/compose/operations/index.d.ts +2 -0
  9. package/dist/src/docker/compose/operations/index.js +2 -0
  10. package/dist/src/docker/operations/containers/index.d.ts +1 -2
  11. package/dist/src/docker/operations/containers/index.js +1 -2
  12. package/dist/src/docker/operations/images/BuildImageOperation.d.ts +3 -2
  13. package/dist/src/docker/operations/images/BuildImageOperation.js +6 -4
  14. package/dist/src/docker/resources/DockerImageResource.js +72 -48
  15. package/dist/src/errors.d.ts +7 -2
  16. package/dist/src/errors.js +12 -3
  17. package/dist/src/monorepo/operations/resources/BuildResourcesOperation.d.ts +1 -0
  18. package/dist/src/monorepo/operations/resources/BuildResourcesOperation.js +31 -13
  19. package/dist/src/monorepo/operations/tasks/RunTasksOperation.js +6 -7
  20. package/dist/src/monorepo/resources/FileResource.js +10 -6
  21. package/dist/src/monorepo/resources/ResourceFactory.d.ts +13 -2
  22. package/dist/src/monorepo/store/index.d.ts +1 -0
  23. package/dist/src/monorepo/store/index.js +12 -1
  24. package/oclif.manifest.json +117 -117
  25. package/package.json +2 -1
  26. /package/dist/src/docker/operations/containers/{ExecContainerOperation.d.ts → ContainerExecOperation.d.ts} +0 -0
  27. /package/dist/src/docker/operations/containers/{ExecContainerOperation.js → ContainerExecOperation.js} +0 -0
package/README.md CHANGED
@@ -14,7 +14,7 @@ $ npm install -g @enspirit/emb
14
14
  $ emb COMMAND
15
15
  running command...
16
16
  $ emb (--version)
17
- @enspirit/emb/0.2.0 darwin-x64 node-v22.12.0
17
+ @enspirit/emb/0.3.1 darwin-x64 node-v22.12.0
18
18
  $ emb --help [COMMAND]
19
19
  USAGE
20
20
  $ emb COMMAND
@@ -1,6 +1,6 @@
1
1
  import { Args, Flags } from '@oclif/core';
2
2
  import { FlavoredCommand, getContext } from '../../index.js';
3
- import { ExecShellOperation } from '../../../docker/index.js';
3
+ import { ComposeExecShellOperation } from '../../../docker/index.js';
4
4
  import { ShellExitError } from '../../../errors.js';
5
5
  export default class ComponentsLogs extends FlavoredCommand {
6
6
  static aliases = ['shell'];
@@ -26,8 +26,8 @@ export default class ComponentsLogs extends FlavoredCommand {
26
26
  const { flags, args } = await this.parse(ComponentsLogs);
27
27
  const { monorepo } = await getContext();
28
28
  try {
29
- await monorepo.run(new ExecShellOperation(), {
30
- component: args.component,
29
+ await monorepo.run(new ComposeExecShellOperation(), {
30
+ service: args.component,
31
31
  shell: flags.shell,
32
32
  });
33
33
  }
@@ -9,19 +9,31 @@ export default class TasksIndex extends BaseCommand {
9
9
  async run() {
10
10
  const { flags } = await this.parse(TasksIndex);
11
11
  const { monorepo: { tasks }, } = await getContext();
12
+ const sortedTasks = tasks.toSorted((a, b) => {
13
+ const ac = a.component;
14
+ const bc = b.component;
15
+ // Put null/undefined first
16
+ if (!ac && bc) {
17
+ return -1;
18
+ }
19
+ if (Boolean(ac) && !bc) {
20
+ return 1;
21
+ }
22
+ // Compare components (if both not null)
23
+ if (Boolean(ac) && Boolean(bc)) {
24
+ const cmp = ac.localeCompare(bc);
25
+ if (cmp !== 0) {
26
+ return cmp;
27
+ }
28
+ }
29
+ // Compare names as fallback
30
+ return a.name.localeCompare(b.name);
31
+ });
12
32
  if (!flags.json) {
13
33
  printTable({
14
34
  ...TABLE_DEFAULTS,
15
35
  columns: ['name', 'component', 'description', 'id'],
16
- data: tasks.toSorted((a, b) => {
17
- if (a.component === b.component) {
18
- return a.name < b.name ? -1 : 1;
19
- }
20
- if (!a.component && b.component) {
21
- return -1;
22
- }
23
- return 0;
24
- }),
36
+ data: sortedTasks,
25
37
  });
26
38
  }
27
39
  return tasks;
@@ -0,0 +1,13 @@
1
+ import { Writable } from 'node:stream';
2
+ import * as z from 'zod';
3
+ import { AbstractOperation } from '../../../operations/index.js';
4
+ declare const schema: z.ZodObject<{
5
+ command: z.ZodString;
6
+ service: z.ZodString;
7
+ }, z.core.$strip>;
8
+ export declare class ComposeExecOperation extends AbstractOperation<typeof schema, void> {
9
+ protected out?: Writable | undefined;
10
+ constructor(out?: Writable | undefined);
11
+ protected _run(input: z.input<typeof schema>): Promise<void>;
12
+ }
13
+ export {};
@@ -0,0 +1,59 @@
1
+ import { spawn } from 'node:child_process';
2
+ import * as z from 'zod';
3
+ import { ComposeExecError } from '../../../errors.js';
4
+ import { AbstractOperation } from '../../../operations/index.js';
5
+ const schema = z.object({
6
+ command: z.string().describe('The command to execute on the service'),
7
+ service: z
8
+ .string()
9
+ .describe('The name of the compose service to exec a shell'),
10
+ });
11
+ export class ComposeExecOperation extends AbstractOperation {
12
+ out;
13
+ constructor(out) {
14
+ super(schema);
15
+ this.out = out;
16
+ }
17
+ async _run(input) {
18
+ const { monorepo } = this.context;
19
+ const cmd = 'docker';
20
+ const args = ['compose', 'exec', input.service, input.command];
21
+ const child = spawn(cmd, args, {
22
+ stdio: 'pipe',
23
+ shell: true,
24
+ cwd: monorepo.rootDir,
25
+ });
26
+ if (this.out) {
27
+ child.stderr.pipe(this.out);
28
+ child.stdout.pipe(this.out);
29
+ }
30
+ const forward = (sig) => {
31
+ try {
32
+ child.kill(sig);
33
+ }
34
+ catch { }
35
+ };
36
+ const signals = [
37
+ 'SIGINT',
38
+ 'SIGTERM',
39
+ 'SIGHUP',
40
+ 'SIGQUIT',
41
+ ];
42
+ signals.forEach((sig) => {
43
+ process.on(sig, () => forward(sig));
44
+ });
45
+ return new Promise((resolve, reject) => {
46
+ child.on('error', (err) => {
47
+ reject(new Error(`Failed to execute docker compose: ${err.message}`));
48
+ });
49
+ child.on('exit', (code, signal) => {
50
+ if (code !== null && code !== 0) {
51
+ reject(new ComposeExecError(`The shell exited unexpectedly. ${code}`, code, signal));
52
+ }
53
+ else {
54
+ resolve();
55
+ }
56
+ });
57
+ });
58
+ }
59
+ }
@@ -2,9 +2,9 @@ import * as z from 'zod';
2
2
  import { AbstractOperation } from '../../../operations/index.js';
3
3
  declare const schema: z.ZodObject<{
4
4
  shell: z.ZodOptional<z.ZodDefault<z.ZodString>>;
5
- component: z.ZodString;
5
+ service: z.ZodString;
6
6
  }, z.core.$strip>;
7
- export declare class ExecShellOperation extends AbstractOperation<typeof schema, void> {
7
+ export declare class ComposeExecShellOperation extends AbstractOperation<typeof schema, void> {
8
8
  constructor();
9
9
  protected _run(input: z.input<typeof schema>): Promise<void>;
10
10
  }
@@ -1,38 +1,24 @@
1
- import { getContext, ListContainersOperation, MultipleContainersFoundError, NoContainerFoundError, ShellExitError, } from '../../../index.js';
2
1
  import { spawn } from 'node:child_process';
3
2
  import * as z from 'zod';
3
+ import { ShellExitError } from '../../../errors.js';
4
4
  import { AbstractOperation } from '../../../operations/index.js';
5
5
  const schema = z.object({
6
6
  shell: z.string().default('bash').optional(),
7
- component: z
7
+ service: z
8
8
  .string()
9
- .describe('The name of the component on which to run a shell'),
9
+ .describe('The name of the compose service to exec a shell'),
10
10
  });
11
- export class ExecShellOperation extends AbstractOperation {
11
+ export class ComposeExecShellOperation extends AbstractOperation {
12
12
  constructor() {
13
13
  super(schema);
14
14
  }
15
15
  async _run(input) {
16
- const { monorepo } = getContext();
17
- const containers = await monorepo.run(new ListContainersOperation(), {
18
- filters: {
19
- label: [
20
- `emb/project=${monorepo.name}`,
21
- `emb/component=${input.component}`,
22
- `emb/flavor=${monorepo.currentFlavor}`,
23
- ],
24
- },
25
- });
26
- if (containers.length === 0) {
27
- throw new NoContainerFoundError(`No container found for component \`${input.component}\``, input.component);
28
- }
29
- if (containers.length > 1) {
30
- throw new MultipleContainersFoundError(`More than one container found for component \`${input.component}\``, input.component);
31
- }
16
+ const { monorepo } = this.context;
32
17
  const cmd = 'docker';
33
- const args = ['exec', '-it', containers[0].Id, input?.shell || 'bash'];
18
+ const args = ['compose', 'exec', input.service, input.shell || 'bash'];
34
19
  const child = spawn(cmd, args, {
35
20
  stdio: 'inherit',
21
+ cwd: monorepo.rootDir,
36
22
  env: {
37
23
  ...process.env,
38
24
  DOCKER_CLI_HINTS: 'false',
@@ -55,11 +41,11 @@ export class ExecShellOperation extends AbstractOperation {
55
41
  });
56
42
  return new Promise((resolve, reject) => {
57
43
  child.on('error', (err) => {
58
- reject(new Error(`Failed to exeucte docker: ${err.message}`));
44
+ reject(new Error(`Failed to execute docker: ${err.message}`));
59
45
  });
60
46
  child.on('exit', (code, signal) => {
61
47
  if (code !== null && code !== 0) {
62
- reject(new ShellExitError(`The shell exited unexpectedly. ${code}`, input.component, code, signal));
48
+ reject(new ShellExitError(`The shell exited unexpectedly. ${code}`, input.service, code, signal));
63
49
  }
64
50
  else {
65
51
  resolve();
@@ -1,2 +1,4 @@
1
1
  export * from './ComposeDownOperation.js';
2
+ export * from './ComposeExecOperation.js';
3
+ export * from './ComposeExecShellOperation.js';
2
4
  export * from './ComposeUpOperation.js';
@@ -1,2 +1,4 @@
1
1
  export * from './ComposeDownOperation.js';
2
+ export * from './ComposeExecOperation.js';
3
+ export * from './ComposeExecShellOperation.js';
2
4
  export * from './ComposeUpOperation.js';
@@ -1,4 +1,3 @@
1
- export * from './ExecContainerOperation.js';
2
- export * from './ExecShellOperation.js';
1
+ export * from './ContainerExecOperation.js';
3
2
  export * from './ListContainersOperation.js';
4
3
  export * from './PruneContainersOperation.js';
@@ -1,4 +1,3 @@
1
- export * from './ExecContainerOperation.js';
2
- export * from './ExecShellOperation.js';
1
+ export * from './ContainerExecOperation.js';
3
2
  export * from './ListContainersOperation.js';
4
3
  export * from './PruneContainersOperation.js';
@@ -1,3 +1,4 @@
1
+ import { Writable } from 'node:stream';
1
2
  import * as z from 'zod';
2
3
  import { AbstractOperation } from '../../../operations/index.js';
3
4
  /**
@@ -13,7 +14,7 @@ export declare const BuildImageOperationInputSchema: z.ZodObject<{
13
14
  target: z.ZodOptional<z.ZodString>;
14
15
  }, z.core.$strip>;
15
16
  export declare class BuildImageOperation extends AbstractOperation<typeof BuildImageOperationInputSchema, Array<unknown>> {
16
- private observer?;
17
- constructor(observer?: ((progress: string) => void) | undefined);
17
+ private out?;
18
+ constructor(out?: Writable | undefined);
18
19
  protected _run(input: z.input<typeof BuildImageOperationInputSchema>): Promise<Array<unknown>>;
19
20
  }
@@ -30,13 +30,14 @@ export const BuildImageOperationInputSchema = z.object({
30
30
  target: z.string().optional().describe('Target build stage'),
31
31
  });
32
32
  export class BuildImageOperation extends AbstractOperation {
33
- observer;
34
- constructor(observer) {
33
+ out;
34
+ constructor(out) {
35
35
  super(BuildImageOperationInputSchema);
36
- this.observer = observer;
36
+ this.out = out;
37
37
  }
38
38
  async _run(input) {
39
39
  const logStream = await this.context.monorepo.store.createWriteStream(`logs/docker/build/${input.tag}.log`);
40
+ this.out?.write('Sending build context to Docker\n');
40
41
  const stream = await this.context.docker.buildImage({
41
42
  context: input.context,
42
43
  src: [...input.src],
@@ -48,6 +49,7 @@ export class BuildImageOperation extends AbstractOperation {
48
49
  target: input.target,
49
50
  version: '2',
50
51
  });
52
+ this.out?.write('Starting build\n');
51
53
  return new Promise((resolve, reject) => {
52
54
  this.context.docker.modem.followProgress(stream, (err, traces) => {
53
55
  logStream.close();
@@ -62,7 +64,7 @@ export class BuildImageOperation extends AbstractOperation {
62
64
  const { vertexes } = await decodeBuildkitStatusResponse(trace.aux);
63
65
  vertexes.forEach((v) => {
64
66
  // logStream.write(JSON.stringify(v) + '\n');
65
- this.observer?.(v.name);
67
+ this.out?.write(v.name + '\n');
66
68
  });
67
69
  }
68
70
  catch (error) {
@@ -1,42 +1,79 @@
1
+ import { fdir as Fdir } from 'fdir';
1
2
  import { stat, statfs } from 'node:fs/promises';
2
3
  import { join } from 'node:path';
3
4
  import pMap from 'p-map';
4
5
  import { GitPrerequisitePlugin } from '../../prerequisites/index.js';
5
6
  import { ResourceFactory, } from '../../monorepo/resources/ResourceFactory.js';
6
7
  import { BuildImageOperation } from '../operations/index.js';
7
- const DockerImageOpFactory = async ({ config, component, monorepo }) => {
8
- const fromConfig = (config.params || {});
9
- const context = fromConfig.context
10
- ? fromConfig.context[0] === '/'
11
- ? monorepo.join(fromConfig.context)
12
- : component.join(fromConfig.context)
13
- : monorepo.join(component.rootDir);
14
- // Ensure the folder exists
15
- await statfs(context);
16
- const plugin = new GitPrerequisitePlugin();
17
- const sources = await plugin.collect(context);
18
- const imageName = [monorepo.name, fromConfig.tag || component.name].join('/');
19
- const tagName = fromConfig.tag || monorepo.defaults.docker?.tag || 'latest';
20
- const buildParams = {
21
- context,
22
- dockerfile: fromConfig.dockerfile || 'Dockerfile',
23
- src: sources.map((s) => s.path),
24
- buildArgs: await monorepo.expand({
25
- ...monorepo.defaults.docker?.buildArgs,
26
- ...fromConfig.buildArgs,
27
- }),
28
- tag: await monorepo.expand(`${imageName}:${tagName}`),
29
- labels: await monorepo.expand({
30
- ...fromConfig.labels,
31
- 'emb/project': monorepo.name,
32
- 'emb/component': component.name,
33
- 'emb/flavor': monorepo.currentFlavor,
34
- }),
35
- target: fromConfig.target,
36
- };
37
- const lastUpdatedInfo = async (sources) => {
8
+ class DockerImageResourceBuilder {
9
+ buildContext;
10
+ context;
11
+ constructor(buildContext) {
12
+ this.buildContext = buildContext;
13
+ this.context = this.config.context
14
+ ? this.config.context[0] === '/'
15
+ ? buildContext.monorepo.join(this.config.context)
16
+ : buildContext.component.join(this.config.context)
17
+ : buildContext.monorepo.join(buildContext.component.rootDir);
18
+ }
19
+ get monorepo() {
20
+ return this.buildContext.monorepo;
21
+ }
22
+ get config() {
23
+ return (this.buildContext.config.params || {});
24
+ }
25
+ get component() {
26
+ return this.buildContext.component;
27
+ }
28
+ async build(out) {
29
+ // Ensure the folder exists
30
+ await statfs(this.context);
31
+ const imageName = [
32
+ this.monorepo.name,
33
+ this.config.tag || this.component.name,
34
+ ].join('/');
35
+ const tagName = this.config.tag || this.monorepo.defaults.docker?.tag || 'latest';
36
+ const crawler = new Fdir();
37
+ const sources = await crawler
38
+ .withRelativePaths()
39
+ .crawl(this.context)
40
+ .withPromise();
41
+ const buildParams = {
42
+ context: this.context,
43
+ dockerfile: this.config.dockerfile || 'Dockerfile',
44
+ src: sources,
45
+ buildArgs: await this.monorepo.expand({
46
+ ...this.monorepo.defaults.docker?.buildArgs,
47
+ ...this.config.buildArgs,
48
+ }),
49
+ tag: await this.monorepo.expand(`${imageName}:${tagName}`),
50
+ labels: await this.monorepo.expand({
51
+ ...this.config.labels,
52
+ 'emb/project': this.monorepo.name,
53
+ 'emb/component': this.component.name,
54
+ 'emb/flavor': this.monorepo.currentFlavor,
55
+ }),
56
+ target: this.config.target,
57
+ };
58
+ return {
59
+ input: buildParams,
60
+ operation: new BuildImageOperation(out),
61
+ };
62
+ }
63
+ async mustBuild(sentinel) {
64
+ const plugin = new GitPrerequisitePlugin();
65
+ const sources = await plugin.collect(this.context);
66
+ const lastUpdated = await this.lastUpdatedInfo(sources);
67
+ if (!sentinel) {
68
+ return lastUpdated;
69
+ }
70
+ return lastUpdated && lastUpdated.time.getTime() > sentinel.mtime
71
+ ? lastUpdated
72
+ : undefined;
73
+ }
74
+ async lastUpdatedInfo(sources) {
38
75
  const stats = await pMap(sources, async (s) => {
39
- const stats = await stat(join(context, s.path));
76
+ const stats = await stat(join(this.context, s.path));
40
77
  return {
41
78
  time: stats.mtime,
42
79
  path: s.path,
@@ -48,20 +85,7 @@ const DockerImageOpFactory = async ({ config, component, monorepo }) => {
48
85
  return stats.reduce((last, entry) => {
49
86
  return last.time > entry.time ? last : entry;
50
87
  }, stats[0]);
51
- };
52
- return {
53
- async mustBuild(sentinel) {
54
- const lastUpdated = await lastUpdatedInfo(sources);
55
- if (!sentinel) {
56
- return lastUpdated;
57
- }
58
- return lastUpdated && lastUpdated.time.getTime() > sentinel.mtime
59
- ? lastUpdated
60
- : undefined;
61
- },
62
- input: buildParams,
63
- operation: new BuildImageOperation(),
64
- };
65
- };
88
+ }
89
+ }
66
90
  // Bring better abstraction and register as part of the plugin initialization
67
- ResourceFactory.register('docker/image', DockerImageOpFactory);
91
+ ResourceFactory.register('docker/image', async (context) => new DockerImageResourceBuilder(context));
@@ -59,10 +59,10 @@ export declare class CircularDependencyError extends EMBError {
59
59
  constructor(msg: string, deps: Array<Array<string>>);
60
60
  }
61
61
  export declare class ShellExitError extends EMBError {
62
- component: string;
62
+ service: string;
63
63
  exitCode: number;
64
64
  signal?: (NodeJS.Signals | null) | undefined;
65
- constructor(msg: string, component: string, exitCode: number, signal?: (NodeJS.Signals | null) | undefined);
65
+ constructor(msg: string, service: string, exitCode: number, signal?: (NodeJS.Signals | null) | undefined);
66
66
  }
67
67
  export declare class NoContainerFoundError extends EMBError {
68
68
  component: string;
@@ -72,3 +72,8 @@ export declare class MultipleContainersFoundError extends EMBError {
72
72
  component: string;
73
73
  constructor(msg: string, component: string);
74
74
  }
75
+ export declare class ComposeExecError extends EMBError {
76
+ exitCode: number;
77
+ signal?: (NodeJS.Signals | null) | undefined;
78
+ constructor(msg: string, exitCode: number, signal?: (NodeJS.Signals | null) | undefined);
79
+ }
@@ -71,12 +71,12 @@ export class CircularDependencyError extends EMBError {
71
71
  }
72
72
  }
73
73
  export class ShellExitError extends EMBError {
74
- component;
74
+ service;
75
75
  exitCode;
76
76
  signal;
77
- constructor(msg, component, exitCode, signal) {
77
+ constructor(msg, service, exitCode, signal) {
78
78
  super('SHELL_EXIT_ERR', msg);
79
- this.component = component;
79
+ this.service = service;
80
80
  this.exitCode = exitCode;
81
81
  this.signal = signal;
82
82
  }
@@ -95,3 +95,12 @@ export class MultipleContainersFoundError extends EMBError {
95
95
  this.component = component;
96
96
  }
97
97
  }
98
+ export class ComposeExecError extends EMBError {
99
+ exitCode;
100
+ signal;
101
+ constructor(msg, exitCode, signal) {
102
+ super('COMPOSE_EXEC_ERR', msg);
103
+ this.exitCode = exitCode;
104
+ this.signal = signal;
105
+ }
106
+ }
@@ -7,6 +7,7 @@ export type BuildResourceMeta = {
7
7
  force?: boolean;
8
8
  resource?: ResourceInfo;
9
9
  builder?: ResourceBuilderInfo<unknown, unknown>;
10
+ builderInput?: unknown;
10
11
  sentinelData?: unknown;
11
12
  cacheHit?: boolean;
12
13
  };
@@ -1,3 +1,4 @@
1
+ import { PRESET_TIMER, } from 'listr2';
1
2
  import * as z from 'zod';
2
3
  import { EMBCollection, findRunOrder, taskManagerFactory, } from '../../index.js';
3
4
  import { ResourceFactory, } from '../../resources/ResourceFactory.js';
@@ -63,6 +64,9 @@ export class BuildResourcesOperation extends AbstractOperation {
63
64
  rendererOptions: {
64
65
  collapseSkips: true,
65
66
  collapseSubtasks: true,
67
+ timer: {
68
+ ...PRESET_TIMER,
69
+ },
66
70
  },
67
71
  ctx: {},
68
72
  });
@@ -84,35 +88,42 @@ export class BuildResourcesOperation extends AbstractOperation {
84
88
  },
85
89
  // Actual build
86
90
  {
87
- title: `Build ${resource.id}`,
91
+ title: `Checking cache for ${resource.id}`,
88
92
  /** Skip the build if the builder knows it can be skipped */
89
- task: async (ctx, task) => {
93
+ task: async (ctx) => {
90
94
  if (ctx.builder?.mustBuild) {
91
95
  const previousSentinelData = await this.readSentinelFile(resource);
92
96
  ctx.sentinelData =
93
97
  await ctx.builder.mustBuild(previousSentinelData);
94
98
  ctx.cacheHit = !ctx.sentinelData;
95
99
  }
96
- if (!ctx.force && (ctx.dryRun || ctx.cacheHit)) {
97
- const prefix = ctx.dryRun ? '[dry run]' : '[cache hit]';
100
+ },
101
+ },
102
+ {
103
+ title: `Build image for ${resource.id}`,
104
+ async task(ctx, task) {
105
+ const skip = (prefix) => {
98
106
  parentTask.title = `${prefix} ${resource.id}`;
99
107
  task.skip();
100
108
  return parentTask.skip();
109
+ };
110
+ if (ctx.cacheHit && !ctx.force && !ctx.dryRun) {
111
+ return skip('[cache hit]');
112
+ }
113
+ const { input, operation } = await ctx.builder.build(task.stdout());
114
+ ctx.builderInput = input;
115
+ if (ctx.dryRun) {
116
+ return skip('[dry run]');
101
117
  }
102
- return ctx.builder.operation.run(ctx.builder?.input);
118
+ return operation.run(ctx.builderInput);
103
119
  },
104
120
  },
105
121
  {
106
122
  // Return build meta data and dump
107
123
  // cache data into sentinel file
108
124
  task: async (ctx) => {
109
- // TODO: clean this
110
- if (ctx.builder?.mustBuild) {
111
- delete ctx.builder.mustBuild;
112
- }
113
- if (ctx.builder?.operation) {
114
- // @ts-expect-error duynno
115
- delete ctx.builder.operation;
125
+ if (ctx.builder) {
126
+ delete ctx.builder;
116
127
  }
117
128
  //
118
129
  parentContext[resource.id] = ctx;
@@ -140,7 +151,14 @@ export class BuildResourcesOperation extends AbstractOperation {
140
151
  }
141
152
  async readSentinelFile(resource) {
142
153
  const path = this.sentinelFilePath(resource);
154
+ const stats = await this.context.monorepo.store.stat(path, false);
155
+ if (!stats) {
156
+ return undefined;
157
+ }
143
158
  const data = await this.context.monorepo.store.readFile(path, false);
144
- return data ? JSON.parse(data) : undefined;
159
+ return {
160
+ data,
161
+ mtime: stats.mtime.getTime(),
162
+ };
145
163
  }
146
164
  }
@@ -1,7 +1,7 @@
1
1
  import { getContext } from '../../../index.js';
2
- import { ContainerExecOperation } from '../../../docker/index.js';
2
+ import { ComposeExecOperation } from '../../../docker/index.js';
3
3
  import { EMBCollection, findRunOrder, taskManagerFactory, } from '../../index.js';
4
- import { ExecuteLocalCommandOperation, GetComponentContainerOperation, } from '../index.js';
4
+ import { ExecuteLocalCommandOperation } from '../index.js';
5
5
  export var ExecutorType;
6
6
  (function (ExecutorType) {
7
7
  ExecutorType["container"] = "container";
@@ -53,18 +53,17 @@ export class RunTasksOperation {
53
53
  }
54
54
  async runDocker(task, out) {
55
55
  const { monorepo } = getContext();
56
- const containerInfo = await monorepo.run(new GetComponentContainerOperation(), task.component);
57
- return monorepo.run(new ContainerExecOperation(out), {
56
+ return monorepo.run(new ComposeExecOperation(out), {
58
57
  attachStderr: true,
59
58
  attachStdout: true,
60
- container: containerInfo.Id,
61
- script: task.script,
59
+ service: task.component,
60
+ command: task.script,
62
61
  });
63
62
  }
64
63
  async runLocal(task) {
65
64
  const { monorepo } = getContext();
66
65
  const cwd = task.component
67
- ? monorepo.component(task.component).rootDir
66
+ ? monorepo.join(monorepo.component(task.component).rootDir)
68
67
  : monorepo.rootDir;
69
68
  return monorepo.run(new ExecuteLocalCommandOperation(), {
70
69
  script: task.script,
@@ -2,12 +2,16 @@ import { CreateFileOperation } from '../index.js';
2
2
  import { ResourceFactory } from './ResourceFactory.js';
3
3
  // Bring better abstraction and register as part of the plugin initialization
4
4
  ResourceFactory.register('file', async ({ config, component }) => {
5
- const fromConfig = (config.params || {});
6
- const input = {
7
- path: component.join(fromConfig?.path || config.name),
8
- };
9
5
  return {
10
- input,
11
- operation: new CreateFileOperation(),
6
+ async build() {
7
+ const fromConfig = (config.params || {});
8
+ const input = {
9
+ path: component.join(fromConfig?.path || config.name),
10
+ };
11
+ return {
12
+ input,
13
+ operation: new CreateFileOperation(),
14
+ };
15
+ },
12
16
  };
13
17
  });
@@ -1,4 +1,5 @@
1
1
  import { Component, Monorepo, ResourceInfo } from '../../index.js';
2
+ import { Writable } from 'node:stream';
2
3
  import { IOperation } from '../../operations/types.js';
3
4
  export type ResourceBuildContext = {
4
5
  config: ResourceInfo;
@@ -10,8 +11,18 @@ export type SentinelData<T = void> = {
10
11
  data: T;
11
12
  };
12
13
  export type ResourceBuilderInfo<I, O, D = unknown> = {
13
- input: I;
14
- operation: IOperation<I, O>;
14
+ /**
15
+ * Returns input and operation required to actually
16
+ * build the resources.
17
+ * This allows the dry-run mechanism to be implemented outside
18
+ * resource builder implementations
19
+ *
20
+ * @param out The Writable to use to write logs
21
+ */
22
+ build(out?: Writable): Promise<{
23
+ input: I;
24
+ operation: IOperation<I, O>;
25
+ }>;
15
26
  mustBuild?: (previousSentinelData: SentinelData<D> | undefined) => Promise<undefined | unknown>;
16
27
  };
17
28
  export type ResourceFactoryOutput<I, O> = Promise<ResourceBuilderInfo<I, O>>;
@@ -14,6 +14,7 @@ export declare class EMBStore {
14
14
  init(): Promise<void>;
15
15
  join(path: string): string;
16
16
  mkdirp(path: string): Promise<void>;
17
+ stat(path: string, mustExist?: boolean): Promise<import("fs").Stats | undefined>;
17
18
  readFile(path: string, mustExist?: boolean): Promise<string | undefined>;
18
19
  trash(): Promise<void>;
19
20
  writeFile(path: string, data: string): Promise<void>;
@@ -1,5 +1,5 @@
1
1
  import { constants, createReadStream, createWriteStream } from 'node:fs';
2
- import { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
2
+ import { access, mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
3
3
  import { dirname, join, normalize } from 'node:path';
4
4
  /**
5
5
  * A first implementation of a "store" where
@@ -54,6 +54,17 @@ export class EMBStore {
54
54
  const normalized = normalize(join('/', path));
55
55
  await mkdir(this.join(normalized), { recursive: true });
56
56
  }
57
+ async stat(path, mustExist = true) {
58
+ try {
59
+ return await stat(this.join(path));
60
+ }
61
+ catch (error) {
62
+ if (error.code === 'ENOENT' && !mustExist) {
63
+ return;
64
+ }
65
+ throw error;
66
+ }
67
+ }
57
68
  async readFile(path, mustExist = true) {
58
69
  try {
59
70
  return (await readFile(this.join(path))).toString();
@@ -268,10 +268,12 @@
268
268
  "shell.js"
269
269
  ]
270
270
  },
271
- "config:print": {
272
- "aliases": [],
271
+ "containers": {
272
+ "aliases": [
273
+ "ps"
274
+ ],
273
275
  "args": {},
274
- "description": "Print the current config.",
276
+ "description": "List docker containers.",
275
277
  "examples": [
276
278
  "<%= config.bin %> <%= command.id %>"
277
279
  ],
@@ -283,21 +285,22 @@
283
285
  "allowNo": false,
284
286
  "type": "boolean"
285
287
  },
286
- "flavor": {
287
- "description": "Specify the flavor to use.",
288
- "name": "flavor",
288
+ "all": {
289
+ "char": "a",
290
+ "description": "Retun all containers. By default, only running containers are shown",
291
+ "name": "all",
289
292
  "required": false,
290
- "hasDynamicHelp": false,
291
- "multiple": false,
292
- "type": "option"
293
+ "allowNo": false,
294
+ "type": "boolean"
293
295
  }
294
296
  },
295
297
  "hasDynamicHelp": false,
296
298
  "hiddenAliases": [],
297
- "id": "config:print",
299
+ "id": "containers",
298
300
  "pluginAlias": "@enspirit/emb",
299
301
  "pluginName": "@enspirit/emb",
300
302
  "pluginType": "core",
303
+ "strict": true,
301
304
  "enableJsonFlag": true,
302
305
  "isESM": true,
303
306
  "relativePath": [
@@ -305,16 +308,14 @@
305
308
  "src",
306
309
  "cli",
307
310
  "commands",
308
- "config",
309
- "print.js"
311
+ "containers",
312
+ "index.js"
310
313
  ]
311
314
  },
312
- "containers": {
313
- "aliases": [
314
- "ps"
315
- ],
315
+ "containers:prune": {
316
+ "aliases": [],
316
317
  "args": {},
317
- "description": "List docker containers.",
318
+ "description": "Prune containers.",
318
319
  "examples": [
319
320
  "<%= config.bin %> <%= command.id %>"
320
321
  ],
@@ -325,19 +326,11 @@
325
326
  "name": "json",
326
327
  "allowNo": false,
327
328
  "type": "boolean"
328
- },
329
- "all": {
330
- "char": "a",
331
- "description": "Retun all containers. By default, only running containers are shown",
332
- "name": "all",
333
- "required": false,
334
- "allowNo": false,
335
- "type": "boolean"
336
329
  }
337
330
  },
338
331
  "hasDynamicHelp": false,
339
332
  "hiddenAliases": [],
340
- "id": "containers",
333
+ "id": "containers:prune",
341
334
  "pluginAlias": "@enspirit/emb",
342
335
  "pluginName": "@enspirit/emb",
343
336
  "pluginType": "core",
@@ -350,13 +343,13 @@
350
343
  "cli",
351
344
  "commands",
352
345
  "containers",
353
- "index.js"
346
+ "prune.js"
354
347
  ]
355
348
  },
356
- "containers:prune": {
349
+ "config:print": {
357
350
  "aliases": [],
358
351
  "args": {},
359
- "description": "Prune containers.",
352
+ "description": "Print the current config.",
360
353
  "examples": [
361
354
  "<%= config.bin %> <%= command.id %>"
362
355
  ],
@@ -367,15 +360,22 @@
367
360
  "name": "json",
368
361
  "allowNo": false,
369
362
  "type": "boolean"
363
+ },
364
+ "flavor": {
365
+ "description": "Specify the flavor to use.",
366
+ "name": "flavor",
367
+ "required": false,
368
+ "hasDynamicHelp": false,
369
+ "multiple": false,
370
+ "type": "option"
370
371
  }
371
372
  },
372
373
  "hasDynamicHelp": false,
373
374
  "hiddenAliases": [],
374
- "id": "containers:prune",
375
+ "id": "config:print",
375
376
  "pluginAlias": "@enspirit/emb",
376
377
  "pluginName": "@enspirit/emb",
377
378
  "pluginType": "core",
378
- "strict": true,
379
379
  "enableJsonFlag": true,
380
380
  "isESM": true,
381
381
  "relativePath": [
@@ -383,8 +383,8 @@
383
383
  "src",
384
384
  "cli",
385
385
  "commands",
386
- "containers",
387
- "prune.js"
386
+ "config",
387
+ "print.js"
388
388
  ]
389
389
  },
390
390
  "images:delete": {
@@ -520,12 +520,18 @@
520
520
  "prune.js"
521
521
  ]
522
522
  },
523
- "tasks": {
523
+ "resources:build": {
524
524
  "aliases": [],
525
- "args": {},
526
- "description": "List tasks.",
525
+ "args": {
526
+ "component": {
527
+ "description": "List of resources to build (defaults to all)",
528
+ "name": "component",
529
+ "required": false
530
+ }
531
+ },
532
+ "description": "Build the resources of the monorepo",
527
533
  "examples": [
528
- "<%= config.bin %> <%= command.id %>"
534
+ "<%= config.bin %> <%= command.id %> build --flavor development"
529
535
  ],
530
536
  "flags": {
531
537
  "json": {
@@ -534,15 +540,38 @@
534
540
  "name": "json",
535
541
  "allowNo": false,
536
542
  "type": "boolean"
543
+ },
544
+ "flavor": {
545
+ "description": "Specify the flavor to use.",
546
+ "name": "flavor",
547
+ "required": false,
548
+ "hasDynamicHelp": false,
549
+ "multiple": false,
550
+ "type": "option"
551
+ },
552
+ "dry-run": {
553
+ "description": "Do not build the resources but only produce build meta information",
554
+ "name": "dry-run",
555
+ "required": false,
556
+ "allowNo": false,
557
+ "type": "boolean"
558
+ },
559
+ "force": {
560
+ "char": "f",
561
+ "description": "Bypass the cache and force the build",
562
+ "name": "force",
563
+ "required": false,
564
+ "allowNo": false,
565
+ "type": "boolean"
537
566
  }
538
567
  },
539
568
  "hasDynamicHelp": false,
540
569
  "hiddenAliases": [],
541
- "id": "tasks",
570
+ "id": "resources:build",
542
571
  "pluginAlias": "@enspirit/emb",
543
572
  "pluginName": "@enspirit/emb",
544
573
  "pluginType": "core",
545
- "strict": true,
574
+ "strict": false,
546
575
  "enableJsonFlag": true,
547
576
  "isESM": true,
548
577
  "relativePath": [
@@ -550,20 +579,14 @@
550
579
  "src",
551
580
  "cli",
552
581
  "commands",
553
- "tasks",
554
- "index.js"
582
+ "resources",
583
+ "build.js"
555
584
  ]
556
585
  },
557
- "tasks:run": {
586
+ "resources": {
558
587
  "aliases": [],
559
- "args": {
560
- "task": {
561
- "description": "List of tasks to run. You can provide either ids or names (eg: component:task or task)",
562
- "name": "task",
563
- "required": true
564
- }
565
- },
566
- "description": "Run tasks.",
588
+ "args": {},
589
+ "description": "List resources.",
567
590
  "examples": [
568
591
  "<%= config.bin %> <%= command.id %>"
569
592
  ],
@@ -575,33 +598,21 @@
575
598
  "allowNo": false,
576
599
  "type": "boolean"
577
600
  },
578
- "executor": {
579
- "char": "x",
580
- "description": "Where to run the task. (experimental!)",
581
- "name": "executor",
601
+ "flavor": {
602
+ "description": "Specify the flavor to use.",
603
+ "name": "flavor",
604
+ "required": false,
582
605
  "hasDynamicHelp": false,
583
606
  "multiple": false,
584
- "options": [
585
- "container",
586
- "local"
587
- ],
588
607
  "type": "option"
589
- },
590
- "all-matching": {
591
- "char": "a",
592
- "description": "Run all tasks matching (when multiple matches)",
593
- "name": "all-matching",
594
- "allowNo": false,
595
- "type": "boolean"
596
608
  }
597
609
  },
598
610
  "hasDynamicHelp": false,
599
611
  "hiddenAliases": [],
600
- "id": "tasks:run",
612
+ "id": "resources",
601
613
  "pluginAlias": "@enspirit/emb",
602
614
  "pluginName": "@enspirit/emb",
603
615
  "pluginType": "core",
604
- "strict": false,
605
616
  "enableJsonFlag": true,
606
617
  "isESM": true,
607
618
  "relativePath": [
@@ -609,22 +620,16 @@
609
620
  "src",
610
621
  "cli",
611
622
  "commands",
612
- "tasks",
613
- "run.js"
623
+ "resources",
624
+ "index.js"
614
625
  ]
615
626
  },
616
- "resources:build": {
627
+ "tasks": {
617
628
  "aliases": [],
618
- "args": {
619
- "component": {
620
- "description": "List of resources to build (defaults to all)",
621
- "name": "component",
622
- "required": false
623
- }
624
- },
625
- "description": "Build the resources of the monorepo",
629
+ "args": {},
630
+ "description": "List tasks.",
626
631
  "examples": [
627
- "<%= config.bin %> <%= command.id %> build --flavor development"
632
+ "<%= config.bin %> <%= command.id %>"
628
633
  ],
629
634
  "flags": {
630
635
  "json": {
@@ -633,38 +638,15 @@
633
638
  "name": "json",
634
639
  "allowNo": false,
635
640
  "type": "boolean"
636
- },
637
- "flavor": {
638
- "description": "Specify the flavor to use.",
639
- "name": "flavor",
640
- "required": false,
641
- "hasDynamicHelp": false,
642
- "multiple": false,
643
- "type": "option"
644
- },
645
- "dry-run": {
646
- "description": "Do not build the resources but only produce build meta information",
647
- "name": "dry-run",
648
- "required": false,
649
- "allowNo": false,
650
- "type": "boolean"
651
- },
652
- "force": {
653
- "char": "f",
654
- "description": "Bypass the cache and force the build",
655
- "name": "force",
656
- "required": false,
657
- "allowNo": false,
658
- "type": "boolean"
659
641
  }
660
642
  },
661
643
  "hasDynamicHelp": false,
662
644
  "hiddenAliases": [],
663
- "id": "resources:build",
645
+ "id": "tasks",
664
646
  "pluginAlias": "@enspirit/emb",
665
647
  "pluginName": "@enspirit/emb",
666
648
  "pluginType": "core",
667
- "strict": false,
649
+ "strict": true,
668
650
  "enableJsonFlag": true,
669
651
  "isESM": true,
670
652
  "relativePath": [
@@ -672,14 +654,20 @@
672
654
  "src",
673
655
  "cli",
674
656
  "commands",
675
- "resources",
676
- "build.js"
657
+ "tasks",
658
+ "index.js"
677
659
  ]
678
660
  },
679
- "resources": {
661
+ "tasks:run": {
680
662
  "aliases": [],
681
- "args": {},
682
- "description": "List resources.",
663
+ "args": {
664
+ "task": {
665
+ "description": "List of tasks to run. You can provide either ids or names (eg: component:task or task)",
666
+ "name": "task",
667
+ "required": true
668
+ }
669
+ },
670
+ "description": "Run tasks.",
683
671
  "examples": [
684
672
  "<%= config.bin %> <%= command.id %>"
685
673
  ],
@@ -691,21 +679,33 @@
691
679
  "allowNo": false,
692
680
  "type": "boolean"
693
681
  },
694
- "flavor": {
695
- "description": "Specify the flavor to use.",
696
- "name": "flavor",
697
- "required": false,
682
+ "executor": {
683
+ "char": "x",
684
+ "description": "Where to run the task. (experimental!)",
685
+ "name": "executor",
698
686
  "hasDynamicHelp": false,
699
687
  "multiple": false,
688
+ "options": [
689
+ "container",
690
+ "local"
691
+ ],
700
692
  "type": "option"
693
+ },
694
+ "all-matching": {
695
+ "char": "a",
696
+ "description": "Run all tasks matching (when multiple matches)",
697
+ "name": "all-matching",
698
+ "allowNo": false,
699
+ "type": "boolean"
701
700
  }
702
701
  },
703
702
  "hasDynamicHelp": false,
704
703
  "hiddenAliases": [],
705
- "id": "resources",
704
+ "id": "tasks:run",
706
705
  "pluginAlias": "@enspirit/emb",
707
706
  "pluginName": "@enspirit/emb",
708
707
  "pluginType": "core",
708
+ "strict": false,
709
709
  "enableJsonFlag": true,
710
710
  "isESM": true,
711
711
  "relativePath": [
@@ -713,10 +713,10 @@
713
713
  "src",
714
714
  "cli",
715
715
  "commands",
716
- "resources",
717
- "index.js"
716
+ "tasks",
717
+ "run.js"
718
718
  ]
719
719
  }
720
720
  },
721
- "version": "0.2.0"
721
+ "version": "0.3.1"
722
722
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@enspirit/emb",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.3.1",
5
5
  "keywords": ["monorepo", "docker", "taskrunner", "ci", "docker compose", "sentinel", "makefile"],
6
6
  "author": "Louis Lambeau <louis.lambeau@enspirit.be>",
7
7
  "license": "ISC",
@@ -47,6 +47,7 @@
47
47
  "dotenv": "^17.2.1",
48
48
  "execa": "^9.6.0",
49
49
  "fast-json-patch": "^3.1.1",
50
+ "fdir": "^6.4.6",
50
51
  "find-up": "^7.0.0",
51
52
  "glob": "^11.0.3",
52
53
  "graphlib": "^2.1.8",