Skip to content

Commit

Permalink
feat(cli): add RC file validation
Browse files Browse the repository at this point in the history
Refs #5 #10
  • Loading branch information
MHCYR committed Oct 5, 2023
1 parent d859692 commit 766ea23
Showing 1 changed file with 62 additions and 23 deletions.
85 changes: 62 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,83 @@ import cloneGitRepository from "./services/clone-git-repository.js";
import findOasFromDir from "./services/find-oas-from-dir.js";

const testRepoSSH = "git@gitlab.sngular.com:os3/manatee/sirenia.git";
const testRepoHTTPS = "https://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
const RC_FILE_NAME = ".apimockrc";

const main = async () => {
const config = {
repoUrl: await input({ message: "Enter the repo url" }),
dirPath: await input({ message: "Enter the directory path" }),
addToGitIgnore: await confirm({
message: "Do you want to add .api-mock-runner to .gitignore?",
}),
saveConfig: await confirm({ message: "Do you want to save the config?" }),
};

console.table(config);

if (config.saveConfig) {
const filePath = `${process.cwd()}/.apimockrc`;
let config;

const configFileExists = fs.existsSync(`${process.cwd()}/.apimockrc`);

if (configFileExists) {
const existingConfig = JSON.parse(
fs.readFileSync(`${process.cwd()}/.apimockrc`)
);
console.table(existingConfig);
const useExistingConfig = await confirm({
message: "Do you want to use the existing config?",
});
if (useExistingConfig) config = existingConfig;
} else {
const schemasOrigin = await input({
message: "Enter the repo url or relative path",
});
const initialPort = await input({
message: "Enter the initial port",
default: 1234,
transformer: (value) => parseInt(value),
});

config = {
schemasOrigin,
initialPort,
};
// Create .apimockrc file
const filePath = `${process.cwd()}/${RC_FILE_NAME}`;
fs.writeFile(filePath, JSON.stringify(config), (err) => {
if (err) {
console.error(err);
} else {
console.log("Config saved");
}
});
const addRcFileToGitignore = await confirm({
message: `Add ${RC_FILE_NAME} to .gitignore?`,
});
if (addRcFileToGitignore) {
// TODO: create function that validates if is already in gitignore
fs.appendFile(`${process.cwd()}/.gitignore`, `\n${RC_FILE_NAME}`, (err) => {
if (err) {
console.error(err);
} else {
console.log(`${RC_FILE_NAME} added to .gitignore`);
}
});
}
}
/*
* TODO:
* validate path type (local or url)
* if remote repo, clone it, add temp dir to gitignore
* 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)
* Start server on selected port
*/

await cloneGitRepository(config.repoUrl || testRepoSSH);
await cloneGitRepository(config.schemasOrigin || testRepoSSH);

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

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

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

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

main();

0 comments on commit 766ea23

Please # to comment.