Skip to content

Commit d95a3ff

Browse files
committed
fix(core): remove dependency on workspace package.json
1 parent 04c1f1d commit d95a3ff

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

packages/core/src/generators/init/generator.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
import type { PackageJson } from 'nx/src/utils/package-json';
2121
import * as path from 'path';
2222

23+
const noop = () => void 0;
24+
2325
export async function initGenerator(
2426
host: Tree,
2527
_: null, // Nx will populate this with options, which are currently unused.
@@ -59,17 +61,21 @@ export async function initGenerator(
5961
export default initGenerator;
6062

6163
function installNpmPackages(host: Tree): GeneratorCallback {
62-
const packageJson = readJson<PackageJson>(host, 'package.json');
63-
const nxVersion: string =
64-
packageJson.devDependencies?.['nx'] ??
65-
(packageJson.dependencies?.['nx'] as string);
66-
return addDependenciesToPackageJson(
67-
host,
68-
{},
69-
{
70-
'@nrwl/js': nxVersion,
71-
},
72-
);
64+
if (host.exists('package.json')) {
65+
const packageJson = readJson<PackageJson>(host, 'package.json');
66+
const nxVersion: string =
67+
packageJson.devDependencies?.['nx'] ??
68+
(packageJson.dependencies?.['nx'] as string);
69+
return addDependenciesToPackageJson(
70+
host,
71+
{},
72+
{
73+
'@nrwl/js': nxVersion,
74+
},
75+
);
76+
} else {
77+
return noop;
78+
}
7379
}
7480

7581
function updateNxJson(host: Tree) {
@@ -90,18 +96,20 @@ function initToolManifest(host: Tree, dotnetClient: DotNetClient) {
9096
}
9197

9298
function addPrepareScript(host: Tree) {
93-
const packageJson = readJson(host, 'package.json');
94-
const prepareSteps: string[] =
95-
packageJson.scripts?.prepare?.split('&&').map((x: string) => x.trim()) ??
96-
[];
97-
98-
const restoreScript = 'nx g @nx-dotnet/core:restore';
99-
if (!prepareSteps.includes(restoreScript)) {
100-
prepareSteps.push(restoreScript);
99+
if (host.exists('package.json')) {
100+
const packageJson = readJson(host, 'package.json');
101+
const prepareSteps: string[] =
102+
packageJson.scripts?.prepare?.split('&&').map((x: string) => x.trim()) ??
103+
[];
104+
105+
const restoreScript = 'nx g @nx-dotnet/core:restore';
106+
if (!prepareSteps.includes(restoreScript)) {
107+
prepareSteps.push(restoreScript);
108+
}
109+
packageJson.scripts ??= {};
110+
packageJson.scripts.prepare = prepareSteps.join(' && ');
111+
writeJson(host, 'package.json', packageJson);
101112
}
102-
packageJson.scripts ??= {};
103-
packageJson.scripts.prepare = prepareSteps.join(' && ');
104-
writeJson(host, 'package.json', packageJson);
105113
}
106114

107115
function initBuildCustomization(host: Tree) {

0 commit comments

Comments
 (0)