@foliwake/cli 0.10.2 → 0.11.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 (94) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +196 -14
  3. package/config/docker/compose.varnish.yaml +44 -0
  4. package/config/docker/compose.yaml +165 -0
  5. package/dist/commands/dev.d.ts +3 -0
  6. package/dist/commands/dev.js +58 -21
  7. package/dist/commands/docker/backup.d.ts +12 -0
  8. package/dist/commands/docker/backup.js +29 -0
  9. package/dist/commands/docker/configure.d.ts +23 -0
  10. package/dist/commands/docker/configure.js +73 -0
  11. package/dist/commands/docker/exec.d.ts +8 -0
  12. package/dist/commands/docker/exec.js +26 -0
  13. package/dist/commands/docker/install.d.ts +33 -0
  14. package/dist/commands/docker/install.js +126 -0
  15. package/dist/commands/docker/link.d.ts +12 -0
  16. package/dist/commands/docker/link.js +24 -0
  17. package/dist/commands/docker/list.d.ts +7 -0
  18. package/dist/commands/docker/list.js +16 -0
  19. package/dist/commands/docker/logs.d.ts +15 -0
  20. package/dist/commands/docker/logs.js +33 -0
  21. package/dist/commands/docker/remove.d.ts +13 -0
  22. package/dist/commands/docker/remove.js +35 -0
  23. package/dist/commands/docker/restart.d.ts +9 -0
  24. package/dist/commands/docker/restart.js +18 -0
  25. package/dist/commands/docker/restore.d.ts +14 -0
  26. package/dist/commands/docker/restore.js +34 -0
  27. package/dist/commands/docker/start.d.ts +9 -0
  28. package/dist/commands/docker/start.js +18 -0
  29. package/dist/commands/docker/status.d.ts +10 -0
  30. package/dist/commands/docker/status.js +22 -0
  31. package/dist/commands/docker/stop.d.ts +9 -0
  32. package/dist/commands/docker/stop.js +18 -0
  33. package/dist/commands/docker/update.d.ts +14 -0
  34. package/dist/commands/docker/update.js +42 -0
  35. package/dist/commands/project/publish.d.ts +1 -0
  36. package/dist/commands/project/publish.js +9 -2
  37. package/dist/core-command.d.ts +4 -1
  38. package/dist/core-command.js +6 -2
  39. package/dist/core-types.d.ts +1 -0
  40. package/dist/dev/container.d.ts +45 -0
  41. package/dist/dev/container.js +128 -0
  42. package/dist/dev/remote-html.js +2 -2
  43. package/dist/docker/asker.d.ts +4 -0
  44. package/dist/docker/asker.js +34 -0
  45. package/dist/docker/backup.d.ts +21 -0
  46. package/dist/docker/backup.js +114 -0
  47. package/dist/docker/cache-plan.d.ts +17 -0
  48. package/dist/docker/cache-plan.js +58 -0
  49. package/dist/docker/compose-file.d.ts +18 -0
  50. package/dist/docker/compose-file.js +58 -0
  51. package/dist/docker/context.d.ts +55 -0
  52. package/dist/docker/context.js +12 -0
  53. package/dist/docker/engine.d.ts +8 -0
  54. package/dist/docker/engine.js +51 -0
  55. package/dist/docker/env-block.d.ts +19 -0
  56. package/dist/docker/env-block.js +117 -0
  57. package/dist/docker/env-keys.d.ts +4 -0
  58. package/dist/docker/env-keys.js +33 -0
  59. package/dist/docker/env-verify.d.ts +17 -0
  60. package/dist/docker/env-verify.js +62 -0
  61. package/dist/docker/install-plan.d.ts +81 -0
  62. package/dist/docker/install-plan.js +332 -0
  63. package/dist/docker/install.d.ts +22 -0
  64. package/dist/docker/install.js +118 -0
  65. package/dist/docker/instance.d.ts +105 -0
  66. package/dist/docker/instance.js +184 -0
  67. package/dist/docker/lifecycle.d.ts +19 -0
  68. package/dist/docker/lifecycle.js +70 -0
  69. package/dist/docker/ports.d.ts +14 -0
  70. package/dist/docker/ports.js +50 -0
  71. package/dist/docker/process-context.d.ts +8 -0
  72. package/dist/docker/process-context.js +26 -0
  73. package/dist/docker/remove.d.ts +7 -0
  74. package/dist/docker/remove.js +82 -0
  75. package/dist/docker/report.d.ts +32 -0
  76. package/dist/docker/report.js +109 -0
  77. package/dist/docker/runner.d.ts +28 -0
  78. package/dist/docker/runner.js +55 -0
  79. package/dist/docker/settings-files.d.ts +40 -0
  80. package/dist/docker/settings-files.js +125 -0
  81. package/dist/docker/trace-url.d.ts +5 -0
  82. package/dist/docker/trace-url.js +36 -0
  83. package/dist/docker/update.d.ts +29 -0
  84. package/dist/docker/update.js +317 -0
  85. package/dist/docker-command.d.ts +17 -0
  86. package/dist/docker-command.js +37 -0
  87. package/dist/project-preview.d.ts +8 -0
  88. package/dist/project-preview.js +27 -5
  89. package/dist/project-publish.d.ts +8 -7
  90. package/dist/project-publish.js +124 -57
  91. package/dist/publish-code.d.ts +75 -0
  92. package/dist/publish-code.js +223 -0
  93. package/dist/render/site-status.js +2 -2
  94. package/package.json +13 -8
