Skip to content

Commit

Permalink
fix(misc): updates the mv schematic to cope with libs created with --…
Browse files Browse the repository at this point in the history
…importPath

ISSUES CLOSED: #3476
  • Loading branch information
catfireparty authored and vsavkin committed Sep 14, 2020
1 parent a9ba084 commit 001b6ee
Show file tree
Hide file tree
Showing 21 changed files with 409 additions and 37 deletions.
14 changes: 14 additions & 0 deletions docs/angular/api-angular/schematics/move.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ Type: `string`

The folder to move the Angular project into

### importPath

Type: `string`

The new import path to use in the tsconfig.base.json

### projectName

Alias(es): project

Type: `string`

The name of the Angular project to move

### updateImportPath

Default: `true`

Type: `boolean`

Should the schematic update the import path to reflect the new location?
14 changes: 14 additions & 0 deletions docs/angular/api-workspace/schematics/move.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ Type: `string`

The folder to move the project into

### importPath

Type: `string`

The new import path to use in the tsconfig.base.json

### projectName

Alias(es): project

Type: `string`

The name of the project to move

### updateImportPath

Default: `true`

Type: `boolean`

Should the schematic update the import path to reflect the new location?
14 changes: 14 additions & 0 deletions docs/node/api-angular/schematics/move.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ Type: `string`

The folder to move the Angular project into

### importPath

Type: `string`

The new import path to use in the tsconfig.base.json

### projectName

Alias(es): project

Type: `string`

The name of the Angular project to move

### updateImportPath

Default: `true`

Type: `boolean`

Should the schematic update the import path to reflect the new location?
14 changes: 14 additions & 0 deletions docs/node/api-workspace/schematics/move.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ Type: `string`

The folder to move the project into

### importPath

Type: `string`

The new import path to use in the tsconfig.base.json

### projectName

Alias(es): project

Type: `string`

The name of the project to move

### updateImportPath

Default: `true`

Type: `boolean`

Should the schematic update the import path to reflect the new location?
14 changes: 14 additions & 0 deletions docs/react/api-angular/schematics/move.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ Type: `string`

The folder to move the Angular project into

### importPath

Type: `string`

The new import path to use in the tsconfig.base.json

### projectName

Alias(es): project

Type: `string`

The name of the Angular project to move

### updateImportPath

Default: `true`

Type: `boolean`

Should the schematic update the import path to reflect the new location?
14 changes: 14 additions & 0 deletions docs/react/api-workspace/schematics/move.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ Type: `string`

The folder to move the project into

### importPath

Type: `string`

The new import path to use in the tsconfig.base.json

### projectName

Alias(es): project

Type: `string`

The name of the project to move

### updateImportPath

Default: `true`

Type: `boolean`

