From dffb3d38e0c5e40316a0a3338cdf464de0506746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Mon, 24 Apr 2023 20:46:13 +0100 Subject: [PATCH] fix(misc): safely iterate over package.json scripts in nx init command (#16508) --- packages/nx/src/nx-init/add-nx-to-nest.ts | 2 +- packages/nx/src/nx-init/add-nx-to-npm-repo.ts | 2 +- packages/nx/src/nx-init/angular/standalone-workspace.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/nx/src/nx-init/add-nx-to-nest.ts b/packages/nx/src/nx-init/add-nx-to-nest.ts index c338b80c5943f..350306d90fd3f 100644 --- a/packages/nx/src/nx-init/add-nx-to-nest.ts +++ b/packages/nx/src/nx-init/add-nx-to-nest.ts @@ -51,7 +51,7 @@ export async function addNxToNest(options: Options, packageJson: PackageJson) { 'test:watch', ]; - const scripts = Object.keys(packageJson.scripts).filter((s) => { + const scripts = Object.keys(packageJson.scripts ?? {}).filter((s) => { if (nestCacheableScripts.includes(s) || nestIgnoreScripts.includes(s)) { return false; } diff --git a/packages/nx/src/nx-init/add-nx-to-npm-repo.ts b/packages/nx/src/nx-init/add-nx-to-npm-repo.ts index e609dded99204..e623df27c12ee 100644 --- a/packages/nx/src/nx-init/add-nx-to-npm-repo.ts +++ b/packages/nx/src/nx-init/add-nx-to-npm-repo.ts @@ -25,7 +25,7 @@ export async function addNxToNpmRepo(options: Options) { let useNxCloud: boolean; const packageJson = readJsonFile('package.json'); - const scripts = Object.keys(packageJson.scripts).filter( + const scripts = Object.keys(packageJson.scripts ?? {}).filter( (s) => !s.startsWith('pre') && !s.startsWith('post') ); diff --git a/packages/nx/src/nx-init/angular/standalone-workspace.ts b/packages/nx/src/nx-init/angular/standalone-workspace.ts index c15bc096ac838..3e9828ae21763 100644 --- a/packages/nx/src/nx-init/angular/standalone-workspace.ts +++ b/packages/nx/src/nx-init/angular/standalone-workspace.ts @@ -43,7 +43,7 @@ export async function setupStandaloneWorkspace( // convert workspace config format to standalone project configs // update its targets outputs and delete angular.json const projects = toNewFormat(angularJson).projects; - for (const [projectName, project] of Object.entries(projects)) { + for (const [projectName, project] of Object.entries(projects ?? {})) { updateProjectOutputs(repoRoot, project); writeJsonFile(join(project.root, 'project.json'), { $schema: normalizePath( @@ -162,7 +162,7 @@ function getWorkspaceCapabilities( karmaProjectConfigFile: false, }; - for (const project of Object.values(projects)) { + for (const project of Object.values(projects ?? {})) { if ( !capabilities.eslintProjectConfigFile && projectHasEslintConfig(project)