@@ -0,0 +1,35 @@
1
+ import { Args, Flags } from '@oclif/core';
2
+ import { DockerCommand } from '../../docker-command.js';
3
+ export default class DockerRemove extends DockerCommand {
4
+ static description = [
5
+ "Stop and remove an instance's containers and network, and its settings on this computer. The data " +
6
+ 'volume is kept unless --delete-data (then the name is typed to confirm, or --yes without a terminal).',
7
+ 'A kept volume shows in `docker list` as "data only"; `docker install <name>` offers to reuse it.',
8
+ ].join('\n');
9
+ static examples = [
10
+ '<%= config.bin %> docker remove staging',
11
+ '<%= config.bin %> docker remove staging --yes',
12
+ '<%= config.bin %> docker remove staging --delete-data --yes',
13
+ ];
14
+ static args = {
15
+ name: Args.string({
16
+ description: 'instance name (default: the only one; asked in a terminal when there are several)',
17
+ }),
18
+ };
19
+ static flags = {
20
+ 'delete-data': Flags.boolean({
21
+ description: 'also delete its volumes: sites, accounts, storage (cannot be undone)',
22
+ }),
23
+ yes: Flags.boolean({ description: 'do not ask; required without a terminal' }),
24
+ };
25
+ async run() {
26
+ const { args, flags } = await this.parse(DockerRemove);
27
+ const ctx = await this.dockerContext(flags.interactive);
28
+ const instance = await this.instance(ctx, args.name, { allowDataOnly: true });
29
+ const { runRemove } = await import('../../docker/remove.js');
30
+ process.exitCode = await runRemove(ctx, instance, {
31
+ deleteData: flags['delete-data'] ? true : undefined,
32
+ yes: flags.yes,
33
+ });
34
+ }
35
+ }
@@ -0,0 +1,9 @@
1
+ import { DockerCommand } from '../../docker-command.js';
2
+ export default class DockerRestart extends DockerCommand {
3
+ static description: string;
4
+ static examples: string[];
5
+ static args: {
6
+ name: import("@oclif/core/interfaces").Arg<string | undefined, Record<string, unknown>>;
7
+ };
8
+ run(): Promise<void>;
9
+ }
@@ -0,0 +1,18 @@
1
+ import { Args } from '@oclif/core';
2
+ import { DockerCommand } from '../../docker-command.js';
3
+ export default class DockerRestart extends DockerCommand {
4
+ static description = 'Restart the containers of an instance';
5
+ static examples = ['<%= config.bin %> docker restart', '<%= config.bin %> docker restart staging'];
6
+ static args = {
7
+ name: Args.string({
8
+ description: 'instance name (default: the only one; asked in a terminal when there are several)',
9
+ }),
10
+ };
11
+ async run() {
12
+ const { args, flags } = await this.parse(DockerRestart);
13
+ const ctx = await this.dockerContext(flags.interactive);
14
+ const instance = await this.instance(ctx, args.name);
15
+ const { runLifecycle } = await import('../../docker/lifecycle.js');
16
+ process.exitCode = await runLifecycle(ctx, instance, 'restart');
17
+ }
18
+ }
@@ -0,0 +1,14 @@
1
+ import { DockerCommand } from '../../docker-command.js';
2
+ export default class DockerRestore extends DockerCommand {
3
+ static description: string;
4
+ static examples: string[];
5
+ static args: {
6
+ name: import("@oclif/core/interfaces").Arg<string | undefined, Record<string, unknown>>;
7
+ };
8
+ static flags: {
9
+ from: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
10
+ replace: import("@oclif/core/interfaces").BooleanFlag<boolean>;
11
+ yes: import("@oclif/core/interfaces").BooleanFlag<boolean>;
12
+ };
13
+ run(): Promise<void>;
14
+ }
@@ -0,0 +1,34 @@
1
+ import { Args, Flags } from '@oclif/core';
2
+ import { DockerCommand } from '../../docker-command.js';
3
+ export default class DockerRestore extends DockerCommand {
4
+ static description = [
5
+ "Restore a `docker backup` archive into an instance's data volume. The instance must be stopped.",
6
+ 'An empty volume takes it as it is; one with data needs --replace (its data is deleted first: the name is',
7
+ 'typed to confirm, or --yes without a terminal).',
8
+ ].join('\n');
9
+ static examples = [
10
+ '<%= config.bin %> docker restore staging --from ./staging.tar.gz',
11
+ '<%= config.bin %> docker restore staging --from ./staging.tar.gz --replace --yes',
12
+ ];
13
+ static args = {
14
+ name: Args.string({
15
+ description: 'instance name (default: the only one; asked in a terminal when there are several)',
16
+ }),
17
+ };
18
+ static flags = {
19
+ from: Flags.string({ description: 'the archive `docker backup` wrote', required: true }),
20
+ replace: Flags.boolean({ description: 'delete the data the volume holds first (cannot be undone)' }),
21
+ yes: Flags.boolean({ description: 'do not ask before --replace; required without a terminal' }),
22
+ };
23
+ async run() {
24
+ const { args, flags } = await this.parse(DockerRestore);
25
+ const ctx = await this.dockerContext(flags.interactive);
26
+ const instance = await this.instance(ctx, args.name, { allowDataOnly: true });
27
+ const { confirmReplace, runRestore } = await import('../../docker/backup.js');
28
+ if (flags.replace && !(await confirmReplace(ctx, instance.name, flags.yes))) {
29
+ process.exitCode = 130;
30
+ return;
31
+ }
32
+ process.exitCode = await runRestore(ctx, instance, { from: flags.from, replace: flags.replace });
33
+ }
34
+ }
@@ -0,0 +1,9 @@
1
+ import { DockerCommand } from '../../docker-command.js';
2
+ export default class DockerStart extends DockerCommand {
3
+ static description: string;
4
+ static examples: string[];
5
+ static args: {
6
+ name: import("@oclif/core/interfaces").Arg<string | undefined, Record<string, unknown>>;
7
+ };
8
+ run(): Promise<void>;
9
+ }
@@ -0,0 +1,18 @@
1
+ import { Args } from '@oclif/core';
2
+ import { DockerCommand } from '../../docker-command.js';
3
+ export default class DockerStart extends DockerCommand {
4
+ static description = 'Start the containers of a stopped instance';
5
+ static examples = ['<%= config.bin %> docker start', '<%= config.bin %> docker start staging'];
6
+ static args = {
7
+ name: Args.string({
8
+ description: 'instance name (default: the only one; asked in a terminal when there are several)',
9
+ }),
10
+ };
11
+ async run() {
12
+ const { args, flags } = await this.parse(DockerStart);
13
+ const ctx = await this.dockerContext(flags.interactive);
14
+ const instance = await this.instance(ctx, args.name);
15
+ const { runLifecycle } = await import('../../docker/lifecycle.js');
16
+ process.exitCode = await runLifecycle(ctx, instance, 'start');
17
+ }
18
+ }
@@ -0,0 +1,10 @@
1
+ import { DockerCommand } from '../../docker-command.js';
2
+ export default class DockerStatus extends DockerCommand {
3
+ static description: string;
4
+ static examples: string[];
5
+ static enableJsonFlag: boolean;
6
+ static args: {
7
+ name: import("@oclif/core/interfaces").Arg<string | undefined, Record<string, unknown>>;
8
+ };
9
+ run(): Promise<unknown>;
10
+ }
@@ -0,0 +1,22 @@
1
+ import { Args } from '@oclif/core';
2
+ import { DockerCommand } from '../../docker-command.js';
3
+ export default class DockerStatus extends DockerCommand {
4
+ static description = 'State, health, image, ports, address, traces and data of an instance; secrets only as "set"';
5
+ static examples = ['<%= config.bin %> docker status', '<%= config.bin %> docker status staging --json'];
6
+ static enableJsonFlag = true;
7
+ static args = {
8
+ name: Args.string({
9
+ description: 'instance name (default: the only one; asked in a terminal when there are several)',
10
+ }),
11
+ };
12
+ async run() {
13
+ const { args, flags } = await this.parse(DockerStatus);
14
+ const ctx = await this.dockerContext(flags.interactive);
15
+ const instance = await this.instance(ctx, args.name, { allowDataOnly: true });
16
+ const { instanceStatus, statusLines } = await import('../../docker/report.js');
17
+ const status = await instanceStatus(ctx, instance);
18
+ for (const line of statusLines(status))
19
+ this.log(line);
20
+ return status;
21
+ }
22
+ }
@@ -0,0 +1,9 @@
1
+ import { DockerCommand } from '../../docker-command.js';
2
+ export default class DockerStop extends DockerCommand {
3
+ static description: string;
4
+ static examples: string[];
5
+ static args: {
6
+ name: import("@oclif/core/interfaces").Arg<string | undefined, Record<string, unknown>>;
7
+ };
8
+ run(): Promise<void>;
9
+ }
@@ -0,0 +1,18 @@
1
+ import { Args } from '@oclif/core';
2
+ import { DockerCommand } from '../../docker-command.js';
3
+ export default class DockerStop extends DockerCommand {
4
+ static description = 'Stop the containers of an instance (data and settings stay)';
5
+ static examples = ['<%= config.bin %> docker stop', '<%= config.bin %> docker stop staging'];
6
+ static args = {
7
+ name: Args.string({
8
+ description: 'instance name (default: the only one; asked in a terminal when there are several)',
9
+ }),
10
+ };
11
+ async run() {
12
+ const { args, flags } = await this.parse(DockerStop);
13
+ const ctx = await this.dockerContext(flags.interactive);
14
+ const instance = await this.instance(ctx, args.name);
15
+ const { runLifecycle } = await import('../../docker/lifecycle.js');
16
+ process.exitCode = await runLifecycle(ctx, instance, 'stop');
17
+ }
18
+ }
@@ -0,0 +1,14 @@
1
+ import { DockerCommand } from '../../docker-command.js';
2
+ export default class DockerUpdate extends DockerCommand {
3
+ static description: string;
4
+ static examples: string[];
5
+ static args: {
6
+ name: import("@oclif/core/interfaces").Arg<string | undefined, Record<string, unknown>>;
7
+ };
8
+ static flags: {
9
+ backup: import("@oclif/core/interfaces").BooleanFlag<boolean>;
10
+ image: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
11
+ pull: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
12
+ };
13
+ run(): Promise<void>;
14
+ }
@@ -0,0 +1,42 @@
1
+ import { Args, Flags } from '@oclif/core';
2
+ import { DockerCommand } from '../../docker-command.js';
3
+ export default class DockerUpdate extends DockerCommand {
4
+ static description = [
5
+ "Pull the instance's image; when it changed, back up the data volume (`docker backup`), recreate the server and",
6
+ "drop every site's cached HTML. The data volume stays. Prints the command that rolls back.",
7
+ "Needs the instance's settings on this computer (~/.config/foliwake/docker/<name>/).",
8
+ ].join('\n');
9
+ static examples = [
10
+ '<%= config.bin %> docker update',
11
+ '<%= config.bin %> docker update staging --image ghcr.io/foliwake/foliwake:sha-1a2b3c4',
12
+ '<%= config.bin %> docker update --image sha256:3f1a… --pull missing',
13
+ ];
14
+ static args = {
15
+ name: Args.string({
16
+ description: 'instance name (default: the only one; asked in a terminal when there are several)',
17
+ }),
18
+ };
19
+ static flags = {
20
+ backup: Flags.boolean({
21
+ allowNo: true,
22
+ default: true,
23
+ description: 'back up the data volume before the switch (default; --no-backup skips it)',
24
+ }),
25
+ image: Flags.string({
26
+ description: 'pin another image (tag, digest, or the id of a local build), kept in the settings',
27
+ }),
28
+ pull: Flags.string({
29
+ options: ['always', 'missing'],
30
+ description: 'always (default): pull, a failed pull stops the update; missing: use the image if it is on this ' +
31
+ 'engine already (e.g. the id of a local build to roll back to)',
32
+ }),
33
+ };
34
+ async run() {
35
+ const { args, flags } = await this.parse(DockerUpdate);
36
+ const ctx = await this.dockerContext(flags.interactive);
37
+ const instance = await this.instance(ctx, args.name);
38
+ const { runUpdate } = await import('../../docker/update.js');
39
+ const pull = flags.pull;
40
+ process.exitCode = await runUpdate(ctx, instance, flags.image, pull, { backup: flags.backup });
41
+ }
42
+ }
@@ -11,6 +11,7 @@ export default class ProjectPublish extends Command {
11
11
  static flags: {
12
12
  expect: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
13
  yes: import("@oclif/core/interfaces").BooleanFlag<boolean>;
14
+ 'with-code': import("@oclif/core/interfaces").BooleanFlag<boolean>;
14
15
  };
15
16
  run(): Promise<unknown>;
16
17
  }
