spanwright
Version:
CLI tool to generate Cloud Spanner E2E testing framework projects with Go database tools and Playwright browser automation
172 lines (169 loc) • 7.58 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfiguration = getConfiguration;
exports.generateEnvironmentContent = generateEnvironmentContent;
const readline = __importStar(require("readline"));
const constants_1 = require("./constants");
const errors_1 = require("./errors");
const validation_1 = require("./validation");
class PromptInterface {
constructor() {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
}
question(query) {
return new Promise(resolve => {
this.rl.question(query, resolve);
});
}
close() {
this.rl.close();
}
}
async function getConfiguration(isNonInteractive) {
if (isNonInteractive) {
return getNonInteractiveConfiguration();
}
else {
return getInteractiveConfiguration();
}
}
function getNonInteractiveConfiguration() {
const dbCount = process.env[constants_1.ENV_VARS.DB_COUNT] || constants_1.DEFAULTS.DB_COUNT;
const primaryDbName = process.env[constants_1.ENV_VARS.PRIMARY_DB_NAME] || constants_1.DEFAULTS.PRIMARY_DB_NAME;
const primarySchemaPath = process.env[constants_1.ENV_VARS.PRIMARY_SCHEMA_PATH] || constants_1.DEFAULTS.PRIMARY_SCHEMA_PATH;
const secondaryDbName = process.env[constants_1.ENV_VARS.SECONDARY_DB_NAME] || constants_1.DEFAULTS.SECONDARY_DB_NAME;
const secondarySchemaPath = process.env[constants_1.ENV_VARS.SECONDARY_SCHEMA_PATH] || constants_1.DEFAULTS.SECONDARY_SCHEMA_PATH;
try {
(0, validation_1.validateDatabaseCount)(dbCount);
}
catch {
throw new errors_1.ConfigurationError(constants_1.MESSAGES.ERRORS.ENV_DB_COUNT_INVALID, constants_1.ENV_VARS.DB_COUNT);
}
console.log(`🤖 Non-interactive mode: Creating project with ${dbCount} database(s)`);
console.log(` Primary DB: ${primaryDbName} (${primarySchemaPath})`);
if (dbCount === '2') {
console.log(` Secondary DB: ${secondaryDbName} (${secondarySchemaPath})`);
}
return {
count: dbCount,
primaryDbName,
primarySchemaPath,
secondaryDbName: dbCount === '2' ? secondaryDbName : undefined,
secondarySchemaPath: dbCount === '2' ? secondarySchemaPath : undefined,
};
}
async function getInteractiveConfiguration() {
const prompt = new PromptInterface();
try {
// Get database count
const dbCountInput = await prompt.question('Select number of databases (1 or 2): ');
const dbCount = (0, validation_1.sanitizeInput)(dbCountInput);
(0, validation_1.validateDatabaseCount)(dbCount);
// Get primary database configuration
const primaryDbNameInput = await prompt.question('Primary DB name (default: primary-db): ');
const primaryDbName = (0, validation_1.sanitizeInput)(primaryDbNameInput) || constants_1.DEFAULTS.PRIMARY_DB_NAME;
// Get primary schema path
const primarySchemaPathInput = await prompt.question('Primary DB schema path: ');
const primarySchemaPath = (0, validation_1.sanitizeInput)(primarySchemaPathInput);
(0, validation_1.validateSchemaPath)(primarySchemaPath, 'Primary schema path');
// Show portability warning for absolute paths
if (primarySchemaPath.startsWith('/')) {
console.log('⚠️ Using absolute path. Consider using relative paths for better portability.');
}
const config = {
count: dbCount,
primaryDbName,
primarySchemaPath,
};
// Get secondary database configuration if needed
if (dbCount === '2') {
const secondaryDbNameInput = await prompt.question('Secondary DB name (default: secondary-db): ');
const secondaryDbName = (0, validation_1.sanitizeInput)(secondaryDbNameInput) || constants_1.DEFAULTS.SECONDARY_DB_NAME;
// Get secondary schema path
const secondarySchemaPathInput = await prompt.question('Secondary DB schema path: ');
const secondarySchemaPath = (0, validation_1.sanitizeInput)(secondarySchemaPathInput);
(0, validation_1.validateSchemaPath)(secondarySchemaPath, 'Secondary schema path');
// Show portability warning for absolute paths
if (secondarySchemaPath.startsWith('/')) {
console.log('⚠️ Using absolute path. Consider using relative paths for better portability.');
}
config.secondaryDbName = secondaryDbName;
config.secondarySchemaPath = secondarySchemaPath;
}
return config;
}
finally {
prompt.close();
}
}
function generateEnvironmentContent(config) {
let envContent = `# ================================================
# Spanner E2E Testing Framework Configuration
# Copy this file to .env and adjust the settings
# ================================================
# 🔧 Database Settings
DB_COUNT=${config.count}
PRIMARY_DB_ID=${config.primaryDbName}
PRIMARY_DATABASE_ID=${config.primaryDbName}
PRIMARY_DB_SCHEMA_PATH=${config.primarySchemaPath}
PRIMARY_SCHEMA_PATH=${config.primarySchemaPath}
`;
if (config.count === '2' && config.secondaryDbName && config.secondarySchemaPath) {
envContent += `SECONDARY_DB_ID=${config.secondaryDbName}
SECONDARY_DATABASE_ID=${config.secondaryDbName}
SECONDARY_DB_SCHEMA_PATH=${config.secondarySchemaPath}
SECONDARY_SCHEMA_PATH=${config.secondarySchemaPath}
`;
}
envContent += `
# 📊 Project Settings (usually no need to change)
PROJECT_ID=${constants_1.DEFAULTS.PROJECT_ID}
INSTANCE_ID=${constants_1.DEFAULTS.INSTANCE_ID}
# 🚨 EMULATOR ONLY - Prevents accidental production connections
SPANNER_EMULATOR_HOST=localhost:${constants_1.DEFAULTS.SPANNER_PORT}
# 🐳 Docker Settings (usually no need to change)
DOCKER_IMAGE=${constants_1.DEFAULTS.DOCKER_IMAGE}
DOCKER_CONTAINER_NAME=${constants_1.DEFAULTS.CONTAINER_NAME}
DOCKER_SPANNER_PORT=${constants_1.DEFAULTS.SPANNER_PORT}
DOCKER_ADMIN_PORT=${constants_1.DEFAULTS.ADMIN_PORT}
DOCKER_STARTUP_WAIT=${constants_1.DEFAULTS.STARTUP_WAIT}
`;
return envContent;
}
//# sourceMappingURL=configuration.js.map