Skip to content

Commit

Permalink
Merge branch 'next' into switch-to-ansis
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus authored Dec 17, 2024
2 parents 5022e61 + b38af54 commit a4c99de
Show file tree
Hide file tree
Showing 35 changed files with 2,068 additions and 1,946 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ aliases:
- &install-deps
run:
name: Install dependencies
command: npm ci --ignore-scripts
command: npm ci --ignore-scripts --legacy-peer-deps
- &build-packages
run:
name: Build
Expand All @@ -31,7 +31,7 @@ jobs:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: Install dependencies
command: npm ci --ignore-scripts
command: npm ci --ignore-scripts --legacy-peer-deps
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
Expand Down
6 changes: 3 additions & 3 deletions actions/add.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export class AddAction extends AbstractAction {
defaultLabel,
);

const answers = await askForProjectName(
const selectedProject = (await askForProjectName(
MESSAGES.LIBRARY_PROJECT_SELECTION_QUESTION,
projects,
);
const project = answers.appName.replace(defaultLabel, '');
)) as string;
const project = selectedProject.replace(defaultLabel, '');
if (project !== configuration.sourceRoot) {
sourceRoot = configurationProjects[project].sourceRoot;
}
Expand Down
169 changes: 98 additions & 71 deletions actions/build.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,87 +78,110 @@ export class BuildAction extends AbstractAction {
(option) => option.name === 'config',
)!.value as string;
const configuration = await this.loader.load(configFileName);
const appName = commandInputs.find((input) => input.name === 'app')!
.value as string;
const buildAll = commandOptions.find((opt) => opt.name === 'all')?.value;

const pathToTsconfig = getTscConfigPath(
configuration,
commandOptions,
appName,
);
const { options: tsOptions } =
this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
const outDir = tsOptions.outDir || defaultOutDir;
let appNames: (string | undefined)[];
if (buildAll) {
// If the "all" flag is set, we need to build all projects in a monorepo.
appNames = [];

const isWebpackEnabled = getValueOrDefault<boolean>(
configuration,
'compilerOptions.webpack',
appName,
'webpack',
commandOptions,
);
const builder = isWebpackEnabled
? { type: 'webpack' }
: getBuilder(configuration, commandOptions, appName);
if (configuration.projects) {
appNames.push(...Object.keys(configuration.projects));
}
} else {
appNames = commandInputs
.filter((input) => input.name === 'app')
.map((input) => input.value) as string[];
}

await deleteOutDirIfEnabled(configuration, appName, outDir);
this.assetsManager.copyAssets(
configuration,
appName,
outDir,
watchAssetsMode,
);
if (appNames.length === 0) {
// If there are no projects, use "undefined" to build the default project.
appNames.push(undefined);
}

