UNPKG

@puls-atlas/cli

Version:

The Puls Atlas CLI tool for managing Atlas projects

116 lines 3.52 kB
import fs from 'fs'; import path from 'path'; import chalk from 'chalk'; import inquirer from 'inquirer'; import { logger } from './../../utils/index.js'; const readFileContent = file => { const filePath = path.join(process.cwd(), file); if (!fs.existsSync(filePath)) { logger.error(`No ${filePath} file found.`); } return fs.readFileSync(filePath).toString(); }; const writeFileContent = (file, content) => { const filePath = path.join(process.cwd(), file); if (!fs.existsSync(filePath)) { logger.error(`No ${filePath} file found.`); } return fs.writeFileSync(filePath, content); }; const replaceMergeVars = (file, vars) => { let content = readFileContent(file); Object.keys(vars).forEach(key => { content = content.replaceAll(`{{ ${key} }}`, vars[key] ?? ''); }); writeFileContent(file, content); }; const normalizeStr = name => name.toLowerCase().replace(/[^a-z0-9]/g, '-').replace(/-+/g, '-'); const getMergeVars = async () => { const { isConfirmed } = await inquirer.prompt([{ type: 'confirm', name: 'isConfirmed', default: true, message: 'First we need to fill in some variables. This can only be done once. Continue?' }]); if (!isConfirmed) { return false; } const client = await inquirer.prompt([{ type: 'input', name: 'name', message: `Enter the ${chalk.yellow('client name')}:` }]); const gcProject = await inquirer.prompt([{ type: 'input', name: 'id', default: normalizeStr(client.name), message: `Enter the Google Cloud ${chalk.yellow('production')} Project ID:` }]); const gcDevProject = await inquirer.prompt([{ type: 'input', name: 'id', default: `dev-${gcProject.id}`, message: `Enter the Google Cloud ${chalk.yellow('development')} Project ID:` }]); const hostname = await inquirer.prompt([{ type: 'input', name: 'value', message: `Enter the ${chalk.yellow('hostname')} for the production environment:` }]); const title = await inquirer.prompt([{ type: 'input', name: 'value', default: hostname.value, message: `Enter the ${chalk.yellow('title')} of the application:` }]); const composerVendor = await inquirer.prompt([{ type: 'input', name: 'name', default: normalizeStr(client.name), message: `Enter the ${chalk.yellow('composer vendor')} name:` }]); return { gcProject, gcDevProject, hostname, title, composerVendor }; }; export default async () => { const mergeVars = await getMergeVars(); if (mergeVars) { try { replaceMergeVars('.firebaserc', { GC_PROJECT: mergeVars.gcProject.id, GC_DEV_PROJECT: mergeVars.gcDevProject.id }); replaceMergeVars('config.php', { GC_PROJECT: mergeVars.gcProject.id, GC_DEV_PROJECT: mergeVars.gcDevProject.id, HOSTNAME: mergeVars.hostname.value, TITLE: mergeVars.title.value }); replaceMergeVars('public/index.php', { TITLE: mergeVars.title.value }); replaceMergeVars('composer.json', { HOSTNAME: mergeVars.hostname.value, TITLE: mergeVars.title.value, VENDOR: mergeVars.composerVendor.name }); replaceMergeVars('app/package.json', { HOSTNAME: mergeVars.hostname.value, TITLE: mergeVars.title.value }); replaceMergeVars('app/public/manifest.json', { TITLE: mergeVars.title.value }); } catch (error) { return Promise.reject(error); } } return Promise.resolve(); };