@ordino.ai/cli
Version:
ordino.ai global command line interface
186 lines (175 loc) ⢠6.02 kB
text/typescript
import { execSync } from "child_process";
import fs from "fs";
import path from "path";
import {
createStandardBoilerplateAPI,
createStandardBoilerplateUI,
} from "./boilerplate/createStandardBoilerplate";
import { Project, Platformtype } from "./models/models";
import {
convertToValidPackageName,
getProjectsList
} from "./prompts/prompts";
import { printAsciiArt, printMessage } from "./utils/printMessage.util";
import { serviceFactory } from "./config/service-factory";
import { projectService } from "./service/ordino.project.service";
import { initializeAuthInterceptors } from "./config/axios.config";
import { cognitoAuthService } from "./service/ordino.auth.service";
import { packageVersion } from "./config/package-version";
import { displayVersionCheck, checkPlaywrightBrowsers } from "./utils/versionCheck.util";
const ENVIRONMENT = "local";
export async function localMain(): Promise<void> {
try {
if (!displayVersionCheck()) {
process.exit(1);
}
serviceFactory.setEnvironment(ENVIRONMENT);
displayWelcomeBanner(packageVersion);
printMessage("š Starting browser-based authentication...", "36", true);
const tokens = await cognitoAuthService.authenticate();
if (!tokens) {
printMessage("ā Authentication failed or was cancelled.", "31", true);
process.exit(1);
}
const authToken = (tokens as any).cliApiKey || tokens.access_token || tokens.id_token;
initializeAuthInterceptors(authToken);
let projects = await projectService.getAllProjects();
var projectSelected = await getProjectsList(projects);
var project = await projectService.getProjectDetails(projectSelected.project.id);
const appPath = setupProjectDirectory(
convertToValidPackageName(project.name)
);
await generateBoilerplate(
authToken,
appPath,
project
);
finalizeSetup(
appPath,
convertToValidPackageName(project.name),
project.repositoryURL,
project.includeExampleApps,
project
);
process.exit(1);
} catch (error: any) {
handleError(error);
}
}
function displayWelcomeBanner(env = "DEV"): void {
const art = `
āāāāāāā āāāāāāā āāāāāāā āāāāāāā āāā āāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāā
āāā āāāāāāāāāāāāāā āāāāāāāāāāāā āāāāāā āāā
āāā āāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāā āāā
āāāāāāāāāāāā āāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāā
āāāāāāā āāā āāāāāāāāāā āāāāāā āāāāā āāāāāāā
`;
printAsciiArt(art, false);
printMessage(
`\n Welcome to Ordino CLI (V${env}) \n`,
"33",
true,
true
);
}
function setupProjectDirectory(projectName: string): string {
const appPath = path.join(process.cwd(), projectName);
if (fs.existsSync(appPath)) {
printMessage(`Directory ${projectName} already exists`, "31", true);
printMessage(`Using existing directory`, "33", true);
} else {
fs.mkdirSync(appPath);
printMessage(`Directory ${projectName} created`, "32", true);
}
return appPath;
}
async function generateBoilerplate(
authToken: string,
appPath: string,
project: Project,
): Promise<void> {
const includeSampleTest = project.includeExampleApps;
const needGitActionSetup = true;
if (project.gui != undefined) {
await createStandardBoilerplateUI(
appPath,
project,
includeSampleTest,
authToken,
needGitActionSetup,
ENVIRONMENT,
project.platform
);
}
if (project.api != undefined) {
await createStandardBoilerplateAPI(
appPath,
project,
includeSampleTest,
authToken,
needGitActionSetup,
ENVIRONMENT,
project.platform
);
}
}
function finalizeSetup(
appPath: string,
projectName: string,
gitUrl: string | null,
includeSampleTest: boolean,
project: Project
): void {
printMessage("Installing dependencies...", "32", true);
execSync("npm run initialize", { cwd: appPath, stdio: "inherit" });
if (project.platform === Platformtype.Playwright) {
printMessage("\nš Checking Playwright browser binaries...", "36", true);
if (!checkPlaywrightBrowsers(appPath)) {
process.exit(1);
}
}
if (includeSampleTest) {
printMessage("Running sample tests...", "32", true);
execSync("npm run oi:run:test", { cwd: appPath, stdio: "inherit" });
}
if (gitUrl) {
try {
printMessage("Initializing Git repository...", "32", true);
execSync("git init", { cwd: appPath, stdio: "inherit" });
execSync("git add .", { cwd: appPath, stdio: "inherit" });
execSync('git commit -m "Initial commit"', {
cwd: appPath,
stdio: "inherit",
});
execSync("git branch -M main", {
cwd: appPath,
stdio: "inherit",
});
execSync(`git remote add origin ${gitUrl}`, {
cwd: appPath,
stdio: "inherit",
});
execSync("git push -u origin main -f", {
cwd: appPath,
stdio: "inherit",
});
} catch (error) {
printMessage(`Git push failed`, "31", true);
}
}
printMessage(
`\nYour local app "${projectName}" has been successfully created! š`,
"32",
true
);
printMessage("To get started:", "34");
printMessage(` cd ${projectName}`, "35", true);
printMessage(` npm run oi:run:test`, "35", true);
printMessage(` npm run oi:open:test`, "35", true);
printMessage("Welcome to Ordino Local!", "32", true);
}
function handleError(error: any): void {
printMessage(`Error: ${error.message || error}`, "31", true);
process.exit(1);
}