feat(js): refactor usages of npmScope (#16947)

This commit is contained in:
Jason Jean 2023-05-15 16:06:24 -04:00 committed by GitHub
parent 9b4ba3af4d
commit 6b928bc250
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
62 changed files with 222 additions and 349 deletions

View File

@ -104,13 +104,11 @@ It only uses language primitives and immutable objects
- [createProjectGraphAsync](../../devkit/documents/nx_devkit#createprojectgraphasync) - [createProjectGraphAsync](../../devkit/documents/nx_devkit#createprojectgraphasync)
- [defaultTasksRunner](../../devkit/documents/nx_devkit#defaulttasksrunner) - [defaultTasksRunner](../../devkit/documents/nx_devkit#defaulttasksrunner)
- [detectPackageManager](../../devkit/documents/nx_devkit#detectpackagemanager) - [detectPackageManager](../../devkit/documents/nx_devkit#detectpackagemanager)
- [detectWorkspaceScope](../../devkit/documents/nx_devkit#detectworkspacescope)
- [ensurePackage](../../devkit/documents/nx_devkit#ensurepackage) - [ensurePackage](../../devkit/documents/nx_devkit#ensurepackage)
- [extractLayoutDirectory](../../devkit/documents/nx_devkit#extractlayoutdirectory) - [extractLayoutDirectory](../../devkit/documents/nx_devkit#extractlayoutdirectory)
- [formatFiles](../../devkit/documents/nx_devkit#formatfiles) - [formatFiles](../../devkit/documents/nx_devkit#formatfiles)
- [generateFiles](../../devkit/documents/nx_devkit#generatefiles) - [generateFiles](../../devkit/documents/nx_devkit#generatefiles)
- [getDependentPackagesForProject](../../devkit/documents/nx_devkit#getdependentpackagesforproject) - [getDependentPackagesForProject](../../devkit/documents/nx_devkit#getdependentpackagesforproject)
- [getImportPath](../../devkit/documents/nx_devkit#getimportpath)
- [getNpmPackageSharedConfig](../../devkit/documents/nx_devkit#getnpmpackagesharedconfig) - [getNpmPackageSharedConfig](../../devkit/documents/nx_devkit#getnpmpackagesharedconfig)
- [getOutputsForTargetAndConfiguration](../../devkit/documents/nx_devkit#getoutputsfortargetandconfiguration) - [getOutputsForTargetAndConfiguration](../../devkit/documents/nx_devkit#getoutputsfortargetandconfiguration)
- [getPackageManagerCommand](../../devkit/documents/nx_devkit#getpackagemanagercommand) - [getPackageManagerCommand](../../devkit/documents/nx_devkit#getpackagemanagercommand)
@ -1139,24 +1137,6 @@ Detects which package manager is used in the workspace based on the lock file.
--- ---
### detectWorkspaceScope
**detectWorkspaceScope**(`packageName`): `string`
Detect workspace scope from the package.json name
#### Parameters
| Name | Type |
| :------------ | :------- |
| `packageName` | `string` |
#### Returns
`string`
---
### ensurePackage ### ensurePackage
**ensurePackage**(`tree`, `pkg`, `requiredVersion`, `options?`): `void` **ensurePackage**(`tree`, `pkg`, `requiredVersion`, `options?`): `void`
@ -1324,25 +1304,6 @@ doesn't get confused about incorrect TypeScript files.
--- ---
### getImportPath
**getImportPath**(`npmScope`, `projectDirectory`): `string`
Prefixes project name with npm scope
#### Parameters
| Name | Type |
| :----------------- | :------- |
| `npmScope` | `string` |
| `projectDirectory` | `string` |
#### Returns
`string`
---
### getNpmPackageSharedConfig ### getNpmPackageSharedConfig
**getNpmPackageSharedConfig**(`pkgName`, `version`): [`SharedLibraryConfig`](../../devkit/documents/nx_devkit#sharedlibraryconfig) \| `undefined` **getNpmPackageSharedConfig**(`pkgName`, `version`): [`SharedLibraryConfig`](../../devkit/documents/nx_devkit#sharedlibraryconfig) \| `undefined`
@ -1471,12 +1432,12 @@ Example:
`Object` `Object`
| Name | Type | | Name | Type | Description |
| :-------------------- | :-------- | | :-------------------- | :-------- | :----------------------------------------------------------------------- |
| `appsDir` | `string` | | `appsDir` | `string` | - |
| `libsDir` | `string` | | `libsDir` | `string` | - |
| `npmScope` | `string` | | `npmScope` | `string` | **`Deprecated`** This will be removed in Nx 17. Use getNpmScope instead. |
| `standaloneAsDefault` | `boolean` | | `standaloneAsDefault` | `boolean` | - |
--- ---

View File

@ -12,7 +12,7 @@
"npmScope": { "npmScope": {
"type": "string", "type": "string",
"description": "Npm scope for importing libs. NOTE: only used if running the generator in an Angular CLI workspace.", "description": "Npm scope for importing libs. NOTE: only used if running the generator in an Angular CLI workspace.",
"x-priority": "important" "x-deprecated": "This will be inferred from your root package.json"
}, },
"defaultBase": { "defaultBase": {
"type": "string", "type": "string",

View File

@ -104,13 +104,11 @@ It only uses language primitives and immutable objects
- [createProjectGraphAsync](../../devkit/documents/nx_devkit#createprojectgraphasync) - [createProjectGraphAsync](../../devkit/documents/nx_devkit#createprojectgraphasync)
- [defaultTasksRunner](../../devkit/documents/nx_devkit#defaulttasksrunner) - [defaultTasksRunner](../../devkit/documents/nx_devkit#defaulttasksrunner)
- [detectPackageManager](../../devkit/documents/nx_devkit#detectpackagemanager) - [detectPackageManager](../../devkit/documents/nx_devkit#detectpackagemanager)
- [detectWorkspaceScope](../../devkit/documents/nx_devkit#detectworkspacescope)
- [ensurePackage](../../devkit/documents/nx_devkit#ensurepackage) - [ensurePackage](../../devkit/documents/nx_devkit#ensurepackage)
- [extractLayoutDirectory](../../devkit/documents/nx_devkit#extractlayoutdirectory) - [extractLayoutDirectory](../../devkit/documents/nx_devkit#extractlayoutdirectory)
- [formatFiles](../../devkit/documents/nx_devkit#formatfiles) - [formatFiles](../../devkit/documents/nx_devkit#formatfiles)
- [generateFiles](../../devkit/documents/nx_devkit#generatefiles) - [generateFiles](../../devkit/documents/nx_devkit#generatefiles)
- [getDependentPackagesForProject](../../devkit/documents/nx_devkit#getdependentpackagesforproject) - [getDependentPackagesForProject](../../devkit/documents/nx_devkit#getdependentpackagesforproject)
- [getImportPath](../../devkit/documents/nx_devkit#getimportpath)
- [getNpmPackageSharedConfig](../../devkit/documents/nx_devkit#getnpmpackagesharedconfig) - [getNpmPackageSharedConfig](../../devkit/documents/nx_devkit#getnpmpackagesharedconfig)
- [getOutputsForTargetAndConfiguration](../../devkit/documents/nx_devkit#getoutputsfortargetandconfiguration) - [getOutputsForTargetAndConfiguration](../../devkit/documents/nx_devkit#getoutputsfortargetandconfiguration)
- [getPackageManagerCommand](../../devkit/documents/nx_devkit#getpackagemanagercommand) - [getPackageManagerCommand](../../devkit/documents/nx_devkit#getpackagemanagercommand)
@ -1139,24 +1137,6 @@ Detects which package manager is used in the workspace based on the lock file.
--- ---
### detectWorkspaceScope
**detectWorkspaceScope**(`packageName`): `string`
Detect workspace scope from the package.json name
#### Parameters
| Name | Type |
| :------------ | :------- |
| `packageName` | `string` |
#### Returns
`string`
---
### ensurePackage ### ensurePackage
**ensurePackage**(`tree`, `pkg`, `requiredVersion`, `options?`): `void` **ensurePackage**(`tree`, `pkg`, `requiredVersion`, `options?`): `void`
@ -1324,25 +1304,6 @@ doesn't get confused about incorrect TypeScript files.
--- ---
### getImportPath
**getImportPath**(`npmScope`, `projectDirectory`): `string`
Prefixes project name with npm scope
#### Parameters
| Name | Type |
| :----------------- | :------- |
| `npmScope` | `string` |
| `projectDirectory` | `string` |
#### Returns
`string`
---
### getNpmPackageSharedConfig ### getNpmPackageSharedConfig
**getNpmPackageSharedConfig**(`pkgName`, `version`): [`SharedLibraryConfig`](../../devkit/documents/nx_devkit#sharedlibraryconfig) \| `undefined` **getNpmPackageSharedConfig**(`pkgName`, `version`): [`SharedLibraryConfig`](../../devkit/documents/nx_devkit#sharedlibraryconfig) \| `undefined`
@ -1471,12 +1432,12 @@ Example:
`Object` `Object`
| Name | Type | | Name | Type | Description |
| :-------------------- | :-------- | | :-------------------- | :-------- | :----------------------------------------------------------------------- |
| `appsDir` | `string` | | `appsDir` | `string` | - |
| `libsDir` | `string` | | `libsDir` | `string` | - |
| `npmScope` | `string` | | `npmScope` | `string` | **`Deprecated`** This will be removed in Nx 17. Use getNpmScope instead. |
| `standaloneAsDefault` | `boolean` | | `standaloneAsDefault` | `boolean` | - |
--- ---

View File

@ -25,10 +25,6 @@
"type": "boolean", "type": "boolean",
"default": true "default": true
}, },
"npmScope": {
"type": "string",
"description": "Npm scope for importing libs."
},
"standaloneApi": { "standaloneApi": {
"description": "Use Standalone Components if generating an Angular application.", "description": "Use Standalone Components if generating an Angular application.",
"type": "boolean", "type": "boolean",

View File

@ -14,10 +14,6 @@
"description": "The name of the application.", "description": "The name of the application.",
"type": "string" "type": "string"
}, },
"npmScope": {
"description": "Npm scope for importing libs.",
"type": "string"
},
"linter": { "linter": {
"description": "The tool to use for running lint checks.", "description": "The tool to use for running lint checks.",
"type": "string", "type": "string",

View File

@ -142,7 +142,6 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
affected: { affected: {
defaultBase: 'main', defaultBase: 'main',
}, },
npmScope: 'projscope',
tasksRunnerOptions: { tasksRunnerOptions: {
default: { default: {
options: { options: {

View File

@ -582,9 +582,9 @@ describe('Workspace Tests', () => {
}); });
it('should work for libraries when scope is unset', async () => { it('should work for libraries when scope is unset', async () => {
const json = readJson('nx.json'); const json = readJson('package.json');
delete json.npmScope; json.name = proj;
updateFile('nx.json', JSON.stringify(json)); updateFile('package.json', JSON.stringify(json));
const lib1 = uniq('mylib'); const lib1 = uniq('mylib');
const lib2 = uniq('mylib'); const lib2 = uniq('mylib');

View File

@ -5,6 +5,9 @@ import {
names, names,
Tree, Tree,
} from '@nx/devkit'; } from '@nx/devkit';
import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope';
import type { Schema } from '../schema'; import type { Schema } from '../schema';
import type { NormalizedSchema } from './normalized-schema'; import type { NormalizedSchema } from './normalized-schema';
import { E2eTestRunner, UnitTestRunner } from '../../../utils/test-runners'; import { E2eTestRunner, UnitTestRunner } from '../../../utils/test-runners';
@ -28,11 +31,8 @@ export function normalizeOptions(
? 'e2e' ? 'e2e'
: `${names(options.name).fileName}-e2e`; : `${names(options.name).fileName}-e2e`;
const { const { appsDir: defaultAppsDir, standaloneAsDefault } =
appsDir: defaultAppsDir, getWorkspaceLayout(host);
npmScope,
standaloneAsDefault,
} = getWorkspaceLayout(host);
const appsDir = layoutDirectory ?? defaultAppsDir; const appsDir = layoutDirectory ?? defaultAppsDir;
const appProjectRoot = options.rootProject const appProjectRoot = options.rootProject
? '.' ? '.'
@ -45,7 +45,11 @@ export function normalizeOptions(
? options.tags.split(',').map((s) => s.trim()) ? options.tags.split(',').map((s) => s.trim())
: []; : [];
const prefix = normalizeNewProjectPrefix(options.prefix, npmScope, 'app'); const prefix = normalizeNewProjectPrefix(
options.prefix,
getNpmScope(host),
'app'
);
options.standaloneConfig = options.standaloneConfig ?? standaloneAsDefault; options.standaloneConfig = options.standaloneConfig ?? standaloneAsDefault;

View File

@ -1,5 +1,5 @@
{ {
"name": "<% if(npmScope) { %><%= npmScope %>/<% } %><%= libFileName %>", "name": "<%= importPath %>",
"version": "0.0.1", "version": "0.0.1",
"peerDependencies": { "peerDependencies": {
"@angular/common": "<%= angularPeerDepVersion %>", "@angular/common": "<%= angularPeerDepVersion %>",

View File

@ -1,7 +1,6 @@
import type { Tree } from '@nx/devkit'; import type { Tree } from '@nx/devkit';
import { import {
generateFiles, generateFiles,
getWorkspaceLayout,
joinPathFragments, joinPathFragments,
names, names,
offsetFromRoot, offsetFromRoot,
@ -18,7 +17,6 @@ export function createFiles(
options: NormalizedSchema, options: NormalizedSchema,
project: AngularProjectConfiguration project: AngularProjectConfiguration
) { ) {
const { npmScope } = getWorkspaceLayout(tree);
const rootOffset = offsetFromRoot(options.libraryOptions.projectRoot); const rootOffset = offsetFromRoot(options.libraryOptions.projectRoot);
const libNames = names(options.libraryOptions.fileName); const libNames = names(options.libraryOptions.fileName);
const pathToComponent = options.componentOptions.flat const pathToComponent = options.componentOptions.flat
@ -42,7 +40,7 @@ export function createFiles(
projectRoot: options.libraryOptions.projectRoot, projectRoot: options.libraryOptions.projectRoot,
routing: options.libraryOptions.routing, routing: options.libraryOptions.routing,
pathToComponent, pathToComponent,
npmScope, importPath: options.libraryOptions.importPath,
rootOffset, rootOffset,
angularPeerDepVersion: `^${major}.${minor}.0`, angularPeerDepVersion: `^${major}.${minor}.0`,
tpl: '', tpl: '',

View File

@ -5,10 +5,14 @@ import {
names, names,
Tree, Tree,
} from '@nx/devkit'; } from '@nx/devkit';
import { getImportPath } from 'nx/src/utils/path';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope';
import { Linter } from '@nx/linter';
import { Schema } from '../schema'; import { Schema } from '../schema';
import { NormalizedSchema } from './normalized-schema'; import { NormalizedSchema } from './normalized-schema';
import { Linter } from '@nx/linter';
import { UnitTestRunner } from '../../../utils/test-runners'; import { UnitTestRunner } from '../../../utils/test-runners';
import { normalizeNewProjectPrefix } from '../../utils/project'; import { normalizeNewProjectPrefix } from '../../utils/project';
@ -37,11 +41,9 @@ export function normalizeOptions(host: Tree, schema: Schema): NormalizedSchema {
? `${names(projectDirectory).fileName}/${name}`.replace(/\/+/g, '/') ? `${names(projectDirectory).fileName}/${name}`.replace(/\/+/g, '/')
: name; : name;
const { const { libsDir: defaultLibsDirectory, standaloneAsDefault } =
libsDir: defaultLibsDirectory, getWorkspaceLayout(host);
npmScope, const npmScope = getNpmScope(host);
standaloneAsDefault,
} = getWorkspaceLayout(host);
const libsDir = layoutDirectory ?? defaultLibsDirectory; const libsDir = layoutDirectory ?? defaultLibsDirectory;
const projectName = fullProjectDirectory const projectName = fullProjectDirectory
@ -60,7 +62,7 @@ export function normalizeOptions(host: Tree, schema: Schema): NormalizedSchema {
options.standaloneConfig = options.standaloneConfig ?? standaloneAsDefault; options.standaloneConfig = options.standaloneConfig ?? standaloneAsDefault;
const importPath = const importPath =
options.importPath || getImportPath(npmScope, fullProjectDirectory); options.importPath || getImportPath(host, fullProjectDirectory);
const ngCliSchematicLibRoot = projectName; const ngCliSchematicLibRoot = projectName;
const allNormalizedOptions = { const allNormalizedOptions = {

View File

@ -98,7 +98,6 @@ exports[`workspace move to nx layout should create nx.json 1`] = `
], ],
"sharedGlobals": [], "sharedGlobals": [],
}, },
"npmScope": "my-org",
"targetDefaults": { "targetDefaults": {
"build": { "build": {
"dependsOn": [ "dependsOn": [

View File

@ -14,7 +14,6 @@ import {
formatFilesTask, formatFilesTask,
getAllProjects, getAllProjects,
getWorkspaceRootFileTypesInfo, getWorkspaceRootFileTypesInfo,
normalizeOptions,
updatePackageJson, updatePackageJson,
updateRootEsLintConfig, updateRootEsLintConfig,
updateRootTsConfig, updateRootTsConfig,
@ -24,11 +23,10 @@ import {
export async function migrateFromAngularCli( export async function migrateFromAngularCli(
tree: Tree, tree: Tree,
rawOptions: GeneratorOptions options: GeneratorOptions
): Promise<GeneratorCallback> { ): Promise<GeneratorCallback> {
validateWorkspace(tree); validateWorkspace(tree);
const projects = getAllProjects(tree); const projects = getAllProjects(tree);
const options = normalizeOptions(tree, rawOptions, projects);
const angularJson = readJson(tree, 'angular.json') as any; const angularJson = readJson(tree, 'angular.json') as any;
ensureAngularDevKitPeerDependenciesAreInstalled(tree); ensureAngularDevKitPeerDependenciesAreInstalled(tree);

View File

@ -3,7 +3,9 @@ import { E2eTestRunner, UnitTestRunner } from '../../utils/test-runners';
import type { Styles } from '../utils/types'; import type { Styles } from '../utils/types';
export interface GeneratorOptions { export interface GeneratorOptions {
skipInstall?: boolean; /**
* @deprecated This should be inferred with {@link getNpmScope}
*/
npmScope?: string; npmScope?: string;
defaultBase?: string; defaultBase?: string;

View File

@ -9,7 +9,7 @@
"npmScope": { "npmScope": {
"type": "string", "type": "string",
"description": "Npm scope for importing libs. NOTE: only used if running the generator in an Angular CLI workspace.", "description": "Npm scope for importing libs. NOTE: only used if running the generator in an Angular CLI workspace.",
"x-priority": "important" "x-deprecated": "This will be inferred from your root package.json"
}, },
"defaultBase": { "defaultBase": {
"type": "string", "type": "string",

View File

@ -1,7 +1,6 @@
export * from './dependencies'; export * from './dependencies';
export * from './format-files-task'; export * from './format-files-task';
export * from './logger'; export * from './logger';
export * from './normalize-options';
export * from './projects'; export * from './projects';
export * from './types'; export * from './types';
export * from './validation-logging'; export * from './validation-logging';

View File

@ -1,46 +0,0 @@
import type { Tree } from '@nx/devkit';
import {
detectWorkspaceScope,
joinPathFragments,
names,
readJson,
} from '@nx/devkit';
import type { GeneratorOptions } from '../schema';
import type { WorkspaceProjects } from './types';
export function normalizeOptions(
tree: Tree,
options: GeneratorOptions,
projects: WorkspaceProjects
): GeneratorOptions {
let npmScope = options.npmScope;
if (npmScope) {
npmScope = names(npmScope).fileName;
} else if (projects.libs.length > 0) {
// try get the scope from any library that have one
for (const lib of projects.libs) {
const packageJsonPath = joinPathFragments(
lib.config.root,
'package.json'
);
if (!tree.exists(packageJsonPath)) {
continue;
}
const { name } = readJson(
tree,
joinPathFragments(lib.config.root, 'package.json')
);
npmScope = detectWorkspaceScope(name);
if (npmScope) {
break;
}
}
}
// use the name (scope if exists) in the root package.json
npmScope =
npmScope || detectWorkspaceScope(readJson(tree, 'package.json').name);
return { ...options, npmScope };
}

View File

@ -45,12 +45,9 @@ export function createNxJson(
options: GeneratorOptions, options: GeneratorOptions,
defaultProject: string | undefined defaultProject: string | undefined
): void { ): void {
const { npmScope } = options;
const targets = getWorkspaceCommonTargets(tree); const targets = getWorkspaceCommonTargets(tree);
writeJson<NxJsonConfiguration>(tree, 'nx.json', { writeJson<NxJsonConfiguration>(tree, 'nx.json', {
...(npmScope ? { npmScope } : {}),
affected: { affected: {
defaultBase: options.defaultBase ?? deduceDefaultBase(), defaultBase: options.defaultBase ?? deduceDefaultBase(),
}, },

View File

@ -1,6 +1,7 @@
import type { Tree } from '@nx/devkit'; import type { Tree } from '@nx/devkit';
import { names, readNxJson, readProjectConfiguration } from '@nx/devkit'; import { names, readNxJson, readProjectConfiguration } from '@nx/devkit';
import type { AngularProjectConfiguration } from '../../utils/types'; import type { AngularProjectConfiguration } from '../../utils/types';
import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope';
export function normalizeDirectory( export function normalizeDirectory(
appName: string, appName: string,
@ -60,6 +61,6 @@ export function getProjectPrefix(
): string | undefined { ): string | undefined {
return ( return (
(readProjectConfiguration(tree, project) as AngularProjectConfiguration) (readProjectConfiguration(tree, project) as AngularProjectConfiguration)
.prefix ?? readNxJson(tree).npmScope .prefix ?? getNpmScope(tree)
); );
} }

View File

@ -1,5 +1,7 @@
import type { Tree } from '@nx/devkit'; import type { Tree } from '@nx/devkit';
import { names, readNxJson } from '@nx/devkit'; import { names } from '@nx/devkit';
import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope';
export function buildSelector( export function buildSelector(
tree: Tree, tree: Tree,
@ -9,7 +11,7 @@ export function buildSelector(
casing: keyof Pick<ReturnType<typeof names>, 'fileName' | 'propertyName'> casing: keyof Pick<ReturnType<typeof names>, 'fileName' | 'propertyName'>
): string { ): string {
let selector = name; let selector = name;
prefix ??= projectPrefix ?? readNxJson(tree)?.npmScope; prefix ??= projectPrefix ?? getNpmScope(tree);
if (prefix) { if (prefix) {
selector = `${prefix}-${selector}`; selector = `${prefix}-${selector}`;
} }

View File

@ -182,12 +182,7 @@ export { stripIndents } from 'nx/src/utils/strip-indents';
/** /**
* @category Utils * @category Utils
*/ */
export { export { joinPathFragments, normalizePath } from 'nx/src/utils/path';
joinPathFragments,
normalizePath,
getImportPath,
detectWorkspaceScope,
} from 'nx/src/utils/path';
// TODO(v16): Change this to export from 'nx/src/utils/workspace-root' // TODO(v16): Change this to export from 'nx/src/utils/workspace-root'
/** /**

View File

@ -18,6 +18,9 @@ export function getWorkspaceLayout(tree: Tree): {
appsDir: string; appsDir: string;
libsDir: string; libsDir: string;
standaloneAsDefault: boolean; standaloneAsDefault: boolean;
/**
* @deprecated This will be removed in Nx 17. Use {@link getNpmScope} instead.
*/
npmScope: string; npmScope: string;
} { } {
const nxJson = readNxJson(tree); const nxJson = readNxJson(tree);

View File

@ -2,14 +2,14 @@ import type { Tree } from '@nx/devkit';
import { import {
convertNxGenerator, convertNxGenerator,
formatFiles, formatFiles,
getImportPath,
getWorkspaceLayout,
joinPathFragments, joinPathFragments,
readProjectConfiguration, readProjectConfiguration,
updateProjectConfiguration, updateProjectConfiguration,
writeJson, writeJson,
} from '@nx/devkit'; } from '@nx/devkit';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { esbuildInitGenerator } from '../init/init'; import { esbuildInitGenerator } from '../init/init';
import { EsBuildExecutorOptions } from '../../executors/esbuild/schema'; import { EsBuildExecutorOptions } from '../../executors/esbuild/schema';
import { EsBuildProjectSchema } from './schema'; import { EsBuildProjectSchema } from './schema';
@ -43,9 +43,8 @@ function addBuildTarget(tree: Tree, options: EsBuildProjectSchema) {
const packageJsonPath = joinPathFragments(project.root, 'package.json'); const packageJsonPath = joinPathFragments(project.root, 'package.json');
if (!tree.exists(packageJsonPath)) { if (!tree.exists(packageJsonPath)) {
const { npmScope } = getWorkspaceLayout(tree);
const importPath = const importPath =
options.importPath || getImportPath(npmScope, options.project); options.importPath || getImportPath(tree, options.project);
writeJson(tree, packageJsonPath, { writeJson(tree, packageJsonPath, {
name: importPath, name: importPath,
version: '0.0.1', version: '0.0.1',

View File

@ -1,10 +1,5 @@
import { import { getWorkspaceLayout, joinPathFragments, names, Tree } from '@nx/devkit';
getImportPath, import { getImportPath } from '@nx/js/src/utils/get-import-path';
getWorkspaceLayout,
joinPathFragments,
names,
Tree,
} from '@nx/devkit';
import { Schema } from '../schema'; import { Schema } from '../schema';
export interface NormalizedSchema extends Schema { export interface NormalizedSchema extends Schema {
@ -28,7 +23,7 @@ export function normalizeOptions(
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-'); const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = projectName; const fileName = projectName;
const { libsDir, npmScope } = getWorkspaceLayout(host); const { libsDir } = getWorkspaceLayout(host);
const projectRoot = joinPathFragments(libsDir, projectDirectory); const projectRoot = joinPathFragments(libsDir, projectDirectory);
const parsedTags = options.tags const parsedTags = options.tags
@ -36,7 +31,7 @@ export function normalizeOptions(
: []; : [];
const importPath = const importPath =
options.importPath || getImportPath(npmScope, projectDirectory); options.importPath || getImportPath(host, projectDirectory);
const appMain = options.js ? 'src/index.js' : 'src/index.ts'; const appMain = options.js ? 'src/index.js' : 'src/index.ts';

View File

@ -18,7 +18,6 @@ import {
updateJson, updateJson,
writeJson, writeJson,
} from '@nx/devkit'; } from '@nx/devkit';
import { getImportPath } from 'nx/src/utils/path';
import { import {
addTsConfigPath, addTsConfigPath,
@ -29,6 +28,7 @@ import { addMinimalPublishScript } from '../../utils/minimal-publish-script';
import { Bundler, LibraryGeneratorSchema } from '../../utils/schema'; import { Bundler, LibraryGeneratorSchema } from '../../utils/schema';
import { addSwcConfig } from '../../utils/swc/add-swc-config'; import { addSwcConfig } from '../../utils/swc/add-swc-config';
import { addSwcDependencies } from '../../utils/swc/add-swc-dependencies'; import { addSwcDependencies } from '../../utils/swc/add-swc-dependencies';
import { getImportPath } from '../../utils/get-import-path';
import { import {
esbuildVersion, esbuildVersion,
nxVersion, nxVersion,
@ -491,8 +491,6 @@ function normalizeOptions(
pascalCaseFiles: options.pascalCaseFiles, pascalCaseFiles: options.pascalCaseFiles,
}); });
const { npmScope } = getWorkspaceLayout(tree);
const projectRoot = joinPathFragments(destinationDir, projectDirectory); const projectRoot = joinPathFragments(destinationDir, projectDirectory);
const parsedTags = options.tags const parsedTags = options.tags
@ -500,7 +498,7 @@ function normalizeOptions(
: []; : [];
const importPath = const importPath =
options.importPath || getImportPath(npmScope, projectDirectory); options.importPath || getImportPath(tree, projectDirectory);
options.minimal ??= false; options.minimal ??= false;

View File

@ -0,0 +1,25 @@
import { getImportPath } from './get-import-path';
import { Tree, writeJson } from '@nx/devkit';
import { createTree } from '@nx/devkit/testing-pre16';
describe('getImportPath', () => {
let tree: Tree;
beforeEach(() => {
tree = createTree();
});
it('should use the npmScope if it is set to anything other than @', () => {
writeJson(tree, 'package.json', {
name: '@myorg/source',
});
expect(getImportPath(tree, 'my-package')).toEqual('@myorg/my-package');
});
it('should just use the package name if npmScope is empty', () => {
writeJson(tree, 'package.json', {
name: 'source',
});
expect(getImportPath(tree, 'my-package')).toEqual('my-package');
});
});

View File

@ -0,0 +1,12 @@
import { Tree } from '@nx/devkit';
import { getNpmScope } from './package-json/get-npm-scope';
/**
* Prefixes project name with npm scope
*/
export function getImportPath(tree: Tree, projectDirectory: string): string {
const npmScope = getNpmScope(tree);
return npmScope
? `${npmScope === '@' ? '' : '@'}${npmScope}/${projectDirectory}`
: projectDirectory;
}

View File

@ -0,0 +1,19 @@
import { Tree, readNxJson, readJson } from '@nx/devkit';
/**
* Read the npm scope that a workspace should use by default
*/
export function getNpmScope(tree: Tree): string | undefined {
const nxJson = readNxJson(tree);
// TODO(v17): Remove reading this from nx.json
if (nxJson?.npmScope) {
return nxJson.npmScope;
}
const { name } = readJson<{ name?: string }>(tree, 'package.json');
if (name?.startsWith('@')) {
return name.split('/')[0].substring(1);
}
}

View File

@ -8,6 +8,7 @@ import {
Tree, Tree,
} from '@nx/devkit'; } from '@nx/devkit';
import type { LibraryGeneratorOptions, NormalizedOptions } from '../schema'; import type { LibraryGeneratorOptions, NormalizedOptions } from '../schema';
import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope';
export function normalizeOptions( export function normalizeOptions(
tree: Tree, tree: Tree,
@ -16,7 +17,7 @@ export function normalizeOptions(
const { layoutDirectory, projectDirectory } = extractLayoutDirectory( const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory options.directory
); );
const { libsDir: defaultLibsDir, npmScope } = getWorkspaceLayout(tree); const { libsDir: defaultLibsDir } = getWorkspaceLayout(tree);
const libsDir = layoutDirectory ?? defaultLibsDir; const libsDir = layoutDirectory ?? defaultLibsDir;
const name = names(options.name).fileName; const name = names(options.name).fileName;
const fullProjectDirectory = projectDirectory const fullProjectDirectory = projectDirectory
@ -39,7 +40,7 @@ export function normalizeOptions(
global: options.global ?? false, global: options.global ?? false,
linter: options.linter ?? Linter.EsLint, linter: options.linter ?? Linter.EsLint,
parsedTags, parsedTags,
prefix: npmScope, // we could also allow customizing this prefix: getNpmScope(tree), // we could also allow customizing this
projectDirectory: fullProjectDirectory, projectDirectory: fullProjectDirectory,
projectName, projectName,
projectRoot, projectRoot,

View File

@ -1,10 +1,5 @@
import { import { getWorkspaceLayout, joinPathFragments, names, Tree } from '@nx/devkit';
getImportPath, import { getImportPath } from '@nx/js/src/utils/get-import-path';
getWorkspaceLayout,
joinPathFragments,
names,
Tree,
} from '@nx/devkit';
import { Schema } from '../schema'; import { Schema } from '../schema';
export interface NormalizedSchema extends Schema { export interface NormalizedSchema extends Schema {
@ -23,10 +18,9 @@ export function normalizeOptions(
const { libsDir } = getWorkspaceLayout(host); const { libsDir } = getWorkspaceLayout(host);
const projectRoot = joinPathFragments(libsDir, projectDirectory); const projectRoot = joinPathFragments(libsDir, projectDirectory);
const { npmScope } = getWorkspaceLayout(host);
return { return {
...options, ...options,
importPath: options.importPath ?? getImportPath(npmScope, projectDirectory), importPath: options.importPath ?? getImportPath(host, projectDirectory),
projectRoot, projectRoot,
}; };
} }

View File

@ -15,7 +15,6 @@ import {
updateProjectConfiguration, updateProjectConfiguration,
updateTsConfigsToJs, updateTsConfigsToJs,
} from '@nx/devkit'; } from '@nx/devkit';
import { getImportPath } from 'nx/src/utils/path';
import { Schema } from './schema'; import { Schema } from './schema';
import { libraryGenerator as jsLibraryGenerator } from '@nx/js'; import { libraryGenerator as jsLibraryGenerator } from '@nx/js';
@ -23,10 +22,10 @@ import { join } from 'path';
import { addSwcDependencies } from '@nx/js/src/utils/swc/add-swc-dependencies'; import { addSwcDependencies } from '@nx/js/src/utils/swc/add-swc-dependencies';
import { addSwcConfig } from '@nx/js/src/utils/swc/add-swc-config'; import { addSwcConfig } from '@nx/js/src/utils/swc/add-swc-config';
import { initGenerator } from '../init/init'; import { initGenerator } from '../init/init';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
export interface NormalizedSchema extends Schema { export interface NormalizedSchema extends Schema {
name: string; name: string;
prefix: string;
fileName: string; fileName: string;
projectRoot: string; projectRoot: string;
projectDirectory: string; projectDirectory: string;
@ -99,11 +98,10 @@ function normalizeOptions(tree: Tree, options: Schema): NormalizedSchema {
: []; : [];
const importPath = const importPath =
options.importPath || getImportPath(npmScope, fullProjectDirectory); options.importPath || getImportPath(tree, fullProjectDirectory);
return { return {
...options, ...options,
prefix: npmScope, // we could also allow customizing this
fileName, fileName,
name: projectName, name: projectName,
projectRoot, projectRoot,

View File

@ -21,7 +21,8 @@
}, },
"npmScope": { "npmScope": {
"type": "string", "type": "string",
"description": "NPM Scope that the workspace uses." "description": "NPM Scope that the workspace uses.",
"x-deprecated": "This is inferred from the package.json in the workspace root instead."
}, },
"tasksRunnerOptions": { "tasksRunnerOptions": {
"type": "object", "type": "object",

View File

@ -423,6 +423,7 @@ export async function workspaceGenerators(args: Arguments) {
}); });
const nxJson: NxJsonConfiguration = readNxJson(); const nxJson: NxJsonConfiguration = readNxJson();
const collection = nxJson.npmScope const collection = nxJson.npmScope
? `@${nxJson.npmScope}/workspace-plugin` ? `@${nxJson.npmScope}/workspace-plugin`
: 'workspace-plugin'; : 'workspace-plugin';

View File

@ -68,6 +68,7 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
*/ */
targetDefaults?: TargetDefaults; targetDefaults?: TargetDefaults;
/** /**
* @deprecated This is inferred from the package.json in the workspace root. Please use {@link getNpmScope} instead.
* NPM Scope that the workspace uses * NPM Scope that the workspace uses
*/ */
npmScope?: string; npmScope?: string;

View File

@ -177,12 +177,7 @@ export { stripIndents } from './utils/strip-indents';
/** /**
* @category Utils * @category Utils
*/ */
export { export { joinPathFragments, normalizePath } from './utils/path';
joinPathFragments,
normalizePath,
getImportPath,
detectWorkspaceScope,
} from './utils/path';
// TODO(v16): Change this to export from './utils/workspace-root' // TODO(v16): Change this to export from './utils/workspace-root'
/** /**

View File

@ -11,6 +11,7 @@ import { NxJsonConfiguration } from '../config/nx-json';
import { Task } from '../config/task-graph'; import { Task } from '../config/task-graph';
import { InputDefinition } from '../config/workspace-json-project-json'; import { InputDefinition } from '../config/workspace-json-project-json';
import { hashTsConfig } from '../plugins/js/hasher/hasher'; import { hashTsConfig } from '../plugins/js/hasher/hasher';
import { createProjectRootMappings } from '../project-graph/utils/find-project-for-path';
type ExpandedSelfInput = type ExpandedSelfInput =
| { fileset: string } | { fileset: string }
@ -181,6 +182,9 @@ class TaskHasher {
[runtime: string]: Promise<PartialHash>; [runtime: string]: Promise<PartialHash>;
} = {}; } = {};
private externalDepsHashCache: { [packageName: string]: PartialHash } = {}; private externalDepsHashCache: { [packageName: string]: PartialHash } = {};
private projectRootMappings = createProjectRootMappings(
this.projectGraph.nodes
);
constructor( constructor(
private readonly nxJson: NxJsonConfiguration, private readonly nxJson: NxJsonConfiguration,
@ -584,7 +588,7 @@ class TaskHasher {
...fileNames, ...fileNames,
...values, ...values,
JSON.stringify({ ...p.data, files: undefined }), JSON.stringify({ ...p.data, files: undefined }),
hashTsConfig(p, this.nxJson, this.options), hashTsConfig(p, this.projectRootMappings, this.options),
]); ]);
res({ res({
value, value,

View File

@ -1,8 +1,10 @@
import { getImportPath } from '../../../utils/path';
import { ProjectGraphProjectNode } from '../../../config/project-graph'; import { ProjectGraphProjectNode } from '../../../config/project-graph';
import { readJsonFile } from '../../../utils/fileutils'; import { readJsonFile } from '../../../utils/fileutils';
import { getRootTsConfigFileName } from '../utils/typescript'; import { getRootTsConfigFileName } from '../utils/typescript';
import { NxJsonConfiguration } from '../../../config/nx-json'; import {
findProjectForPath,
ProjectRootMappings,
} from '../../../project-graph/utils/find-project-for-path';
interface CompilerOptions { interface CompilerOptions {
paths: Record<string, string[]>; paths: Record<string, string[]>;
@ -28,14 +30,14 @@ let tsConfigJson: TsconfigJsonConfiguration;
export function hashTsConfig( export function hashTsConfig(
p: ProjectGraphProjectNode, p: ProjectGraphProjectNode,
nxJson: NxJsonConfiguration, projectRootMappings: ProjectRootMappings,
{ selectivelyHashTsConfig }: { selectivelyHashTsConfig: boolean } { selectivelyHashTsConfig }: { selectivelyHashTsConfig: boolean }
) { ) {
if (!tsConfigJson) { if (!tsConfigJson) {
tsConfigJson = readTsConfigJson(); tsConfigJson = readTsConfigJson();
} }
if (selectivelyHashTsConfig) { if (selectivelyHashTsConfig) {
return removeOtherProjectsPathRecords(p, tsConfigJson, nxJson); return removeOtherProjectsPathRecords(p, tsConfigJson, projectRootMappings);
} else { } else {
return JSON.stringify(tsConfigJson); return JSON.stringify(tsConfigJson);
} }
@ -44,19 +46,28 @@ export function hashTsConfig(
function removeOtherProjectsPathRecords( function removeOtherProjectsPathRecords(
p: ProjectGraphProjectNode, p: ProjectGraphProjectNode,
tsConfigJson: TsconfigJsonConfiguration, tsConfigJson: TsconfigJsonConfiguration,
nxJson: NxJsonConfiguration projectRootMapping: ProjectRootMappings
) { ) {
const { paths, ...compilerOptions } = tsConfigJson.compilerOptions; const { paths, ...compilerOptions } = tsConfigJson.compilerOptions;
const rootPath = p.data.root.split('/'); const filteredPaths: Record<string, string[]> = {};
rootPath.shift();
const pathAlias = getImportPath(nxJson?.npmScope, rootPath.join('/')); if (!paths) {
return '';
}
for (const [key, files] of Object.entries(paths)) {
for (const filePath of files) {
if (p.name === findProjectForPath(filePath, projectRootMapping)) {
filteredPaths[key] = files;
break;
}
}
}
return JSON.stringify({ return JSON.stringify({
compilerOptions: { compilerOptions: {
...compilerOptions, ...compilerOptions,
paths: { paths: filteredPaths,
[pathAlias]: paths[pathAlias] ?? [],
},
}, },
}); });
} }

View File

@ -6,7 +6,7 @@ import {
ProjectGraph, ProjectGraph,
} from '../../../../config/project-graph'; } from '../../../../config/project-graph';
import { parseJson } from '../../../../utils/json'; import { parseJson } from '../../../../utils/json';
import { getImportPath, joinPathFragments } from '../../../../utils/path'; import { joinPathFragments } from '../../../../utils/path';
import { ProjectsConfigurations } from '../../../../config/workspace-json-project-json'; import { ProjectsConfigurations } from '../../../../config/workspace-json-project-json';
import { NxJsonConfiguration } from '../../../../config/nx-json'; import { NxJsonConfiguration } from '../../../../config/nx-json';
import { ExplicitDependency } from './explicit-project-dependencies'; import { ExplicitDependency } from './explicit-project-dependencies';
@ -56,9 +56,13 @@ function createPackageNameMap(
) )
) )
); );
// TODO(v17): Stop reading nx.json for the npmScope
const npmScope = nxJsonConfiguration.npmScope;
res[ res[
packageJson.name ?? packageJson.name ??
getImportPath(nxJsonConfiguration.npmScope, projectName) (npmScope
? `${npmScope === '@' ? '' : '@'}${npmScope}/${projectName}`
: projectName)
] = projectName; ] = projectName;
} catch (e) {} } catch (e) {}
} }

View File

@ -1,4 +1,4 @@
import { getImportPath, joinPathFragments, normalizePath } from './path'; import { joinPathFragments, normalizePath } from './path';
describe('normalizePath', () => { describe('normalizePath', () => {
it('should remove drive letters', () => { it('should remove drive letters', () => {
@ -21,17 +21,3 @@ describe('joinPathFragments', () => {
); );
}); });
}); });
describe('getImportPath', () => {
it('should use the npmScope if it is set to anything other than @', () => {
expect(getImportPath('myorg', 'my-package')).toEqual('@myorg/my-package');
});
it('should allow for a single @ to be used as the npmScope', () => {
expect(getImportPath('@', 'my-package')).toEqual('@/my-package');
});
it('should just use the package name if npmScope is empty', () => {
expect(getImportPath('', 'my-package')).toEqual('my-package');
});
});

View File

@ -17,26 +17,3 @@ export function normalizePath(osSpecificPath: string): string {
export function joinPathFragments(...fragments: string[]): string { export function joinPathFragments(...fragments: string[]): string {
return normalizePath(path.join(...fragments)); return normalizePath(path.join(...fragments));
} }
/**
* Detect workspace scope from the package.json name
* @param packageName
* @returns
*/
export function detectWorkspaceScope(packageName: string): string {
return packageName?.startsWith('@')
? packageName.substring(1).split('/')[0]
: '';
}
/**
* Prefixes project name with npm scope
*/
export function getImportPath(
npmScope: string,
projectDirectory: string
): string {
return npmScope
? `${npmScope === '@' ? '' : '@'}${npmScope}/${projectDirectory}`
: projectDirectory;
}

View File

@ -1,12 +1,12 @@
import { import {
extractLayoutDirectory, extractLayoutDirectory,
getImportPath,
getWorkspaceLayout, getWorkspaceLayout,
joinPathFragments, joinPathFragments,
names, names,
Tree, Tree,
} from '@nx/devkit'; } from '@nx/devkit';
import { Schema } from '../schema'; import { Schema } from '../schema';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
export interface NormalizedSchema extends Schema { export interface NormalizedSchema extends Schema {
name: string; name: string;
@ -15,7 +15,6 @@ export interface NormalizedSchema extends Schema {
projectRoot: string; projectRoot: string;
projectDirectory: string; projectDirectory: string;
parsedTags: string[]; parsedTags: string[];
npmScope: string;
npmPackageName: string; npmPackageName: string;
bundler: 'swc' | 'tsc'; bundler: 'swc' | 'tsc';
publishable: boolean; publishable: boolean;
@ -27,7 +26,7 @@ export function normalizeOptions(
const { layoutDirectory, projectDirectory } = extractLayoutDirectory( const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory options.directory
); );
const { npmScope, libsDir: defaultLibsDir } = getWorkspaceLayout(host); const { libsDir: defaultLibsDir } = getWorkspaceLayout(host);
const libsDir = layoutDirectory ?? defaultLibsDir; const libsDir = layoutDirectory ?? defaultLibsDir;
const name = names(options.name).fileName; const name = names(options.name).fileName;
const fullProjectDirectory = projectDirectory const fullProjectDirectory = projectDirectory
@ -48,13 +47,12 @@ export function normalizeOptions(
? options.tags.split(',').map((s) => s.trim()) ? options.tags.split(',').map((s) => s.trim())
: []; : [];
const npmPackageName = options.importPath || getImportPath(npmScope, name); const npmPackageName = options.importPath || getImportPath(host, name);
return { return {
...options, ...options,
bundler: options.compiler ?? 'tsc', bundler: options.compiler ?? 'tsc',
fileName, fileName,
npmScope,
libsDir, libsDir,
name: projectName, name: projectName,
projectRoot, projectRoot,

View File

@ -1,10 +1,5 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { import { Tree, readProjectConfiguration, readJson } from '@nx/devkit';
Tree,
readProjectConfiguration,
readJson,
readNxJson,
} from '@nx/devkit';
import generator from './generator'; import generator from './generator';
import { PackageJson } from 'nx/src/utils/package-json'; import { PackageJson } from 'nx/src/utils/package-json';
@ -24,6 +19,6 @@ describe('preset generator', () => {
expect(config).toBeDefined(); expect(config).toBeDefined();
const packageJson = readJson<PackageJson>(tree, 'package.json'); const packageJson = readJson<PackageJson>(tree, 'package.json');
expect(packageJson.dependencies).toHaveProperty('@nx/devkit'); expect(packageJson.dependencies).toHaveProperty('@nx/devkit');
expect(readNxJson(tree).npmScope).not.toBeDefined(); expect(readJson(tree, 'nx.json').npmScope).not.toBeDefined();
}); });
}); });

View File

@ -1,5 +1,5 @@
import { getWorkspaceLayout, joinPathFragments, names, Tree } from '@nx/devkit'; import { getWorkspaceLayout, joinPathFragments, names, Tree } from '@nx/devkit';
import { getImportPath } from 'nx/src/utils/path'; import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { Schema } from '../schema'; import { Schema } from '../schema';
export interface NormalizedSchema extends Schema { export interface NormalizedSchema extends Schema {
@ -24,7 +24,7 @@ export function normalizeOptions(
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-'); const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = projectName; const fileName = projectName;
const { libsDir, npmScope } = getWorkspaceLayout(host); const { libsDir } = getWorkspaceLayout(host);
const projectRoot = joinPathFragments(libsDir, projectDirectory); const projectRoot = joinPathFragments(libsDir, projectDirectory);
const parsedTags = options.tags const parsedTags = options.tags
@ -32,7 +32,7 @@ export function normalizeOptions(
: []; : [];
const importPath = const importPath =
options.importPath || getImportPath(npmScope, projectDirectory); options.importPath || getImportPath(host, projectDirectory);
const normalized: NormalizedSchema = { const normalized: NormalizedSchema = {
...options, ...options,

View File

@ -1,6 +1,5 @@
import { import {
extractLayoutDirectory, extractLayoutDirectory,
getImportPath,
getProjects, getProjects,
getWorkspaceLayout, getWorkspaceLayout,
joinPathFragments, joinPathFragments,
@ -9,6 +8,8 @@ import {
normalizePath, normalizePath,
Tree, Tree,
} from '@nx/devkit'; } from '@nx/devkit';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { assertValidStyle } from '../../../utils/assertion'; import { assertValidStyle } from '../../../utils/assertion';
import { NormalizedSchema, Schema } from '../schema'; import { NormalizedSchema, Schema } from '../schema';
@ -26,7 +27,7 @@ export function normalizeOptions(
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-'); const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = projectName; const fileName = projectName;
const { libsDir: defaultLibsDir, npmScope } = getWorkspaceLayout(host); const { libsDir: defaultLibsDir } = getWorkspaceLayout(host);
const libsDir = layoutDirectory ?? defaultLibsDir; const libsDir = layoutDirectory ?? defaultLibsDir;
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory); const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);
@ -35,7 +36,7 @@ export function normalizeOptions(
: []; : [];
const importPath = const importPath =
options.importPath || getImportPath(npmScope, fullProjectDirectory); options.importPath || getImportPath(host, fullProjectDirectory);
let bundler = options.bundler ?? 'none'; let bundler = options.bundler ?? 'none';

View File

@ -1,11 +1,10 @@
import { import {
addDependenciesToPackageJson, addDependenciesToPackageJson,
applyChangesToString, applyChangesToString,
getWorkspaceLayout, joinPathFragments,
names, names,
Tree,
} from '@nx/devkit'; } from '@nx/devkit';
import { Tree } from 'nx/src/generators/tree';
import { getImportPath, joinPathFragments } from 'nx/src/utils/path';
import type * as ts from 'typescript'; import type * as ts from 'typescript';
import { NormalizedSchema } from '../schema'; import { NormalizedSchema } from '../schema';
@ -21,6 +20,7 @@ import {
typesReactRouterDomVersion, typesReactRouterDomVersion,
} from '../../../utils/versions'; } from '../../../utils/versions';
import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript'; import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
let tsModule: typeof import('typescript'); let tsModule: typeof import('typescript');
@ -78,13 +78,12 @@ export function updateAppRoutes(host: Tree, options: NormalizedSchema) {
{ {
const { content: componentContent, source: componentSource } = const { content: componentContent, source: componentSource } =
readComponent(host, appComponentPath); readComponent(host, appComponentPath);
const { npmScope } = getWorkspaceLayout(host);
const changes = applyChangesToString( const changes = applyChangesToString(
componentContent, componentContent,
addRoute(appComponentPath, componentSource, { addRoute(appComponentPath, componentSource, {
routePath: options.routePath, routePath: options.routePath,
componentName: names(options.name).className, componentName: names(options.name).className,
moduleName: getImportPath(npmScope, options.projectDirectory), moduleName: getImportPath(host, options.projectDirectory),
}) })
); );
host.write(appComponentPath, changes); host.write(appComponentPath, changes);

View File

@ -2,13 +2,12 @@ import type { Tree } from '@nx/devkit';
import { import {
convertNxGenerator, convertNxGenerator,
formatFiles, formatFiles,
getImportPath,
getWorkspaceLayout,
joinPathFragments, joinPathFragments,
readProjectConfiguration, readProjectConfiguration,
updateProjectConfiguration, updateProjectConfiguration,
writeJson, writeJson,
} from '@nx/devkit'; } from '@nx/devkit';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { rollupInitGenerator } from '../init/init'; import { rollupInitGenerator } from '../init/init';
import { RollupExecutorOptions } from '../../executors/rollup/schema'; import { RollupExecutorOptions } from '../../executors/rollup/schema';
@ -43,9 +42,8 @@ function addBuildTarget(tree: Tree, options: RollupProjectSchema) {
const packageJsonPath = joinPathFragments(project.root, 'package.json'); const packageJsonPath = joinPathFragments(project.root, 'package.json');
if (!tree.exists(packageJsonPath)) { if (!tree.exists(packageJsonPath)) {
const { npmScope } = getWorkspaceLayout(tree);
const importPath = const importPath =
options.importPath || getImportPath(npmScope, options.project); options.importPath || getImportPath(tree, options.project);
writeJson(tree, packageJsonPath, { writeJson(tree, packageJsonPath, {
name: importPath, name: importPath,
version: '0.0.1', version: '0.0.1',

View File

@ -1,7 +1,7 @@
import { getWorkspaceLayout, ProjectConfiguration, Tree } from '@nx/devkit'; import { ProjectConfiguration, Tree } from '@nx/devkit';
import { getImportPath } from 'nx/src/utils/path';
import type { NormalizedSchema, Schema } from '../schema'; import type { NormalizedSchema, Schema } from '../schema';
import { getDestination, getNewProjectName, normalizeSlashes } from './utils'; import { getDestination, getNewProjectName, normalizeSlashes } from './utils';
import { getImportPath } from '../../../utilities/get-import-path';
export function normalizeSchema( export function normalizeSchema(
tree: Tree, tree: Tree,
@ -13,14 +13,12 @@ export function normalizeSchema(
: schema.destination; : schema.destination;
const newProjectName = const newProjectName =
schema.newProjectName ?? getNewProjectName(destination); schema.newProjectName ?? getNewProjectName(destination);
const { npmScope } = getWorkspaceLayout(tree);
return { return {
...schema, ...schema,
destination, destination,
importPath: importPath:
schema.importPath ?? schema.importPath ?? normalizeSlashes(getImportPath(tree, destination)),
normalizeSlashes(getImportPath(npmScope, destination)),
newProjectName, newProjectName,
relativeToRootDestination: getDestination( relativeToRootDestination: getDestination(
tree, tree,

View File

@ -10,7 +10,6 @@ import {
visitNotIgnoredFiles, visitNotIgnoredFiles,
writeJson, writeJson,
readJson, readJson,
getImportPath,
} from '@nx/devkit'; } from '@nx/devkit';
import type * as ts from 'typescript'; import type * as ts from 'typescript';
import { import {
@ -21,6 +20,7 @@ import { NormalizedSchema } from '../schema';
import { normalizeSlashes } from './utils'; import { normalizeSlashes } from './utils';
import { relative } from 'path'; import { relative } from 'path';
import { ensureTypescript } from '../../../utilities/typescript'; import { ensureTypescript } from '../../../utilities/typescript';
import { getImportPath } from '../../../utilities/get-import-path';
let tsModule: typeof import('typescript'); let tsModule: typeof import('typescript');
@ -62,7 +62,7 @@ export function updateImports(
fromPath || fromPath ||
normalizeSlashes( normalizeSlashes(
getImportPath( getImportPath(
npmScope, tree,
project.root.slice(libsDir.length).replace(/^\/|\\/, '') project.root.slice(libsDir.length).replace(/^\/|\\/, '')
) )
), ),

View File

@ -8,7 +8,7 @@ exports[`new --preset should generate necessary npm dependencies for empty prese
"nx": "0.0.1", "nx": "0.0.1",
}, },
"license": "MIT", "license": "MIT",
"name": "my-workspace", "name": "@my-workspace/source",
"private": true, "private": true,
"scripts": {}, "scripts": {},
"version": "0.0.0", "version": "0.0.0",
@ -28,7 +28,6 @@ exports[`new should generate an empty nx.json 1`] = `
], ],
"sharedGlobals": [], "sharedGlobals": [],
}, },
"npmScope": "npmScope",
"targetDefaults": { "targetDefaults": {
"build": { "build": {
"dependsOn": [ "dependsOn": [

View File

@ -1,5 +1,5 @@
{ {
"name": "<%= formattedNames.fileName %>", "name": "@<%= formattedNames.fileName %>/source",
"version": "0.0.0", "version": "0.0.0",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {

View File

@ -1,5 +1,5 @@
{ {
"name": "<%= formattedNames.fileName %>", "name": "@<%= formattedNames.fileName %>/source",
"version": "0.0.0", "version": "0.0.0",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {

View File

@ -72,7 +72,6 @@ export function generatePreset(host: Tree, opts: NormalizedSchema) {
`--name=${opts.appName}`, `--name=${opts.appName}`,
opts.style ? `--style=${opts.style}` : null, opts.style ? `--style=${opts.style}` : null,
opts.linter ? `--linter=${opts.linter}` : null, opts.linter ? `--linter=${opts.linter}` : null,
opts.npmScope ? `--npmScope=${opts.npmScope}` : `--npmScope=${opts.name}`,
opts.preset ? `--preset=${opts.preset}` : null, opts.preset ? `--preset=${opts.preset}` : null,
opts.bundler ? `--bundler=${opts.bundler}` : null, opts.bundler ? `--bundler=${opts.bundler}` : null,
opts.framework ? `--framework=${opts.framework}` : null, opts.framework ? `--framework=${opts.framework}` : null,

View File

@ -85,7 +85,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
expect(nxJson).toMatchInlineSnapshot(` expect(nxJson).toMatchInlineSnapshot(`
{ {
"$schema": "./node_modules/nx/schemas/nx-schema.json", "$schema": "./node_modules/nx/schemas/nx-schema.json",
"npmScope": "proj",
"targetDefaults": { "targetDefaults": {
"build": { "build": {
"dependsOn": [ "dependsOn": [
@ -134,7 +133,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
], ],
"sharedGlobals": [], "sharedGlobals": [],
}, },
"npmScope": "proj",
"targetDefaults": { "targetDefaults": {
"build": { "build": {
"dependsOn": [ "dependsOn": [

View File

@ -50,7 +50,6 @@ function setPresetProperty(tree: Tree, options: NormalizedSchema) {
delete json.implicitDependencies; delete json.implicitDependencies;
delete json.targetDefaults; delete json.targetDefaults;
delete json.workspaceLayout; delete json.workspaceLayout;
delete json.npmScope;
} }
return json; return json;
}); });
@ -79,11 +78,10 @@ function createAppsAndLibsFolders(tree: Tree, options: NormalizedSchema) {
function createNxJson( function createNxJson(
tree: Tree, tree: Tree,
{ directory, npmScope, packageManager, defaultBase, preset }: NormalizedSchema { directory, defaultBase, preset }: NormalizedSchema
) { ) {
const nxJson: NxJsonConfiguration & { $schema: string } = { const nxJson: NxJsonConfiguration & { $schema: string } = {
$schema: './node_modules/nx/schemas/nx-schema.json', $schema: './node_modules/nx/schemas/nx-schema.json',
npmScope: npmScope,
affected: { affected: {
defaultBase, defaultBase,
}, },
@ -209,7 +207,6 @@ function normalizeOptions(options: NormalizedSchema) {
const name = names(options.name).fileName; const name = names(options.name).fileName;
return { return {
name, name,
npmScope: name,
...options, ...options,
defaultBase, defaultBase,
}; };

View File

@ -39,7 +39,6 @@ describe('new', () => {
...defaultOptions, ...defaultOptions,
name: 'my-workspace', name: 'my-workspace',
directory: 'my-workspace', directory: 'my-workspace',
npmScope: 'npmScope',
appName: 'app', appName: 'app',
}); });
expect(readJson(tree, 'my-workspace/nx.json')).toMatchSnapshot(); expect(readJson(tree, 'my-workspace/nx.json')).toMatchSnapshot();
@ -51,7 +50,6 @@ describe('new', () => {
...defaultOptions, ...defaultOptions,
name: 'my-workspace', name: 'my-workspace',
directory: 'my-workspace', directory: 'my-workspace',
npmScope: 'npmScope',
appName: 'app', appName: 'app',
preset: Preset.Empty, preset: Preset.Empty,
}); });
@ -64,7 +62,6 @@ describe('new', () => {
...defaultOptions, ...defaultOptions,
name: 'my-workspace', name: 'my-workspace',
directory: 'my-workspace', directory: 'my-workspace',
npmScope: 'npmScope',
appName: 'app', appName: 'app',
preset: Preset.ReactMonorepo, preset: Preset.ReactMonorepo,
bundler: 'vite', bundler: 'vite',
@ -85,7 +82,6 @@ describe('new', () => {
...defaultOptions, ...defaultOptions,
name: 'my-workspace', name: 'my-workspace',
directory: 'my-workspace', directory: 'my-workspace',
npmScope: 'npmScope',
appName: 'app', appName: 'app',
preset: Preset.AngularMonorepo, preset: Preset.AngularMonorepo,
}); });
@ -140,7 +136,6 @@ describe('new', () => {
...defaultOptions, ...defaultOptions,
name: 'my-workspace', name: 'my-workspace',
directory: 'my-workspace', directory: 'my-workspace',
npmScope: 'npmScope',
appName: 'app', appName: 'app',
preset, preset,
}); });
@ -177,7 +172,6 @@ describe('new', () => {
...defaultOptions, ...defaultOptions,
name: 'my-workspace', name: 'my-workspace',
directory: 'my-workspace', directory: 'my-workspace',
npmScope: 'npmScope',
appName: 'app', appName: 'app',
}); });
@ -193,7 +187,6 @@ describe('new', () => {
...defaultOptions, ...defaultOptions,
name: 'my-workspace', name: 'my-workspace',
directory: 'my-workspace', directory: 'my-workspace',
npmScope: 'npmScope',
appName: 'app', appName: 'app',
}); });
fail('Generating into a non-empty directory should error.'); fail('Generating into a non-empty directory should error.');

View File

@ -16,7 +16,6 @@ interface Schema {
directory: string; directory: string;
name: string; name: string;
appName?: string; appName?: string;
npmScope?: string;
skipInstall?: boolean; skipInstall?: boolean;
style?: string; style?: string;
nxCloud?: boolean; nxCloud?: boolean;

View File

@ -25,10 +25,6 @@
"type": "boolean", "type": "boolean",
"default": true "default": true
}, },
"npmScope": {
"type": "string",
"description": "Npm scope for importing libs."
},
"standaloneApi": { "standaloneApi": {
"description": "Use Standalone Components if generating an Angular application.", "description": "Use Standalone Components if generating an Angular application.",
"type": "boolean", "type": "boolean",

View File

@ -9,7 +9,7 @@ import {
writeJson, writeJson,
} from '@nx/devkit'; } from '@nx/devkit';
import { join } from 'path'; import { join } from 'path';
import { getImportPath } from 'nx/src/utils/path'; import { getImportPath } from '../../utilities/get-import-path';
export interface ProjectOptions { export interface ProjectOptions {
name: string; name: string;
@ -20,15 +20,10 @@ function normalizeOptions(options: ProjectOptions): ProjectOptions {
return options; return options;
} }
function addFiles( function addFiles(projectRoot: string, tree: Tree, options: ProjectOptions) {
projectRoot: string,
tree: Tree,
npmScope: string,
options: ProjectOptions
) {
const packageJsonPath = join(projectRoot, 'package.json'); const packageJsonPath = join(projectRoot, 'package.json');
writeJson(tree, packageJsonPath, { writeJson(tree, packageJsonPath, {
name: getImportPath(npmScope, options.name), name: getImportPath(tree, options.name),
version: '0.0.0', version: '0.0.0',
scripts: { scripts: {
test: 'node index.js', test: 'node index.js',
@ -41,7 +36,7 @@ function addFiles(
export async function npmPackageGenerator(tree: Tree, options: ProjectOptions) { export async function npmPackageGenerator(tree: Tree, options: ProjectOptions) {
options = normalizeOptions(options); options = normalizeOptions(options);
const { libsDir, npmScope } = getWorkspaceLayout(tree); const { libsDir } = getWorkspaceLayout(tree);
const projectRoot = join(libsDir, options.name); const projectRoot = join(libsDir, options.name);
addProjectConfiguration(tree, options.name, { addProjectConfiguration(tree, options.name, {
@ -53,7 +48,7 @@ export async function npmPackageGenerator(tree: Tree, options: ProjectOptions) {
const isEmpty = fileCount === 0 || (fileCount === 1 && projectJsonExists); const isEmpty = fileCount === 0 || (fileCount === 1 && projectJsonExists);
if (isEmpty) { if (isEmpty) {
addFiles(projectRoot, tree, npmScope, options); addFiles(projectRoot, tree, options);
} }
await formatFiles(tree); await formatFiles(tree);

View File

@ -3,7 +3,6 @@ import { PackageManager } from '@nx/devkit';
export interface Schema { export interface Schema {
name: string; name: string;
npmScope?: string;
style?: string; style?: string;
linter?: string; linter?: string;
preset: Preset; preset: Preset;

View File

@ -14,10 +14,6 @@
"description": "The name of the application.", "description": "The name of the application.",
"type": "string" "type": "string"
}, },
"npmScope": {
"description": "Npm scope for importing libs.",
"type": "string"
},
"linter": { "linter": {
"description": "The tool to use for running lint checks.", "description": "The tool to use for running lint checks.",
"type": "string", "type": "string",

View File

@ -0,0 +1,25 @@
import { Tree } from 'nx/src/generators/tree';
import { readNxJson } from 'nx/src/generators/utils/nx-json';
import { readJson } from 'nx/src/generators/utils/json';
export function getImportPath(tree: Tree, projectDirectory: string): string {
const npmScope = getNpmScope(tree);
return npmScope
? `${npmScope === '@' ? '' : '@'}${npmScope}/${projectDirectory}`
: projectDirectory;
}
function getNpmScope(tree: Tree) {
const nxJson = readNxJson(tree);
// TODO(v17): Remove reading this from nx.json
if (nxJson.npmScope) {
return nxJson.npmScope;
}
const { name } = readJson<{ name?: string }>(tree, 'package.json');
if (name?.startsWith('@')) {
return name.split('/')[0].substring(1);
}
}