2
2
3
3
import { spawnSync } from 'node:child_process' ;
4
4
import { join , resolve } from 'node:path' ;
5
- import { readFileSync } from 'node:fs' ;
5
+ import { readFileSync , writeFileSync } from 'node:fs' ;
6
6
import * as minimist from 'minimist' ;
7
7
import type { PackageJson } from 'type-fest' ;
8
8
9
9
const { properties } = JSON . parse (
10
10
readFileSync ( require . resolve ( '@schematics/angular/ng-new/schema' ) . replace ( / \. j s $ / , '.json' ) , { encoding : 'utf-8' } )
11
11
) as { properties : Record < string , { alias ?: string } > } ;
12
- const { version } = JSON . parse (
12
+ const { version, dependencies , devDependencies } = JSON . parse (
13
13
readFileSync ( resolve ( __dirname , 'package.json' ) , { encoding : 'utf-8' } )
14
14
) as PackageJson ;
15
15
@@ -73,7 +73,7 @@ const logo = `
73
73
'|. || || || || ||
74
74
''|...|' '|.' '|.' '|...' .||.
75
75
` ;
76
- const packageManager = process . env . npm_config_user_agent ?. split ( '/' ) [ 0 ] ;
76
+
77
77
const binPath = join ( require . resolve ( '@angular/cli/package.json' ) , '../bin/ng.js' ) ;
78
78
const args = process . argv . slice ( 2 ) . filter ( ( a ) => a !== '--create-application' ) ;
79
79
@@ -87,16 +87,15 @@ if (!args.some((a) => a.startsWith('--preset'))) {
87
87
args . push ( '--preset' , 'basic' ) ;
88
88
}
89
89
90
- const hasPackageManagerArg = args . some ( ( a ) => a . startsWith ( '--package-manager' ) ) ;
91
- if ( ! hasPackageManagerArg ) {
92
- if ( packageManager && [ 'npm' , 'pnpm' , 'yarn' , 'cnpm' ] . includes ( packageManager ) ) {
93
- args . push ( '--package-manager' , packageManager ) ;
94
- }
95
- }
96
-
97
90
args . push ( '--no-create-application' ) ;
98
91
99
92
const argv = minimist ( args ) ;
93
+ const packageManagerEnv = process . env . npm_config_user_agent ?. split ( '/' ) [ 0 ] ;
94
+ let defaultPackageManager = 'npm' ;
95
+ if ( packageManagerEnv && [ 'npm' , 'yarn' ] . includes ( packageManagerEnv ) ) {
96
+ defaultPackageManager = packageManagerEnv ;
97
+ }
98
+ const packageManager = argv [ 'package-manager' ] || defaultPackageManager ;
100
99
101
100
if ( argv . _ . length === 0 ) {
102
101
// eslint-disable-next-line no-console
@@ -113,7 +112,7 @@ const isNgNewOptions = (arg: string) => {
113
112
if ( arg . startsWith ( '--' ) ) {
114
113
return entries . some ( ( [ key ] ) => [ `--${ key } ` , `--no-${ key } ` , `--${ key . replaceAll ( / ( [ A - Z ] ) / g, '-$1' ) . toLowerCase ( ) } ` , `--no-${ key . replaceAll ( / ( [ A - Z ] ) / g, '-$1' ) . toLowerCase ( ) } ` ] . includes ( arg ) ) ;
115
114
} else if ( arg . startsWith ( '-' ) ) {
116
- return entries . some ( ( [ , { alias} ] ) => alias && arg === `-${ alias } ` ) ;
115
+ return entries . some ( ( [ , { alias } ] ) => alias && arg === `-${ alias } ` ) ;
117
116
}
118
117
119
118
return true ;
@@ -131,7 +130,7 @@ const createNgProject = () => {
131
130
const options = schematicsCliOptions
132
131
. filter ( ( [ key ] ) => isNgNewOptions ( key ) )
133
132
. flat ( ) ;
134
- const { error } = spawnSync ( process . execPath , [ binPath , 'new' , ...argv . _ , ...options , '--skip-install' ] , {
133
+ const { error } = spawnSync ( process . execPath , [ binPath , 'new' , ...argv . _ , ...options ] , {
135
134
stdio : 'inherit'
136
135
} ) ;
137
136
@@ -142,14 +141,31 @@ const createNgProject = () => {
142
141
}
143
142
} ;
144
143
145
- const addOtterFramework = ( relativeDirectory = '.' ) => {
144
+ const addOtterFramework = ( relativeDirectory = '.' , projectPackageManager = 'npm' ) => {
145
+ const mandatoryDependencies = [
146
+ '@angular-devkit/schematics' ,
147
+ '@schematics/angular' ,
148
+ '@angular-devkit/core' ,
149
+ '@angular-devkit/architect'
150
+ ] ;
146
151
const cwd = resolve ( process . cwd ( ) , relativeDirectory ) ;
147
152
const options = schematicsCliOptions
148
153
. flat ( ) ;
149
- const { error } = spawnSync ( process . execPath , [ binPath , 'add' , `@o3r/core@${ version || 'latest' } ` , ...options ] , {
150
- stdio : 'inherit' ,
151
- cwd
154
+
155
+ const packageJsonPath = resolve ( cwd , 'package.json' ) ;
156
+ const packageJson : PackageJson = JSON . parse ( readFileSync ( packageJsonPath , { encoding : 'utf-8' } ) ) ;
157
+ packageJson . devDependencies ||= { } ;
158
+ mandatoryDependencies . forEach ( ( dep ) => {
159
+ packageJson . devDependencies ! [ dep ] = dependencies ?. [ dep ] || devDependencies ?. [ dep ] || 'latest' ;
152
160
} ) ;
161
+ writeFileSync ( packageJsonPath , JSON . stringify ( packageJson , null , 2 ) ) ;
162
+
163
+ const { error } = spawnSync ( process . platform === 'win32' ? `${ projectPackageManager } .cmd` : projectPackageManager ,
164
+ [ 'exec' , 'ng' , 'add' , `@o3r/core@~${ version } ` , ...( projectPackageManager === 'npm' ? [ '--' ] : [ ] ) , ...options ] , {
165
+ stdio : 'inherit' ,
166
+ cwd
167
+ }
168
+ ) ;
153
169
154
170
if ( error ) {
155
171
// eslint-disable-next-line no-console
@@ -162,4 +178,4 @@ const projectFolder = argv._[0]?.replaceAll(' ', '-').toLowerCase() || '.';
162
178
163
179
console . info ( logo ) ;
164
180
createNgProject ( ) ;
165
- addOtterFramework ( projectFolder ) ;
181
+ addOtterFramework ( projectFolder , packageManager ) ;
0 commit comments