1- import { simpleGit } from "simple-git" ;
21import axios from "axios" ;
32import * as path from "path" ;
43import filetype from "file-type" ;
@@ -18,18 +17,20 @@ import {
1817} from "../../utils/utils.js" ;
1918import { getValidationSummary } from "../api/validate.js" ;
2019import { AuthorizationError , GetValidationParams } from "../../types/api/validate.js" ;
21- import { metadataFileContent , staticPortalRepoUrl } from "../../config/env.js" ;
20+ import { metadataFileContent } from "../../config/env.js" ;
2221import { PortalQuickstartPrompts } from "../../prompts/portal/quickstart.js" ;
2322import { AuthenticationError } from "../../types/utils.js" ;
2423import { ZipService } from "../../infrastructure/zip-service.js" ;
2524import { FilePath } from "../../types/file/filePath.js" ;
2625import { DirectoryPath } from "../../types/file/directoryPath.js" ;
2726import { FileName } from "../../types/file/fileName.js" ;
27+ import { withDirPath } from "../../infrastructure/tmp-extensions.js" ;
28+ import { FileService } from "../../infrastructure/file-service.js" ;
2829
2930export class PortalQuickstartController {
3031 private readonly zipService = new ZipService ( ) ;
31- private readonly specUrl =
32- "https://github.com/apimatic/static-portal-workflow/blob/master/spec/openapi.json" ;
32+ private readonly fileService = new FileService ( ) ;
33+ private readonly specUrl = "https://github.com/apimatic/static-portal-workflow/blob/master/spec/openapi.json" ;
3334
3435 async isUserAuthenticated ( configDir : string ) : Promise < boolean > {
3536 const storedAuth = await getAuthInfo ( configDir ) ;
@@ -114,10 +115,13 @@ export class PortalQuickstartController {
114115 }
115116 } else {
116117 specPath = path . normalize ( specPath ) ;
117- const fileType = await filetype . fromFile ( specPath ) ;
118+ const fileType = await filetype . fromFile ( specPath ) ;
118119
119120 if ( fileType ?. ext === "zip" ) {
120- await this . zipService . unArchive ( new FilePath ( new DirectoryPath ( path . dirname ( specPath ) ) , new FileName ( path . basename ( specPath ) ) ) , new DirectoryPath ( tempSpecDir ) ) ;
121+ await this . zipService . unArchive (
122+ new FilePath ( new DirectoryPath ( path . dirname ( specPath ) ) , new FileName ( path . basename ( specPath ) ) ) ,
123+ new DirectoryPath ( tempSpecDir )
124+ ) ;
121125 } else {
122126 const destinationPath = path . join ( tempSpecDir , path . basename ( specPath ) ) ;
123127 await fsExtra . copy ( specPath , destinationPath ) ;
@@ -208,27 +212,41 @@ export class PortalQuickstartController {
208212 }
209213 }
210214
215+ private async downloadRepositoryFromGitHub ( targetFolder : string ) : Promise < void > {
216+ return await withDirPath ( async ( tempDirectory ) => {
217+ const zipUrl = `https://github.com/apimatic/static-portal-workflow/archive/refs/heads/master.zip` ;
218+ const response = await fetch ( zipUrl ) ;
219+ const repositoryFolderName = "static-portal-workflow-master" ;
220+
221+ if ( ! response . ok ) {
222+ throw new Error ( `Unable to setup your portal, please try again later.` ) ;
223+ }
224+ const arrayBuffer = await response . arrayBuffer ( ) ;
225+ const tempZipPath = new FilePath ( tempDirectory , new FileName ( "static-repo.zip" ) ) ;
226+ await this . fileService . writeBuffer ( tempZipPath , Buffer . from ( arrayBuffer ) ) ;
227+
228+ await this . zipService . unArchive ( tempZipPath , tempDirectory ) ;
229+
230+ const extractedFolderPath = new DirectoryPath ( tempDirectory . toString ( ) , repositoryFolderName ) ;
231+ await this . fileService . copyDirectoryContents ( extractedFolderPath , new DirectoryPath ( targetFolder ) ) ;
232+ } ) ;
233+ }
234+
211235 async setupBuildDirectory (
212236 prompts : PortalQuickstartPrompts ,
213237 targetFolder : string ,
214238 specFile : SpecFile ,
215239 validationSummary : ApiValidationSummary ,
216240 languages : string [ ]
217241 ) : Promise < void > {
218- const git = simpleGit ( {
219- timeout : {
220- block : 60 * 1000 // 1 minute timeout.
221- }
222- } ) ;
223-
224242 fsExtra . emptyDirSync ( targetFolder ) ;
225243
226244 try {
227- await git . clone ( staticPortalRepoUrl , targetFolder ) ;
245+ await this . downloadRepositoryFromGitHub ( targetFolder ) ;
228246 } catch ( error ) {
229247 prompts . displayBuildDirectoryGenerationErrorMessage ( ) ;
230248 if ( error instanceof Error ) {
231- if ( error . message . includes ( "timed out" ) ) {
249+ if ( error . message . includes ( "timed out" ) || error . message . includes ( "timeout" ) ) {
232250 throw new Error (
233251 getMessageInRedColor (
234252 "The operation timed out while setting up the build directory. Please check your internet connection and try again."
@@ -246,7 +264,6 @@ export class PortalQuickstartController {
246264 }
247265 }
248266
249- await clearDirectory ( path . join ( targetFolder , ".git" ) ) ;
250267 await clearDirectory ( path . join ( targetFolder , ".github" ) ) ;
251268
252269 if ( specFile . localPath && validationSummary . success ) {
0 commit comments