Skip to content

Commit

Permalink
feat(core): add option importPath to workspace remove library (#9486)
Browse files Browse the repository at this point in the history
Co-authored-by: Sven Wuendrich <sven.wuendrich@ottogroup.com>
  • Loading branch information
swuendri and swuendri committed Mar 28, 2022
1 parent 8498fe9 commit 0ad3eb3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/generated/packages/workspace.json
Expand Up @@ -276,6 +276,10 @@
"aliases": ["skip-format"],
"description": "Skip formatting files.",
"default": false
},
"importPath": {
"type": "string",
"description": "The library name used at creation time"
}
},
"required": ["projectName"],
Expand Down
Expand Up @@ -7,6 +7,7 @@ import { libraryGenerator } from '../../library/library';
describe('updateTsconfig', () => {
let tree: Tree;
let schema: Schema;
let schemaWithImportPath: Schema;

beforeEach(async () => {
tree = createTreeWithEmptyWorkspace();
Expand All @@ -16,6 +17,13 @@ describe('updateTsconfig', () => {
skipFormat: false,
forceRemove: false,
};

schemaWithImportPath = {
projectName: 'my-lib',
skipFormat: false,
forceRemove: false,
importPath: '@proj/whatever-name',
};
});

it('should delete project ref from the root tsconfig.base.json', async () => {
Expand All @@ -31,6 +39,20 @@ describe('updateTsconfig', () => {
expect(tsConfig.compilerOptions.paths).toEqual({});
});

it('should delete project ref with importPath from the root tsconfig.base.json', async () => {
await libraryGenerator(tree, {
name: 'my-lib',
standaloneConfig: false,
importPath: '@proj/whatever-name',
});
const project = readProjectConfiguration(tree, 'my-lib');

updateTsconfig(tree, schemaWithImportPath, project);

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

it('should delete project ref from the root tsconfig.json when no tsconfig.base.json', async () => {
tree.rename('tsconfig.base.json', 'tsconfig.json');
await libraryGenerator(tree, {
Expand All @@ -44,4 +66,19 @@ describe('updateTsconfig', () => {
const tsConfig = readJson(tree, '/tsconfig.json');
expect(tsConfig.compilerOptions.paths).toEqual({});
});

it('should delete project ref with importPath from the root tsconfig.json when no tsconfig.base.json', async () => {
tree.rename('tsconfig.base.json', 'tsconfig.json');
await libraryGenerator(tree, {
name: 'my-lib',
standaloneConfig: false,
importPath: '@proj/whatever-name',
});
const project = readProjectConfiguration(tree, 'my-lib');

updateTsconfig(tree, schemaWithImportPath, project);

const tsConfig = readJson(tree, '/tsconfig.json');
expect(tsConfig.compilerOptions.paths).toEqual({});
});
});
15 changes: 7 additions & 8 deletions packages/workspace/src/generators/remove/lib/update-tsconfig.ts
Expand Up @@ -20,16 +20,15 @@ export function updateTsconfig(
const { appsDir, libsDir, npmScope } = getWorkspaceLayout(tree);

const tsConfigPath = getRootTsConfigPathInTree(tree);
const defaultImportPath = `@${npmScope}/${project.root.substr(
project.projectType === 'application'
? appsDir.length + 1
: libsDir.length + 1
)}`;
const importPath = schema.importPath || defaultImportPath;
if (tree.exists(tsConfigPath)) {
updateJson(tree, tsConfigPath, (json) => {
delete json.compilerOptions.paths[
`@${npmScope}/${project.root.substr(
project.projectType === 'application'
? appsDir.length + 1
: libsDir.length + 1
)}`
];

delete json.compilerOptions.paths[importPath];
return json;
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/generators/remove/schema.d.ts
Expand Up @@ -6,4 +6,5 @@ export interface Schema extends Json {
projectName: string;
skipFormat: boolean;
forceRemove: boolean;
importPath?: string;
}
4 changes: 4 additions & 0 deletions packages/workspace/src/generators/remove/schema.json
Expand Up @@ -36,6 +36,10 @@
"aliases": ["skip-format"],
"description": "Skip formatting files.",
"default": false
},
"importPath": {
"type": "string",
"description": "The library name used at creation time"
}
},
"required": ["projectName"]
Expand Down

1 comment on commit 0ad3eb3

@vercel
Copy link

@vercel vercel bot commented on 0ad3eb3 Mar 28, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.