@@ -17,7 +17,6 @@ import { execProcess } from "../../core/process.ts";
1717import { ProjectContext } from "../../project/types.ts" ;
1818import {
1919 AccountToken ,
20- AccountTokenType ,
2120 PublishFiles ,
2221 PublishProvider ,
2322} from "../provider-types.ts" ;
@@ -27,10 +26,13 @@ import { sleep } from "../../core/wait.ts";
2726import { joinUrl } from "../../core/url.ts" ;
2827import { completeMessage , withSpinner } from "../../core/console.ts" ;
2928import { renderForPublish } from "../common/publish.ts" ;
30- import { websiteBaseurl } from "../../project/types/website/website-config.ts" ;
3129import { RenderFlags } from "../../command/render/types.ts" ;
32- import SemVer from "semver/mod.ts" ;
33- import { gitHubContext } from "../../core/github.ts" ;
30+ import { gitCmds , gitVersion } from "../../core/git.ts" ;
31+ import {
32+ anonymousAccount ,
33+ gitHubContextForPublish ,
34+ verifyContext ,
35+ } from "../common/git.ts" ;
3436
3537export const kGhpages = "gh-pages" ;
3638const kGhpagesDescription = "GitHub Pages" ;
@@ -50,35 +52,13 @@ export const ghpagesProvider: PublishProvider = {
5052 isNotFound,
5153} ;
5254
53- function anonymousAccount ( ) : AccountToken {
54- return {
55- type : AccountTokenType . Anonymous ,
56- name : "anonymous" ,
57- server : null ,
58- token : "anonymous" ,
59- } ;
60- }
61-
6255function accountTokens ( ) {
6356 return Promise . resolve ( [ anonymousAccount ( ) ] ) ;
6457}
6558
6659async function authorizeToken ( options : PublishOptions ) {
6760 const ghContext = await gitHubContextForPublish ( options . input ) ;
68-
69- if ( ! ghContext . git ) {
70- throwUnableToPublish ( "git does not appear to be installed on this system" ) ;
71- }
72-
73- // validate we are in a git repo
74- if ( ! ghContext . repo ) {
75- throwUnableToPublish ( "the target directory is not a git repository" ) ;
76- }
77-
78- // validate that we have an origin
79- if ( ! ghContext . originUrl ) {
80- throwUnableToPublish ( "the git repository does not have a remote origin" ) ;
81- }
61+ verifyContext ( ghContext , "GitHub Pages" ) ;
8262
8363 // good to go!
8464 return Promise . resolve ( anonymousAccount ( ) ) ;
@@ -106,28 +86,6 @@ function resolveTarget(
10686 return Promise . resolve ( target ) ;
10787}
10888
109- async function gitVersion ( ) : Promise < SemVer > {
110- const result = await execProcess (
111- {
112- cmd : [ "git" , "--version" ] ,
113- stdout : "piped" ,
114- } ,
115- ) ;
116- if ( ! result . success ) {
117- throw new Error (
118- "Unable to determine git version. Please check that git is installed and available on your PATH." ,
119- ) ;
120- }
121- const match = result . stdout ?. match ( / g i t v e r s i o n ( \d + \. \d + \. \d + ) / ) ;
122- if ( match ) {
123- return new SemVer ( match [ 1 ] ) ;
124- } else {
125- throw new Error (
126- `Unable to determine git version from string ${ result . stdout } ` ,
127- ) ;
128- }
129- }
130-
13189async function publish (
13290 _account : AccountToken ,
13391 type : "document" | "site" ,
@@ -405,38 +363,3 @@ async function gitCreateGhPages(dir: string) {
405363 [ "push" , "origin" , `HEAD:gh-pages` ] ,
406364 ] ) ;
407365}
408-
409- async function gitCmds ( dir : string , cmds : Array < string [ ] > ) {
410- for ( const cmd of cmds ) {
411- if (
412- ! ( await execProcess ( {
413- cmd : [ "git" , ...cmd ] ,
414- cwd : dir ,
415- } ) ) . success
416- ) {
417- throw new Error ( ) ;
418- }
419- }
420- }
421-
422- // validate we have git
423- const throwUnableToPublish = ( reason : string ) => {
424- throw new Error (
425- `Unable to publish to GitHub Pages (${ reason } )` ,
426- ) ;
427- } ;
428-
429- async function gitHubContextForPublish ( input : string | ProjectContext ) {
430- // Create the base context
431- const dir = typeof input === "string" ? dirname ( input ) : input . dir ;
432- const context = await gitHubContext ( dir ) ;
433-
434- // always prefer configured website URL
435- if ( typeof input !== "string" ) {
436- const configSiteUrl = websiteBaseurl ( input ?. config ) ;
437- if ( configSiteUrl ) {
438- context . siteUrl = configSiteUrl ;
439- }
440- }
441- return context ;
442- }
0 commit comments