@saws/cli 2.0.0-beta.0 → 2.0.0-beta.3
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/README.md +96 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/bin/saws.d.ts +0 -1
- package/dist/bin/saws.js +28 -40
- package/dist/commands/deploy/command.d.ts +2 -7
- package/dist/commands/deploy/command.js +13 -19
- package/dist/commands/deploy/index.d.ts +0 -1
- package/dist/commands/deploy/index.js +1 -4
- package/dist/commands/dev/command.d.ts +1 -7
- package/dist/commands/dev/command.js +46 -48
- package/dist/commands/dev/index.d.ts +0 -1
- package/dist/commands/dev/index.js +1 -6
- package/dist/commands/dev/tui/dev-tui.d.ts +18 -3
- package/dist/commands/dev/tui/dev-tui.js +263 -29
- package/dist/commands/host/command.d.ts +2 -2
- package/dist/commands/host/command.js +130 -7
- package/dist/commands/host/index.d.ts +0 -1
- package/dist/commands/host/index.js +4 -3
- package/dist/commands/init/command.d.ts +1 -7
- package/dist/commands/init/command.js +24 -15
- package/dist/commands/init/index.d.ts +0 -1
- package/dist/commands/init/index.js +1 -5
- package/dist/commands/init/templates/gitignore.template.d.ts +1 -0
- package/dist/commands/init/templates/gitignore.template.js +4 -0
- package/dist/commands/init/templates/saws-ts.template.d.ts +3 -0
- package/dist/commands/init/templates/saws-ts.template.js +7 -0
- package/dist/commands/init/templates/tsconfig-json.template.d.ts +1 -0
- package/dist/commands/init/templates/tsconfig-json.template.js +4 -0
- package/dist/commands/secrets/command.d.ts +2 -2
- package/dist/commands/secrets/command.js +21 -9
- package/dist/commands/secrets/index.d.ts +0 -1
- package/dist/commands/secrets/index.js +7 -5
- package/dist/hosts.d.ts +1 -3
- package/dist/hosts.js +11 -10
- package/package.json +8 -25
- package/src/bin/saws.ts +59 -0
- package/src/commands/deploy/command.ts +20 -0
- package/src/commands/deploy/index.ts +8 -0
- package/src/commands/dev/command.ts +68 -0
- package/src/commands/dev/index.ts +5 -0
- package/src/commands/dev/tui/dev-tui.ts +471 -0
- package/src/commands/execute/command.ts.tmp +43 -0
- package/src/commands/execute/index.ts.tmp +9 -0
- package/src/commands/host/command.ts +188 -0
- package/src/commands/host/index.ts +18 -0
- package/src/commands/init/command.ts +28 -0
- package/src/commands/init/index.ts +8 -0
- package/src/commands/init/templates/gitignore.template.ts +4 -0
- package/src/commands/init/templates/saws-ts.template.ts +8 -0
- package/src/commands/init/templates/tsconfig-json.template.ts +4 -0
- package/src/commands/secrets/command.ts +42 -0
- package/src/commands/secrets/index.ts +13 -0
- package/src/hosts.ts +21 -0
- package/tsconfig.json +9 -0
- package/dist/bin/saws.d.ts.map +0 -1
- package/dist/commands/deploy/command.d.ts.map +0 -1
- package/dist/commands/deploy/index.d.ts.map +0 -1
- package/dist/commands/dev/command.d.ts.map +0 -1
- package/dist/commands/dev/index.d.ts.map +0 -1
- package/dist/commands/dev/tui/dev-tui.d.ts.map +0 -1
- package/dist/commands/host/command.d.ts.map +0 -1
- package/dist/commands/host/command.test.d.ts +0 -2
- package/dist/commands/host/command.test.d.ts.map +0 -1
- package/dist/commands/host/command.test.js +0 -13
- package/dist/commands/host/index.d.ts.map +0 -1
- package/dist/commands/init/command.d.ts.map +0 -1
- package/dist/commands/init/command.test.d.ts +0 -2
- package/dist/commands/init/command.test.d.ts.map +0 -1
- package/dist/commands/init/command.test.js +0 -40
- package/dist/commands/init/index.d.ts.map +0 -1
- package/dist/commands/secrets/command.d.ts.map +0 -1
- package/dist/commands/secrets/index.d.ts.map +0 -1
- package/dist/hosts.d.ts.map +0 -1
|
@@ -1,14 +1,42 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { chmod, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import { ParameterNotFoundError, SecretsManager, ServiceDefinition, getSawsConfigModule, hostSshPublicKeyEnvName, } from "@saws/core";
|
|
2
7
|
import { findConfiguredHosts } from "../../hosts.js";
|
|
3
8
|
export async function configureHostCommand(name, options) {
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
const config = await getSawsConfigModule(options.config);
|
|
10
|
+
if (!(config.secrets instanceof SecretsManager)) {
|
|
11
|
+
throw new Error('saws.ts must export a SecretsManager instance named "secrets"');
|
|
12
|
+
}
|
|
13
|
+
if (!(config.default instanceof ServiceDefinition)) {
|
|
14
|
+
throw new Error("saws.ts must default-export a ServiceDefinition");
|
|
15
|
+
}
|
|
16
|
+
const host = selectHost(findConfiguredHosts(config.default), name);
|
|
17
|
+
validatePrivateKeyReference(host, config.secrets);
|
|
18
|
+
if (options.dryRun) {
|
|
19
|
+
await host.configure({
|
|
20
|
+
bootstrapUser: options.user,
|
|
21
|
+
deploymentPublicKey: "[redacted deployment public key]",
|
|
22
|
+
dryRun: true,
|
|
23
|
+
});
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const { privateKey, created } = await getOrCreateDeploymentKey(config.secrets, host);
|
|
27
|
+
const publicKey = await derivePublicKey(privateKey);
|
|
28
|
+
if (created) {
|
|
29
|
+
await config.secrets.global.set(host.sshPrivateKey.name, privateKey);
|
|
30
|
+
}
|
|
31
|
+
await updatePublicKeyEnvironment(config.secrets.rootDir, hostSshPublicKeyEnvName(host.name), publicKey);
|
|
32
|
+
await host.configure({
|
|
33
|
+
bootstrapUser: options.user,
|
|
34
|
+
deploymentPublicKey: publicKey,
|
|
35
|
+
});
|
|
8
36
|
}
|
|
9
37
|
export function selectHost(hosts, name) {
|
|
10
38
|
if (hosts.length === 0) {
|
|
11
|
-
throw new Error("No
|
|
39
|
+
throw new Error("No hosts are configured");
|
|
12
40
|
}
|
|
13
41
|
const host = name == null
|
|
14
42
|
? hosts.length === 1
|
|
@@ -17,8 +45,103 @@ export function selectHost(hosts, name) {
|
|
|
17
45
|
: hosts.find((candidate) => candidate.name === name);
|
|
18
46
|
if (host != null)
|
|
19
47
|
return host;
|
|
20
|
-
const available = hosts
|
|
48
|
+
const available = hosts
|
|
49
|
+
.map((candidate) => candidate.name)
|
|
50
|
+
.sort()
|
|
51
|
+
.join(", ");
|
|
21
52
|
throw new Error(name == null
|
|
22
53
|
? `Host name is required. Available hosts: ${available}`
|
|
23
54
|
: `Host "${name}" was not found. Available hosts: ${available}`);
|
|
24
55
|
}
|
|
56
|
+
function validatePrivateKeyReference(host, manager) {
|
|
57
|
+
if (host.sshPrivateKey == null || host.sshPrivateKey.scope !== "global") {
|
|
58
|
+
throw new Error(`Host "${host.name}" must configure sshPrivateKey with secrets.global.reference(...)`);
|
|
59
|
+
}
|
|
60
|
+
if (!host.sshPrivateKey.isManagedBy(manager)) {
|
|
61
|
+
throw new Error(`Host "${host.name}" sshPrivateKey must use the SecretsManager exported as "secrets"`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function getOrCreateDeploymentKey(manager, host) {
|
|
65
|
+
try {
|
|
66
|
+
return {
|
|
67
|
+
privateKey: await manager.global.get(host.sshPrivateKey.name),
|
|
68
|
+
created: false,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (!(error instanceof ParameterNotFoundError))
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
privateKey: await generatePrivateKey(),
|
|
77
|
+
created: true,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async function generatePrivateKey() {
|
|
81
|
+
return withTemporaryDirectory(async (directory) => {
|
|
82
|
+
const keyPath = path.join(directory, "id_ed25519");
|
|
83
|
+
await execFileAsync("ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-f", keyPath]);
|
|
84
|
+
return readFile(keyPath, "utf8");
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async function derivePublicKey(privateKey) {
|
|
88
|
+
return withTemporaryDirectory(async (directory) => {
|
|
89
|
+
const keyPath = path.join(directory, "id_ed25519");
|
|
90
|
+
await writeFile(keyPath, privateKey, { mode: 0o600 });
|
|
91
|
+
const { stdout } = await execFileAsync("ssh-keygen", ["-y", "-f", keyPath]);
|
|
92
|
+
const publicKey = stdout.trim();
|
|
93
|
+
if (publicKey.length === 0) {
|
|
94
|
+
throw new Error("Could not derive the deployment public key");
|
|
95
|
+
}
|
|
96
|
+
return publicKey;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async function withTemporaryDirectory(callback) {
|
|
100
|
+
const directory = await mkdtemp(path.join(tmpdir(), "saws-key-"));
|
|
101
|
+
try {
|
|
102
|
+
return await callback(directory);
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
await rm(directory, { recursive: true, force: true });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async function updatePublicKeyEnvironment(rootDir, variableName, publicKey) {
|
|
109
|
+
const envPath = path.resolve(rootDir, ".env");
|
|
110
|
+
let contents = "";
|
|
111
|
+
try {
|
|
112
|
+
contents = await readFile(envPath, "utf8");
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (error.code !== "ENOENT")
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
const matcher = new RegExp(`^(?:export\\s+)?${escapeRegExp(variableName)}\\s*=`);
|
|
119
|
+
const lines = contents.split(/\r?\n/);
|
|
120
|
+
const replacement = `${variableName}=${publicKey}`;
|
|
121
|
+
let replaced = false;
|
|
122
|
+
const updated = lines.flatMap((line) => {
|
|
123
|
+
if (!matcher.test(line))
|
|
124
|
+
return [line];
|
|
125
|
+
if (replaced)
|
|
126
|
+
return [];
|
|
127
|
+
replaced = true;
|
|
128
|
+
return [replacement];
|
|
129
|
+
});
|
|
130
|
+
while (updated.at(-1) === "")
|
|
131
|
+
updated.pop();
|
|
132
|
+
if (!replaced)
|
|
133
|
+
updated.push(replacement);
|
|
134
|
+
const temporaryPath = `${envPath}.${process.pid}.tmp`;
|
|
135
|
+
try {
|
|
136
|
+
await writeFile(temporaryPath, `${updated.join("\n")}\n`, { mode: 0o600 });
|
|
137
|
+
await rename(temporaryPath, envPath);
|
|
138
|
+
await chmod(envPath, 0o600);
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
await rm(temporaryPath, { force: true });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function escapeRegExp(value) {
|
|
145
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
146
|
+
}
|
|
147
|
+
const execFileAsync = promisify(execFile);
|
|
@@ -3,8 +3,9 @@ import { configureHostCommand } from "./command.js";
|
|
|
3
3
|
export const createCommand = () => new Command("host")
|
|
4
4
|
.description("configure deployment hosts")
|
|
5
5
|
.addCommand(new Command("configure")
|
|
6
|
-
.description("
|
|
6
|
+
.description("bootstrap a deployment user and apply host security policy")
|
|
7
7
|
.argument("[name]", "host name; optional when exactly one host exists")
|
|
8
|
-
.
|
|
9
|
-
.option("--
|
|
8
|
+
.requiredOption("--user <bootstrap-user>", "existing SSH account used for initial configuration")
|
|
9
|
+
.option("--config <path>", "path to saws.ts")
|
|
10
|
+
.option("--dry-run", "describe configuration without making changes")
|
|
10
11
|
.action(configureHostCommand));
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
rootDir?: string;
|
|
3
|
-
config?: string;
|
|
4
|
-
dryRun?: boolean;
|
|
5
|
-
}
|
|
6
|
-
export declare function initCommand(serviceName: string, path: string | undefined, options: InitCommandOptions): Promise<void>;
|
|
7
|
-
//# sourceMappingURL=command.d.ts.map
|
|
1
|
+
export declare const initCommand: (serviceName?: string, configPath?: string) => Promise<void>;
|
|
@@ -1,15 +1,24 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import { findServiceDefinition, getSawsConfig } from "@saws/core";
|
|
4
|
+
import { installDependencies } from "@saws/core/utils/dependency-management";
|
|
5
|
+
import { createFileIfNotExists } from "@saws/core/utils/create-file-if-not-exists";
|
|
6
|
+
import { sawsTsTemplate } from "./templates/saws-ts.template.js";
|
|
7
|
+
import { tsconfigJsonTemplate } from "./templates/tsconfig-json.template.js";
|
|
8
|
+
import { gitignoreTemplate } from "./templates/gitignore.template.js";
|
|
9
|
+
export const initCommand = async (serviceName, configPath) => {
|
|
10
|
+
if (serviceName != null) {
|
|
11
|
+
const serviceDefinition = await getSawsConfig(configPath);
|
|
12
|
+
await findServiceDefinition(serviceDefinition, serviceName).init();
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const name = path.parse(path.resolve(".")).name;
|
|
16
|
+
// not used for now
|
|
17
|
+
await installDependencies([]);
|
|
18
|
+
await installDependencies(["@saws/core", "typescript", "@tsconfig/node26"], {
|
|
19
|
+
development: true,
|
|
20
|
+
});
|
|
21
|
+
await fs.writeFile("./tsconfig.json", tsconfigJsonTemplate(), {});
|
|
22
|
+
await createFileIfNotExists("./saws.ts", sawsTsTemplate({ name }));
|
|
23
|
+
await createFileIfNotExists("./.gitignore", gitignoreTemplate());
|
|
24
|
+
};
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
import { initCommand } from "./command.js";
|
|
3
3
|
export const createCommand = () => new Command("init")
|
|
4
|
-
.
|
|
5
|
-
.argument("<service>", "name of the service to initialize")
|
|
4
|
+
.argument("[service]", "name of the service to initialize")
|
|
6
5
|
.argument("[config]", "path to service definition")
|
|
7
|
-
.option("--root-dir <string>", "project root used for generated files")
|
|
8
|
-
.option("--config <string>", "path to service definition")
|
|
9
|
-
.option("--dry-run", "allow service initializers to preview their changes")
|
|
10
6
|
.action(initCommand);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const gitignoreTemplate: () => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const tsconfigJsonTemplate: () => string;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export interface SecretsCommandOptions {
|
|
2
|
+
config?: string;
|
|
2
3
|
stage?: string;
|
|
4
|
+
global?: boolean;
|
|
3
5
|
set?: string;
|
|
4
6
|
get?: boolean;
|
|
5
|
-
rootDir?: string;
|
|
6
7
|
}
|
|
7
8
|
export declare function secretsCommand(name: string, options: SecretsCommandOptions): Promise<void>;
|
|
8
|
-
//# sourceMappingURL=command.d.ts.map
|
|
@@ -1,16 +1,28 @@
|
|
|
1
|
-
import { SecretsManager } from "@saws/
|
|
1
|
+
import { getSawsConfigModule, SecretsManager } from "@saws/core";
|
|
2
2
|
export async function secretsCommand(name, options) {
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
const value = options.set;
|
|
4
|
+
if (options.get && value != null) {
|
|
5
|
+
throw new Error("secrets accepts only one of --get or --set <value>");
|
|
6
|
+
}
|
|
7
|
+
if (!options.get && value == null) {
|
|
8
|
+
throw new Error("secrets requires either --get or --set <value>");
|
|
9
|
+
}
|
|
10
|
+
if (!options.global) {
|
|
11
|
+
process.env.STAGE = options.stage ?? "local";
|
|
12
|
+
}
|
|
13
|
+
const config = await getSawsConfigModule(options.config);
|
|
14
|
+
const manager = config.secrets;
|
|
15
|
+
if (!(manager instanceof SecretsManager)) {
|
|
16
|
+
throw new Error('saws.ts must export a SecretsManager instance named "secrets"');
|
|
17
|
+
}
|
|
18
|
+
const secrets = options.global ? manager.global : manager;
|
|
7
19
|
if (options.get) {
|
|
8
|
-
console.log(await
|
|
20
|
+
console.log(await secrets.get(name));
|
|
9
21
|
return;
|
|
10
22
|
}
|
|
11
|
-
if (
|
|
12
|
-
throw new Error("secrets requires
|
|
23
|
+
if (value == null) {
|
|
24
|
+
throw new Error("secrets requires --set <value>");
|
|
13
25
|
}
|
|
14
|
-
await
|
|
26
|
+
await secrets.set(name, value);
|
|
15
27
|
console.log("Set secret");
|
|
16
28
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
import { secretsCommand } from "./command.js";
|
|
3
3
|
export const createCommand = () => new Command("secrets")
|
|
4
|
-
.
|
|
5
|
-
.
|
|
6
|
-
.option("--
|
|
7
|
-
.option("--
|
|
8
|
-
.option("--
|
|
4
|
+
.description("get or set encrypted project secrets")
|
|
5
|
+
.argument("<name>", "secret name")
|
|
6
|
+
.option("--stage <string>", "stage for a stage-scoped secret", "local")
|
|
7
|
+
.option("--global", "use the global secret scope")
|
|
8
|
+
.option("--set <string>", "set the secret value")
|
|
9
|
+
.option("--get", "get the secret value")
|
|
10
|
+
.option("--config <string>", "path to service definition")
|
|
9
11
|
.action(secretsCommand);
|
package/dist/hosts.d.ts
CHANGED
package/dist/hosts.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { Host } from "@saws/
|
|
1
|
+
import { Host } from "@saws/core";
|
|
2
2
|
export function findConfiguredHosts(root) {
|
|
3
3
|
const hosts = new Set();
|
|
4
|
-
const visited = new
|
|
5
|
-
const visit = (
|
|
6
|
-
if (visited.has(
|
|
4
|
+
const visited = new WeakSet();
|
|
5
|
+
const visit = (value) => {
|
|
6
|
+
if (value == null || typeof value !== "object" || visited.has(value))
|
|
7
7
|
return;
|
|
8
|
-
visited.add(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
visited.add(value);
|
|
9
|
+
if (value instanceof Host) {
|
|
10
|
+
hosts.add(value);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
for (const child of Object.values(value))
|
|
14
|
+
visit(child);
|
|
14
15
|
};
|
|
15
16
|
visit(root);
|
|
16
17
|
return [...hosts];
|
package/package.json
CHANGED
|
@@ -1,34 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saws/cli",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
4
|
-
"description": "
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "git+https://github.com/shichongrui/saws.git",
|
|
8
|
-
"directory": "packages/cli"
|
|
9
|
-
},
|
|
10
|
-
"type": "module",
|
|
3
|
+
"version": "2.0.0-beta.3",
|
|
4
|
+
"description": "",
|
|
11
5
|
"bin": {
|
|
12
|
-
"saws": "dist/bin/saws.js"
|
|
13
|
-
},
|
|
14
|
-
"files": [
|
|
15
|
-
"dist"
|
|
16
|
-
],
|
|
17
|
-
"scripts": {
|
|
18
|
-
"prebuild": "node -e \"fs.rmSync('dist', { recursive: true, force: true })\"",
|
|
19
|
-
"build": "tsc -p tsconfig.json",
|
|
20
|
-
"postbuild": "chmod +x dist/bin/saws.js",
|
|
21
|
-
"test": "npm run build && node --test dist/commands/**/*.test.js",
|
|
22
|
-
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
6
|
+
"saws": "./dist/bin/saws.js"
|
|
23
7
|
},
|
|
8
|
+
"type": "module",
|
|
24
9
|
"dependencies": {
|
|
25
|
-
"@saws/core": "2.0.0-beta.
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"commander": "^14.0.3"
|
|
10
|
+
"@saws/core": "2.0.0-beta.3",
|
|
11
|
+
"commander": "^15.0.0",
|
|
12
|
+
"find-package-json": "^1.2.0"
|
|
29
13
|
},
|
|
30
14
|
"devDependencies": {
|
|
31
|
-
"@types/
|
|
32
|
-
"typescript": "^5.3.3"
|
|
15
|
+
"@types/find-package-json": "^1.2.7"
|
|
33
16
|
}
|
|
34
17
|
}
|
package/src/bin/saws.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
process.on("uncaughtException", (e) => {
|
|
4
|
+
console.log(e);
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
import { default as finder } from "find-package-json";
|
|
8
|
+
import { Command, program } from "commander";
|
|
9
|
+
import { getSawsConfig, ServiceDefinition } from "@saws/core";
|
|
10
|
+
|
|
11
|
+
import { createCommand as createDevCommand } from "../commands/dev/index.js";
|
|
12
|
+
import { createCommand as createDeployCommand } from "../commands/deploy/index.js";
|
|
13
|
+
// import { createCommand as createExecuteCommand } from "../commands/execute/index.js";
|
|
14
|
+
import { createCommand as createInitCommand } from "../commands/init/index.js";
|
|
15
|
+
import { createCommand as createHostCommand } from "../commands/host/index.js";
|
|
16
|
+
import { createCommand as createSecretsCommand } from "../commands/secrets/index.js";
|
|
17
|
+
|
|
18
|
+
const pkg = finder(import.meta.dirname).next().value;
|
|
19
|
+
|
|
20
|
+
program
|
|
21
|
+
.name("saws")
|
|
22
|
+
.description("A tool for building apps quickly")
|
|
23
|
+
.version(pkg?.version ?? "0.0.0");
|
|
24
|
+
|
|
25
|
+
program.addCommand(createDevCommand());
|
|
26
|
+
program.addCommand(createDeployCommand());
|
|
27
|
+
// program.addCommand(createExecuteCommand());
|
|
28
|
+
program.addCommand(createInitCommand());
|
|
29
|
+
program.addCommand(createHostCommand());
|
|
30
|
+
program.addCommand(createSecretsCommand());
|
|
31
|
+
|
|
32
|
+
type ServiceConstructor = typeof ServiceDefinition & {
|
|
33
|
+
getCommands?: (services?: ServiceDefinition[]) => Command[];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const isInitCommand = process.argv[2] === "init";
|
|
37
|
+
|
|
38
|
+
if (!isInitCommand) {
|
|
39
|
+
try {
|
|
40
|
+
const service = await getSawsConfig();
|
|
41
|
+
|
|
42
|
+
const servicesByClass = new Map<ServiceConstructor, ServiceDefinition[]>();
|
|
43
|
+
for (const serviceDefinition of service.getAllDependencies()) {
|
|
44
|
+
const serviceClass = serviceDefinition.constructor as ServiceConstructor;
|
|
45
|
+
servicesByClass.set(serviceClass, [
|
|
46
|
+
...(servicesByClass.get(serviceClass) ?? []),
|
|
47
|
+
serviceDefinition,
|
|
48
|
+
]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
for (const [serviceClass, services] of servicesByClass) {
|
|
52
|
+
serviceClass.getCommands?.(services)?.forEach((command) => program.addCommand(command));
|
|
53
|
+
}
|
|
54
|
+
} catch {
|
|
55
|
+
// Project commands are unavailable until a saws.ts file exists.
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await program.parseAsync(process.argv);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// import { createCacheDir } from "@saws/utils/create-directories";
|
|
2
|
+
import { getSawsConfig } from "@saws/core";
|
|
3
|
+
|
|
4
|
+
export const deployCommand = async (path: string, { stage }: { stage: string }) => {
|
|
5
|
+
if (stage == null || stage.length === 0) {
|
|
6
|
+
throw new Error("deploy requires --stage <string>");
|
|
7
|
+
}
|
|
8
|
+
if (stage === "local") {
|
|
9
|
+
console.warn("Can not deploy to local stage");
|
|
10
|
+
process.exit();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
process.env.STAGE = stage;
|
|
14
|
+
|
|
15
|
+
// await createCacheDir();
|
|
16
|
+
|
|
17
|
+
const serviceDefinition = await getSawsConfig(path);
|
|
18
|
+
|
|
19
|
+
await serviceDefinition.deploy(stage);
|
|
20
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { deployCommand } from "./command.js";
|
|
3
|
+
|
|
4
|
+
export const createCommand = () =>
|
|
5
|
+
new Command("deploy")
|
|
6
|
+
.option("--stage <string>", "stage to deploy")
|
|
7
|
+
.argument("[string]", "path to service definition")
|
|
8
|
+
.action(deployCommand);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createCacheDir } from "@saws/core/utils/create-directories";
|
|
2
|
+
import { onProcessExit } from "@saws/core/utils/on-exit";
|
|
3
|
+
import { getSawsConfig, type ServiceDefinition } from "@saws/core";
|
|
4
|
+
import { DevTui } from "./tui/dev-tui.js";
|
|
5
|
+
|
|
6
|
+
export const devCommand = async (path: string) => {
|
|
7
|
+
process.env.NODE_ENV = "development";
|
|
8
|
+
process.env.STAGE = "local";
|
|
9
|
+
process.env.AWS_REGION = "us-west-2";
|
|
10
|
+
|
|
11
|
+
await createCacheDir();
|
|
12
|
+
|
|
13
|
+
const serviceDefinition = await getSawsConfig(path);
|
|
14
|
+
const services = collectServices(serviceDefinition);
|
|
15
|
+
const useTui = process.stdout.isTTY && process.stdin.isTTY;
|
|
16
|
+
const tui = new DevTui(services.map((service) => service.name));
|
|
17
|
+
|
|
18
|
+
const shutdown = () => {
|
|
19
|
+
try {
|
|
20
|
+
serviceDefinition.exit();
|
|
21
|
+
} finally {
|
|
22
|
+
tui.stop();
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
onProcessExit(shutdown);
|
|
27
|
+
process.once("SIGTERM", () => {
|
|
28
|
+
shutdown();
|
|
29
|
+
process.exit();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (useTui) {
|
|
33
|
+
serviceDefinition.setRuntimeLogSink(tui.logSink);
|
|
34
|
+
tui.start();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
await serviceDefinition.dev();
|
|
38
|
+
|
|
39
|
+
if (useTui) {
|
|
40
|
+
for (const service of services) {
|
|
41
|
+
service.getStdOut()?.on("data", (chunk: Buffer) => {
|
|
42
|
+
tui.logSink({
|
|
43
|
+
serviceName: service.name,
|
|
44
|
+
stream: "stdout",
|
|
45
|
+
chunk: chunk.toString("utf8"),
|
|
46
|
+
timestamp: new Date(),
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
service.getStdErr()?.on("data", (chunk: Buffer) => {
|
|
50
|
+
tui.logSink({
|
|
51
|
+
serviceName: service.name,
|
|
52
|
+
stream: "stderr",
|
|
53
|
+
chunk: chunk.toString("utf8"),
|
|
54
|
+
timestamp: new Date(),
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
for (const service of services) {
|
|
60
|
+
service.getStdOut()?.pipe(process.stdout);
|
|
61
|
+
service.getStdErr()?.pipe(process.stderr);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
function collectServices(root: ServiceDefinition) {
|
|
67
|
+
return [...new Set(root.getAllDependencies())];
|
|
68
|
+
}
|