Skip to content

Commit

Permalink
feat(js): add a TS/JS standalone preset
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed May 25, 2023
1 parent d030a21 commit 7e8d377
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 20 deletions.
41 changes: 40 additions & 1 deletion packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ async function normalizeArgsMiddleware(
preset = Preset.AngularStandalone;
} else if (monorepoStyle === 'node-standalone') {
preset = Preset.NodeStandalone;
} else if (monorepoStyle === 'ts-standalone') {
preset = Preset.TsStandalone;
} else {
// when choose integrated monorepo, further prompt for preset
preset = await determinePreset(argv);
Expand Down Expand Up @@ -269,6 +271,9 @@ async function normalizeArgsMiddleware(
argv.routing ??
(argv.interactive ? await determineRouting(argv) : true);
}
} else if (preset === Preset.TsStandalone) {
name = await determinePackageName(preset, argv);
appName = name;
} else {
name = await determineRepoName(argv);
appName = await determineAppName(preset as Preset, argv);
Expand Down Expand Up @@ -402,6 +407,11 @@ async function determineMonorepoStyle(): Promise<string> {
message:
'Standalone Node app: Nx configures a framework (e.g. Express), esbuild, ESlint and Jest.',
},
{
name: 'ts-standalone',
message:
'Standalone TS/JS package: Nx configures a TypeScript/JavaScript package suitable for publishing to npm.',
},
],
},
]);
Expand Down Expand Up @@ -482,6 +492,34 @@ async function determineAppName(
});
}

async function determinePackageName(
preset: Preset,
parsedArgs: yargs.Arguments<Arguments>
): Promise<string> {
if (parsedArgs.name) {
return Promise.resolve(parsedArgs.name);
}

return enquirer
.prompt<{ PackageName: string }>([
{
name: 'PackageName',
message: `Package name `,
type: 'input',
},
])
.then((a) => {
if (!a.PackageName) {
output.error({
title: 'Invalid name',
bodyLines: [`Name cannot be empty`],
});
process.exit(1);
}
return a.PackageName;
});
}

async function determineFramework(
parsedArgs: yargs.Arguments<Arguments>
): Promise<string> {
Expand Down Expand Up @@ -671,7 +709,8 @@ async function determineStyle(
preset === Preset.Express ||
preset === Preset.ReactNative ||
preset === Preset.Expo ||
preset === Preset.NodeStandalone
preset === Preset.NodeStandalone ||
preset === Preset.TsStandalone
) {
return Promise.resolve(null);
}
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 @@ -18,6 +18,7 @@ export enum Preset {
React = 'react',
Angular = 'angular',
NodeStandalone = 'node-standalone',
TsStandalone = 'ts-standalone',
}

/**
Expand Down
14 changes: 0 additions & 14 deletions packages/js/src/generators/library/files/lib/tsconfig.json__tmpl__

This file was deleted.

31 changes: 30 additions & 1 deletion packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
} from '../../utils/versions';
import jsInitGenerator from '../init/init';
import { PackageJson } from 'nx/src/utils/package-json';
import { tsConfigBaseOptions } from '../../utils/typescript/create-ts-config';

export async function libraryGenerator(
tree: Tree,
Expand All @@ -62,6 +63,7 @@ export async function projectGenerator(
await jsInitGenerator(tree, {
...schema,
skipFormat: true,
tsConfigName: schema.rootProject ? 'tsconfig.json' : 'tsconfig.base.json',
})
);
const options = normalizeOptions(tree, schema, destinationDir);
Expand Down Expand Up @@ -270,6 +272,9 @@ function addBabelRc(tree: Tree, options: NormalizedSchema) {

function createFiles(tree: Tree, options: NormalizedSchema, filesDir: string) {
const { className, name, propertyName } = names(options.name);

createProjectTsConfigJson(tree, options);

generateFiles(tree, filesDir, options.projectRoot, {
...options,
dot: '.',
Expand All @@ -281,7 +286,6 @@ function createFiles(tree: Tree, options: NormalizedSchema, filesDir: string) {
strict: undefined,
tmpl: '',
offsetFromRoot: offsetFromRoot(options.projectRoot),
rootTsConfigPath: getRelativePathToRootTsConfig(tree, options.projectRoot),
buildable: options.bundler && options.bundler !== 'none',
hasUnitTestRunner: options.unitTestRunner !== 'none',
});
Expand Down Expand Up @@ -590,5 +594,30 @@ function getOutputPath(options: NormalizedSchema, destinationDir?: string) {
return joinPathFragments(...parts);
}

function createProjectTsConfigJson(tree: Tree, options: NormalizedSchema) {
const tsconfig = {
extends: options.rootProject
? undefined
: getRelativePathToRootTsConfig(tree, options.projectRoot),
compilerOptions: {
...tsConfigBaseOptions,
module: 'commonjs',
allowJs: options.js ? true : undefined,
},
files: [],
include: [],
references: [
{
path: './tsconfig.lib.json',
},
],
};
writeJson(
tree,
joinPathFragments(options.projectRoot, 'tsconfig.json'),
tsconfig
);
}

export default libraryGenerator;
export const librarySchematic = convertNxGenerator(libraryGenerator);
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,27 @@ Visit the [Nx Documentation](https://nx.dev) to learn more.
"
`;
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://raw.githubusercontent.com/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)** ✨
## Understand this workspace
Run \`nx graph\` to see a diagram of the dependencies of the projects.
## Remote caching
Run \`npx nx connect-to-nx-cloud\` to enable [remote caching](https://nx.app) and make CI faster.
## Further help
Visit the [Nx Documentation](https://nx.dev) to learn more.
"
`;
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 @@ -3,11 +3,11 @@
<a alt="Nx logo" href="https://nx.dev" target="_blank" rel="noreferrer"><img src="https://raw.githubusercontent.com/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)** ✨<% if (!!appName) { %>

<% if (includeServe) { %>
## Development server

Run `nx serve <%= appName %>` for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.<% } %>

<% } %>
## Understand this workspace

Run `nx graph` to see a diagram of the dependencies of the projects.
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/generators/new/generate-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,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
11 changes: 11 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,17 @@ 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',
rootProject: true,
});
} else if (options.preset === Preset.NodeStandalone) {
const { applicationGenerator: nodeApplicationGenerator } = require('@nx' +
'/node');
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/generators/utils/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export enum Preset {
Nest = 'nest',
Express = 'express',
NodeStandalone = 'node-standalone',
TsStandalone = 'ts-standalone',
}

0 comments on commit 7e8d377

Please sign in to comment.