Skip to content

Commit 04d4669

Browse files
authored
fix: remove simple-git dependency (#151)
1 parent ea4910d commit 04d4669

5 files changed

Lines changed: 52 additions & 42 deletions

File tree

package-lock.json

Lines changed: 0 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
"neverthrow": "^8.2.0",
7171
"open": "^8.4.0",
7272
"prettier": "^2.8.8",
73-
"simple-git": "^3.27.0",
7473
"striptags": "^3.2.0",
7574
"tmp-promise": "^3.0.3",
7675
"treeify": "^1.1.0",

src/config/env.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export const baseURL = "https://api.apimatic.io";
2-
export const staticPortalRepoUrl = "https://github.com/apimatic/static-portal-workflow.git";
32
export const metadataFileContent = {
43
ImportSettings: {
54
AutoGenerateTestCases: false,

src/controllers/portal/quickstart.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { simpleGit } from "simple-git";
21
import axios from "axios";
32
import * as path from "path";
43
import filetype from "file-type";
@@ -18,18 +17,20 @@ import {
1817
} from "../../utils/utils.js";
1918
import { getValidationSummary } from "../api/validate.js";
2019
import { AuthorizationError, GetValidationParams } from "../../types/api/validate.js";
21-
import { metadataFileContent, staticPortalRepoUrl } from "../../config/env.js";
20+
import { metadataFileContent } from "../../config/env.js";
2221
import { PortalQuickstartPrompts } from "../../prompts/portal/quickstart.js";
2322
import { AuthenticationError } from "../../types/utils.js";
2423
import { ZipService } from "../../infrastructure/zip-service.js";
2524
import { FilePath } from "../../types/file/filePath.js";
2625
import { DirectoryPath } from "../../types/file/directoryPath.js";
2726
import { FileName } from "../../types/file/fileName.js";
27+
import { withDirPath } from "../../infrastructure/tmp-extensions.js";
28+
import { FileService } from "../../infrastructure/file-service.js";
2829

2930
export 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) {

src/infrastructure/file-service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "fs";
22
import fsExtra from "fs-extra";
3+
import * as path from "path";
34
import { FilePath } from "../types/file/filePath.js";
45
import { DirectoryPath } from "../types/file/directoryPath.js";
56
import { pipeline } from "stream";
@@ -38,6 +39,21 @@ export class FileService {
3839
await fsExtra.emptyDir(dir.toString()); // removes everything inside, keeps the dir
3940
}
4041

42+
public async copyDirectory(source: DirectoryPath, destination: DirectoryPath) {
43+
await fsExtra.copy(source.toString(), destination.toString());
44+
}
45+
46+
public async copyDirectoryContents(source: DirectoryPath, destination: DirectoryPath) {
47+
const entries = await fsExtra.readdir(source.toString());
48+
await Promise.all(
49+
entries.map(async (entry) => {
50+
const srcEntry = path.join(source.toString(), entry);
51+
const destEntry = path.join(destination.toString(), entry);
52+
await fsExtra.copy(srcEntry, destEntry);
53+
})
54+
);
55+
}
56+
4157
public async deleteFile(filePath: FilePath): Promise<void> {
4258
const exists = await this.fileExists(filePath);
4359
if (exists) {
@@ -62,6 +78,10 @@ export class FileService {
6278
await fsExtra.writeFile(filePath.toString(), contents, 'utf-8');
6379
}
6480

81+
public async writeBuffer(filePath: FilePath, buffer: Buffer) {
82+
await fsExtra.writeFile(filePath.toString(), buffer);
83+
}
84+
6585
public async copy(source: FilePath, destination: FilePath) {
6686
await fsExtra.copyFile(source.toString(), destination.toString());
6787
}

0 commit comments

Comments
 (0)