forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): update project package.json on move
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
packages/workspace/src/generators/move/lib/update-package-json.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
packages/workspace/src/generators/move/lib/update-package-json.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters