Skip to content

Commit

Permalink
fix(core): update project package.json on move
Browse files Browse the repository at this point in the history
  • Loading branch information
puku0x authored and vsavkin committed May 27, 2021
1 parent d4a79db commit 9c67c69
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
ProjectConfiguration,
readProjectConfiguration,
Tree,
readJson,
writeJson,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Schema } from '../schema';
import { updatePackageJson } from './update-package-json';
import { libraryGenerator } from '../../library/library';

describe('updatePackageJson', () => {
let tree: Tree;
let schema: Schema;
let projectConfig: ProjectConfiguration;

beforeEach(async () => {
schema = {
projectName: 'my-lib',
destination: 'my-destination',
importPath: undefined,
updateImportPath: true,
};

tree = createTreeWithEmptyWorkspace();
await libraryGenerator(tree, { name: 'my-lib' });
projectConfig = readProjectConfiguration(tree, 'my-lib');
});

it('should handle package.json not existing', async () => {
expect(() => {
updatePackageJson(tree, schema, projectConfig);
}).not.toThrow();
});

it('should update the name', async () => {
const packageJson = {
name: '@proj/my-lib',
};

writeJson(tree, '/libs/my-destination/package.json', packageJson);

updatePackageJson(tree, schema, projectConfig);

expect(readJson(tree, '/libs/my-destination/package.json')).toEqual({
...packageJson,
name: '@proj/my-destination',
});
});
});
35 changes: 35 additions & 0 deletions packages/workspace/src/generators/move/lib/update-package-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Tree, getWorkspaceLayout } from '@nrwl/devkit';
import * as path from 'path';
import { Schema } from '../schema';
import { getDestination, normalizeSlashes } from './utils';
import { ProjectConfiguration } from '@nrwl/tao/src/shared/workspace';

interface PartialPackageJson {
name: string;
}

/**
* Updates the name in the package.json if it exists.
*
* @param schema The options provided to the schematic
*/
export function updatePackageJson(
tree: Tree,
schema: Schema,
project: ProjectConfiguration
) {
const destination = getDestination(tree, schema, project);
const packageJsonPath = path.join(destination, 'package.json');

if (!tree.exists(packageJsonPath)) {
// nothing to do
return;
}

const { npmScope } = getWorkspaceLayout(tree);
const packageJson = JSON.parse(
tree.read(packageJsonPath).toString('utf-8')
) as PartialPackageJson;
packageJson.name = normalizeSlashes(`@${npmScope}/${schema.destination}`);
tree.write(packageJsonPath, JSON.stringify(packageJson));
}
2 changes: 2 additions & 0 deletions packages/workspace/src/generators/move/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { updateEslintrcJson } from './lib/update-eslintrc-json';
import { moveProjectConfiguration } from './lib/move-project-configuration';
import { updateBuildTargets } from './lib/update-build-targets';
import { updateReadme } from './lib/update-readme';
import { updatePackageJson } from './lib/update-package-json';

export async function moveGenerator(tree: Tree, schema: Schema) {
const projectConfig = readProjectConfiguration(tree, schema.projectName);
Expand All @@ -31,6 +32,7 @@ export async function moveGenerator(tree: Tree, schema: Schema) {
updateStorybookConfig(tree, schema, projectConfig);
updateEslintrcJson(tree, schema, projectConfig);
updateReadme(tree, schema, projectConfig);
updatePackageJson(tree, schema, projectConfig);
moveProjectConfiguration(tree, schema, projectConfig);
updateBuildTargets(tree, schema);
updateDefaultProject(tree, schema);
Expand Down

0 comments on commit 9c67c69

Please sign in to comment.