@@ -5,11 +5,14 @@ export default class ProjectPublish extends Command {
5
5
  static description = `In a folder linked to a site: with no paths, the pages the last \`foliwake preview\` of ` +
6
6
  `this folder changed, refused when the preview changed since; with paths (files or folders under content/), ` +
7
7
  `their pages. Lists them and asks in a terminal; --yes for scripts. --expect sr_… publishes a preview you ` +
8
- `reviewed elsewhere. In a linked folder, \`foliwake publish\` runs this command.`;
8
+ `reviewed elsewhere. When the preview was built with new site code (the last commit's styles, scripts or ` +
9
+ `blocks, not live yet), that code goes live with the pages: the one question names both, and scripts pass ` +
10
+ `--yes --with-code (--yes alone refuses). In a linked folder, \`foliwake publish\` runs this command.`;
9
11
  static examples = [
10
12
  '<%= config.bin %> publish',
11
13
  '<%= config.bin %> publish content/index.md',
12
14
  '<%= config.bin %> publish content/lessons/ --yes',
15
+ '<%= config.bin %> publish --yes --with-code',
13
16
  ];
14
17
  static enableJsonFlag = true;
15
18
  static strict = false;
@@ -21,6 +24,9 @@ export default class ProjectPublish extends Command {
21
24
  description: 'the preview release you reviewed (sr_…); default: the one the last `foliwake preview` here made',
22
25
  }),
