@nestjs/cli
Version:
The Nest CLI: create NestJS applications, generate modules, controllers and CRUD resources, and build, run and watch projects and monorepos.
87 lines (86 loc) • 3.8 kB
JavaScript
import { select } from '@inquirer/prompts';
import * as fs from 'fs';
import { join } from 'path';
import { getValueOrDefault } from '../compiler/helpers/get-value-or-default.js';
import { generateSelect } from '../questions/questions.js';
import { gracefullyExitOnPromptError } from './gracefully-exit-on-prompt-error.js';
export function shouldAskForProject(schematic, configurationProjects, appName) {
return (['app', 'sub-app', 'library', 'lib'].includes(schematic) === false &&
configurationProjects &&
Object.entries(configurationProjects).length !== 0 &&
!appName);
}
export function shouldGenerateSpec(configuration, schematic, appName, specValue, specPassedAsInput) {
if (specPassedAsInput === true || specPassedAsInput === undefined) {
// CLI parameters has the highest priority
return specValue;
}
let specConfiguration = getValueOrDefault(configuration, 'generateOptions.spec', appName || '');
if (typeof specConfiguration === 'boolean') {
return specConfiguration;
}
if (typeof specConfiguration === 'object' &&
specConfiguration[schematic] !== undefined) {
return specConfiguration[schematic];
}
if (typeof specConfiguration === 'object' && appName) {
// The appName has a generateOption spec, but not for the schematic trying to generate
// Check if the global generateOptions has a spec to use instead
specConfiguration = getValueOrDefault(configuration, 'generateOptions.spec', '');
if (typeof specConfiguration === 'boolean') {
return specConfiguration;
}
if (typeof specConfiguration === 'object' &&
specConfiguration[schematic] !== undefined) {
return specConfiguration[schematic];
}
}
return specValue;
}
/**
* Whether the project's package.json lists `@nestjs/testing`, which generated
* spec files import. True when no package.json says otherwise.
*/
export function hasTestingPackage(dir = process.cwd()) {
try {
const pkg = JSON.parse(fs.readFileSync(join(dir, 'package.json'), 'utf8'));
return ['dependencies', 'devDependencies'].some((section) => !!pkg[section]?.['@nestjs/testing']);
}
catch {
return true;
}
}
export function shouldGenerateFlat(configuration, appName, flatValue) {
// CLI parameters have the highest priority
if (flatValue === true) {
return flatValue;
}
const flatConfiguration = getValueOrDefault(configuration, 'generateOptions.flat', appName || '');
if (typeof flatConfiguration === 'boolean') {
return flatConfiguration;
}
return flatValue;
}
export function getSpecFileSuffix(configuration, appName, specFileSuffixValue) {
// CLI parameters have the highest priority
if (specFileSuffixValue) {
return specFileSuffixValue;
}
const specFileSuffixConfiguration = getValueOrDefault(configuration, 'generateOptions.specFileSuffix', appName || '', undefined, undefined, 'spec');
if (typeof specFileSuffixConfiguration === 'string') {
return specFileSuffixConfiguration;
}
return specFileSuffixValue;
}
export async function askForProjectName(promptQuestion, projects) {
const projectNameSelect = generateSelect('appName')(promptQuestion)(projects);
return select(projectNameSelect).catch(gracefullyExitOnPromptError);
}
export function moveDefaultProjectToStart(configuration, defaultProjectName, defaultLabel) {
let projects = configuration.projects != null ? Object.keys(configuration.projects) : [];
if (configuration.sourceRoot !== 'src') {
projects = projects.filter((p) => p !== defaultProjectName.replace(defaultLabel, ''));
}
projects.unshift(defaultProjectName);
return projects;
}