Skip to content

Commit db1b922

Browse files
committed
support doc publishing for gh-pages
1 parent c193ab0 commit db1b922

File tree

8 files changed

+68
-70
lines changed

8 files changed

+68
-70
lines changed

src/command/publish/cmd.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,6 @@ async function publishAction(
160160
// coalesce options
161161
const publishOptions = await createPublishOptions(options, path);
162162

163-
// helper to validate
164-
const validatePublish = (publishProvider: PublishProvider) => {
165-
// validate that we can publish this
166-
const isDocument = typeof (publishOptions.input) === "string";
167-
if (isDocument && !publishProvider.canPublishDocuments) {
168-
throw new Error(
169-
`Publishing single documents is not supported for ${publishProvider.description}`,
170-
);
171-
}
172-
};
173-
174-
// validate if we can
175-
if (provider) {
176-
validatePublish(provider);
177-
}
178-
179163
// helper to publish (w/ account confirmation)
180164
const doPublish = async (
181165
publishProvider: PublishProvider,
@@ -194,9 +178,6 @@ async function publishAction(
194178
publishTarget,
195179
);
196180

197-
// validate that we can publish this
198-
validatePublish(publishProvider);
199-
200181
if (account) {
201182
// do the publish
202183
await publish(

src/command/publish/deployment.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { warning } from "log/mod.ts";
9+
import { dirname } from "path/mod.ts";
910

1011
import { Select } from "cliffy/prompt/select.ts";
1112
import { Confirm } from "cliffy/prompt/confirm.ts";
@@ -105,23 +106,21 @@ export async function publishDeployments(
105106
): Promise<PublishDeploymentWithAccount[]> {
106107
const deployments: PublishDeploymentWithAccount[] = [];
107108

108-
// see if provider has a static PublishRecord
109-
const isProject = typeof (options.input) !== "string";
110-
if (isProject) {
111-
for (const provider of await publishProviders()) {
112-
if (
113-
(!providerFilter || providerFilter === provider.name) &&
114-
provider.publishRecord
115-
) {
116-
const record = await (provider.publishRecord(
117-
(options.input as ProjectContext).dir,
118-
));
119-
if (record) {
120-
deployments.push({
121-
provider,
122-
target: record,
123-
});
124-
}
109+
// see if there are any static publish records for this directory
110+
const publishDir = typeof (options.input) === "string"
111+
? dirname(options.input)
112+
: (options.input as ProjectContext).dir;
113+
for (const provider of await publishProviders()) {
114+
if (
115+
(!providerFilter || providerFilter === provider.name) &&
116+
provider.publishRecord
117+
) {
118+
const record = await (provider.publishRecord(publishDir));
119+
if (record) {
120+
deployments.push({
121+
provider,
122+
target: record,
123+
});
125124
}
126125
}
127126
}

src/publish/common/publish.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,13 @@ export async function handlePublish<
109109
target = target!;
110110

111111
// render
112-
let publishFiles = await render(target.url);
113-
114-
// validate that the main document is html or pdf
115-
if (
116-
type === "document" &&
117-
!isHtmlContent(publishFiles.rootFile) &&
118-
!isPdfContent(publishFiles.rootFile)
119-
) {
120-
throw new Error(
121-
`Documents published to ${handler.name} must be either HTML or PDF.`,
122-
);
123-
}
124-
125-
// if this is a document then stage the files
126-
if (type === "document") {
127-
publishFiles = stageDocumentPublish(title, publishFiles);
128-
}
112+
const publishFiles = await renderForPublish(
113+
render,
114+
handler.name,
115+
type,
116+
title,
117+
target.url,
118+
);
129119

130120
// function to resolve the full path of a file
131121
// (given that redirects could be in play)
@@ -237,6 +227,35 @@ export async function handlePublish<
237227
];
238228
}
239229

230+
export async function renderForPublish(
231+
render: (siteUrl?: string) => Promise<PublishFiles>,
232+
providerName: string,
233+
type: "document" | "site",
234+
title: string,
235+
siteUrl?: string,
236+
) {
237+
// render
238+
let publishFiles = await render(siteUrl);
239+
240+
// validate that the main document is html or pdf
241+
if (
242+
type === "document" &&
243+
!isHtmlContent(publishFiles.rootFile) &&
244+
!isPdfContent(publishFiles.rootFile)
245+
) {
246+
throw new Error(
247+
`Documents published to ${providerName} must be either HTML or PDF.`,
248+
);
249+
}
250+
251+
// if this is a document then stage the files
252+
if (type === "document") {
253+
publishFiles = stageDocumentPublish(title, publishFiles);
254+
}
255+
256+
return publishFiles;
257+
}
258+
240259
function stageDocumentPublish(title: string, publishFiles: PublishFiles) {
241260
// create temp dir
242261
const publishDir = globalTempContext().createDir();

src/publish/gh-pages/gh-pages.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
*/
77

88
import { info } from "log/mod.ts";
9-
import { join, relative } from "path/mod.ts";
9+
import { dirname, join, relative } from "path/mod.ts";
1010
import { copy, existsSync } from "fs/mod.ts";
1111
import * as colors from "fmt/colors.ts";
1212

1313
import { Confirm } from "cliffy/prompt/confirm.ts";
1414

1515
import { removeIfExists, which } from "../../core/path.ts";
1616
import { execProcess } from "../../core/process.ts";
17-
import { projectContext } from "../../project/project-context.ts";
1817

19-
import { kProjectOutputDir, ProjectContext } from "../../project/types.ts";
18+
import { ProjectContext } from "../../project/types.ts";
2019
import {
2120
AccountToken,
2221
anonymousAccount,
@@ -28,6 +27,7 @@ import { shortUuid } from "../../core/uuid.ts";
2827
import { sleep } from "../../core/wait.ts";
2928
import { joinUrl } from "../../core/url.ts";
3029
import { completeMessage, withSpinner } from "../../core/console.ts";
30+
import { renderForPublish } from "../common/publish.ts";
3131

3232
export const kGhpages = "gh-pages";
3333
const kGhpagesDescription = "GitHub Pages";
@@ -36,7 +36,6 @@ export const ghpagesProvider: PublishProvider = {
3636
name: kGhpages,
3737
description: kGhpagesDescription,
3838
requiresServer: false,
39-
canPublishDocuments: false,
4039
listOriginOnly: false,
4140
accountTokens,
4241
authorizeToken,
@@ -96,14 +95,17 @@ function resolveTarget(
9695

9796
async function publish(
9897
_account: AccountToken,
99-
_type: "document" | "site",
98+
type: "document" | "site",
10099
input: string,
101-
_title: string,
100+
title: string,
102101
_slug: string,
103102
render: (siteUrl?: string) => Promise<PublishFiles>,
104103
options: PublishOptions,
105-
_target?: PublishRecord,
104+
target?: PublishRecord,
106105
): Promise<[PublishRecord | undefined, URL | undefined]> {
106+
// convert input to dir if necessary
107+
input = Deno.statSync(input).isDirectory ? input : dirname(input);
108+
107109
// get context
108110
const ghContext = await gitHubContext(input);
109111

@@ -143,12 +145,13 @@ async function publish(
143145
]);
144146

145147
// render
146-
const project = (await projectContext(input))!;
147-
const outputDir = project.config?.project[kProjectOutputDir];
148-
if (outputDir === undefined) {
149-
throwUnableToPublish("no output-dir defined for project");
150-
}
151-
const renderResult = await render(ghContext.siteUrl);
148+
const renderResult = await renderForPublish(
149+
render,
150+
"gh-pages",
151+
type,
152+
title,
153+
target?.url,
154+
);
152155

153156
// allocate worktree dir
154157
const tempDir = Deno.makeTempDirSync({ dir: input });

src/publish/netlify/netlify.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export const netlifyProvider: PublishProvider = {
4141
name: kNetlify,
4242
description: kNetlifyDescription,
4343
requiresServer: false,
44-
canPublishDocuments: true,
4544
listOriginOnly: false,
4645
accountTokens,
4746
authorizeToken,

src/publish/provider.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export interface PublishProvider {
7777
name: string;
7878
description: string;
7979
requiresServer: boolean;
80-
canPublishDocuments: boolean;
8180
listOriginOnly: boolean;
8281
publishRecord?: (dir: string) => Promise<PublishRecord | undefined>;
8382
accountTokens: () => Promise<AccountToken[]>;

src/publish/quarto-pub/quarto-pub.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export const quartoPubProvider: PublishProvider = {
3232
name: kQuartoPub,
3333
description: "Quarto Pub",
3434
requiresServer: false,
35-
canPublishDocuments: true,
3635
listOriginOnly: false,
3736
accountTokens,
3837
authorizeToken,

src/publish/rsconnect/rsconnect.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export const rsconnectProvider: PublishProvider = {
4141
name: kRSConnect,
4242
description: kRSConnectDescription,
4343
requiresServer: true,
44-
canPublishDocuments: true,
4544
listOriginOnly: true,
4645
accountTokens,
4746
authorizeToken,

0 commit comments

Comments
 (0)