23
26
  yes: Flags.boolean({ description: 'do not ask (scripts)' }),
27
+ 'with-code': Flags.boolean({
28
+ description: "with --yes: also release the preview's new site code (styles, scripts, blocks) with the pages",
29
+ }),
24
30
  };
25
31
  async run() {
26
32
  const { argv, flags } = await this.parse(ProjectPublish);
@@ -32,9 +38,10 @@ export default class ProjectPublish extends Command {
32
38
  paths: argv,
33
39
  expect: flags.expect,
34
40
  yes: flags.yes,
41
+ withCode: flags['with-code'],
35
42
  confirm: json ? null : terminalConfirm(process.stdin, process.stdout),
36
43
  }, { out: (text = '') => (json ? undefined : this.log(text)), err: (text = '') => this.logToStderr(text) });
37
44
  process.exitCode = outcome.exit;
38
- return { pages: outcome.pages, operation: outcome.operation };
45
+ return { pages: outcome.pages, operation: outcome.operation, code: outcome.code };
39
46
  }
40
47
  }
@@ -3,7 +3,10 @@ import type { CoreCaller } from '@foliwake/core-contract';
3
3
  import { type LocalCore } from './local-runtime.js';
4
4
  import type { Target } from './remote/servers.js';
5
5
  import { type Operation } from './remote/wait-slices.js';
