From 4287d59a2a522fcd89727664db4b27b909fb67a1 Mon Sep 17 00:00:00 2001 From: Jason Gerbes Date: Fri, 25 Feb 2022 11:01:43 +1300 Subject: [PATCH] fix(js): update dependencies in package.json (#9073) Co-authored-by: Jason Gerbes --- docs/generated/api-js/executors/swc.md | 18 +++++++ docs/generated/api-js/executors/tsc.md | 18 +++++++ packages/js/src/executors/swc/schema.json | 11 ++++ packages/js/src/executors/swc/swc.impl.ts | 55 +++++++++++--------- packages/js/src/executors/tsc/schema.json | 11 ++++ packages/js/src/executors/tsc/tsc.impl.ts | 6 +-- packages/js/src/utils/check-dependencies.ts | 10 +++- packages/js/src/utils/schema.d.ts | 2 + packages/js/src/utils/update-package-json.ts | 47 ++++++++++++----- 9 files changed, 135 insertions(+), 43 deletions(-) diff --git a/docs/generated/api-js/executors/swc.md b/docs/generated/api-js/executors/swc.md index 55d74347871b9..04cb70fec0091 100644 --- a/docs/generated/api-js/executors/swc.md +++ b/docs/generated/api-js/executors/swc.md @@ -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` @@ -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` diff --git a/docs/generated/api-js/executors/tsc.md b/docs/generated/api-js/executors/tsc.md index 88cf454429536..d969811292034 100644 --- a/docs/generated/api-js/executors/tsc.md +++ b/docs/generated/api-js/executors/tsc.md @@ -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` diff --git a/packages/js/src/executors/swc/schema.json b/packages/js/src/executors/swc/schema.json index 8914c39400f15..11a8c1bc64d0b 100644 --- a/packages/js/src/executors/swc/schema.json +++ b/packages/js/src/executors/swc/schema.json @@ -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"], diff --git a/packages/js/src/executors/swc/swc.impl.ts b/packages/js/src/executors/swc/swc.impl.ts index 873afa161d338..1f4a70f9d79c1 100644 --- a/packages/js/src/executors/swc/swc.impl.ts +++ b/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'; @@ -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 @@ -71,14 +71,17 @@ function normalizeOptions( function processAssetsAndPackageJsonOnce( assetHandler: CopyAssetsHandler, options: NormalizedSwcExecutorOptions, - projectRoot: string + context: ExecutorContext, + target: ProjectGraphProjectNode, + dependencies: DependentBuildableProjectNode[] ) { return async () => { await assetHandler.processAllAssetsOnce(); updatePackageJson( - options.main, - options.outputPath, - projectRoot, + options, + context, + target, + dependencies, !options.skipTypeCheck ); }; @@ -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 ); @@ -126,9 +118,10 @@ export async function* swcExecutor( 'package.json', () => updatePackageJson( - options.main, - options.outputPath, - projectRoot, + options, + context, + target, + dependencies, !options.skipTypeCheck ) ); @@ -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 + ) ); } } diff --git a/packages/js/src/executors/tsc/schema.json b/packages/js/src/executors/tsc/schema.json index 6eb1e72e35074..71b0d6a0169cc 100644 --- a/packages/js/src/executors/tsc/schema.json +++ b/packages/js/src/executors/tsc/schema.json @@ -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"], diff --git a/packages/js/src/executors/tsc/tsc.impl.ts b/packages/js/src/executors/tsc/tsc.impl.ts index b3e6ddc7eb178..2a02159d267a5 100644 --- a/packages/js/src/executors/tsc/tsc.impl.ts +++ b/packages/js/src/executors/tsc/tsc.impl.ts @@ -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 ); @@ -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(); @@ -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); }); } diff --git a/packages/js/src/utils/check-dependencies.ts b/packages/js/src/utils/check-dependencies.ts index f03904ff79ab4..520f6a133a120 100644 --- a/packages/js/src/utils/check-dependencies.ts +++ b/packages/js/src/utils/check-dependencies.ts @@ -1,11 +1,11 @@ -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, @@ -13,6 +13,8 @@ export function checkDependencies( ): { tmpTsConfig: string | null; projectRoot: string; + target: ProjectGraphProjectNode; + dependencies: DependentBuildableProjectNode[]; } { const projectGraph = readCachedProjectGraph(); const { target, dependencies, nonBuildableDependencies } = @@ -55,11 +57,15 @@ export function checkDependencies( dependencies ), projectRoot, + target, + dependencies, }; } return { tmpTsConfig: null, projectRoot, + target, + dependencies, }; } diff --git a/packages/js/src/utils/schema.d.ts b/packages/js/src/utils/schema.d.ts index e07eccf1ba26b..9549334fc3439 100644 --- a/packages/js/src/utils/schema.d.ts +++ b/packages/js/src/utils/schema.d.ts @@ -36,6 +36,8 @@ export interface ExecutorOptions { tsConfig: string; watch: boolean; transformers: TransformerEntry[]; + updateBuildableProjectDepsInPackageJson?: boolean; + buildableProjectDepsInPackageJsonType?: 'dependencies' | 'peerDependencies'; } export interface NormalizedExecutorOptions extends ExecutorOptions { diff --git a/packages/js/src/utils/update-package-json.ts b/packages/js/src/utils/update-package-json.ts index f8571d270fe81..af12d5e09bc62 100644 --- a/packages/js/src/utils/update-package-json.ts +++ b/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, @@ -12,20 +21,18 @@ function getMainFileDirRelativeToProjectRoot( } export function updatePackageJson( - main: string, - outputPath: string, - projectRoot: string, + options: NormalizedExecutorOptions, + context: ExecutorContext, + target: ProjectGraphProjectNode, + 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`; @@ -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 + ); + } }