UNPKG

@puls-atlas/cli

Version:

The Puls Atlas CLI tool for managing Atlas projects

79 lines 2.51 kB
import fs from 'fs'; import path from 'path'; import chalk from 'chalk'; import inquirer from 'inquirer'; import { logger, npm } from '../../utils/index.js'; const selectCodebase = async () => { const firebaseJsonPath = path.join(process.cwd(), 'firebase.json'); if (!fs.existsSync(firebaseJsonPath)) { logger.warn('No firebase.json file found.'); return null; } const firebaseJson = JSON.parse(fs.readFileSync(firebaseJsonPath)); if (firebaseJson.functions?.length > 1) { const { codebase } = await inquirer.prompt([{ type: 'list', name: 'codebase', message: 'Select a codebase:', choices: firebaseJson.functions.map(({ codebase, source }) => ({ name: `${codebase} (${source})`, value: codebase, short: codebase })) }]); return codebase; } return null; }; export default async (options = {}) => { let functionsDir = path.join(process.cwd(), 'functions'); if (!fs.existsSync(functionsDir)) { logger.error('The /functions directory does not exist. Make sure you are in the root directory of your project.'); } const codebase = options.codebase ?? (await selectCodebase()); if (codebase) { functionsDir = path.join(functionsDir, codebase); } const { isConfirmed } = await inquirer.prompt([{ type: 'confirm', name: 'isConfirmed', default: true, message: 'Continue installing ' + `${chalk.yellow('NPM dependencies')} ` + 'for your ' + `${chalk.yellow('Cloud Functions')} ` + `in ${chalk.cyan(functionsDir.replace(process.cwd(), ''))}?` }]); if (isConfirmed) { const hasLockFile = fs.existsSync(path.join(functionsDir, 'package-lock.json')); if (hasLockFile) { logger.info('Lockfile detected.'); const { installType } = await inquirer.prompt([{ type: 'list', name: 'installFromLockFile', default: 'lockfile', message: 'Where do you want to install the NPM dependencies from?', choices: [{ name: 'lockfile (recommended)', value: 'lockfile', short: 'lockfile' }, { name: 'package.json', value: 'package.json', short: 'package.json' }] }]); if (installType === 'lockfile') { return npm.cleanInstall(functionsDir, options); } } else { logger.warning('No lockfile detected. Installing from package.json.'); } return npm.install(functionsDir, options); } };