Skip to content

Commit 73a3d83

Browse files
committed
strong concept of a publish provider
1 parent b3a86a8 commit 73a3d83

File tree

7 files changed

+123
-96
lines changed

7 files changed

+123
-96
lines changed

src/command/publish/cmd.ts

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,69 @@
77

88
import { Command } from "cliffy/command/mod.ts";
99
import { Select } from "cliffy/prompt/select.ts";
10-
import { PublishOptions } from "./common.ts";
11-
12-
import { ghpagesCommand, ghpagesPublish, kGhpages } from "./ghpages.ts";
13-
import { kNetlify, netlifyCommand, netlifyPublish } from "./netlify.ts";
14-
15-
export const publishCommand = new Command()
16-
.name("publish")
17-
.arguments("[path:string]")
18-
.option(
19-
"--no-render",
20-
"Do not render before publishing.",
21-
)
22-
.description(
23-
"Publish a document or project to a variety of destinations.",
24-
// deno-lint-ignore no-explicit-any
25-
).action(async (options: any, path?: string) => {
26-
// shared options
27-
const publishOptions: PublishOptions = {
28-
path: path || Deno.cwd(),
29-
render: !!options.render,
30-
};
31-
32-
// select method
33-
const method: string = await Select.prompt({
34-
message: "Select destination:",
35-
options: [
36-
{ name: "Netlify", value: kNetlify },
37-
{ name: "GitHub Pages", value: kGhpages },
38-
],
39-
});
40-
41-
switch (method) {
42-
case kNetlify:
43-
netlifyPublish(publishOptions);
44-
break;
45-
46-
case kGhpages:
47-
ghpagesPublish(publishOptions);
48-
break;
49-
}
50-
})
51-
.command(kNetlify, netlifyCommand)
52-
.command(kGhpages, ghpagesCommand);
10+
11+
import { PublishOptions } from "./provider.ts";
12+
13+
import { ghpagesProvider } from "./ghpages.ts";
14+
import { netlifyProvider } from "./netlify.ts";
15+
16+
const kPublishProviders = [netlifyProvider, ghpagesProvider];
17+
18+
export const publishCommand = withProviders(
19+
new Command()
20+
.name("publish")
21+
.arguments("[path:string]")
22+
.option(
23+
"--no-render",
24+
"Do not render before publishing.",
25+
)
26+
.description(
27+
"Publish a document or project to a variety of destinations.",
28+
// deno-lint-ignore no-explicit-any
29+
).action(async (options: any, path?: string) => {
30+
// shared options
31+
const publishOptions: PublishOptions = {
32+
path: path || Deno.cwd(),
33+
render: !!options.render,
34+
};
35+
36+
// select provider
37+
const name: string = await Select.prompt({
38+
message: "Select destination:",
39+
options: kPublishProviders.map((provider) => ({
40+
name: provider.description,
41+
value: provider.name,
42+
})),
43+
});
44+
45+
// call provider
46+
const provider = findProvider(name);
47+
if (provider) {
48+
provider.configure(publishOptions);
49+
}
50+
}),
51+
);
52+
53+
// deno-lint-ignore no-explicit-any
54+
function withProviders(command: Command<any>): Command<any> {
55+
for (const provider of kPublishProviders) {
56+
command.command(
57+
provider.name,
58+
provider.command(
59+
new Command()
60+
.name(provider.name)
61+
.description(provider.description)
62+
.arguments("[path:string]")
63+
.option(
64+
"--no-render",
65+
"Do not render before publishing.",
66+
),
67+
),
68+
);
69+
}
70+
return command;
71+
}
72+
73+
function findProvider(name: string) {
74+
return kPublishProviders.find((provider) => provider.name === name);
75+
}

src/command/publish/common.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/command/publish/ghpages.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@
55
*
66
*/
77

8-
import { PublishOptions, publishSubcommand } from "./common.ts";
8+
import { Command } from "cliffy/command/mod.ts";
99

10-
export const kGhpages = "ghpages";
10+
import { PublishOptions, PublishProvider } from "./provider.ts";
1111

12-
export const ghpagesCommand = publishSubcommand(
13-
kGhpages,
14-
"Publish to GitHub Pages",
15-
)
16-
// deno-lint-ignore no-explicit-any
17-
.action((options: any, path?: string) => {
18-
path = path || Deno.cwd();
19-
ghpagesPublish({
20-
path,
21-
render: !!options.render,
22-
});
23-
console.log("ghpages");
24-
console.log(path);
25-
});
12+
export const ghpagesProvider: PublishProvider = {
13+
name: "ghpages",
14+
description: "GitHub Pages",
15+
command: (command: Command) => {
16+
return command
17+
// deno-lint-ignore no-explicit-any
18+
.action((options: any, path?: string) => {
19+
ghpagesConfigure({
20+
path: path || Deno.cwd(),
21+
render: !!options.render,
22+
});
23+
});
24+
},
25+
configure: ghpagesConfigure,
26+
};
2627

27-
export function ghpagesPublish(options: PublishOptions) {
28+
function ghpagesConfigure(options: PublishOptions) {
2829
console.log("ghpages");
2930
console.log(options);
3031
}

src/command/publish/netlify.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@
55
*
66
*/
77

8-
import { PublishOptions, publishSubcommand } from "./common.ts";
8+
import { Command } from "cliffy/command/mod.ts";
99

10-
export const kNetlify = "netlify";
10+
import { PublishOptions, PublishProvider } from "./provider.ts";
1111

12-
export const netlifyCommand = publishSubcommand(kNetlify, "Publish to netlify")
13-
// deno-lint-ignore no-explicit-any
14-
.action((options: any, path?: string) => {
15-
path = path || Deno.cwd();
16-
netlifyPublish({
17-
path,
18-
render: !!options.render,
19-
});
20-
});
12+
export const netlifyProvider: PublishProvider = {
13+
name: "netlify",
14+
description: "Netlify",
15+
command: (command: Command) => {
16+
return command
17+
// deno-lint-ignore no-explicit-any
18+
.action((options: any, path?: string) => {
19+
netlifyConfigure({
20+
path: path || Deno.cwd(),
21+
render: !!options.render,
22+
});
23+
});
24+
},
25+
configure: netlifyConfigure,
26+
};
2127

22-
export function netlifyPublish(options: PublishOptions) {
28+
function netlifyConfigure(options: PublishOptions) {
2329
console.log("netlify");
2430
console.log(options);
2531
}

src/command/publish/provider.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* common.ts
3+
*
4+
* Copyright (C) 2020 by RStudio, PBC
5+
*
6+
*/
7+
8+
import { Command } from "cliffy/command/mod.ts";
9+
10+
export interface PublishOptions {
11+
path: string;
12+
render: boolean;
13+
}
14+
15+
export interface PublishProvider {
16+
name: string;
17+
description: string;
18+
// deno-lint-ignore no-explicit-any
19+
command: (command: Command<any>) => Command<any>;
20+
configure: (options: PublishOptions) => void;
21+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)