Skip to content

Commit

Permalink
feat: add ssh git clone functionality
Browse files Browse the repository at this point in the history
First approach, waiting feedback to finish feature.
Tested on MacOS, testing on Windows needed.
  • Loading branch information
javier-sierra-sngular committed Oct 2, 2023
1 parent 251b802 commit ee09f2d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.api-mock-runner
19 changes: 12 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const OpenApiMocker = require('open-api-mocker')
const OpenApiMocker = require("open-api-mocker");
const cloneGitRepository = require("./services/clone-git-repository");

const main = async () => {
const testRepoSSH = "git@gitlab.sngular.com:os3/manatee/sirenia.git"; // TODO: replace by user input
const testRepoHTTPS = "https://gitlab.sngular.com/os3/manatee/sirenia.git"; // TODO: replace by user input
await cloneGitRepository(testRepoSSH);

const openApiMocker = new OpenApiMocker({
port: 5000,
schema: `${__dirname}/../tests/schema-examples/basic.yaml`,
watch: true
})
watch: true,
});

await openApiMocker.validate()
await openApiMocker.validate();

await openApiMocker.mock()
}
await openApiMocker.mock();
};

main()
main();
62 changes: 62 additions & 0 deletions src/services/clone-git-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// TODO:
// - Refactor
// - console.logs
// - tests
// - doc

const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const process = require("process");

const TEMP_FOLDER_NAME = ".api-mock-runner"; // TODO: extract to configuration file?

async function cloneSchemaRepo(repositoryURL) {
resetTempDir();
cloneRepository(repositoryURL);
printDirectoryContent();
await waitUserInput(); // FIXME: Temp until command line interface is finished
}

function resetTempDir() {
removeTempDir();
fs.mkdirSync(TEMP_FOLDER_NAME);
}

function removeTempDir() {
if (fs.existsSync(TEMP_FOLDER_NAME)) {
fs.rmSync(TEMP_FOLDER_NAME, { recursive: true });
}
}

function cloneRepository(repositoryURL) {
execSync(`git clone ${repositoryURL} .`, {
cwd: path.resolve(process.cwd(), TEMP_FOLDER_NAME), // path to where you want to save the file
});
}

function printDirectoryContent() {
console.log("Directory content:"); // FIXME: console.log
fs.readdirSync(TEMP_FOLDER_NAME).forEach((file) => {
console.log(` - ${file}`);
});
}

async function waitUserInput() {
const readLineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const message = "Waiting user input to finish...";
return new Promise((resolve) =>
readLineInterface.question(message, answer => {
readLineInterface.close();
removeTempDir();
resolve(answer);
})
);
}

module.exports = cloneSchemaRepo;

0 comments on commit ee09f2d

Please # to comment.