Skip to content

Commit

Permalink
fix(js): pass buildTargetOptions in executor (nrwl#17459)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini authored and jaysoo committed Jun 7, 2023
1 parent 3cb7b8f commit 2302f5b
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/generated/cli/create-nx-workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Package manager to use

Type: `string`

Customizes the initial content of your workspace. Default presets include: ["apps", "empty", "core", "npm", "ts", "web-components", "angular-monorepo", "angular-standalone", "react-monorepo", "react-standalone", "next", "nextjs-standalone", "react-native", "expo", "nest", "express", "react", "angular", "node-standalone", "node-monorepo"]. To build your own see https://nx.dev/plugins/recipes/create-preset
Customizes the initial content of your workspace. Default presets include: ["apps", "empty", "core", "npm", "ts", "web-components", "angular-monorepo", "angular-standalone", "react-monorepo", "react-standalone", "next", "nextjs-standalone", "react-native", "expo", "nest", "express", "react", "angular", "node-standalone", "node-monorepo", "ts-standalone"]. To build your own see https://nx.dev/plugins/recipes/create-preset

### routing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Package manager to use

Type: `string`

Customizes the initial content of your workspace. Default presets include: ["apps", "empty", "core", "npm", "ts", "web-components", "angular-monorepo", "angular-standalone", "react-monorepo", "react-standalone", "next", "nextjs-standalone", "react-native", "expo", "nest", "express", "react", "angular", "node-standalone", "node-monorepo"]. To build your own see https://nx.dev/plugins/recipes/create-preset
Customizes the initial content of your workspace. Default presets include: ["apps", "empty", "core", "npm", "ts", "web-components", "angular-monorepo", "angular-standalone", "react-monorepo", "react-standalone", "next", "nextjs-standalone", "react-native", "expo", "nest", "express", "react", "angular", "node-standalone", "node-monorepo", "ts-standalone"]. To build your own see https://nx.dev/plugins/recipes/create-preset

### routing

Expand Down
62 changes: 58 additions & 4 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ interface NoneArguments extends BaseArguments {
workspaceType: 'package-based' | 'integrated';
}

interface TsArguments extends BaseArguments {
stack: 'ts';
workspaceType: 'standalone' | 'integrated';
js: boolean;
}

interface ReactArguments extends BaseArguments {
stack: 'react';
workspaceType: 'standalone' | 'integrated';
Expand Down Expand Up @@ -72,6 +78,7 @@ interface UnknownStackArguments extends BaseArguments {

type Arguments =
| NoneArguments
| TsArguments
| ReactArguments
| AngularArguments
| NodeArguments
Expand Down Expand Up @@ -309,7 +316,7 @@ async function determineFolder(

async function determineStack(
parsedArgs: yargs.Arguments<Arguments>
): Promise<'none' | 'react' | 'angular' | 'node' | 'unknown'> {
): Promise<'none' | 'ts' | 'react' | 'angular' | 'node' | 'unknown'> {
if (parsedArgs.preset) {
switch (parsedArgs.preset) {
case Preset.Angular:
Expand All @@ -333,6 +340,8 @@ async function determineStack(
case Preset.NPM:
return 'none';
case Preset.TS:
case Preset.TsStandalone:
return 'ts';
case Preset.WebComponents:
case Preset.ReactNative:
case Preset.Expo:
Expand All @@ -342,7 +351,7 @@ async function determineStack(
}

const { stack } = await enquirer.prompt<{
stack: 'none' | 'react' | 'angular' | 'node';
stack: 'none' | 'ts' | 'react' | 'angular' | 'node';
}>([
{
name: 'stack',
Expand All @@ -353,6 +362,10 @@ async function determineStack(
name: `none`,
message: `None: Configures a minimal structure without specific frameworks or technologies.`,
},
{
name: `ts`,
message: `TS/JS: Configures a TypeScript or JavaScript package without specific frameworks or platforms.`,
},
{
name: `react`,
message: `React: Configures a React app with your framework of choice.`,
Expand All @@ -377,7 +390,9 @@ async function determinePresetOptions(
): Promise<Partial<Arguments>> {
switch (parsedArgs.stack) {
case 'none':
return determineNoneptions(parsedArgs);
return determineNoneOptions(parsedArgs);
case 'ts':
return determineTsOptions(parsedArgs);
case 'react':
return determineReactOptions(parsedArgs);
case 'angular':
Expand All @@ -389,7 +404,7 @@ async function determinePresetOptions(
}
}

async function determineNoneptions(
async function determineNoneOptions(
parsedArgs: yargs.Arguments<Arguments>
): Promise<Partial<Arguments>> {
if (parsedArgs.preset) return parsedArgs;
Expand Down Expand Up @@ -428,6 +443,45 @@ async function determineNoneptions(
}
}

async function determineTsOptions(
parsedArgs: yargs.Arguments<TsArguments>
): Promise<Partial<Arguments>> {
let preset: Preset;
let workspaceType: 'standalone' | 'integrated' | undefined = undefined;
let js: boolean;

if (parsedArgs.preset) {
preset = parsedArgs.preset;
} else {
workspaceType = await determineStandAloneOrMonorepo();
preset = workspaceType === 'standalone' ? Preset.TsStandalone : Preset.TS;
}

if (parsedArgs.js !== undefined) {
js = parsedArgs.js;
} else {
const reply = await enquirer.prompt<{ ts: 'Yes' | 'No' }>([
{
name: 'ts',
message: `Would you like to use TypeScript with this project?`,
type: 'autocomplete',
choices: [
{
name: 'Yes',
},
{
name: 'No',
},
],
initial: 'Yes' as any,
},
]);
js = reply.ts === 'No';
}

return { preset, js };
}

async function determineReactOptions(
parsedArgs: yargs.Arguments<ReactArguments>
): Promise<Partial<Arguments>> {
Expand Down
7 changes: 4 additions & 3 deletions packages/create-nx-workspace/src/internal-utils/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as yargs from 'yargs';
import { messages } from '../utils/nx/ab-testing';
import enquirer = require('enquirer');
import { CI } from '../utils/ci/ci-list';
import { output } from '../utils/output';
import { deduceDefaultBase } from '../utils/git/default-base';
Expand All @@ -9,7 +8,9 @@ import {
PackageManager,
packageManagerList,
} from '../utils/package-manager';
import { Preset } from '../utils/preset/preset';
import { stringifyCollection } from '../utils/string-utils';
import enquirer = require('enquirer');

export async function determineNxCloud(
parsedArgs: yargs.Arguments<{ nxCloud: boolean }>
Expand Down Expand Up @@ -98,7 +99,7 @@ export async function determineDefaultBase(
.prompt<{ DefaultBase: string }>([
{
name: 'DefaultBase',
message: `Main branch name `,
message: `Main branch name`,
initial: `main`,
type: 'input',
},
Expand Down Expand Up @@ -142,7 +143,7 @@ export async function determinePackageManager(
.prompt<{ packageManager: PackageManager }>([
{
name: 'packageManager',
message: `Which package manager to use `,
message: `Which package manager to use`,
initial: 'npm' as any,
type: 'autocomplete',
choices: [
Expand Down
1 change: 1 addition & 0 deletions packages/create-nx-workspace/src/utils/preset/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export enum Preset {
Angular = 'angular',
NodeStandalone = 'node-standalone',
NodeMonorepo = 'node-monorepo',
TsStandalone = 'ts-standalone',
}

/**
Expand Down
9 changes: 7 additions & 2 deletions packages/js/src/executors/node/node.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ export async function* nodeExecutor(
context.projectGraph
);

const buildOptions = project.data.targets[buildTarget.target]?.options;
if (!buildOptions) {
let buildOptions: Record<string, any>;
if (project.data.targets[buildTarget.target]) {
buildOptions = {
...project.data.targets[buildTarget.target]?.options,
...options.buildTargetOptions,
};
} else {
throw new Error(
`Cannot find build target ${chalk.bold(
options.buildTarget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,67 @@ Nx comes with local caching already built-in (check your \`nx.json\`). On CI you
"
`;
exports[`@nx/workspace:generateWorkspaceFiles README.md should be created for TsStandalone preset 1`] = `
"# Proj
<a alt="Nx logo" href="https://nx.dev" target="_blank" rel="noreferrer"><img src="https://github.com/raw/nrwl/nx/master/images/nx-logo.png" width="45"></a>
✨ **This workspace has been generated by [Nx, a Smart, fast and extensible build system.](https://nx.dev)** ✨
## Generate code
If you happen to use Nx plugins, you can leverage code generators that might come with it.
Run \`nx list\` to get a list of available plugins and whether they have generators. Then run \`nx list <plugin-name>\` to see what generators are available.
Learn more about [Nx generators on the docs](https://nx.dev/plugin-features/use-code-generators).
## Running tasks
To execute tasks with Nx use the following syntax:
\`\`\`
nx <target> <project> <...options>
\`\`\`
You can also run multiple targets:
\`\`\`
nx run-many -t <target1> <target2>
\`\`\`
..or add \`-p\` to filter specific projects
\`\`\`
nx run-many -t <target1> <target2> -p <proj1> <proj2>
\`\`\`
Targets can be defined in the \`package.json\` or \`projects.json\`. Learn more [in the docs](https://nx.dev/core-features/run-tasks).
## Want better Editor Integration?
Have a look at the [Nx Console extensions](https://nx.dev/nx-console). It provides autocomplete support, a UI for exploring and running tasks & generators, and more! Available for VSCode, IntelliJ and comes with a LSP for Vim users.
## Ready to deploy?
Just run \`nx build demoapp\` to build the application. The build artifacts will be stored in the \`dist/\` directory, ready to be deployed.
## Set up CI!
Nx comes with local caching already built-in (check your \`nx.json\`). On CI you might want to go a step further.
- [Set up remote caching](https://nx.dev/core-features/share-your-cache)
- [Set up task distribution across multiple machines](https://nx.dev/core-features/distribute-task-execution)
- [Learn more how to setup CI](https://nx.dev/recipes/ci)
## Connect with us!
- [Join the community](https://nx.dev/community)
- [Subscribe to the Nx Youtube Channel](https://www.youtube.com/@nxdevtools)
- [Follow us on Twitter](https://twitter.com/nxdevtools)
"
`;
exports[`@nx/workspace:generateWorkspaceFiles README.md should be created for WebComponents preset 1`] = `
"# Proj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

✨ **This workspace has been generated by [Nx, a Smart, fast and extensible build system.](https://nx.dev)** ✨<% if (!!appName) { %>

## Start the app
<% if (includeServe) { %>
## Start the app

To start the development server run `nx serve <%= appName %>`. Open your browser and navigate to http://localhost:4200/. Happy coding!<% } %>
<% } %>

## Generate code

Expand Down
2 changes: 2 additions & 0 deletions packages/workspace/src/generators/new/generate-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export function generatePreset(host: Tree, opts: NormalizedSchema) {
opts.bundler ? `--bundler=${opts.bundler}` : null,
opts.framework ? `--framework=${opts.framework}` : null,
opts.docker ? `--docker=${opts.docker}` : null,
opts.js ? `--js` : null,
opts.nextAppDir ? `--nextAppDir=${opts.nextAppDir}` : null,
opts.packageManager ? `--packageManager=${opts.packageManager}` : null,
opts.standaloneApi !== undefined
Expand All @@ -98,6 +99,7 @@ function getPresetDependencies({
}: NormalizedSchema) {
switch (preset) {
case Preset.TS:
case Preset.TsStandalone:
return { dependencies: {}, dev: { '@nx/js': nxVersion } };

case Preset.AngularMonorepo:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
Preset.Express,
Preset.NodeStandalone,
Preset.NextJsStandalone,
Preset.TsStandalone,
].includes(Preset[preset])
) {
appName = 'app1';
Expand Down
16 changes: 14 additions & 2 deletions packages/workspace/src/generators/new/generate-workspace-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function createAppsAndLibsFolders(tree: Tree, options: NormalizedSchema) {
options.preset === Preset.ReactStandalone ||
options.preset === Preset.NodeStandalone ||
options.preset === Preset.NextJsStandalone ||
options.preset === Preset.TsStandalone ||
options.isCustomPreset
) {
// don't generate any folders
Expand Down Expand Up @@ -126,7 +127,8 @@ function createFiles(tree: Tree, options: NormalizedSchema) {
options.preset === Preset.AngularStandalone ||
options.preset === Preset.ReactStandalone ||
options.preset === Preset.NodeStandalone ||
options.preset === Preset.NextJsStandalone
options.preset === Preset.NextJsStandalone ||
options.preset === Preset.TsStandalone
? './files-root-app'
: options.preset === Preset.NPM || options.preset === Preset.Core
? './files-package-based-repo'
Expand All @@ -145,11 +147,12 @@ function createFiles(tree: Tree, options: NormalizedSchema) {

function createReadme(
tree: Tree,
{ name, appName, directory }: NormalizedSchema
{ name, appName, directory, preset }: NormalizedSchema
) {
const formattedNames = names(name);
generateFiles(tree, join(__dirname, './files-readme'), directory, {
formattedNames,
includeServe: preset !== Preset.TsStandalone,
appName,
name,
});
Expand Down Expand Up @@ -189,6 +192,15 @@ function addNpmScripts(tree: Tree, options: NormalizedSchema) {
return json;
});
}
if (options.preset === Preset.TsStandalone) {
updateJson(tree, join(options.directory, 'package.json'), (json) => {
Object.assign(json.scripts, {
build: 'nx build',
test: 'nx test',
});
return json;
});
}
}

function addPropertyWithStableKeys(obj: any, key: string, value: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/generators/new/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface Schema {
defaultBase: string;
framework?: string;
docker?: boolean;
js?: boolean;
nextAppDir?: boolean;
linter?: Linter;
bundler?: 'vite' | 'webpack';
Expand Down
12 changes: 12 additions & 0 deletions packages/workspace/src/generators/preset/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ async function createPreset(tree: Tree, options: Schema) {
};
updateNxJson(tree, c);
return initGenerator(tree, {});
} else if (options.preset === Preset.TsStandalone) {
const c = readNxJson(tree);
const { libraryGenerator } = require('@nx' + '/js');
updateNxJson(tree, c);
return libraryGenerator(tree, {
name: options.name,
bundler: 'swc',
unitTestRunner: 'jest',
testEnvironment: 'node',
js: options.js,
rootProject: true,
});
} else if (options.preset === Preset.NodeStandalone) {
const { applicationGenerator: nodeApplicationGenerator } = require('@nx' +
'/node');
Expand Down
3 changes: 2 additions & 1 deletion packages/workspace/src/generators/preset/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { PackageManager } from '@nx/devkit';

export interface Schema {
name: string;
preset: Preset;
style?: string;
linter?: string;
preset: Preset;
standaloneConfig?: boolean;
framework?: string;
packageManager?: PackageManager;
Expand All @@ -15,4 +15,5 @@ export interface Schema {
routing?: boolean;
standaloneApi?: boolean;
e2eTestRunner?: 'cypress' | 'jest' | 'detox' | 'none';
js?: boolean;
}
Loading

0 comments on commit 2302f5b

Please sign in to comment.