Skip to content

Commit

Permalink
fix(misc): fix move and remove to be not reliant on workspace layout (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Dec 5, 2022
1 parent b71b28e commit 9453a75
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 45 deletions.
73 changes: 63 additions & 10 deletions packages/workspace/src/generators/move/lib/update-imports.spec.ts
@@ -1,23 +1,21 @@
import { readJson, readProjectConfiguration, Tree } from '@nrwl/devkit';
import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import { libraryGenerator } from '../../library/library';
import { NormalizedSchema } from '../schema';
import { Schema } from '../schema';
import { updateImports } from './update-imports';
import { normalizeSchema } from './normalize-schema';

describe('updateImports', () => {
let tree: Tree;
let schema: NormalizedSchema;
let schema: Schema;

beforeEach(async () => {
tree = createTreeWithEmptyV1Workspace();

schema = {
projectName: 'my-source',
destination: 'my-destination',
importPath: '@proj/my-destination',
updateImportPath: true,
newProjectName: 'my-destination',
relativeToRootDestination: 'libs/my-destination',
};
});

Expand Down Expand Up @@ -48,7 +46,11 @@ describe('updateImports', () => {
);
const projectConfig = readProjectConfiguration(tree, 'my-source');

updateImports(tree, schema, projectConfig);
updateImports(
tree,
normalizeSchema(tree, schema, projectConfig),
projectConfig
);

expect(tree.read(importerFilePath, 'utf-8')).toMatchSnapshot();
});
Expand Down Expand Up @@ -261,7 +263,18 @@ export MyExtendedClass extends MyClass {};`
schema.updateImportPath = false;
const projectConfig = readProjectConfiguration(tree, 'my-source');

updateImports(tree, { ...schema, updateImportPath: false }, projectConfig);
updateImports(
tree,
normalizeSchema(
tree,
{
...schema,
updateImportPath: false,
},
projectConfig
),
projectConfig
);

expect(tree.read(importerFilePath).toString()).toContain(
`import { MyClass } from '@proj/my-source';`
Expand All @@ -275,14 +288,38 @@ export MyExtendedClass extends MyClass {};`
});
const projectConfig = readProjectConfiguration(tree, 'my-source');

updateImports(tree, schema, projectConfig);
updateImports(
tree,
normalizeSchema(tree, schema, projectConfig),
projectConfig
);

const tsConfig = readJson(tree, '/tsconfig.base.json');
expect(tsConfig.compilerOptions.paths).toEqual({
'@proj/my-destination': ['libs/my-destination/src/index.ts'],
});
});

it('should update project ref of a project not under libs in the root tsconfig.base.json', async () => {
tree.delete('libs');
await libraryGenerator(tree, {
name: 'my-source',
standaloneConfig: false,
});
const projectConfig = readProjectConfiguration(tree, 'my-source');

updateImports(
tree,
normalizeSchema(tree, schema, projectConfig),
projectConfig
);

const tsConfig = readJson(tree, '/tsconfig.base.json');
expect(tsConfig.compilerOptions.paths).toEqual({
'@proj/my-destination': ['my-destination/src/index.ts'],
});
});

