incode-cli
Version:
CLI to scaffold a Vite-based Incode Web SDK integration project
203 lines (167 loc) • 5.29 kB
JavaScript
#!/usr/bin/env node
import inquirer from 'inquirer';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import chalk from 'chalk';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const QUESTIONS = [
{ type: 'input', name: 'projectName', message: 'Project name:', default: 'my-integration' },
{ type: 'input', name: 'configId', message: 'Incode Configuration ID:' },
{ type: 'input', name: 'apiKey', message: 'Incode API Key:' },
{ type: 'input', name: 'apiUrl', message: 'Incode API URL:' },
{ type: 'input', name: 'sdkUrl', message: 'Web SDK URL (CDN):', default: 'https://sdk.incode.com/sdk/onBoarding-latest.js' },
];
const FILES = {
'index.html': `<!DOCTYPE html>
<html lang="en" translate="no">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="%VITE_INCODE_WEB_SDK_URL%" defer></script>
<script type="module" src="/main.js" defer></script>
<title>Incode Web SDK Integration Example</title>
</head>
<body>
<section id="incode-container"></section>
</body>
</html>`,
'main.js': `import "./styles.css";
import incodeSession from "./session-start.js";
let incode;
let session;
const container = document.querySelector("#incode-container");
function initializeIncodeSDK() {
return window.OnBoarding.create({ apiURL: import.meta.env.VITE_SDK_ENDPOINT });
}
function createIncodeSession() {
return incodeSession.start();
}
function captureIdFrontSide() {
incode.renderCamera("front", container, {
onSuccess: captureIdBackSide,
onError: console.log,
token: session,
numberOfTries: 3,
showTutorial: true,
});
}
function captureIdBackSide() {
incode.renderCamera("back", container, {
onSuccess: processId,
onError: console.log,
token: session,
numberOfTries: 3,
showTutorial: true,
});
}
function processId() {
return incode
.processId({ token: session.token })
.then(() => captureSelfie())
.catch(console.error);
}
function captureSelfie() {
incode.renderCamera("selfie", container, {
onSuccess: finishOnboarding,
onError: console.log,
token: session,
numberOfTries: 3,
});
}
function finishOnboarding() {
incode.getFinishStatus(null, { token: session.token })
.then(console.log)
.catch(console.error);
}
function saveDeviceData() {
incode.sendGeolocation({ token: session.token }).catch(console.error);
incode.sendFingerprint({ token: session.token }).catch(console.error);
}
async function app() {
incode = initializeIncodeSDK();
session = await createIncodeSession();
saveDeviceData();
captureIdFrontSide();
}
document.addEventListener("DOMContentLoaded", app);`,
'session-start.js': `export default {
start: () => {
let myHeaders = new Headers();
myHeaders.append("Accept", "application/json");
myHeaders.append("Content-Type", "application/json");
myHeaders.append("api-version", "1.0");
myHeaders.append("x-api-key", import.meta.env.VITE_INCODE_API_KEY);
let raw = JSON.stringify({
countryCode: "ALL",
configurationId: import.meta.env.VITE_INCODE_CONFIG_ID,
});
let requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
return fetch(\`\${import.meta.env.VITE_SDK_ENDPOINT}/omni/start\`, requestOptions)
.then((res) => res.json())
.catch(console.error);
},
};`,
'styles.css': `:root { --font:sans-serif; } * { font-family: var(--font); }`,
'vite.config.js': `import { defineConfig, loadEnv } from "vite";
import mkcert from "vite-plugin-mkcert";
export default ({ mode }) => {
const env = loadEnv(mode, process.cwd(), "");
return defineConfig({
server: { https: true, host: true },
plugins: [mkcert()],
});
};`,
'package.json': `{
"name": "incode-web-sdk-examples",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"start": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"vite": "^4.2.0",
"vite-plugin-mkcert": "^1.13.4"
}
}`,
'README.md': `# Incode Web SDK Vite Integration
Install dependencies and start the dev server:
\`\`\`bash
npm install
npm run start
\`\`\`
Ensure HTTPS support via mkcert.`
};
const generateFiles = (targetDir, answers) => {
fs.mkdirSync(targetDir, { recursive: true });
for (const [name, content] of Object.entries(FILES)) {
fs.writeFileSync(path.join(targetDir, name), content);
}
const env = `VITE_INCODE_CONFIG_ID=${answers.configId}
VITE_INCODE_API_KEY=${answers.apiKey}
VITE_SDK_ENDPOINT=${answers.apiUrl}
VITE_INCODE_WEB_SDK_URL=${answers.sdkUrl}
`;
fs.writeFileSync(path.join(targetDir, ".env"), env);
};
const run = async () => {
const answers = await inquirer.prompt(QUESTIONS);
const projectDir = path.join(process.cwd(), answers.projectName);
generateFiles(projectDir, answers);
console.log(chalk.green(`\n✔ Project "${answers.projectName}" created.`));
console.log(chalk.blue(`\nNext steps:`));
console.log(` cd ${answers.projectName}`);
console.log(` npm install`);
console.log(` npm run start`);
};
run();