const typeCheck = getValueOrDefault<boolean>(
configuration,
'compilerOptions.typeCheck',
appName,
'typeCheck',
commandOptions,
);
if (typeCheck && builder.type !== 'swc') {
console.warn(
INFO_PREFIX +
` "typeCheck" will not have any effect when "builder" is not "swc".`,
for (const appName of appNames) {
const pathToTsconfig = getTscConfigPath(
configuration,
commandOptions,
appName,
);
}
const { options: tsOptions } =
this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
const outDir = tsOptions.outDir || defaultOutDir;

switch (builder.type) {
case 'tsc':
return this.runTsc(
watchMode,
commandOptions,
configuration,
pathToTsconfig,
appName,
onSuccess,
);
case 'webpack':
return this.runWebpack(
configuration,
appName,
commandOptions,
pathToTsconfig,
isDebugEnabled,
watchMode,
onSuccess,
);
case 'swc':
return this.runSwc(
configuration,
appName,
pathToTsconfig,
watchMode,
commandOptions,
tsOptions,
onSuccess,
const isWebpackEnabled = getValueOrDefault<boolean>(
configuration,
'compilerOptions.webpack',
appName,
'webpack',
commandOptions,
);
const builder = isWebpackEnabled
? { type: 'webpack' }
: getBuilder(configuration, commandOptions, appName);

await deleteOutDirIfEnabled(configuration, appName, outDir);
this.assetsManager.copyAssets(
configuration,
appName,
outDir,
watchAssetsMode,
);

const typeCheck = getValueOrDefault<boolean>(
configuration,
'compilerOptions.typeCheck',
appName,
'typeCheck',
commandOptions,
);
if (typeCheck && builder.type !== 'swc') {
console.warn(
INFO_PREFIX +
` "typeCheck" will not have any effect when "builder" is not "swc".`,
);
}

switch (builder.type) {
case 'tsc':
await this.runTsc(
watchMode,
commandOptions,
configuration,
pathToTsconfig,
appName,
onSuccess,
);
break;
case 'webpack':
await this.runWebpack(
configuration,
appName,
commandOptions,
pathToTsconfig,
isDebugEnabled,
watchMode,
onSuccess,
);
break;
case 'swc':
await this.runSwc(
configuration,
appName,
pathToTsconfig,
watchMode,
commandOptions,
tsOptions,
onSuccess,
);
break;
}
}
}

private async runSwc(
configuration: Required<Configuration>,
appName: string,
appName: string | undefined,
pathToTsconfig: string,
watchMode: boolean,
options: Input[],
Expand All @@ -167,6 +190,7 @@ export class BuildAction extends AbstractAction {
) {
const { SwcCompiler } = await import('../lib/compiler/swc/swc-compiler');
const swc = new SwcCompiler(this.pluginsLoader);

await swc.run(
configuration,
pathToTsconfig,
Expand All @@ -189,7 +213,7 @@ export class BuildAction extends AbstractAction {

private async runWebpack(
configuration: Required<Configuration>,
appName: string,
appName: string | undefined,
commandOptions: Input[],
pathToTsconfig: string,
debug: boolean,
Expand All @@ -209,6 +233,7 @@ export class BuildAction extends AbstractAction {
webpackPath,
defaultWebpackConfigFilename,
);

return webpackCompiler.run(
configuration,
pathToTsconfig,
Expand All @@ -229,7 +254,7 @@ export class BuildAction extends AbstractAction {
options: Input[],
configuration: Required<Configuration>,
pathToTsconfig: string,
appName: string,
appName: string | undefined,
onSuccess: (() => void) | undefined,
) {
if (watchMode) {
Expand All @@ -243,6 +268,7 @@ export class BuildAction extends AbstractAction {
(option) =>
option.name === 'preserveWatchOutput' && option.value === true,
)?.value as boolean | undefined;

watchCompiler.run(
configuration,
pathToTsconfig,
Expand All @@ -257,6 +283,7 @@ export class BuildAction extends AbstractAction {
this.tsConfigProvider,
this.tsLoader,
);

compiler.run(
configuration,
pathToTsconfig,
Expand Down
13 changes: 6 additions & 7 deletions actions/generate.action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { red } from 'ansis';
import * as path from 'path';
import { Answers } from 'inquirer';
import { Input } from '../commands';
import { getValueOrDefault } from '../lib/compiler/helpers/get-value-or-default';
import {
Expand Down Expand Up @@ -94,28 +93,28 @@ const generateFiles = async (inputs: Input[]) => {
defaultLabel,
);

const answers: Answers = await askForProjectName(
const selectedProjectName = (await askForProjectName(
MESSAGES.PROJECT_SELECTION_QUESTION,
projects,
);
)) as string;

const project: string = answers.appName.replace(defaultLabel, '');
const project: string = selectedProjectName.replace(defaultLabel, '');
if (project !== configuration.sourceRoot) {
sourceRoot = configurationProjects[project].sourceRoot;
}

if (answers.appName !== defaultProjectName) {
if (selectedProjectName !== defaultProjectName) {
// Only overwrite if the appName is not the default- as it has already been loaded above
generateSpec = shouldGenerateSpec(
configuration,
schematic,
answers.appName,
selectedProjectName,
specValue,
specOptions.passedAsInput,
);
generateFlat = shouldGenerateFlat(
configuration,
answers.appNames,
selectedProjectName,
flatValue,
);
generateSpecFileSuffix = getSpecFileSuffix(
Expand Down
53 changes: 26 additions & 27 deletions actions/new.action.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { input, select } from '@inquirer/prompts';
import * as ansis from 'ansis';
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as inquirer from 'inquirer';
import { Answers, Question } from 'inquirer';
import { Answers } from 'inquirer';
import { join } from 'path';
import { Input } from '../commands';
import { defaultGitIgnore } from '../lib/configuration/defaults';
Expand All @@ -21,6 +21,7 @@ import {
} from '../lib/schematics';
import { EMOJIS, MESSAGES } from '../lib/ui';
import { normalizeToKebabOrSnakeCase } from '../lib/utils/formatting';
import { gracefullyExitOnPromptError } from '../lib/utils/gracefully-exit-on-prompt-error';
import { AbstractAction } from './abstract.action';

export class NewAction extends AbstractAction {
Expand Down Expand Up @@ -84,32 +85,34 @@ const askForMissingInformation = async (inputs: Input[], options: Input[]) => {
console.info(MESSAGES.PROJECT_INFORMATION_START);
console.info();

const prompt: inquirer.PromptModule = inquirer.createPromptModule();

const nameInput = getApplicationNameInput(inputs);
if (!nameInput!.value) {
const message = 'What name would you like to use for the new project?';
const questions = [generateInput('name', message)('nest-app')];
const answers: Answers = await prompt(questions as ReadonlyArray<Question>);
replaceInputMissingInformation(inputs, answers);
const message = MESSAGES.PROJECT_NAME_QUESTION;
const question = generateInput('name', message)('nest-app');
const answer = await input(question).catch(gracefullyExitOnPromptError);
replaceInputMissingInformation(inputs, { name: 'name', value: answer });
}

const packageManagerInput = getPackageManagerInput(options);

if (!packageManagerInput!.value) {
const answers = await askForPackageManager();
replaceInputMissingInformation(options, answers);
const answer = await askForPackageManager();
replaceInputMissingInformation(options, {
name: 'packageManager',
value: answer,
});
}
};

const replaceInputMissingInformation = (
inputs: Input[],
answers: Answers,
): Input[] => {
return inputs.map(
(input) =>
(input.value =
input.value !== undefined ? input.value : answers[input.name]),
);
answer: Answers,
): void => {
const input = inputs.find((input) => input.name === answer.name);

if (input) {
input.value = input.value !== undefined ? input.value : answer.value;
}
};

const generateApplicationFiles = async (args: Input[], options: Input[]) => {
Expand Down Expand Up @@ -162,16 +165,12 @@ const installPackages = async (
}
};

const askForPackageManager = async (): Promise<Answers> => {
const questions: Question[] = [
generateSelect('packageManager')(MESSAGES.PACKAGE_MANAGER_QUESTION)([
PackageManager.NPM,
PackageManager.YARN,
PackageManager.PNPM,
]),
];
const prompt = inquirer.createPromptModule();
return await prompt(questions);
const askForPackageManager = async () => {
const question = generateSelect('packageManager')(
MESSAGES.PACKAGE_MANAGER_QUESTION,
)([PackageManager.NPM, PackageManager.YARN, PackageManager.PNPM]);

return select(question).catch(gracefullyExitOnPromptError);
};

const initializeGitRepository = async (dir: string) => {
Expand Down
Loading

0 comments on commit a4c99de

Please sign in to comment.