Skip to content

Commit

Permalink
feat(cli): handle remote and local repositories
Browse files Browse the repository at this point in the history
Refs #5, #10
  • Loading branch information
MHCYR committed Oct 5, 2023
1 parent 58bbb07 commit 0838fbc
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { input, confirm } from "@inquirer/prompts";
import { input, confirm, select } from "@inquirer/prompts";
import * as fs from "node:fs";
import OpenApiMocker from "open-api-mocker";
import cloneGitRepository from "./services/clone-git-repository.js";
import findOasFromDir from "./services/find-oas-from-dir.js";

const testRepoSSH = "[email protected]:os3/manatee/sirenia.git";
// const testRepoHTTPS = "https://gitlab.sngular.com/os3/manatee/sirenia.git"; // TODO: replace by user input
const RC_FILE_NAME = ".apimockrc";
// TODO: extract to configuration file?

const RC_FILE_NAME = ".apimockrc";
const TEMP_FOLDER_NAME = ".api-mock-runner";
const main = async () => {
let config;

Expand All @@ -21,6 +23,8 @@ const main = async () => {
const useExistingConfig = await confirm({
message: "Do you want to use the existing config?",
});

// TODO: ask values to user if not using existing config
if (useExistingConfig) config = existingConfig;
} else {
const schemasOrigin = await input({
Expand Down Expand Up @@ -66,7 +70,7 @@ const main = async () => {
/*
* TODO:
* validate path type (local or url) [DONE]
* if remote repo, clone it, add temp dir to gitignore
* if remote repo, clone it, add temp dir to gitignore [DONE]
* run findOasFromDir on the temp or local dir
* CLI shows the list of schemas found
* CLI asks for the schema to mock (select from list)
Expand All @@ -83,13 +87,37 @@ const main = async () => {

const isOriginRemote = isOriginRemoteRegex.test(config.schemasOrigin);

await cloneGitRepository(config.schemasOrigin);
if (isOriginRemote) {
await cloneGitRepository(config.schemasOrigin);
// TODO: DRY, create write to .gitignore function
fs.appendFile(
`${process.cwd()}/.gitignore`,
`\n${TEMP_FOLDER_NAME}/$}`,
(err) => {
if (err) {
console.error(err);
} else {
console.log(`${TEMP_FOLDER_NAME} added to .gitignore`);
}
}
);
}

const schemasDir = isOriginRemote ? TEMP_FOLDER_NAME : config.schemasOrigin;

const schemas = await findOasFromDir("./tests");
const schemas = await findOasFromDir(schemasDir);

// TODO: change to checkboxes when multiple schemas are supported
const selectedSchema = await select({
message: "Select a schema",
choices: schemas.map((schema) => {
return { name: schema.fileName, value: schema.filePath };
}),
});

const openApiMocker = new OpenApiMocker({
port: config.initialPort,
schema: schemas[0].filePath,
schema: selectedSchema,
watch: true,
});

Expand Down

0 comments on commit 0838fbc

Please sign in to comment.