Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): revert change to use new lockfile utils #13962

Merged
merged 1 commit into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/js/src/utils/package-json/update-package-json.spec.ts
@@ -1,8 +1,8 @@
import { updatePackageJsonContent } from './update-package-json';
import { getUpdatedPackageJsonContent } from './update-package-json';

describe('getUpdatedPackageJsonContent', () => {
it('should update fields for commonjs only (default)', () => {
const json = updatePackageJsonContent(
const json = getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand All @@ -23,7 +23,7 @@ describe('getUpdatedPackageJsonContent', () => {
});

it('should update fields for esm only', () => {
const json = updatePackageJsonContent(
const json = getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand All @@ -47,7 +47,7 @@ describe('getUpdatedPackageJsonContent', () => {
});

it('should update fields for commonjs + esm', () => {
const json = updatePackageJsonContent(
const json = getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand All @@ -70,7 +70,7 @@ describe('getUpdatedPackageJsonContent', () => {
});

it('should support skipping types', () => {
const json = updatePackageJsonContent(
const json = getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand All @@ -91,7 +91,7 @@ describe('getUpdatedPackageJsonContent', () => {
});

it('should support generated exports field', () => {
const json = updatePackageJsonContent(
const json = getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('getUpdatedPackageJsonContent', () => {
});

it('should support different CJS file extension', () => {
const json = updatePackageJsonContent(
const json = getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('getUpdatedPackageJsonContent', () => {
});

it('should not set types when { skipTypings: true }', () => {
const json = updatePackageJsonContent(
const json = getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand All @@ -170,7 +170,7 @@ describe('getUpdatedPackageJsonContent', () => {
it('should support different exports field shape', () => {
// exports: string
expect(
updatePackageJsonContent(
getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand All @@ -196,7 +196,7 @@ describe('getUpdatedPackageJsonContent', () => {

// exports: { '.': string }
expect(
updatePackageJsonContent(
getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('getUpdatedPackageJsonContent', () => {

// exports: { './custom': string }
expect(
updatePackageJsonContent(
getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
Expand Down
116 changes: 32 additions & 84 deletions packages/js/src/utils/package-json/update-package-json.ts
@@ -1,21 +1,17 @@
import {
createLockFile,
createPackageJson,
ExecutorContext,
getOutputsForTargetAndConfiguration,
joinPathFragments,
normalizePath,
ProjectGraphProjectNode,
readJsonFile,
workspaceRoot,
writeJsonFile,
} from '@nrwl/devkit';
import { DependentBuildableProjectNode } from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import {
DependentBuildableProjectNode,
updateBuildableProjectPackageJsonDependencies,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { basename, dirname, join, parse, relative } from 'path';
import { fileExists } from 'nx/src/utils/fileutils';
import type { PackageJson } from 'nx/src/utils/package-json';
import { getLockFileName } from 'nx/src/lock-file/lock-file';
import { writeFileSync } from 'fs-extra';
import { isNpmProject } from 'nx/src/project-graph/operators';

function getMainFileDirRelativeToProjectRoot(
main: string,
Expand Down Expand Up @@ -48,93 +44,45 @@ export function updatePackageJson(
target: ProjectGraphProjectNode,
dependencies: DependentBuildableProjectNode[]
): void {
const packageJson = createPackageJson(
context.projectName,
context.projectGraph,
{
root: context.root,
// By default we remove devDependencies since this is a production build.
isProduction: true,
}
const pathToPackageJson = join(
context.root,
options.projectRoot,
'package.json'
);
// make custom modifications to package.json
updatePackageJsonContent(packageJson, options);

const packageJson = fileExists(pathToPackageJson)
? readJsonFile(pathToPackageJson)
: { name: context.projectName };

if (options.excludeLibsInPackageJson) {
dependencies = dependencies.filter((dep) => dep.node.type !== 'lib');
}
if (options.updateBuildableProjectDepsInPackageJson) {
addMissingDependencies(
packageJson,
context,

writeJsonFile(
`${options.outputPath}/package.json`,
getUpdatedPackageJsonContent(packageJson, options)
);

if (
dependencies.length > 0 &&
options.updateBuildableProjectDepsInPackageJson
) {
updateBuildableProjectPackageJsonDependencies(
context.root,
context.projectName,
context.targetName,
context.configurationName,
target,
dependencies,
options.buildableProjectDepsInPackageJsonType
);
}

writeJsonFile(`${options.outputPath}/package.json`, packageJson);
const lockFile = createLockFile(packageJson);
writeFileSync(`${options.outputPath}/${getLockFileName()}`, lockFile, {
encoding: 'utf-8',
});
}

function addMissingDependencies(
packageJson: PackageJson,
{ projectName, targetName, configurationName, root }: ExecutorContext,
dependencies: DependentBuildableProjectNode[],
propType: 'dependencies' | 'peerDependencies' = 'dependencies'
) {
const workspacePackageJson = readJsonFile(
joinPathFragments(workspaceRoot, 'package.json')
);
dependencies.forEach((entry) => {
if (isNpmProject(entry.node)) {
const { packageName, version } = entry.node.data;
if (
packageJson.dependencies?.[packageName] ||
packageJson.devDependencies?.[packageName] ||
packageJson.peerDependencies?.[packageName]
) {
return;
}
if (workspacePackageJson.devDependencies?.[packageName]) {
return;
}

packageJson[propType] ??= {};
packageJson[propType][packageName] = version;
} else {
const packageName = entry.name;
if (
!packageJson.dependencies?.[packageName] &&
!packageJson.peerDependencies?.[packageName]
) {
const outputs = getOutputsForTargetAndConfiguration(
{
overrides: {},
target: {
project: projectName,
target: targetName,
configuration: configurationName,
},
},
entry.node
);

const depPackageJsonPath = join(root, outputs[0], 'package.json');
const version = readJsonFile(depPackageJsonPath).version;

packageJson[propType] ??= {};
packageJson[propType][packageName] = version;
}
}
});
}

export function updatePackageJsonContent(
export function getUpdatedPackageJsonContent(
packageJson: PackageJson,
options: UpdatePackageJsonOption
): PackageJson {
) {
// Default is CJS unless esm is explicitly passed.
const hasCjsFormat = !options.format || options.format?.includes('cjs');
const hasEsmFormat = options.format?.includes('esm');
Expand Down