6
- /** What a command that needs the engine says, with exit 2. */
6
+ /**
7
+ * What a command that needs the engine says, with exit 2. A Foliwake server in Docker (`docker install`) has the
8
+ * engine inside: `docker exec` runs a command there, with the server's own configuration.
9
+ */
7
10
  export declare const NEEDS_ENGINE: string;
8
11
  export declare abstract class CoreCommand extends Command {
9
12
  static enableJsonFlag: boolean;
@@ -12,8 +12,12 @@ import { cliVersion } from './version.js';
12
12
  const COMMIT_WAIT_MS = 24 * 60 * 60 * 1000;
13
13
  /** Default waits start from this settle delay (the server's own is not known here). */
14
14
  const REMOTE_SETTLE_DELAY_MS = 30_000;
15
- /** What a command that needs the engine says, with exit 2. */
16
- export const NEEDS_ENGINE = `needs the Foliwake engine: the Docker image (\`${COMMAND} docker install\`) or the ` + `engine checkout.`;
15
+ /**
16
+ * What a command that needs the engine says, with exit 2. A Foliwake server in Docker (`docker install`) has the
17
+ * engine inside: `docker exec` runs a command there, with the server's own configuration.
18
+ */
19
+ export const NEEDS_ENGINE = 'needs the Foliwake engine: the engine checkout on this computer, or the engine inside a Foliwake server in ' +
20
+ `Docker (\`${COMMAND} docker exec -- <command>\`, with the server's own configuration).`;
17
21
  const NO_SERVER = `No Foliwake server is named: pass --server, link this folder (\`${COMMAND} site link <id>\`), ` +
18
22
  `set FOLIWAKE_SERVER, or sign in with \`${COMMAND} login\`.`;
19
23
  /** Registered before oclif loads the commands, so flags read it when this module loads. */
@@ -15,6 +15,7 @@ export type AddAnswer = CoreOutput<'sources.add'>;
15
15
  export type BranchList = CoreOutput<'branches.list'>;
16
16
  export type BranchHistory = CoreOutput<'branches.history'>;
17
17
  export type Changes = CoreOutput<'content.changes'>;
18
+ export type ChannelChanges = CoreOutput<'channels.changes'>;
18
19
  export type Validation = CoreOutput<'content.validate'>;
19
20
  export type CredentialList = CoreOutput<'credentials.list'>;
20
21
  export type CredentialSetResult = CoreOutput<'credentials.set'>;
@@ -0,0 +1,45 @@
1
+ import { type DockerContext } from '../docker/context.js';
2
+ import { type Instance } from '../docker/instance.js';
3
+ export declare const DEV_PORT = 3000;
4
+ /** The flags of `dev` in local mode, and the image to run it in. */
5
+ export interface ContainerDevRequest {
6
+ port: number | undefined;
7
+ watch: boolean | undefined;
8
+ polling: boolean | undefined;
9
+ content: string | undefined;
10
+ config: string | undefined;
11
+ site: string | undefined;
12
+ image: string | undefined;
13
+ }
14
+ /** The image `dev` runs in, and why that one (printed). */
15
+ export interface DevImage {
16
+ image: string;
17
+ from: string;
18
+ }
19
+ /** What one `docker run` of the engine's `dev` needs. */
20
+ export interface DevRun {
21
+ /** This computer's folder. */
22
+ cwd: string;
23
+ /** The folder in the container. */
24
+ mount: string;
25
+ port: number;
26
+ image: string;
27
+ /** Attach a terminal (stdin and stdout are one). */
28
+ tty: boolean;
29
+ /** `uid:gid` of this user, or null where there is none (Windows). */
30
+ user: string | null;
31
+ /** The container's name, for `docker stop` on SIGTERM. */
32
+ name: string;
33
+ }
34
+ /** Where this computer's folder is in the container: the same path; /site on Windows. */
35
+ export declare function mountPoint(cwd: string, platform?: NodeJS.Platform): string;
36
+ /** The instance this folder is linked to (by its server's address), else the only one that has a server. */
37
+ export declare function devInstance(instances: readonly Instance[], cwd: string): Instance | null;
38
+ /** --image, else the exact image the matching instance's server runs, else the default image. */
39
+ export declare function devImage(ctx: DockerContext, given: string | undefined, cwd: string): Promise<DevImage>;
40
+ /** The engine's `dev` command line: the same port inside, the local flags as given. */
41
+ export declare function engineDevArgs(request: ContainerDevRequest, port: number): string[];
42
+ /** `docker run …` of the engine's `dev` in a throwaway container. */
43
+ export declare function dockerRunArgs(run: DevRun, engineArgs: readonly string[]): string[];
44
+ /** `foliwake dev` in Docker until Ctrl+C; the exit code of the engine's `dev` (2: a refused command line). */
45
+ export declare function runDevInContainer(ctx: DockerContext, request: ContainerDevRequest, cwd?: string): Promise<number>;
@@ -0,0 +1,128 @@
1
+ // `foliwake dev` without the engine on this computer: the engine's own `dev` in a throwaway container of the
2
+ // Foliwake image, the one the local Docker instance runs. The folder is bind-mounted at the same path (so the
3
+ // paths in messages are this computer's; /site on Windows) and the dev port is published on 127.0.0.1 only.
4
+ // The container gets none of this process's environment, runs as this user, and is removed when dev stops.
5
+ import { randomBytes } from 'node:crypto';
6
+ import { DEFAULT_IMAGE } from '../docker/compose-file.js';
7
+ import { DockerUsageError } from '../docker/context.js';
8
+ import { checkDaemon } from '../docker/engine.js';
9
+ import { containerOf, discoverInstances } from '../docker/instance.js';
10
+ import { canListen } from '../docker/ports.js';
11
+ import { readConnection } from '../remote/connection.js';
12
+ export const DEV_PORT = 3000;
13
+ /**
14
+ * The engine's dev server listens on 127.0.0.1 unless told otherwise; in the container it must listen on every
15
+ * interface of the container for the published port to reach it. The host publishes it on loopback only.
16
+ */
17
+ const LISTEN_EVERYWHERE = 'FOLIWAKE_DEV_LISTEN_HOST=0.0.0.0';
18
+ /** The image sets FOLIWAKE_CONFIG to its server's file; empty, the engine uses the folder's configuration. */
19
+ const NO_SERVER_CONFIG = 'FOLIWAKE_CONFIG=';
20
+ /** Where this computer's folder is in the container: the same path; /site on Windows. */
21
+ export function mountPoint(cwd, platform = process.platform) {
22
+ return platform === 'win32' ? '/site' : cwd;
23
+ }
24
+ const withoutSlash = (origin) => origin.replace(/\/+$/, '');
25
+ /** True when the instance's server is reached at `origin` (its public address). */
26
+ function servesOrigin(instance, origin) {
27
+ const own = containerOf(instance, 'server')?.env.publicOrigin;
28
+ return own !== null && own !== undefined && withoutSlash(own) === withoutSlash(origin);
29
+ }
30
+ /** The instance this folder is linked to (by its server's address), else the only one that has a server. */
31
+ export function devInstance(instances, cwd) {
32
+ const withServer = instances.filter((instance) => containerOf(instance, 'server'));
33
+ const linkedTo = readConnection(cwd)?.server;
34
+ const linked = linkedTo ? withServer.find((instance) => servesOrigin(instance, linkedTo)) : undefined;
35
+ if (linked)
36
+ return linked;
37
+ return withServer.length === 1 ? withServer[0] : null;
38
+ }
39
+ /** --image, else the exact image the matching instance's server runs, else the default image. */
40
+ export async function devImage(ctx, given, cwd) {
41
+ if (given)
42
+ return { image: given, from: '--image' };
43
+ const instance = devInstance(await discoverInstances(ctx), cwd);
44
+ const server = instance ? containerOf(instance, 'server') : undefined;
45
+ if (instance && server)
46
+ return { image: server.imageId, from: `the image of instance ${instance.name}` };
47
+ return { image: DEFAULT_IMAGE, from: 'no instance of this folder on this Docker engine' };
48
+ }
49
+ /** The engine's `dev` command line: the same port inside, the local flags as given. */
50
+ export function engineDevArgs(request, port) {
51
+ const args = ['dev', '--port', String(port)];
52
+ if (request.watch)
53
+ args.push('--watch');
54
+ if (request.polling !== undefined)
55
+ args.push(request.polling ? '--watch-polling' : '--no-watch-polling');
56
+ for (const name of ['config', 'site', 'content']) {
57
+ const value = request[name];
58
+ if (value !== undefined)
59
+ args.push(`--${name}`, value);
60
+ }
61
+ return args;
62
+ }
63
+ /**
64
+ * This user's uid has no entry in the image's /etc/passwd: HOME and SHELL are given, so nothing asks for one
65
+ * (oclif reads the shell from the passwd entry when SHELL is unset).
66
+ */
67
+ const AS_ANY_USER = ['-e', 'HOME=/tmp', '-e', 'SHELL=/bin/sh'];
68
+ /** The same hardening as the instance's server: read-only root, a private /tmp, no new privileges. */
69
+ const HARDENING = ['--read-only', '--tmpfs', '/tmp', '--security-opt', 'no-new-privileges:true'];
70
+ /** `docker run …` of the engine's `dev` in a throwaway container. */
71
+ export function dockerRunArgs(run, engineArgs) {
72
+ const terminal = run.tty ? ['-i', '-t'] : ['-i'];
73
+ const folder = ['--mount', `type=bind,source=${run.cwd},target=${run.mount}`, '--workdir', run.mount];
74
+ const port = ['-p', `127.0.0.1:${run.port}:${run.port}`];
75
+ const env = ['-e', LISTEN_EVERYWHERE, '-e', NO_SERVER_CONFIG, ...AS_ANY_USER];
76
+ const user = run.user ? ['--user', run.user] : [];
77
+ const options = [...terminal, '--name', run.name, ...folder, ...port, ...env, ...user, ...HARDENING];
78
+ return ['run', '--rm', ...options, run.image, ...engineArgs];
79
+ }
80
+ /** `uid:gid` of this process, so files the engine writes in the folder are this user's. */
81
+ function hostUser() {
82
+ const uid = process.getuid?.();
83
+ const gid = process.getgid?.();
84
+ return uid === undefined || gid === undefined ? null : `${uid}:${gid}`;
85
+ }
86
+ /** Refuses what the container cannot do before anything starts. */
87
+ async function checkRun(ctx, cwd, port) {
88
+ if (port === 0)
89
+ throw new DockerUsageError('--port 0 does not apply when dev runs in Docker: name a port.');
90
+ if (cwd.includes(',')) {
91
+ throw new DockerUsageError(`dev in Docker cannot mount a folder whose path holds a comma: ${cwd}`);
92
+ }
93
+ await checkDaemon(ctx);
94
+ if (!(await (ctx.portFree ?? canListen)('127.0.0.1', port))) {
95
+ throw new DockerUsageError(`Port ${port} is already in use. Stop the other process or pass --port <n>.`);
96
+ }
97
+ }
98
+ /**
99
+ * Runs `attach` with the container's lifetime tied to this process: Ctrl+C reaches the container through
100
+ * docker itself (the terminal, or its signal proxy), so this process waits for it; SIGTERM and SIGHUP stop it.
101
+ */
102
+ async function tiedTo(ctx, name, attach) {
103
+ const waitForDocker = () => { };
104
+ const stop = () => void ctx.docker.run(['stop', name]);
105
+ process.on('SIGINT', waitForDocker);
106
+ process.on('SIGTERM', stop);
107
+ process.on('SIGHUP', stop);
108
+ try {
109
+ return await attach();
110
+ }
111
+ finally {
112
+ process.off('SIGINT', waitForDocker);
113
+ process.off('SIGTERM', stop);
114
+ process.off('SIGHUP', stop);
115
+ }
116
+ }
117
+ /** `foliwake dev` in Docker until Ctrl+C; the exit code of the engine's `dev` (2: a refused command line). */
118
+ export async function runDevInContainer(ctx, request, cwd = process.cwd()) {
119
+ const port = request.port ?? DEV_PORT;
120
+ await checkRun(ctx, cwd, port);
121
+ const { image, from } = await devImage(ctx, request.image, cwd);
122
+ const mount = mountPoint(cwd);
123
+ ctx.err(`The Foliwake engine serves dev in Docker: ${image} (${from}); this folder is ${mount} there.`);
124
+ const tty = process.stdin.isTTY === true && process.stdout.isTTY === true;
125
+ const name = `foliwake-dev-${randomBytes(4).toString('hex')}`;
126
+ const run = { cwd, mount, port, image, tty, user: hostUser(), name };
127
+ return tiedTo(ctx, name, () => ctx.docker.attached(dockerRunArgs(run, engineDevArgs(request, port))));
128
+ }
@@ -52,8 +52,8 @@ export function blockCatalogStub() {
52
52
  title: 'Block catalog not available',
53
53
  lines: [
54
54
  'The block catalog renders the examples of your blocks (components/<name>/<name>.example.md).',
55
- `Rendering them needs the Foliwake engine (\`foliwake dev\` from the engine) or the open block ` +
56
- 'renderer, which is not available yet.',
55
+ 'Rendering them needs the Foliwake engine (`foliwake dev` without --remote: the engine on this ' +
56
+ 'computer, else in Docker) or the open block renderer, which is not available yet.',
57
57
  'Pages from the remote site work as usual: open any page path.',
58
58
  ],
59
59
  });
