Skip to content

Commit

Permalink
feat(js): refactor usages of npmScope (#16947)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored May 15, 2023
1 parent 9b4ba3a commit 6b928bc
Show file tree
Hide file tree
Showing 62 changed files with 222 additions and 349 deletions.
51 changes: 6 additions & 45 deletions docs/generated/devkit/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,11 @@ It only uses language primitives and immutable objects
- [createProjectGraphAsync](../../devkit/documents/nx_devkit#createprojectgraphasync)
- [defaultTasksRunner](../../devkit/documents/nx_devkit#defaulttasksrunner)
- [detectPackageManager](../../devkit/documents/nx_devkit#detectpackagemanager)
- [detectWorkspaceScope](../../devkit/documents/nx_devkit#detectworkspacescope)
- [ensurePackage](../../devkit/documents/nx_devkit#ensurepackage)
- [extractLayoutDirectory](../../devkit/documents/nx_devkit#extractlayoutdirectory)
- [formatFiles](../../devkit/documents/nx_devkit#formatfiles)
- [generateFiles](../../devkit/documents/nx_devkit#generatefiles)
- [getDependentPackagesForProject](../../devkit/documents/nx_devkit#getdependentpackagesforproject)
- [getImportPath](../../devkit/documents/nx_devkit#getimportpath)
- [getNpmPackageSharedConfig](../../devkit/documents/nx_devkit#getnpmpackagesharedconfig)
- [getOutputsForTargetAndConfiguration](../../devkit/documents/nx_devkit#getoutputsfortargetandconfiguration)
- [getPackageManagerCommand](../../devkit/documents/nx_devkit#getpackagemanagercommand)
Expand Down Expand Up @@ -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**(`tree`, `pkg`, `requiredVersion`, `options?`): `void`
Expand Down Expand Up @@ -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**(`pkgName`, `version`): [`SharedLibraryConfig`](../../devkit/documents/nx_devkit#sharedlibraryconfig) \| `undefined`
Expand Down Expand Up @@ -1471,12 +1432,12 @@ Example:

`Object`

| Name | Type |
| :-------------------- | :-------- |
| `appsDir` | `string` |
| `libsDir` | `string` |
| `npmScope` | `string` |
| `standaloneAsDefault` | `boolean` |
| Name | Type | Description |
| :-------------------- | :-------- | :----------------------------------------------------------------------- |
| `appsDir` | `string` | - |
| `libsDir` | `string` | - |
| `npmScope` | `string` | **`Deprecated`** This will be removed in Nx 17. Use getNpmScope instead. |
| `standaloneAsDefault` | `boolean` | - |

---

Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages/angular/generators/ng-add.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"npmScope": {
"type": "string",
"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": {
"type": "string",
Expand Down
51 changes: 6 additions & 45 deletions docs/generated/packages/devkit/documents/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,11 @@ It only uses language primitives and immutable objects
- [createProjectGraphAsync](../../devkit/documents/nx_devkit#createprojectgraphasync)
- [defaultTasksRunner](../../devkit/documents/nx_devkit#defaulttasksrunner)
- [detectPackageManager](../../devkit/documents/nx_devkit#detectpackagemanager)
- [detectWorkspaceScope](../../devkit/documents/nx_devkit#detectworkspacescope)
- [ensurePackage](../../devkit/documents/nx_devkit#ensurepackage)
- [extractLayoutDirectory](../../devkit/documents/nx_devkit#extractlayoutdirectory)
- [formatFiles](../../devkit/documents/nx_devkit#formatfiles)
- [generateFiles](../../devkit/documents/nx_devkit#generatefiles)
- [getDependentPackagesForProject](../../devkit/documents/nx_devkit#getdependentpackagesforproject)
- [getImportPath](../../devkit/documents/nx_devkit#getimportpath)
- [getNpmPackageSharedConfig](../../devkit/documents/nx_devkit#getnpmpackagesharedconfig)
- [getOutputsForTargetAndConfiguration](../../devkit/documents/nx_devkit#getoutputsfortargetandconfiguration)
- [getPackageManagerCommand](../../devkit/documents/nx_devkit#getpackagemanagercommand)
Expand Down Expand Up @@ -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**(`tree`, `pkg`, `requiredVersion`, `options?`): `void`
Expand Down Expand Up @@ -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**(`pkgName`, `version`): [`SharedLibraryConfig`](../../devkit/documents/nx_devkit#sharedlibraryconfig) \| `undefined`
Expand Down Expand Up @@ -1471,12 +1432,12 @@ Example:

`Object`

| Name | Type |
| :-------------------- | :-------- |
| `appsDir` | `string` |
| `libsDir` | `string` |
| `npmScope` | `string` |
| `standaloneAsDefault` | `boolean` |
| Name | Type | Description |
| :-------------------- | :-------- | :----------------------------------------------------------------------- |
| `appsDir` | `string` | - |
| `libsDir` | `string` | - |
| `npmScope` | `string` | **`Deprecated`** This will be removed in Nx 17. Use getNpmScope instead. |
| `standaloneAsDefault` | `boolean` | - |

---

Expand Down
4 changes: 0 additions & 4 deletions docs/generated/packages/workspace/generators/new.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
"type": "boolean",
"default": true
},
"npmScope": {
"type": "string",
"description": "Npm scope for importing libs."
},
"standaloneApi": {
"description": "Use Standalone Components if generating an Angular application.",
"type": "boolean",
Expand Down
4 changes: 0 additions & 4 deletions docs/generated/packages/workspace/generators/preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
"description": "The name of the application.",
"type": "string"
},
"npmScope": {
"description": "Npm scope for importing libs.",
"type": "string"
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
Expand Down
1 change: 0 additions & 1 deletion e2e/angular-core/src/ng-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
affected: {
defaultBase: 'main',
},
npmScope: 'projscope',
tasksRunnerOptions: {
default: {
options: {
Expand Down
6 changes: 3 additions & 3 deletions e2e/nx-misc/src/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,9 @@ describe('Workspace Tests', () => {
});

it('should work for libraries when scope is unset', async () => {
const json = readJson('nx.json');
delete json.npmScope;
updateFile('nx.json', JSON.stringify(json));
const json = readJson('package.json');
json.name = proj;
updateFile('package.json', JSON.stringify(json));

const lib1 = uniq('mylib');
const lib2 = uniq('mylib');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
names,
Tree,
} from '@nx/devkit';

import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope';

import type { Schema } from '../schema';
import type { NormalizedSchema } from './normalized-schema';
import { E2eTestRunner, UnitTestRunner } from '../../../utils/test-runners';
Expand All @@ -28,11 +31,8 @@ export function normalizeOptions(
? 'e2e'
: `${names(options.name).fileName}-e2e`;

const {
appsDir: defaultAppsDir,
npmScope,
standaloneAsDefault,
} = getWorkspaceLayout(host);
const { appsDir: defaultAppsDir, standaloneAsDefault } =
getWorkspaceLayout(host);
const appsDir = layoutDirectory ?? defaultAppsDir;
const appProjectRoot = options.rootProject
? '.'
Expand All @@ -45,7 +45,11 @@ export function normalizeOptions(
? 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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "<% if(npmScope) { %><%= npmScope %>/<% } %><%= libFileName %>",
"name": "<%= importPath %>",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "<%= angularPeerDepVersion %>",
Expand Down
4 changes: 1 addition & 3 deletions packages/angular/src/generators/library/lib/create-files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Tree } from '@nx/devkit';
import {
generateFiles,
getWorkspaceLayout,
joinPathFragments,
names,
offsetFromRoot,
Expand All @@ -18,7 +17,6 @@ export function createFiles(
options: NormalizedSchema,
project: AngularProjectConfiguration
) {
const { npmScope } = getWorkspaceLayout(tree);
const rootOffset = offsetFromRoot(options.libraryOptions.projectRoot);
const libNames = names(options.libraryOptions.fileName);
const pathToComponent = options.componentOptions.flat
Expand All @@ -42,7 +40,7 @@ export function createFiles(
projectRoot: options.libraryOptions.projectRoot,
routing: options.libraryOptions.routing,
pathToComponent,
npmScope,
importPath: options.libraryOptions.importPath,
rootOffset,
angularPeerDepVersion: `^${major}.${minor}.0`,
tpl: '',
Expand Down
18 changes: 10 additions & 8 deletions packages/angular/src/generators/library/lib/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import {
names,
Tree,
} 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 { NormalizedSchema } from './normalized-schema';
import { Linter } from '@nx/linter';
import { UnitTestRunner } from '../../../utils/test-runners';
import { normalizeNewProjectPrefix } from '../../utils/project';

Expand Down Expand Up @@ -37,11 +41,9 @@ export function normalizeOptions(host: Tree, schema: Schema): NormalizedSchema {
? `${names(projectDirectory).fileName}/${name}`.replace(/\/+/g, '/')
: name;

const {
libsDir: defaultLibsDirectory,
npmScope,
standaloneAsDefault,
} = getWorkspaceLayout(host);
const { libsDir: defaultLibsDirectory, standaloneAsDefault } =
getWorkspaceLayout(host);
const npmScope = getNpmScope(host);
const libsDir = layoutDirectory ?? defaultLibsDirectory;

const projectName = fullProjectDirectory
Expand All @@ -60,7 +62,7 @@ export function normalizeOptions(host: Tree, schema: Schema): NormalizedSchema {
options.standaloneConfig = options.standaloneConfig ?? standaloneAsDefault;

const importPath =
options.importPath || getImportPath(npmScope, fullProjectDirectory);
options.importPath || getImportPath(host, fullProjectDirectory);

const ngCliSchematicLibRoot = projectName;
const allNormalizedOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ exports[`workspace move to nx layout should create nx.json 1`] = `
],
"sharedGlobals": [],
},
"npmScope": "my-org",
"targetDefaults": {
"build": {
"dependsOn": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
formatFilesTask,
getAllProjects,
getWorkspaceRootFileTypesInfo,
normalizeOptions,
updatePackageJson,
updateRootEsLintConfig,
updateRootTsConfig,
Expand All @@ -24,11 +23,10 @@ import {

export async function migrateFromAngularCli(
tree: Tree,
rawOptions: GeneratorOptions
options: GeneratorOptions
): Promise<GeneratorCallback> {
validateWorkspace(tree);
const projects = getAllProjects(tree);
const options = normalizeOptions(tree, rawOptions, projects);

const angularJson = readJson(tree, 'angular.json') as any;
ensureAngularDevKitPeerDependenciesAreInstalled(tree);
Expand Down
4 changes: 3 additions & 1 deletion packages/angular/src/generators/ng-add/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { E2eTestRunner, UnitTestRunner } from '../../utils/test-runners';
import type { Styles } from '../utils/types';

export interface GeneratorOptions {
skipInstall?: boolean;
/**
* @deprecated This should be inferred with {@link getNpmScope}
*/
npmScope?: string;
defaultBase?: string;

Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/generators/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"npmScope": {
"type": "string",
"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": {
"type": "string",
Expand Down
1 change: 0 additions & 1 deletion packages/angular/src/generators/ng-add/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './dependencies';
export * from './format-files-task';
export * from './logger';
export * from './normalize-options';
export * from './projects';
export * from './types';
export * from './validation-logging';
Expand Down
Loading

1 comment on commit 6b928bc

@vercel
Copy link

@vercel vercel bot commented on 6b928bc May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app

Please sign in to comment.