it('should update project ref in the root tsconfig.json when no tsconfig.base.json', async () => {
tree.rename('tsconfig.base.json', 'tsconfig.json');
await libraryGenerator(tree, {
Expand All @@ -291,7 +328,11 @@ export MyExtendedClass extends MyClass {};`
});
const projectConfig = readProjectConfiguration(tree, 'my-source');

updateImports(tree, schema, projectConfig);
updateImports(
tree,
normalizeSchema(tree, schema, projectConfig),
projectConfig
);

const tsConfig = readJson(tree, '/tsconfig.json');
expect(tsConfig.compilerOptions.paths).toEqual({
Expand All @@ -306,7 +347,19 @@ export MyExtendedClass extends MyClass {};`
});
const projectConfig = readProjectConfiguration(tree, 'my-source');

updateImports(tree, { ...schema, updateImportPath: false }, projectConfig);
updateImports(
tree,
normalizeSchema(
tree,
{
...schema,
updateImportPath: false,
},
projectConfig
),

projectConfig
);

const tsConfig = readJson(tree, '/tsconfig.base.json');
expect(tsConfig.compilerOptions.paths).toEqual({
Expand Down
8 changes: 5 additions & 3 deletions packages/workspace/src/generators/move/lib/update-imports.ts
Expand Up @@ -3,6 +3,7 @@ import {
ChangeType,
getProjects,
getWorkspaceLayout,
joinPathFragments,
ProjectConfiguration,
StringChange,
Tree,
Expand All @@ -15,6 +16,7 @@ import { getRootTsConfigPathInTree } from '../../../utilities/typescript';
import { findNodes } from 'nx/src/utils/typescript';
import { NormalizedSchema } from '../schema';
import { normalizeSlashes } from './utils';
import { relative } from 'path';

/**
* Updates all the imports in the workspace and modifies the tsconfig appropriately.
Expand Down Expand Up @@ -81,8 +83,8 @@ export function updateImports(
}

const projectRoot = {
from: project.root.slice(libsDir.length).replace(/^\/|\\/, ''),
to: schema.destination,
from: project.root,
to: schema.relativeToRootDestination,
};

if (tsConfig) {
Expand All @@ -96,7 +98,7 @@ export function updateImports(
);
}
const updatedPath = path.map((x) =>
x.replace(new RegExp(projectRoot.from, 'g'), projectRoot.to)
joinPathFragments(projectRoot.to, relative(projectRoot.from, x))
);

if (schema.updateImportPath) {
Expand Down
149 changes: 138 additions & 11 deletions packages/workspace/src/generators/remove/lib/update-tsconfig.spec.ts
@@ -1,16 +1,28 @@
import { readJson, readProjectConfiguration, Tree } from '@nrwl/devkit';
import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import {
ProjectGraph,
readJson,
readProjectConfiguration,
Tree,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Schema } from '../schema';
import { updateTsconfig } from './update-tsconfig';
import { libraryGenerator } from '../../library/library';

let graph: ProjectGraph;
jest.mock('@nrwl/devkit', () => {
return {
...jest.requireActual('@nrwl/devkit'),
createProjectGraphAsync: jest.fn().mockImplementation(() => graph),
};
});
describe('updateTsconfig', () => {
let tree: Tree;
let schema: Schema;
let schemaWithImportPath: Schema;

beforeEach(async () => {
tree = createTreeWithEmptyV1Workspace();
tree = createTreeWithEmptyWorkspace();

schema = {
projectName: 'my-lib',
Expand All @@ -31,9 +43,47 @@ describe('updateTsconfig', () => {
name: 'my-lib',
standaloneConfig: false,
});
const project = readProjectConfiguration(tree, 'my-lib');

updateTsconfig(tree, schema, project);
graph = {
nodes: {
'my-lib': {
name: 'my-lib',
type: 'lib',
data: {
root: readProjectConfiguration(tree, 'my-lib').root,
},
},
},
dependencies: {},
};

await updateTsconfig(tree, schema);

const tsConfig = readJson(tree, '/tsconfig.base.json');
expect(tsConfig.compilerOptions.paths).toEqual({});
});

it('should delete project ref not under libs from the root tsconfig.base.json', async () => {
tree.delete('libs');
await libraryGenerator(tree, {
name: 'my-lib',
standaloneConfig: false,
});

graph = {
nodes: {
'my-lib': {
name: 'my-lib',
type: 'lib',
data: {
root: readProjectConfiguration(tree, 'my-lib').root,
},
},
},
dependencies: {},
};

await updateTsconfig(tree, schema);

const tsConfig = readJson(tree, '/tsconfig.base.json');
expect(tsConfig.compilerOptions.paths).toEqual({});
Expand All @@ -45,9 +95,21 @@ describe('updateTsconfig', () => {
standaloneConfig: false,
importPath: '@proj/whatever-name',
});
const project = readProjectConfiguration(tree, 'my-lib');

updateTsconfig(tree, schemaWithImportPath, project);
graph = {
nodes: {
'my-lib': {
name: 'my-lib',
type: 'lib',
data: {
root: readProjectConfiguration(tree, 'my-lib').root,
},
},
},
dependencies: {},
};

await updateTsconfig(tree, schemaWithImportPath);

const tsConfig = readJson(tree, '/tsconfig.base.json');
expect(tsConfig.compilerOptions.paths).toEqual({});
Expand All @@ -59,9 +121,21 @@ describe('updateTsconfig', () => {
name: 'my-lib',
standaloneConfig: false,
});
const project = readProjectConfiguration(tree, 'my-lib');

updateTsconfig(tree, schema, project);
graph = {
nodes: {
'my-lib': {
name: 'my-lib',
type: 'lib',
data: {
root: readProjectConfiguration(tree, 'my-lib').root,
},
},
},
dependencies: {},
};

await updateTsconfig(tree, schema);

const tsConfig = readJson(tree, '/tsconfig.json');
expect(tsConfig.compilerOptions.paths).toEqual({});
Expand All @@ -74,11 +148,64 @@ describe('updateTsconfig', () => {
standaloneConfig: false,
importPath: '@proj/whatever-name',
});
const project = readProjectConfiguration(tree, 'my-lib');

updateTsconfig(tree, schemaWithImportPath, project);
graph = {
nodes: {
'my-lib': {
name: 'my-lib',
type: 'lib',
data: {
root: readProjectConfiguration(tree, 'my-lib').root,
},
},
},
dependencies: {},
};

await updateTsconfig(tree, schemaWithImportPath);

const tsConfig = readJson(tree, '/tsconfig.json');
expect(tsConfig.compilerOptions.paths).toEqual({});
});

it('should not delete importPaths of nested projects from tsconfig.base.json', async () => {
await libraryGenerator(tree, {
name: 'my-lib',
standaloneConfig: false,
importPath: '@proj/whatever-name',
});
await libraryGenerator(tree, {
name: 'nested-lib',
directory: 'libs/my-lib',
standaloneConfig: false,
importPath: '@proj/nested/whatever-name',
});

graph = {
nodes: {
'my-lib': {
name: 'my-lib',
type: 'lib',
data: {
root: readProjectConfiguration(tree, 'my-lib').root,
},
},
'my-lib-nested-lib': {
name: 'my-lib-nested-lib',
type: 'lib',
data: {
root: readProjectConfiguration(tree, 'my-lib-nested-lib').root,
},
},
},
dependencies: {},
};

await updateTsconfig(tree, schemaWithImportPath);

const tsConfig = readJson(tree, '/tsconfig.base.json');
expect(tsConfig.compilerOptions.paths).toEqual({
'@proj/nested/whatever-name': ['libs/my-lib/nested-lib/src/index.ts'],
});
});
});

1 comment on commit 9453a75

@vercel
Copy link

@vercel vercel bot commented on 9453a75 Dec 5, 2022

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-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.