Skip to content

Commit

Permalink
fix(js): update dependencies in package.json (#9073)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Gerbes <jason.gerbes@vista.co>
  • Loading branch information
jasongerbes and jasongerbes-vista committed Feb 24, 2022
1 parent 68e9435 commit 4287d59
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 43 deletions.
18 changes: 18 additions & 0 deletions docs/generated/api-js/executors/swc.md
Expand Up @@ -35,6 +35,16 @@ Type: `array`

List of static assets.

### buildableProjectDepsInPackageJsonType

Default: `peerDependencies`

Type: `string`

Possible values: `dependencies`, `peerDependencies`

When `updateBuildableProjectDepsInPackageJson` is `true`, this adds dependencies to either `peerDependencies` or `dependencies`.

### skipTypeCheck

Default: `false`
Expand All @@ -49,6 +59,14 @@ Type: `array`

List of SWC Glob/Regex to be excluded from compilation (https://swc.rs/docs/configuration/compilation#exclude)

### updateBuildableProjectDepsInPackageJson

Default: `true`

Type: `boolean`

Whether to update the buildable project dependencies in package.json.

### watch

Default: `false`
Expand Down
18 changes: 18 additions & 0 deletions docs/generated/api-js/executors/tsc.md
Expand Up @@ -35,12 +35,30 @@ Type: `array`

List of static assets.

### buildableProjectDepsInPackageJsonType

Default: `peerDependencies`

Type: `string`

Possible values: `dependencies`, `peerDependencies`

When `updateBuildableProjectDepsInPackageJson` is `true`, this adds dependencies to either `peerDependencies` or `dependencies`.

### transformers

Type: `array`

List of TypeScript Transformer Plugins.

### updateBuildableProjectDepsInPackageJson

Default: `true`

Type: `boolean`

Whether to update the buildable project dependencies in package.json.

### watch

Default: `false`
Expand Down
11 changes: 11 additions & 0 deletions packages/js/src/executors/swc/schema.json
Expand Up @@ -45,6 +45,17 @@
"./**/jest-setup.ts$",
"./**/.*.js$"
]
},
"updateBuildableProjectDepsInPackageJson": {
"type": "boolean",
"description": "Whether to update the buildable project dependencies in package.json.",
"default": true
},
"buildableProjectDepsInPackageJsonType": {
"type": "string",
"description": "When `updateBuildableProjectDepsInPackageJson` is `true`, this adds dependencies to either `peerDependencies` or `dependencies`.",
"enum": ["dependencies", "peerDependencies"],
"default": "peerDependencies"
}
},
"required": ["main", "outputPath", "tsConfig"],
Expand Down
55 changes: 30 additions & 25 deletions packages/js/src/executors/swc/swc.impl.ts
@@ -1,8 +1,9 @@
import { ExecutorContext, logger } from '@nrwl/devkit';
import { ExecutorContext, ProjectGraphProjectNode } from '@nrwl/devkit';
import {
assetGlobsToFiles,
FileInputOutput,
} from '@nrwl/workspace/src/utilities/assets';
import { DependentBuildableProjectNode } from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { join, relative, resolve } from 'path';

import { checkDependencies } from '../../utils/check-dependencies';
Expand All @@ -18,7 +19,6 @@ import { watchForSingleFileChanges } from '../../utils/watch-for-single-file-cha

