Skip to content

Commit

Permalink
feat(js): generate experimental simplified library with ts solution s…
Browse files Browse the repository at this point in the history
…etup (#27910)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

---------

Co-authored-by: Jack Hsu <[email protected]>
  • Loading branch information
leosvelperez and jaysoo authored Sep 27, 2024
1 parent 5724deb commit 49c5a73
Show file tree
Hide file tree
Showing 80 changed files with 2,997 additions and 1,464 deletions.
16 changes: 8 additions & 8 deletions docs/generated/packages/js/generators/init.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{
"name": "init",
"factory": "./src/generators/init/init#initGenerator",
"factory": "./src/generators/init/init#initGeneratorInternal",
"schema": {
"$schema": "https://json-schema.org/schema",
"$id": "NxTypescriptInit",
"cli": "nx",
"title": "Init nx/js",
"description": "Init generator placeholder for nx/js.",
"properties": {
"formatter": {
"description": "The tool to use for code formatting.",
"type": "string",
"enum": ["none", "prettier"],
"default": "none"
},
"js": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -40,12 +46,6 @@
"type": "string",
"description": "Customize the generated base tsconfig file name.",
"x-priority": "internal"
},
"setUpPrettier": {
"type": "boolean",
"description": "Add Prettier and corresponding configuration files.",
"x-priority": "internal",
"default": false
}
},
"presets": []
Expand All @@ -54,7 +54,7 @@
"x-type": "init",
"description": "Initialize a TS/JS workspace.",
"hidden": true,
"implementation": "/packages/js/src/generators/init/init#initGenerator.ts",
"implementation": "/packages/js/src/generators/init/init#initGeneratorInternal.ts",
"path": "/packages/js/src/generators/init/schema.json",
"type": "generator"
}
35 changes: 18 additions & 17 deletions docs/generated/packages/js/generators/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,28 @@
"description": "A directory where the lib is placed.",
"x-priority": "important"
},
"projectNameAndRootFormat": {
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
"bundler": {
"description": "The bundler to use. Choosing 'none' means this library is not buildable.",
"type": "string",
"enum": ["as-provided", "derived"]
"enum": ["swc", "tsc", "rollup", "vite", "esbuild", "none"],
"x-priority": "important"
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint", "none"],
"default": "eslint"
"enum": ["none", "eslint"],
"x-priority": "important"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "vitest", "none"],
"description": "Test runner to use for unit tests.",
"x-prompt": "Which unit test runner would you like to use?"
"type": "string",
"enum": ["none", "jest", "vitest"],
"x-priority": "important"
},
"projectNameAndRootFormat": {
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
"type": "string",
"enum": ["as-provided", "derived"]
},
"tags": {
"type": "string",
Expand Down Expand Up @@ -112,18 +118,9 @@
"compiler": {
"type": "string",
"enum": ["tsc", "swc"],
"default": "tsc",
"description": "The compiler used by the build and test targets",
"x-deprecated": "Use the `bundler` option for greater control (swc, tsc, rollup, vite, esbuild, none)."
},
"bundler": {
"description": "The bundler to use. Choosing 'none' means this library is not buildable.",
"type": "string",
"enum": ["swc", "tsc", "rollup", "vite", "esbuild", "none"],
"default": "tsc",
"x-prompt": "Which bundler would you like to use to build the library? Choose 'none' to skip build setup.",
"x-priority": "important"
},
"skipTypeCheck": {
"type": "boolean",
"description": "Whether to skip TypeScript type checking for SWC compiler.",
Expand All @@ -138,6 +135,10 @@
"description": "Don't include the directory in the generated file name.",
"type": "boolean",
"default": false
},
"useProjectJson": {
"type": "boolean",
"description": "Use a `project.json` configuration file instead of inlining the Nx configuration in the `package.json` file."
}
},
"required": ["name"],
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/packages/workspace/generators/new.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
"prefix": {
"description": "The prefix to use for Angular component and directive selectors.",
"type": "string"
},
"formatter": {
"description": "The tool to use for code formatting.",
"type": "string",
"enum": ["none", "prettier"],
"default": "none"
}
},
"additionalProperties": true,
Expand Down
2 changes: 1 addition & 1 deletion e2e/eslint/src/linter-legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Linter (legacy)', () => {
env: { NX_ADD_PLUGINS: 'false' },
}
);
runCLI(`generate @nx/js:lib ${mylib} --directory=apps/${mylib}`, {
runCLI(`generate @nx/js:lib ${mylib} --directory=libs/${mylib}`, {
env: { NX_ADD_PLUGINS: 'false' },
});
});
Expand Down
54 changes: 27 additions & 27 deletions packages/angular/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1212,34 +1212,34 @@ describe('lib', () => {
module.exports = [
...baseConfig,
...nx.configs["flat/angular"],
...nx.configs["flat/angular-template"],
{
files: ["**/*.ts"],
rules: {
"@angular-eslint/directive-selector": [
"error",
{
type: "attribute",
prefix: "lib",
style: "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
type: "element",
prefix: "lib",
style: "kebab-case"
}
]
...nx.configs["flat/angular"],
...nx.configs["flat/angular-template"],
{
files: ["**/*.ts"],
rules: {
"@angular-eslint/directive-selector": [
"error",
{
type: "attribute",
prefix: "lib",
style: "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
type: "element",
prefix: "lib",
style: "kebab-case"
}
]
}
},
{
files: ["**/*.html"],
// Override or add rules here
rules: {}
}
},
{
files: ["**/*.html"],
// Override or add rules here
rules: {}
}
];
"
`);
Expand Down
109 changes: 74 additions & 35 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ interface BaseArguments extends CreateWorkspaceOptions {

interface NoneArguments extends BaseArguments {
stack: 'none';
workspaceType: 'package-based' | 'integrated' | 'standalone';
js: boolean;
appName: string | undefined;
workspaceType?: 'package-based' | 'integrated' | 'standalone';
js?: boolean;
appName?: string | undefined;
formatter?: 'none' | 'prettier';
}

interface ReactArguments extends BaseArguments {
Expand Down Expand Up @@ -394,7 +395,11 @@ async function determineStack(
choices: [
{
name: `none`,
message: `None: Configures a TypeScript/JavaScript project with minimal structure.`,
message:
process.env.NX_ADD_PLUGINS !== 'false' &&
process.env.NX_ADD_TS_PLUGIN === 'true'
? `None: Configures a TypeScript/JavaScript monorepo.`
: `None: Configures a TypeScript/JavaScript project with minimal structure.`,
},
{
name: `react`,
Expand Down Expand Up @@ -441,34 +446,14 @@ async function determinePresetOptions(
async function determineNoneOptions(
parsedArgs: yargs.Arguments<NoneArguments>
): Promise<Partial<NoneArguments>> {
let preset: Preset;
let workspaceType: 'package-based' | 'standalone' | 'integrated' | undefined =
undefined;
let appName: string | undefined = undefined;
let js: boolean | undefined;

if (parsedArgs.preset) {
preset = parsedArgs.preset;
} else {
workspaceType = await determinePackageBasedOrIntegratedOrStandalone();
if (workspaceType === 'standalone') {
preset = Preset.TsStandalone;
} else if (workspaceType === 'integrated') {
preset = Preset.Apps;
} else {
preset = Preset.NPM;
}
}

if (parsedArgs.js !== undefined) {
js = parsedArgs.js;
} else if (preset === Preset.TsStandalone) {
// Only standalone TS preset generates a default package, so we need to provide --js and --appName options.
appName = parsedArgs.name;
const reply = await enquirer.prompt<{ ts: 'Yes' | 'No' }>([
if (
process.env.NX_ADD_PLUGINS !== 'false' &&
process.env.NX_ADD_TS_PLUGIN === 'true'
) {
const reply = await enquirer.prompt<{ prettier: 'Yes' | 'No' }>([
{
name: 'ts',
message: `Would you like to use TypeScript with this project?`,
name: 'prettier',
message: `Would you like to use Prettier for code formatting?`,
type: 'autocomplete',
choices: [
{
Expand All @@ -478,14 +463,68 @@ async function determineNoneOptions(
name: 'No',
},
],
initial: 0,
initial: 1,
skip: !parsedArgs.interactive || isCI(),
},
]);
js = reply.ts === 'No';
}
return {
preset: Preset.TS,
formatter: reply.prettier === 'Yes' ? 'prettier' : 'none',
};
} else {
let preset: Preset;
let workspaceType:
| 'package-based'
| 'standalone'
| 'integrated'
| undefined = undefined;
let appName: string | undefined = undefined;
let js: boolean | undefined;

if (parsedArgs.preset) {
preset = parsedArgs.preset;
} else {
workspaceType = await determinePackageBasedOrIntegratedOrStandalone();
if (workspaceType === 'standalone') {
preset = Preset.TsStandalone;
} else if (workspaceType === 'integrated') {
preset = Preset.Apps;
} else {
preset = Preset.NPM;
}
}

if (preset === Preset.TS) {
return { preset, formatter: 'prettier' };
}

return { preset, js, appName };
if (parsedArgs.js !== undefined) {
js = parsedArgs.js;
} else if (preset === Preset.TsStandalone) {
// Only standalone TS preset generates a default package, so we need to provide --js and --appName options.
appName = parsedArgs.name;
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: 0,
skip: !parsedArgs.interactive || isCI(),
},
]);
js = reply.ts === 'No';
}

return { preset, js, appName };
}
}

async function determineReactOptions(
Expand Down
Loading

0 comments on commit 49c5a73

Please sign in to comment.