Skip to content

Commit 3e20cba

Browse files
committed
some cleanup of publish functions
1 parent f19735c commit 3e20cba

File tree

7 files changed

+88
-64
lines changed

7 files changed

+88
-64
lines changed

src/command/publish/cmd.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
handleUnauthorized,
3939
resolveAccount,
4040
} from "./account.ts";
41-
import { updateProjectPublishConfig } from "../../publish/config.ts";
41+
import { recordProjectPublishDeployment } from "../../publish/config.ts";
4242
import { render, renderServices } from "../render/render-shared.ts";
4343
import { projectOutputDir } from "../../project/project-shared.ts";
4444
import { PublishRecord } from "../../publish/types.ts";
@@ -130,9 +130,11 @@ async function publish(
130130
target,
131131
);
132132
if (publishRecord) {
133-
await updateProjectPublishConfig(options.project, {
134-
[provider.name]: [publishRecord],
135-
});
133+
recordProjectPublishDeployment(
134+
options.project,
135+
provider.name,
136+
publishRecord,
137+
);
136138
}
137139

138140
// open browser if requested

src/command/publish/deployment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Confirm } from "cliffy/prompt/confirm.ts";
1212

1313
import { findProvider, PublishDeployment } from "../../publish/provider.ts";
1414

15-
import { projectPublishConfig } from "../../publish/config.ts";
15+
import { projectPublishDeployments } from "../../publish/config.ts";
1616