function normalizeOptions(
options: SwcExecutorOptions,
layoutDir: string,
contextRoot: string,
sourceRoot?: string,
projectRoot?: string
Expand Down Expand Up @@ -71,14 +71,17 @@ function normalizeOptions(
function processAssetsAndPackageJsonOnce(
assetHandler: CopyAssetsHandler,
options: NormalizedSwcExecutorOptions,
projectRoot: string
context: ExecutorContext,
target: ProjectGraphProjectNode<any>,
dependencies: DependentBuildableProjectNode[]
) {
return async () => {
await assetHandler.processAllAssetsOnce();
updatePackageJson(
options.main,
options.outputPath,
projectRoot,
options,
context,
target,
dependencies,
!options.skipTypeCheck
);
};
Expand All @@ -88,21 +91,10 @@ export async function* swcExecutor(
_options: SwcExecutorOptions,
context: ExecutorContext
) {
const { sourceRoot, root, projectType } =
context.workspace.projects[context.projectName];
const layoutDir =
projectType === 'library'
? context.workspace.workspaceLayout.libsDir
: context.workspace.workspaceLayout.appsDir;
const options = normalizeOptions(
_options,
layoutDir,
context.root,
sourceRoot,
root
);
const { sourceRoot, root } = context.workspace.projects[context.projectName];
const options = normalizeOptions(_options, context.root, sourceRoot, root);
options.swcCliOptions.swcrcPath = addTempSwcrc(options);
const { tmpTsConfig, projectRoot } = checkDependencies(
const { tmpTsConfig, projectRoot, target, dependencies } = checkDependencies(
context,
options.tsConfig
);
Expand All @@ -126,9 +118,10 @@ export async function* swcExecutor(
'package.json',
() =>
updatePackageJson(
options.main,
options.outputPath,
projectRoot,
options,
context,
target,
dependencies,
!options.skipTypeCheck
)
);
Expand All @@ -144,13 +137,25 @@ export async function* swcExecutor(
return yield* compileSwcWatch(
context,
options,
processAssetsAndPackageJsonOnce(assetHandler, options, projectRoot)
processAssetsAndPackageJsonOnce(
assetHandler,
options,
context,
target,
dependencies
)
);
} else {
return yield compileSwc(
context,
options,
processAssetsAndPackageJsonOnce(assetHandler, options, projectRoot)
processAssetsAndPackageJsonOnce(
assetHandler,
options,
context,
target,
dependencies
)
);
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/js/src/executors/tsc/schema.json
Expand Up @@ -36,6 +36,17 @@
"items": {
"$ref": "#/definitions/transformerPattern"
}
},
"updateBuildableProjectDepsInPackageJson": {
"type": "boolean",
"description": "Whether to update the buildable project dependencies in package.json.",
"default": true
},
"buildableProjectDepsInPackageJsonType": {
"type": "string",
"description": "When `updateBuildableProjectDepsInPackageJson` is `true`, this adds dependencies to either `peerDependencies` or `dependencies`.",
"enum": ["dependencies", "peerDependencies"],
"default": "peerDependencies"
}
},
"required": ["main", "outputPath", "tsConfig"],
Expand Down
6 changes: 3 additions & 3 deletions packages/js/src/executors/tsc/tsc.impl.ts
Expand Up @@ -51,7 +51,7 @@ export async function* tscExecutor(
const { sourceRoot, root } = context.workspace.projects[context.projectName];
const options = normalizeOptions(_options, context.root, sourceRoot, root);

const { projectRoot, tmpTsConfig } = checkDependencies(
const { projectRoot, tmpTsConfig, target, dependencies } = checkDependencies(
context,
_options.tsConfig
);
Expand All @@ -73,7 +73,7 @@ export async function* tscExecutor(
const disposePackageJsonChanged = await watchForSingleFileChanges(
join(context.root, projectRoot),
'package.json',
() => updatePackageJson(options.main, options.outputPath, projectRoot)
() => updatePackageJson(options, context, target, dependencies)
);
process.on('exit', async () => {
await disposeWatchAssetChanges();
Expand All @@ -87,7 +87,7 @@ export async function* tscExecutor(

return yield* compileTypeScriptFiles(options, context, async () => {
await assetHandler.processAllAssetsOnce();
updatePackageJson(options.main, options.outputPath, projectRoot);
updatePackageJson(options, context, target, dependencies);
});
}

Expand Down
10 changes: 8 additions & 2 deletions packages/js/src/utils/check-dependencies.ts
@@ -1,18 +1,20 @@
import { ExecutorContext } from '@nrwl/devkit';
import { ExecutorContext, ProjectGraphProjectNode } from '@nrwl/devkit';
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import {
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,
createTmpTsConfig,
DependentBuildableProjectNode,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { join } from 'path';

export function checkDependencies(
context: ExecutorContext,
tsConfigPath: string
): {
tmpTsConfig: string | null;
projectRoot: string;
target: ProjectGraphProjectNode<any>;
dependencies: DependentBuildableProjectNode[];
} {
const projectGraph = readCachedProjectGraph();
const { target, dependencies, nonBuildableDependencies } =
Expand Down Expand Up @@ -55,11 +57,15 @@ export function checkDependencies(
dependencies
),
projectRoot,
target,
dependencies,
};
}

return {
tmpTsConfig: null,
projectRoot,
target,
dependencies,
};
}
2 changes: 2 additions & 0 deletions packages/js/src/utils/schema.d.ts
Expand Up @@ -36,6 +36,8 @@ export interface ExecutorOptions {
tsConfig: string;
watch: boolean;
transformers: TransformerEntry[];
updateBuildableProjectDepsInPackageJson?: boolean;
buildableProjectDepsInPackageJsonType?: 'dependencies' | 'peerDependencies';
}

export interface NormalizedExecutorOptions extends ExecutorOptions {
Expand Down
47 changes: 34 additions & 13 deletions packages/js/src/utils/update-package-json.ts
@@ -1,6 +1,15 @@
import { normalizePath } from '@nrwl/devkit';
import {
ExecutorContext,
normalizePath,
ProjectGraphProjectNode,
} from '@nrwl/devkit';
import { readJsonFile, writeJsonFile } from '@nrwl/tao/src/utils/fileutils';
import {
DependentBuildableProjectNode,
updateBuildableProjectPackageJsonDependencies,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { basename, dirname, join, relative } from 'path';
import { NormalizedExecutorOptions } from './schema';

function getMainFileDirRelativeToProjectRoot(
main: string,
Expand All @@ -12,20 +21,18 @@ function getMainFileDirRelativeToProjectRoot(
}

export function updatePackageJson(
main: string,
outputPath: string,
projectRoot: string,
options: NormalizedExecutorOptions,
context: ExecutorContext,
target: ProjectGraphProjectNode<any>,
dependencies: DependentBuildableProjectNode[],
withTypings = true
): void {
const packageJson = readJsonFile(join(projectRoot, 'package.json'));
if (packageJson.main && packageJson.typings) {
return;
}
const packageJson = readJsonFile(join(options.projectRoot, 'package.json'));

const mainFile = basename(main).replace(/\.[tj]s$/, '');
const mainFile = basename(options.main).replace(/\.[tj]s$/, '');
const relativeMainFileDir = getMainFileDirRelativeToProjectRoot(
main,
projectRoot
options.main,
options.projectRoot
);
const mainJsFile = `${relativeMainFileDir}${mainFile}.js`;
const typingsFile = `${relativeMainFileDir}${mainFile}.d.ts`;
Expand All @@ -36,6 +43,20 @@ export function updatePackageJson(
packageJson.typings = packageJson.typings ?? typingsFile;
}

const outputPackageJson = join(outputPath, 'package.json');
writeJsonFile(outputPackageJson, packageJson);
writeJsonFile(`${options.outputPath}/package.json`, packageJson);

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

1 comment on commit 4287d59

@vercel
Copy link

@vercel vercel bot commented on 4287d59 Feb 24, 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.