Should the schematic update the import path to reflect the new location?
147 changes: 143 additions & 4 deletions e2e/workspace/src/workspace-aux-commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { NxJson } from '@nrwl/workspace';
import { classify } from '@nrwl/workspace/src/utils/strings';
import {
checkFilesExist,
ensureProject,
Expand All @@ -16,6 +14,8 @@ import {
updateFile,
workspaceConfigName,
} from '@nrwl/e2e/utils';
import { NxJson } from '@nrwl/workspace';
import { classify } from '@nrwl/workspace/src/utils/strings';

forEachCli((cli) => {
describe('lint', () => {
Expand Down Expand Up @@ -718,7 +718,146 @@ forEachCli((cli) => {
`libs/${lib2}/ui/src/lib/${lib2}-ui.ts`,
`import { fromLibOne } from '@proj/${lib1}/data-access';
export const fromLibTwo = () => fromLibOne(); }`
export const fromLibTwo = () => fromLibOne();`
);

/**
* Create a library which has an implicit dependency on lib1
*/

runCLI(`generate @nrwl/workspace:lib ${lib3}`);
let nxJson = JSON.parse(readFile('nx.json')) as NxJson;
nxJson.projects[lib3].implicitDependencies = [`${lib1}-data-access`];
updateFile(`nx.json`, JSON.stringify(nxJson));

/**
* Now try to move lib1
*/

const moveOutput = runCLI(
`generate @nrwl/workspace:move --project ${lib1}-data-access shared/${lib1}/data-access`
);

expect(moveOutput).toContain(`DELETE libs/${lib1}/data-access`);
expect(exists(`libs/${lib1}/data-access`)).toBeFalsy();

const newPath = `libs/shared/${lib1}/data-access`;
const newName = `shared-${lib1}-data-access`;

const readmePath = `${newPath}/README.md`;
expect(moveOutput).toContain(`CREATE ${readmePath}`);
checkFilesExist(readmePath);

const jestConfigPath = `${newPath}/jest.config.js`;
expect(moveOutput).toContain(`CREATE ${jestConfigPath}`);
checkFilesExist(jestConfigPath);
const jestConfig = readFile(jestConfigPath);
expect(jestConfig).toContain(`name: 'shared-${lib1}-data-access'`);
expect(jestConfig).toContain(`preset: '../../../../jest.config.js'`);
expect(jestConfig).toContain(
`coverageDirectory: '../../../../coverage/${newPath}'`
);

const tsConfigPath = `${newPath}/tsconfig.json`;
expect(moveOutput).toContain(`CREATE ${tsConfigPath}`);
checkFilesExist(tsConfigPath);

const tsConfigLibPath = `${newPath}/tsconfig.lib.json`;
expect(moveOutput).toContain(`CREATE ${tsConfigLibPath}`);
checkFilesExist(tsConfigLibPath);
const tsConfigLib = readJson(tsConfigLibPath);
expect(tsConfigLib.compilerOptions.outDir).toEqual(
'../../../../dist/out-tsc'
);

const tsConfigSpecPath = `${newPath}/tsconfig.spec.json`;
expect(moveOutput).toContain(`CREATE ${tsConfigSpecPath}`);
checkFilesExist(tsConfigSpecPath);
const tsConfigSpec = readJson(tsConfigSpecPath);
expect(tsConfigSpec.compilerOptions.outDir).toEqual(
'../../../../dist/out-tsc'
);

const indexPath = `${newPath}/src/index.ts`;
expect(moveOutput).toContain(`CREATE ${indexPath}`);
checkFilesExist(indexPath);

const rootClassPath = `${newPath}/src/lib/${lib1}-data-access.ts`;
expect(moveOutput).toContain(`CREATE ${rootClassPath}`);
checkFilesExist(rootClassPath);

expect(moveOutput).toContain('UPDATE nx.json');
nxJson = JSON.parse(readFile('nx.json')) as NxJson;
expect(nxJson.projects[`${lib1}-data-access`]).toBeUndefined();
expect(nxJson.projects[newName]).toEqual({
tags: [],
});
expect(nxJson.projects[lib3].implicitDependencies).toEqual([
`shared-${lib1}-data-access`,
]);

expect(moveOutput).toContain('UPDATE tsconfig.base.json');
const rootTsConfig = readJson('tsconfig.base.json');
expect(
rootTsConfig.compilerOptions.paths[`@proj/${lib1}/data-access`]
).toBeUndefined();
expect(
rootTsConfig.compilerOptions.paths[`@proj/shared/${lib1}/data-access`]
).toEqual([`libs/shared/${lib1}/data-access/src/index.ts`]);

expect(moveOutput).toContain(`UPDATE ${workspace}.json`);
const workspaceJson = readJson(`${workspace}.json`);
expect(workspaceJson.projects[`${lib1}-data-access`]).toBeUndefined();
const project = workspaceJson.projects[newName];
expect(project).toBeTruthy();
expect(project.root).toBe(newPath);
expect(project.sourceRoot).toBe(`${newPath}/src`);
expect(project.architect.lint.options.tsConfig).toEqual([
`libs/shared/${lib1}/data-access/tsconfig.lib.json`,
`libs/shared/${lib1}/data-access/tsconfig.spec.json`,
]);

/**
* Check that the import in lib2 has been updated
*/
const lib2FilePath = `libs/${lib2}/ui/src/lib/${lib2}-ui.ts`;
const lib2File = readFile(lib2FilePath);
expect(lib2File).toContain(
`import { fromLibOne } from '@proj/shared/${lib1}/data-access';`
);
});

it('should work for libs created with --importPath', () => {
const importPath = '@wibble/fish';
const lib1 = uniq('mylib');
const lib2 = uniq('mylib');
const lib3 = uniq('mylib');
newProject();
runCLI(
`generate @nrwl/workspace:lib ${lib1}/data-access --importPath=${importPath}`
);

updateFile(
`libs/${lib1}/data-access/src/lib/${lib1}-data-access.ts`,
`export function fromLibOne() { console.log('This is completely pointless'); }`
);

updateFile(
`libs/${lib1}/data-access/src/index.ts`,
`export * from './lib/${lib1}-data-access.ts'`
);

/**
* Create a library which imports a class from lib1
*/

runCLI(`generate @nrwl/workspace:lib ${lib2}/ui`);

updateFile(
`libs/${lib2}/ui/src/lib/${lib2}-ui.ts`,
`import { fromLibOne } from '${importPath}';
export const fromLibTwo = () => fromLibOne();`
);

/**
Expand Down Expand Up @@ -859,7 +998,7 @@ forEachCli((cli) => {
`packages/${lib2}/ui/src/lib/${lib2}-ui.ts`,
`import { fromLibOne } from '@proj/${lib1}/data-access';
export const fromLibTwo = () => fromLibOne(); }`
export const fromLibTwo = () => fromLibOne();`
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('updateModuleName Rule', () => {
const schema: Schema = {
projectName: 'my-source',
destination: 'my-destination',
updateImportPath: true,
};

const modulePath = '/libs/my-destination/src/lib/my-destination.module.ts';
Expand Down
2 changes: 2 additions & 0 deletions packages/angular/src/schematics/move/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface Schema {
projectName: string;
destination: string;
importPath?: string;
updateImportPath: boolean;
}
9 changes: 9 additions & 0 deletions packages/angular/src/schematics/move/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
"$source": "argv",
"index": 0
}
},
"importPath": {
"type": "string",
"description": "The new import path to use in the tsconfig.base.json"
},
"updateImportPath": {
"type": "boolean",
"description": "Should the schematic update the import path to reflect the new location?",
"default": true
}
},
"required": ["projectName", "destination"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('checkDestination Rule', () => {
const schema: Schema = {
projectName: 'my-lib',
destination: '../apps/not-an-app',
importPath: undefined,
updateImportPath: true,
};

await expect(callRule(checkDestination(schema), tree)).rejects.toThrow(
Expand All @@ -29,6 +31,8 @@ describe('checkDestination Rule', () => {
const schema: Schema = {
projectName: 'my-lib',
destination: 'my-other-lib',
importPath: undefined,
updateImportPath: true,
};

await expect(callRule(checkDestination(schema), tree)).rejects.toThrow(
Expand All @@ -40,6 +44,8 @@ describe('checkDestination Rule', () => {
const schema: Schema = {
projectName: 'my-lib',
destination: 'my-other-lib',
importPath: undefined,
updateImportPath: true,
};

await expect(
Expand All @@ -51,6 +57,8 @@ describe('checkDestination Rule', () => {
const schema: Schema = {
projectName: 'my-lib',
destination: '/my-other-lib//wibble',
importPath: undefined,
updateImportPath: true,
};

await callRule(checkDestination(schema), tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('moveProject Rule', () => {
const schema: Schema = {
projectName: 'my-lib',
destination: 'my-destination',
importPath: undefined,
updateImportPath: true,
};

// TODO - Currently this test will fail due to
Expand Down
Loading

0 comments on commit 001b6ee

Please sign in to comment.