1717
import { resolveAccount } from "./account.ts";
1818
import { PublishOptions } from "./options.ts";
@@ -74,7 +74,7 @@ export async function publishDeployments(
7474
providerFilter?: string,
7575
): Promise<PublishDeployment[]> {
7676
const deployments: PublishDeployment[] = [];
77-
const config = projectPublishConfig(options.project);
77+
const config = projectPublishDeployments(options.project);
7878
for (const providerName of Object.keys(config)) {
7979
if (providerFilter && (providerName !== providerFilter)) {
8080
continue;

src/project/project-shared.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ export function projectVarsFile(dir: string): string | undefined {
5151
.find(existsSync);
5252
}
5353

54-
export const kDefaultProjectPublishFile = "_publish.yml";
55-
56-
export function projectPublishFile(dir: string): string | undefined {
57-
return [kDefaultProjectPublishFile, "_publish.yaml"]
58-
.map((file) => join(dir, file))
59-
.find(existsSync);
60-
}
61-
6254
export function projectOffset(context: ProjectContext, input: string) {
6355
const projDir = Deno.realPathSync(context.dir);
6456
const inputDir = Deno.realPathSync(dirname(input));

src/publish/common/publish.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface PublishDeploy {
2727
id?: string;
2828
state?: string;
2929
required?: string[];
30+
url?: string;
3031
admin_url?: string;
3132
}
3233

@@ -103,12 +104,7 @@ export async function publishSite<
103104
// wait for it to be ready
104105
while (true) {
105106
siteDeploy = await handler.getDeploy(siteDeploy.id!);
106-
if (siteDeploy.state === "prepared") {
107-
if (!siteDeploy.required) {
108-
throw new Error(
109-
"Site deploy prepared but no required files provided",
110-
);
111-
}
107+
if (siteDeploy.state === "prepared" || siteDeploy.state === "ready") {
112108
break;
113109
}
114110
await sleep(250);
@@ -141,21 +137,23 @@ export async function publishSite<
141137
completeMessage(`Uploading files (complete)`);
142138

143139
// wait on ready
140+
let targetUrl = target.url;
144141
let adminUrl = target.url;
145142
await withSpinner({
146143
message: "Deploying published site",
147144
}, async () => {
148145
while (true) {
149146
const deployReady = await handler.getDeploy(siteDeploy?.id!);
150147
if (deployReady.state === "ready") {
148+
targetUrl = deployReady.url || targetUrl;
151149
adminUrl = deployReady.admin_url || adminUrl;
152150
break;
153151
}
154152
await sleep(500);
155153
}
156154
});
157155

158-
completeMessage(`Published: ${target.url}\n`);
156+
completeMessage(`Published: ${targetUrl}\n`);
159157

160-
return [target, new URL(adminUrl)];
158+
return [{ ...target, url: targetUrl }, new URL(adminUrl)];
161159
}

src/publish/config.ts

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,57 @@
77

88
import { stringify } from "encoding/yaml.ts";
99
import { join } from "path/mod.ts";
10+
import { existsSync } from "fs/mod.ts";
1011

1112
import { Metadata } from "../config/types.ts";
12-
import { mergeConfigs } from "../core/config.ts";
1313
import { readYaml, readYamlFromString } from "../core/yaml.ts";
14-
import {
15-
kDefaultProjectPublishFile,
16-
projectPublishFile,
17-
} from "../project/project-shared.ts";
1814
import { ProjectContext } from "../project/types.ts";
19-
import { ProjectPublish } from "./types.ts";
15+
import { PublishDeployments, PublishRecord } from "./types.ts";
2016

21-
export function projectPublishConfig(
17+
export function projectPublishDeployments(
2218
project: ProjectContext,
23-
): ProjectPublish {
24-
const projectDir = project.dir;
25-
const publishFile = projectPublishFile(projectDir);
26-
if (publishFile) {
27-
return readYaml(publishFile) as ProjectPublish;
19+
): PublishDeployments {
20+
const deplomentsFile = publishDeploymentsFile(project.dir);
21+
if (deplomentsFile) {
22+
return readYaml(deplomentsFile) as PublishDeployments;
2823
} else {
29-
return {} as ProjectPublish;
24+
return {} as PublishDeployments;
3025
}
3126
}
3227

33-
export function updateProjectPublishConfig(
34-
target: ProjectContext,
35-
updateConfig: ProjectPublish,
28+
export function recordProjectPublishDeployment(
29+
project: ProjectContext,
30+
provider: string,
31+
publish: PublishRecord,
3632
) {
33+
// read base config
3734
let indent = 2;
38-
let baseConfig: Metadata = {};
39-
const projectDir = target.dir;
40-
const publishFile = projectPublishFile(projectDir);
41-
if (publishFile) {
42-
const publishFileYaml = Deno.readTextFileSync(publishFile);
43-
indent = detectIndentLevel(publishFileYaml);
44-
baseConfig = readYamlFromString(publishFileYaml) as Metadata;
35+
let deployments: PublishDeployments = {};
36+
const projectDir = project.dir;
37+
const deploymentsFile = publishDeploymentsFile(project.dir);
38+
if (deploymentsFile) {
39+
const deploymentsFileYaml = Deno.readTextFileSync(deploymentsFile);
40+
indent = detectIndentLevel(deploymentsFileYaml);
41+
deployments = readYamlFromString(deploymentsFileYaml) as PublishDeployments;
42+
}
43+
44+
// update as required
45+
if (deployments[provider]) {
46+
const deploymentIdx = deployments[provider].findIndex(
47+
(published) => published.id === publish.id,
48+
);
49+
if (deploymentIdx !== -1) {
50+
deployments[provider][deploymentIdx] = publish;
51+
} else {
52+
deployments[provider].push(publish);
53+
}
54+
} else {
55+
deployments[provider] = [publish];
4556
}
46-
const updatedConfig = mergeConfigs(baseConfig, updateConfig);
57+
4758
Deno.writeTextFileSync(
48-
publishFile || join(projectDir, kDefaultProjectPublishFile),
49-
stringifyPublishConfig(updatedConfig, indent),
59+
deploymentsFile || join(projectDir, kDefaultPublishDeploymetsFile),
60+
stringifyPublishConfig(deployments, indent),
5061
);
5162
}
5263

@@ -65,3 +76,11 @@ function detectIndentLevel(yaml: string) {
6576
const spaceMatch = yaml.match(/\n(\s+)/);
6677
return spaceMatch ? spaceMatch[1].length : 2;
6778
}
79+
80+
const kDefaultPublishDeploymetsFile = "_publish.yml";
81+
82+
export function publishDeploymentsFile(dir: string): string | undefined {
83+
return [kDefaultPublishDeploymetsFile, "_publish.yaml"]
84+
.map((file) => join(dir, file))
85+
.find(existsSync);
86+
}

src/publish/netlify/netlify.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,31 @@ function publish(
157157
const handler: PublishHandler<Site, Deploy> = {
158158
name: kNetlify,
159159
createSite: async () => {
160-
return await client.site.createSite({
161-
site: {
162-
force_ssl: true,
163-
},
164-
}) as unknown as Site;
160+
return withSslUrl(
161+
await client.site.createSite({
162+
site: {
163+
force_ssl: true,
164+
},
165+
}) as unknown as Site,
166+
);
165167
},
166168
createDeploy: async (siteId: string, files: Record<string, string>) => {
167-
return await client.deploy.createSiteDeploy({
168-
siteId,
169-
deploy: {
170-
files,
171-
async: true,
172-
},
173-
});
169+
return withSslUrl(
170+
await client.deploy.createSiteDeploy({
171+
siteId,
172+
deploy: {
173+
files,
174+
async: true,
175+
},
176+
}),
177+
);
174178
},
175179
getDeploy: async (deployId: string) => {
176-
return await client.deploy.getDeploy({
177-
deployId,
178-
});
180+
return withSslUrl(
181+
await client.deploy.getDeploy({
182+
deployId,
183+
}),
184+
);
179185
},
180186
uploadDeployFile: async (
181187
deployId: string,
@@ -195,6 +201,13 @@ function publish(
195201
return publishSite<Site, Deploy>(handler, render, target);
196202
}
197203

204+
function withSslUrl(obj: { ssl_url?: string; url?: string }) {
205+
return {
206+
...obj,
207+
url: obj.ssl_url || obj.url,
208+
};
209+
}
210+
198211
function isUnauthorized(err: Error) {
199212
return err instanceof ApiError && err.status === 401;
200213
}

src/publish/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
*/
77

8-
export type ProjectPublish = Record<string, Array<PublishRecord>>;
8+
export type PublishDeployments = Record<string, Array<PublishRecord>>;
99

1010
export type PublishRecord = {
1111
id: string;

0 commit comments

Comments
 (0)