@@ -0,0 +1,4 @@
1
+ import { type Asker, type DockerContext, type PromptStreams } from './context.js';
2
+ export declare function clackAsker(io: PromptStreams): Asker;
3
+ /** The context's questions (clack on its streams unless a test gave its own). */
4
+ export declare const askerOf: (ctx: DockerContext) => Asker;
@@ -0,0 +1,34 @@
1
+ // The questions of `foliwake docker` in a terminal, drawn by @clack/prompts as in `foliwake create`
2
+ // (src/create/prompts.ts). Secrets use the hidden `password` prompt and are never echoed.
3
+ // Ctrl+C or Escape throws DockerCancelled.
4
+ import { confirm, intro, isCancel, password, select, text } from '@clack/prompts';
5
+ import { DockerCancelled } from './context.js';
6
+ function answered(value) {
7
+ if (isCancel(value))
8
+ throw new DockerCancelled();
9
+ return value;
10
+ }
11
+ export function clackAsker(io) {
12
+ return {
13
+ intro: (title) => intro(title, io),
14
+ text: async (message, { initial, validate } = {}) => {
15
+ // An empty answer takes the initial value; it is validated as that value.
16
+ const defaults = initial !== undefined ? { placeholder: initial, defaultValue: initial } : {};
17
+ const check = (typed) => validate?.(typed || initial || '');
18
+ const value = answered(await text({ ...io, message, ...defaults, validate: check }));
19
+ return value === '' && initial !== undefined ? initial : value;
20
+ },
21
+ secret: async (message, { optional = false } = {}) => {
22
+ const check = (typed) => (optional || typed ? undefined : 'Required');
23
+ return answered(await password({ ...io, message, validate: check }));
24
+ },
25
+ select: async (message, choices, initial) => {
26
+ const options = choices.map((choice) => ({ ...choice }));
27
+ const value = answered(await select({ ...io, message, initialValue: initial, options }));
28
+ return value;
29
+ },
30
+ confirm: async (message, initial) => answered(await confirm({ ...io, message, initialValue: initial })),
31
+ };
32
+ }
33
+ /** The context's questions (clack on its streams unless a test gave its own). */
34
+ export const askerOf = (ctx) => ctx.asker ?? clackAsker(ctx.prompts);
@@ -0,0 +1,21 @@
1
+ import { type DockerContext } from './context.js';
2
+ import { type Instance } from './instance.js';
3
+ /** `<instance folder>/backups/<UTC time>.tar.gz` on this computer. */
4
+ export declare function defaultBackupFile(env: NodeJS.ProcessEnv, name: string, now?: Date): string;
5
+ /**
6
+ * Writes the backup of the instance's data volume to `out` (default: defaultBackupFile); the file. `image`
7
+ * runs the backup (default: the server's): `docker update` passes the image it switches to, so a fix of
8
+ * the backup itself takes effect before the switch.
9
+ */
10
+ export declare function backupInstance(ctx: DockerContext, instance: Instance, out?: string, image?: string): Promise<string>;
11
+ /** `docker backup [name] [--out FILE]`. */
12
+ export declare function runBackup(ctx: DockerContext, instance: Instance, out?: string): Promise<number>;
13
+ export interface RestoreRequest {
14
+ from: string;
15
+ /** Delete the volume's current data first (confirmed by the caller). */
16
+ replace: boolean;
17
+ }
18
+ /** `docker restore [name] --from FILE [--replace]`: the server must be stopped. */
19
+ export declare function runRestore(ctx: DockerContext, instance: Instance, request: RestoreRequest): Promise<number>;
20
+ /** --replace deletes the volume's data: the name typed in a terminal, --yes without one; false: cancelled. */
21
+ export declare function confirmReplace(ctx: DockerContext, name: string, yes: boolean): Promise<boolean>;