envshed 0.22.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +10 -4
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +34 -82
- package/dist/api.js.map +1 -1
- package/dist/api.test.d.ts +2 -0
- package/dist/api.test.d.ts.map +1 -0
- package/dist/api.test.js +23 -0
- package/dist/api.test.js.map +1 -0
- package/dist/commands/agent.test.js +1 -0
- package/dist/commands/agent.test.js.map +1 -1
- package/dist/commands/branch.d.ts.map +1 -1
- package/dist/commands/branch.js +24 -45
- package/dist/commands/branch.js.map +1 -1
- package/dist/commands/branch.test.js +13 -80
- package/dist/commands/branch.test.js.map +1 -1
- package/dist/commands/completions.d.ts.map +1 -1
- package/dist/commands/completions.js +18 -7
- package/dist/commands/completions.js.map +1 -1
- package/dist/commands/definition.js +1 -1
- package/dist/commands/definition.js.map +1 -1
- package/dist/commands/env-selection.test.d.ts +2 -0
- package/dist/commands/env-selection.test.d.ts.map +1 -0
- package/dist/commands/env-selection.test.js +68 -0
- package/dist/commands/env-selection.test.js.map +1 -0
- package/dist/commands/env.d.ts.map +1 -1
- package/dist/commands/env.js +44 -3
- package/dist/commands/env.js.map +1 -1
- package/dist/commands/env.test.js +6 -6
- package/dist/commands/env.test.js.map +1 -1
- package/dist/commands/init.d.ts +13 -0
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +243 -326
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/init.test.js +202 -115
- package/dist/commands/init.test.js.map +1 -1
- package/dist/commands/login.d.ts +9 -0
- package/dist/commands/login.d.ts.map +1 -1
- package/dist/commands/login.js +56 -7
- package/dist/commands/login.js.map +1 -1
- package/dist/commands/login.test.js +47 -4
- package/dist/commands/login.test.js.map +1 -1
- package/dist/commands/pull.js +1 -1
- package/dist/commands/pull.js.map +1 -1
- package/dist/commands/push.js +5 -5
- package/dist/commands/push.js.map +1 -1
- package/dist/commands/run.d.ts.map +1 -1
- package/dist/commands/run.js +3 -10
- package/dist/commands/run.js.map +1 -1
- package/dist/commands/run.test.js +3 -3
- package/dist/commands/run.test.js.map +1 -1
- package/dist/commands/setup.process.test.d.ts +2 -0
- package/dist/commands/setup.process.test.d.ts.map +1 -0
- package/dist/commands/setup.process.test.js +116 -0
- package/dist/commands/setup.process.test.js.map +1 -0
- package/dist/commands/share.d.ts.map +1 -1
- package/dist/commands/share.js +2 -0
- package/dist/commands/share.js.map +1 -1
- package/dist/config.d.ts +49 -12
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +134 -45
- package/dist/config.js.map +1 -1
- package/dist/config.test.js +22 -0
- package/dist/config.test.js.map +1 -1
- package/dist/context-resolution.test.d.ts +2 -0
- package/dist/context-resolution.test.d.ts.map +1 -0
- package/dist/context-resolution.test.js +89 -0
- package/dist/context-resolution.test.js.map +1 -0
- package/dist/index.js +13 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/context-store.d.ts +15 -0
- package/dist/lib/context-store.d.ts.map +1 -0
- package/dist/lib/context-store.js +69 -0
- package/dist/lib/context-store.js.map +1 -0
- package/dist/lib/setup-discovery.d.ts +19 -0
- package/dist/lib/setup-discovery.d.ts.map +1 -0
- package/dist/lib/setup-discovery.js +101 -0
- package/dist/lib/setup-discovery.js.map +1 -0
- package/dist/lib/setup-discovery.test.d.ts +2 -0
- package/dist/lib/setup-discovery.test.d.ts.map +1 -0
- package/dist/lib/setup-discovery.test.js +39 -0
- package/dist/lib/setup-discovery.test.js.map +1 -0
- package/dist/lib/setup-guide.d.ts +16 -0
- package/dist/lib/setup-guide.d.ts.map +1 -0
- package/dist/lib/setup-guide.js +112 -0
- package/dist/lib/setup-guide.js.map +1 -0
- package/dist/lib/setup-guide.test.d.ts +2 -0
- package/dist/lib/setup-guide.test.d.ts.map +1 -0
- package/dist/lib/setup-guide.test.js +50 -0
- package/dist/lib/setup-guide.test.js.map +1 -0
- package/dist/lib/setup-plan.d.ts +23 -0
- package/dist/lib/setup-plan.d.ts.map +1 -0
- package/dist/lib/setup-plan.js +96 -0
- package/dist/lib/setup-plan.js.map +1 -0
- package/dist/locales/en.json +122 -12
- package/dist/locales/es.json +122 -12
- package/dist/locales/pt-BR.json +122 -12
- package/dist/personal-connection.test.d.ts +2 -0
- package/dist/personal-connection.test.d.ts.map +1 -0
- package/dist/personal-connection.test.js +103 -0
- package/dist/personal-connection.test.js.map +1 -0
- package/package.json +3 -3
package/dist/commands/init.js
CHANGED
|
@@ -1,352 +1,269 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import chalk from "chalk";
|
|
5
|
-
import ora from "ora";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
6
4
|
import inquirer from "inquirer";
|
|
7
|
-
import { apiGet, apiPost } from "../api.js";
|
|
5
|
+
import { apiGet, readJson, apiPost, ApiError } from "../api.js";
|
|
6
|
+
import { clearToken, getApiUrl, setApiUrlOverride, getConfig, saveConfig, getStoredToken, getOriginalCwd, getProjectConfigDir, findConnection, personalConnectionPath } from "../config.js";
|
|
8
7
|
import { t } from "../i18n.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
import { loginWithDevice } from "./login.js";
|
|
9
|
+
import { discoverApplications, applicationAt, slugify } from "../lib/setup-discovery.js";
|
|
10
|
+
import { ensureTarget, persistTarget, readSetupConfig } from "../lib/setup-plan.js";
|
|
11
|
+
import { guideSetup } from "../lib/setup-guide.js";
|
|
12
|
+
const CREATE = "__create__";
|
|
13
|
+
const SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
14
|
+
function requireSlug(slug) { if (!SLUG.test(slug))
|
|
15
|
+
throw new Error(t("setup.invalidSlug")); return slug; }
|
|
16
|
+
async function ask(questions, interactive) {
|
|
17
|
+
if (!interactive)
|
|
18
|
+
throw new Error(t("setup.nonInteractive"));
|
|
19
|
+
return inquirer.prompt(questions);
|
|
20
|
+
}
|
|
21
|
+
async function confirm(message, interactive) {
|
|
22
|
+
const { confirmed } = await ask([{ type: "confirm", name: "confirmed", message, default: false }], interactive);
|
|
23
|
+
return confirmed;
|
|
24
|
+
}
|
|
25
|
+
async function authenticate(interactive) {
|
|
26
|
+
const credential = process.env.ENVSHED_TOKEN || getStoredToken();
|
|
27
|
+
if (!credential) {
|
|
28
|
+
if (!interactive)
|
|
29
|
+
throw new Error(t("setup.needsLogin"));
|
|
30
|
+
await loginWithDevice();
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
return (await readJson(await apiGet("/orgs"))).organizations;
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
if (!(err instanceof ApiError) || err.status !== 401)
|
|
37
|
+
throw err;
|
|
38
|
+
if (process.env.ENVSHED_TOKEN || !interactive)
|
|
39
|
+
throw new Error(t("setup.needsLogin"));
|
|
40
|
+
await loginWithDevice();
|
|
41
|
+
return (await readJson(await apiGet("/orgs"))).organizations;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export async function runInit(opts, interactive = !!process.stdin.isTTY && !!process.stdout.isTTY && !process.env.CI) {
|
|
45
|
+
const started = Date.now();
|
|
46
|
+
const cwd = getOriginalCwd();
|
|
47
|
+
const configured = getProjectConfigDir();
|
|
48
|
+
let boundary = cwd;
|
|
49
|
+
while (path.dirname(boundary) !== boundary && !fs.existsSync(path.join(boundary, ".git")))
|
|
50
|
+
boundary = path.dirname(boundary);
|
|
51
|
+
const outsideBoundary = configured && fs.existsSync(path.join(boundary, ".git")) && path.relative(boundary, configured).split(path.sep)[0] === "..";
|
|
52
|
+
const existing = findConnection();
|
|
53
|
+
// Who the connection is for: the team (.envshed.json in the repository, the
|
|
54
|
+
// default) or only this user (kept on this machine, outside the repository).
|
|
55
|
+
let personal = !!opts.personal;
|
|
56
|
+
if (!personal && !opts.yes && interactive) {
|
|
57
|
+
const { audience } = await ask([{ type: "select", name: "audience", message: t("setup.audience"), default: existing?.source === "personal" ? "personal" : "team", choices: [{ name: t("setup.audienceTeam"), value: "team" }, { name: t("setup.audiencePersonal"), value: "personal" }] }], interactive);
|
|
58
|
+
personal = audience === "personal";
|
|
59
|
+
}
|
|
60
|
+
// A personal connection belongs to a checkout: its root is the folder with .git.
|
|
61
|
+
if (personal && !fs.existsSync(path.join(boundary, ".git")))
|
|
62
|
+
throw new Error(t("setup.personalNeedsGit"));
|
|
63
|
+
const root = personal ? boundary : outsideBoundary ? cwd : configured || cwd;
|
|
64
|
+
const rootFile = personal ? personalConnectionPath(root) : path.join(root, ".envshed.json");
|
|
65
|
+
const shared = readSetupConfig(rootFile);
|
|
66
|
+
for (const value of [opts.org, opts.project, opts.env])
|
|
67
|
+
if (value)
|
|
68
|
+
requireSlug(value);
|
|
69
|
+
if (opts.apiUrl) {
|
|
70
|
+
const url = new URL(opts.apiUrl);
|
|
71
|
+
if (!(["https:", "http:"].includes(url.protocol)) || url.username || url.password || url.search || url.hash)
|
|
72
|
+
throw new Error(t("setup.invalidApi"));
|
|
73
|
+
// Plain HTTP would send the bearer token in clear text: loopback only.
|
|
74
|
+
const host = url.hostname.toLowerCase().replace(/^\[|\]$/g, "");
|
|
75
|
+
const loopback = host === "localhost" || host === "127.0.0.1" || host === "::1" || host.endsWith(".localhost");
|
|
76
|
+
if (url.protocol === "http:" && !loopback)
|
|
77
|
+
throw new Error(t("setup.invalidApi"));
|
|
78
|
+
// An explicit CLI choice is user-controlled. Persist outside the committed repo.
|
|
79
|
+
if (!opts.yes && !await confirm(t("setup.apiConfirm", { url: url.origin }), interactive))
|
|
80
|
+
return [];
|
|
81
|
+
// A stored token belongs to the API it was issued by. Never send it to a
|
|
82
|
+
// different destination: drop it so the new destination needs its own login.
|
|
83
|
+
if (new URL(getApiUrl()).origin !== url.origin)
|
|
84
|
+
clearToken();
|
|
85
|
+
setApiUrlOverride(url.href.replace(/\/$/, ""));
|
|
86
|
+
saveConfig({ ...getConfig(), apiUrl: url.href.replace(/\/$/, "") });
|
|
87
|
+
}
|
|
88
|
+
console.log(t("setup.destination", { api: new URL(getApiUrl()).origin, directory: root }));
|
|
89
|
+
const organizations = await authenticate(interactive);
|
|
90
|
+
let discovered;
|
|
91
|
+
try {
|
|
92
|
+
discovered = discoverApplications(root);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
throw new Error(t("setup.invalidWorkspace"));
|
|
96
|
+
}
|
|
97
|
+
if (discovered.limited)
|
|
98
|
+
console.log(t("setup.discoveryLimited"));
|
|
99
|
+
let applications;
|
|
100
|
+
if (opts.workspace) {
|
|
23
101
|
try {
|
|
24
|
-
|
|
102
|
+
applications = [applicationAt(root, opts.workspace)];
|
|
25
103
|
}
|
|
26
104
|
catch {
|
|
27
|
-
|
|
105
|
+
throw new Error(t("setup.invalidPath"));
|
|
28
106
|
}
|
|
29
107
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
console.log(chalk.dim(JSON.stringify(existing, null, 2)));
|
|
36
|
-
console.log();
|
|
37
|
-
}
|
|
38
|
-
// Ensure org is set
|
|
39
|
-
let orgSlug = opts.org || existing.org;
|
|
40
|
-
if (!orgSlug) {
|
|
41
|
-
orgSlug = await selectOrCreateOrg();
|
|
42
|
-
}
|
|
43
|
-
existing.org = orgSlug;
|
|
44
|
-
// Clean up single-project fields when switching to workspace mode
|
|
45
|
-
delete existing.project;
|
|
46
|
-
delete existing.defaultEnv;
|
|
47
|
-
console.log(chalk.bold(`\n${t("init.configureWorkspace", { path: wsPath })}\n`));
|
|
48
|
-
const projectSlug = opts.project || (await selectOrCreateProject(orgSlug));
|
|
49
|
-
const envSlug = opts.env || (await selectOrCreateEnv(orgSlug, projectSlug));
|
|
50
|
-
const file = await selectEnvFileName();
|
|
51
|
-
if (!existing.workspaces) {
|
|
52
|
-
existing.workspaces = {};
|
|
53
|
-
}
|
|
54
|
-
const wsConfig = {
|
|
55
|
-
project: projectSlug,
|
|
56
|
-
defaultEnv: envSlug,
|
|
57
|
-
};
|
|
58
|
-
if (file)
|
|
59
|
-
wsConfig.file = file;
|
|
60
|
-
existing.workspaces[wsPath] = wsConfig;
|
|
61
|
-
if (opts.apiUrl)
|
|
62
|
-
existing.apiUrl = opts.apiUrl;
|
|
63
|
-
fs.writeFileSync(configPath, JSON.stringify(existing, null, 2) + "\n");
|
|
64
|
-
console.log(chalk.green("\n" + t("init.addedWorkspace", { path: chalk.bold(wsPath) })));
|
|
65
|
-
console.log(chalk.dim(JSON.stringify(existing, null, 2)));
|
|
66
|
-
return;
|
|
108
|
+
else if (opts.all)
|
|
109
|
+
applications = discovered.applications.filter((app) => app.suggested);
|
|
110
|
+
else if (discovered.applications.length > 1) {
|
|
111
|
+
const { selected } = await ask([{ type: "checkbox", name: "selected", message: t("setup.selectApps"), choices: discovered.applications.map((app) => ({ name: `${app.path} (${app.name})${app.suggested ? "" : ` - ${t("setup.optionalPackage")}`}`, value: app.path, checked: app.suggested })) }], interactive);
|
|
112
|
+
applications = discovered.applications.filter((app) => selected.includes(app.path));
|
|
67
113
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
console.log(
|
|
72
|
-
|
|
73
|
-
{
|
|
74
|
-
type: "confirm",
|
|
75
|
-
name: "update",
|
|
76
|
-
message: t("init.confirmUpdate"),
|
|
77
|
-
default: true,
|
|
78
|
-
},
|
|
79
|
-
]);
|
|
80
|
-
if (!update) {
|
|
81
|
-
process.exit(0);
|
|
82
|
-
}
|
|
114
|
+
else
|
|
115
|
+
applications = discovered.applications.length ? discovered.applications : [applicationAt(root, ".")];
|
|
116
|
+
if (!applications.length) {
|
|
117
|
+
console.log(t("setup.nothingSelected"));
|
|
118
|
+
return [];
|
|
83
119
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
{
|
|
91
|
-
|
|
92
|
-
name: "selectedPackages",
|
|
93
|
-
message: t("init.selectPackages"),
|
|
94
|
-
choices: monorepoPackages.map((p) => ({ name: p, value: p, checked: true })),
|
|
95
|
-
},
|
|
96
|
-
]);
|
|
97
|
-
if (selectedPackages.length > 0) {
|
|
98
|
-
const orgSlug = await selectOrCreateOrg();
|
|
99
|
-
const config = { ...existing, org: orgSlug };
|
|
100
|
-
if (opts.apiUrl)
|
|
101
|
-
config.apiUrl = opts.apiUrl;
|
|
102
|
-
// Set up root project (the monorepo root itself)
|
|
103
|
-
console.log(chalk.bold(`\n${t("init.configureRootProject")}\n`));
|
|
104
|
-
const rootProjectSlug = await selectOrCreateProject(orgSlug);
|
|
105
|
-
const rootEnvSlug = await selectOrCreateEnv(orgSlug, rootProjectSlug);
|
|
106
|
-
config.project = rootProjectSlug;
|
|
107
|
-
config.defaultEnv = rootEnvSlug;
|
|
108
|
-
if (!config.workspaces)
|
|
109
|
-
config.workspaces = {};
|
|
110
|
-
for (const pkg of selectedPackages) {
|
|
111
|
-
console.log(chalk.bold(`\n${t("init.configureWorkspace", { path: pkg })}\n`));
|
|
112
|
-
const projectSlug = await selectOrCreateProject(orgSlug);
|
|
113
|
-
const envSlug = await selectOrCreateEnv(orgSlug, projectSlug);
|
|
114
|
-
const file = await selectEnvFileName();
|
|
115
|
-
const wsConfig = {
|
|
116
|
-
project: projectSlug,
|
|
117
|
-
defaultEnv: envSlug,
|
|
118
|
-
};
|
|
119
|
-
if (file)
|
|
120
|
-
wsConfig.file = file;
|
|
121
|
-
config.workspaces[pkg] = wsConfig;
|
|
122
|
-
}
|
|
123
|
-
fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
124
|
-
console.log(chalk.green("\n" + t("init.createdConfig", { file: CONFIG_FILE })));
|
|
125
|
-
console.log(chalk.dim(JSON.stringify(config, null, 2)));
|
|
126
|
-
return;
|
|
120
|
+
let org = opts.org || shared.org;
|
|
121
|
+
let newOrgName;
|
|
122
|
+
if (!org) {
|
|
123
|
+
if (organizations.length === 1)
|
|
124
|
+
org = organizations[0].slug;
|
|
125
|
+
else {
|
|
126
|
+
const choice = await ask([{ type: "select", name: "org", message: t("init.selectOrg"), choices: [...organizations.map((item) => ({ name: item.name, value: item.slug })), { name: t("init.createNewOrg"), value: CREATE }] }], interactive);
|
|
127
|
+
org = choice.org;
|
|
127
128
|
}
|
|
128
129
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (opts.org && opts.project) {
|
|
134
|
-
orgSlug = opts.org;
|
|
135
|
-
projectSlug = opts.project;
|
|
136
|
-
envSlug = opts.env;
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
orgSlug = await selectOrCreateOrg();
|
|
140
|
-
projectSlug = await selectOrCreateProject(orgSlug);
|
|
141
|
-
envSlug = await selectOrCreateEnv(orgSlug, projectSlug);
|
|
130
|
+
if (org === CREATE || !organizations.length && !opts.org && !shared.org) {
|
|
131
|
+
const { name } = await ask([{ type: "input", name: "name", message: t("init.orgName"), validate: (value) => SLUG.test(slugify(value)) || t("setup.invalidSlug") }], interactive);
|
|
132
|
+
newOrgName = name.trim();
|
|
133
|
+
org = requireSlug(slugify(name));
|
|
142
134
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const match = content.match(/packages:\s*\n((?:\s+-\s+.+\n?)+)/);
|
|
166
|
-
if (match) {
|
|
167
|
-
for (const line of match[1].split("\n")) {
|
|
168
|
-
const m = line.match(/^\s+-\s+['"]?([^'"]+)['"]?\s*$/);
|
|
169
|
-
if (m)
|
|
170
|
-
patterns.push(m[1]);
|
|
135
|
+
else if (!organizations.some((item) => item.slug === org))
|
|
136
|
+
throw new Error(t("setup.orgMissing"));
|
|
137
|
+
org = requireSlug(org);
|
|
138
|
+
const canCreate = newOrgName !== undefined || ["owner", "admin"].includes(organizations.find((item) => item.slug === org)?.role ?? "");
|
|
139
|
+
const projects = newOrgName ? [] : (await readJson(await apiGet(`/projects/${org}`))).projects;
|
|
140
|
+
const plan = [];
|
|
141
|
+
const snapshots = new Map();
|
|
142
|
+
for (const app of applications) {
|
|
143
|
+
const appDir = path.resolve(root, app.path);
|
|
144
|
+
const configFile = app.independent ? (personal ? personalConnectionPath(appDir) : path.join(appDir, ".envshed.json")) : rootFile;
|
|
145
|
+
const configBase = app.independent ? appDir : root;
|
|
146
|
+
const saved = readSetupConfig(configFile);
|
|
147
|
+
const mapping = app.independent || app.path === "." ? saved : saved.workspaces?.[app.path];
|
|
148
|
+
let project = opts.project || (mapping?.org && mapping.org !== org ? undefined : mapping?.project);
|
|
149
|
+
let projectName = project;
|
|
150
|
+
let createProject = false;
|
|
151
|
+
if (!project) {
|
|
152
|
+
const match = projects.find((item) => item.slug === slugify(app.name));
|
|
153
|
+
if (opts.yes) {
|
|
154
|
+
project = match?.slug || requireSlug(slugify(app.name));
|
|
155
|
+
projectName = project;
|
|
156
|
+
createProject = !match;
|
|
171
157
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
158
|
+
else {
|
|
159
|
+
const { selected } = await ask([{ type: "select", name: "selected", message: t("setup.projectFor", { path: app.path }), default: match?.slug || (canCreate ? CREATE : projects[0]?.slug), choices: [...projects.map((item) => ({ name: item.name, value: item.slug })), ...(canCreate ? [{ name: t("init.createNewProject"), value: CREATE }] : [])] }], interactive);
|
|
160
|
+
if (selected === CREATE) {
|
|
161
|
+
const { name } = await ask([{ type: "input", name: "name", message: t("init.projectName"), default: slugify(app.name), validate: (value) => SLUG.test(slugify(value)) || t("setup.invalidSlug") }], interactive);
|
|
162
|
+
project = requireSlug(slugify(name));
|
|
163
|
+
projectName = name.trim();
|
|
164
|
+
createProject = !projects.some((item) => item.slug === project);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
project = requireSlug(selected);
|
|
168
|
+
projectName = project;
|
|
183
169
|
}
|
|
184
170
|
}
|
|
185
171
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
172
|
+
else if (!projects.some((item) => item.slug === project)) {
|
|
173
|
+
// Explicit flags from dashboard must name real projects, unless the user reviews creation.
|
|
174
|
+
if (opts.yes || !canCreate || !await confirm(t("setup.createNamedProject", { project, path: app.path }), interactive))
|
|
175
|
+
throw new Error(t("setup.projectMissing", { project }));
|
|
176
|
+
createProject = true;
|
|
177
|
+
}
|
|
178
|
+
if (createProject && !canCreate)
|
|
179
|
+
throw new Error(t("setup.noCreatePermission"));
|
|
180
|
+
project = requireSlug(project);
|
|
181
|
+
if (plan.some((target) => target.project === project && target.createProject && createProject))
|
|
182
|
+
throw new Error(t("setup.nameCollision", { project }));
|
|
183
|
+
const environments = createProject ? [] : (await readJson(await apiGet(`/environments/${org}/${project}`))).environments;
|
|
184
|
+
let env = opts.env || mapping?.defaultEnv;
|
|
185
|
+
if (!env) {
|
|
186
|
+
const safe = environments.find((item) => /^(development|dev|local)$/.test(item.slug));
|
|
187
|
+
if (safe)
|
|
188
|
+
env = safe.slug;
|
|
189
|
+
else if (createProject)
|
|
190
|
+
env = "development";
|
|
191
|
+
else {
|
|
192
|
+
const choice = await ask([{ type: "select", name: "env", message: t("setup.envFor", { path: app.path }), choices: [...environments.map((item) => ({ name: item.name, value: item.slug })), ...(canCreate ? [{ name: t("init.createNewEnv"), value: CREATE }] : [])] }], interactive);
|
|
193
|
+
if (choice.env === CREATE) {
|
|
194
|
+
const { name } = await ask([{ type: "input", name: "name", message: t("init.envName"), default: "development", validate: (value) => SLUG.test(value) || t("setup.invalidSlug") }], interactive);
|
|
195
|
+
env = name;
|
|
207
196
|
}
|
|
197
|
+
else
|
|
198
|
+
env = choice.env;
|
|
208
199
|
}
|
|
209
200
|
}
|
|
210
|
-
|
|
211
|
-
|
|
201
|
+
env = requireSlug(env);
|
|
202
|
+
const createEnv = !environments.some((item) => item.slug === env);
|
|
203
|
+
if (createEnv && !canCreate)
|
|
204
|
+
throw new Error(t("setup.envMissing", { env }));
|
|
205
|
+
if (!snapshots.has(configFile))
|
|
206
|
+
snapshots.set(configFile, fs.existsSync(configFile) ? fs.readFileSync(configFile, "utf8") : undefined);
|
|
207
|
+
plan.push({ app, org, project, projectName: projectName, createProject, env, createEnv, configFile, configBase, personal, removeLegacyApiUrl: !!opts.apiUrl });
|
|
208
|
+
}
|
|
209
|
+
console.log(t("setup.planTitle"));
|
|
210
|
+
if (newOrgName)
|
|
211
|
+
console.log(t("setup.orgPlan", { org }));
|
|
212
|
+
for (const target of plan)
|
|
213
|
+
console.log(t("setup.planLine", { path: target.app.path, org, project: target.project, env: target.env, action: t(target.createProject ? "setup.create" : target.createEnv ? "setup.reuseWithNewEnv" : "setup.reuse"), file: target.configFile }));
|
|
214
|
+
console.log(t("setup.personalUnchanged"));
|
|
215
|
+
if (personal)
|
|
216
|
+
console.log(t("setup.personalPlan"));
|
|
217
|
+
if (!opts.yes && !await confirm(t("setup.confirmPlan"), interactive)) {
|
|
218
|
+
console.log(t("env.aborted"));
|
|
219
|
+
return [];
|
|
220
|
+
}
|
|
221
|
+
const complete = [];
|
|
222
|
+
try {
|
|
223
|
+
if (newOrgName) {
|
|
224
|
+
const result = await apiPost("/orgs", { name: newOrgName, slug: org });
|
|
225
|
+
if (result.organization.slug !== org)
|
|
226
|
+
throw new Error(t("setup.projectChanged"));
|
|
227
|
+
}
|
|
228
|
+
for (const target of plan) {
|
|
229
|
+
await ensureTarget(target);
|
|
230
|
+
snapshots.set(target.configFile, persistTarget(root, target, snapshots.get(target.configFile)));
|
|
231
|
+
complete.push(target);
|
|
232
|
+
console.log(t("setup.targetReady", { path: target.app.path }));
|
|
212
233
|
}
|
|
213
234
|
}
|
|
214
|
-
|
|
235
|
+
catch (err) {
|
|
236
|
+
console.error(t("setup.partial", { count: complete.length }));
|
|
237
|
+
throw err;
|
|
238
|
+
}
|
|
239
|
+
console.log(t("setup.complete", { count: complete.length, seconds: Math.round((Date.now() - started) / 1000) }));
|
|
240
|
+
if (opts.guide !== false && interactive)
|
|
241
|
+
await guideSetup(root, complete);
|
|
242
|
+
return complete;
|
|
215
243
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
{
|
|
232
|
-
type: "input",
|
|
233
|
-
name: "customFile",
|
|
234
|
-
message: t("init.customFileName"),
|
|
235
|
-
validate: (v) => (v.trim() ? true : t("common.nameRequired")),
|
|
236
|
-
},
|
|
237
|
-
]);
|
|
238
|
-
return customFile.trim();
|
|
244
|
+
export const initCommand = new Command("init")
|
|
245
|
+
.description(t("init.description"))
|
|
246
|
+
.option("-o, --org <slug>", t("common.orgOption"))
|
|
247
|
+
.option("-p, --project <slug>", t("common.projectOption"))
|
|
248
|
+
.option("-e, --env <slug>", t("common.envOption"))
|
|
249
|
+
.option("--api-url <url>", t("common.apiUrlOption"))
|
|
250
|
+
.option("-w, --workspace <path>", t("init.workspaceOption"))
|
|
251
|
+
.option("--all", t("setup.allOption"))
|
|
252
|
+
.option("-y, --yes", t("setup.yesOption"))
|
|
253
|
+
.option("--no-guide", t("setup.noGuideOption"))
|
|
254
|
+
.option("--personal", t("setup.personalOption"))
|
|
255
|
+
.action(async (opts) => {
|
|
256
|
+
const globalOpts = initCommand.parent?.opts() ?? {};
|
|
257
|
+
try {
|
|
258
|
+
await runInit({ ...opts, workspace: opts.workspace || globalOpts.workspace, all: opts.all || globalOpts.all });
|
|
239
259
|
}
|
|
240
|
-
|
|
241
|
-
|
|
260
|
+
catch (err) {
|
|
261
|
+
if (err instanceof Error && err.name === "ExitPromptError") {
|
|
262
|
+
console.error(t("setup.cancelled"));
|
|
263
|
+
process.exitCode = 1;
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
throw err;
|
|
242
267
|
}
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
async function selectOrCreateOrg() {
|
|
246
|
-
const spinner = ora(t("init.fetchingOrgs")).start();
|
|
247
|
-
const res = await apiGet("/orgs");
|
|
248
|
-
const { organizations } = (await res.json());
|
|
249
|
-
spinner.stop();
|
|
250
|
-
const choices = [
|
|
251
|
-
...organizations.map((o) => ({
|
|
252
|
-
name: `${o.name} ${chalk.dim(`(${o.slug})`)}`,
|
|
253
|
-
value: o.slug,
|
|
254
|
-
})),
|
|
255
|
-
{ name: chalk.cyan(t("init.createNewOrg")), value: CREATE_NEW },
|
|
256
|
-
];
|
|
257
|
-
const { orgSlug } = await inquirer.prompt([
|
|
258
|
-
{
|
|
259
|
-
type: "select",
|
|
260
|
-
name: "orgSlug",
|
|
261
|
-
message: t("init.selectOrg"),
|
|
262
|
-
choices,
|
|
263
|
-
},
|
|
264
|
-
]);
|
|
265
|
-
if (orgSlug !== CREATE_NEW)
|
|
266
|
-
return orgSlug;
|
|
267
|
-
const { name } = await inquirer.prompt([
|
|
268
|
-
{
|
|
269
|
-
type: "input",
|
|
270
|
-
name: "name",
|
|
271
|
-
message: t("init.orgName"),
|
|
272
|
-
validate: (v) => (v.trim() ? true : t("common.nameRequired")),
|
|
273
|
-
},
|
|
274
|
-
]);
|
|
275
|
-
const createSpinner = ora(t("init.creatingOrg")).start();
|
|
276
|
-
const { organization } = (await apiPost("/orgs", { name: name.trim() }));
|
|
277
|
-
createSpinner.succeed(t("init.createdOrg", { name: chalk.bold(organization.name) }));
|
|
278
|
-
return organization.slug;
|
|
279
|
-
}
|
|
280
|
-
async function selectOrCreateProject(orgSlug) {
|
|
281
|
-
const spinner = ora(t("init.fetchingProjects")).start();
|
|
282
|
-
const res = await apiGet(`/projects/${orgSlug}`);
|
|
283
|
-
const { projects } = (await res.json());
|
|
284
|
-
spinner.stop();
|
|
285
|
-
const choices = [
|
|
286
|
-
...projects.map((p) => ({
|
|
287
|
-
name: `${p.name} ${chalk.dim(`(${p.slug})`)}`,
|
|
288
|
-
value: p.slug,
|
|
289
|
-
})),
|
|
290
|
-
{ name: chalk.cyan(t("init.createNewProject")), value: CREATE_NEW },
|
|
291
|
-
];
|
|
292
|
-
const { projectSlug } = await inquirer.prompt([
|
|
293
|
-
{
|
|
294
|
-
type: "select",
|
|
295
|
-
name: "projectSlug",
|
|
296
|
-
message: t("init.selectProject"),
|
|
297
|
-
choices,
|
|
298
|
-
},
|
|
299
|
-
]);
|
|
300
|
-
if (projectSlug !== CREATE_NEW)
|
|
301
|
-
return projectSlug;
|
|
302
|
-
const { name } = await inquirer.prompt([
|
|
303
|
-
{
|
|
304
|
-
type: "input",
|
|
305
|
-
name: "name",
|
|
306
|
-
message: t("init.projectName"),
|
|
307
|
-
validate: (v) => (v.trim() ? true : t("common.nameRequired")),
|
|
308
|
-
},
|
|
309
|
-
]);
|
|
310
|
-
const createSpinner = ora(t("init.creatingProject")).start();
|
|
311
|
-
const { project } = (await apiPost(`/projects/${orgSlug}`, {
|
|
312
|
-
name: name.trim(),
|
|
313
|
-
}));
|
|
314
|
-
createSpinner.succeed(t("init.createdProject", { name: chalk.bold(project.name) }));
|
|
315
|
-
return project.slug;
|
|
316
|
-
}
|
|
317
|
-
async function selectOrCreateEnv(orgSlug, projectSlug) {
|
|
318
|
-
const spinner = ora(t("init.fetchingEnvs")).start();
|
|
319
|
-
const res = await apiGet(`/environments/${orgSlug}/${projectSlug}`);
|
|
320
|
-
const { environments } = (await res.json());
|
|
321
|
-
spinner.stop();
|
|
322
|
-
const choices = [
|
|
323
|
-
...environments.map((e) => ({
|
|
324
|
-
name: `${e.name} ${chalk.dim(`(${e.slug})`)}`,
|
|
325
|
-
value: e.slug,
|
|
326
|
-
})),
|
|
327
|
-
{ name: chalk.cyan(t("init.createNewEnv")), value: CREATE_NEW },
|
|
328
|
-
];
|
|
329
|
-
const { envSlug } = await inquirer.prompt([
|
|
330
|
-
{
|
|
331
|
-
type: "select",
|
|
332
|
-
name: "envSlug",
|
|
333
|
-
message: t("init.selectEnv"),
|
|
334
|
-
choices,
|
|
335
|
-
},
|
|
336
|
-
]);
|
|
337
|
-
if (envSlug !== CREATE_NEW)
|
|
338
|
-
return envSlug;
|
|
339
|
-
const { name } = await inquirer.prompt([
|
|
340
|
-
{
|
|
341
|
-
type: "input",
|
|
342
|
-
name: "name",
|
|
343
|
-
message: t("init.envName"),
|
|
344
|
-
validate: (v) => (v.trim() ? true : t("common.nameRequired")),
|
|
345
|
-
},
|
|
346
|
-
]);
|
|
347
|
-
const createSpinner = ora(t("init.creatingEnv")).start();
|
|
348
|
-
const { environment } = (await apiPost(`/environments/${orgSlug}/${projectSlug}`, { name: name.trim() }));
|
|
349
|
-
createSpinner.succeed(t("init.createdEnv", { name: chalk.bold(environment.name) }));
|
|
350
|
-
return environment.slug;
|
|
351
|
-
}
|
|
268
|
+
});
|
|
352
269
|
//# sourceMappingURL=init.js.map
|