@@ -20,6 +20,8 @@ import {
2020import type { PackageJson } from 'nx/src/utils/package-json' ;
2121import * as path from 'path' ;
2222
23+ const noop = ( ) => void 0 ;
24+
2325export 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(
5961export default initGenerator ;
6062
6163function 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
7581function updateNxJson ( host : Tree ) {
@@ -90,18 +96,20 @@ function initToolManifest(host: Tree, dotnetClient: DotNetClient) {
9096}
9197
9298function 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
107115function initBuildCustomization ( host : Tree ) {
0 commit comments