Skip to content

Commit 58c1784

Browse files
authored
fix(create): run ngAdd in new project context (#1281)
## Proposed change Run ngAdd in new project context ## Related issues - 🐛 fixes #1203
2 parents ca47c5f + 7cb3b11 commit 58c1784

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

packages/@ama-sdk/create/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ const getSchematicStepInfo = (schematic: string) => ({
7979

8080
const run = () => {
8181

82-
const steps: { args: string[]; cwd?: string }[] = [
82+
const steps: { args: string[]; cwd?: string; runner?: string }[] = [
8383
getSchematicStepInfo(schematicsToRun[0]),
8484
...(
8585
packageManager === 'yarn'
86-
? [{ args: ['yarn', 'set', 'version', getYarnVersion()], cwd: resolve(process.cwd(), targetDirectory)}]
86+
? [{ runner: 'yarn', args: ['set', 'version', getYarnVersion()], cwd: resolve(process.cwd(), targetDirectory)}]
8787
: []
8888
),
8989
...schematicsToRun.slice(1).map(getSchematicStepInfo)
9090
];
9191

9292
const errors = steps
93-
.map((step) => spawnSync(process.execPath, step.args, { stdio: 'pipe', cwd: step.cwd || process.cwd() }))
93+
.map((step) => spawnSync(step.runner || process.execPath, step.args, { stdio: 'pipe', cwd: step.cwd || process.cwd() }))
9494
.map(({error}) => error)
9595
.filter((err) => !!err);
9696

packages/@o3r/create/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"tslib": "^2.5.3"
2222
},
2323
"devDependencies": {
24+
"@angular-devkit/architect": "~0.1700.3",
25+
"@angular-devkit/core": "~17.0.3",
26+
"@angular-devkit/schematics": "~17.0.3",
2427
"@angular-eslint/eslint-plugin": "~17.2.0",
2528
"@angular-eslint/eslint-plugin-template": "~17.2.0",
2629
"@nx/eslint": "~17.2.0",

packages/@o3r/create/src/index.ts

+33-17
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import { spawnSync } from 'node:child_process';
44
import { join, resolve } from 'node:path';
5-
import { readFileSync } from 'node:fs';
5+
import { readFileSync, writeFileSync } from 'node:fs';
66
import * as minimist from 'minimist';
77
import type { PackageJson } from 'type-fest';
88

99
const { properties } = JSON.parse(
1010
readFileSync(require.resolve('@schematics/angular/ng-new/schema').replace(/\.js$/, '.json'), { encoding: 'utf-8' })
1111
) as { properties: Record<string, { alias?: string }> };
12-
const { version } = JSON.parse(
12+
const { version, dependencies, devDependencies } = JSON.parse(
1313
readFileSync(resolve(__dirname, 'package.json'), { encoding: 'utf-8' })
1414
) as PackageJson;
1515

@@ -73,7 +73,7 @@ const logo = `
7373
'|. || || || || ||
7474
''|...|' '|.' '|.' '|...' .||.
7575
`;
76-
const packageManager = process.env.npm_config_user_agent?.split('/')[0];
76+
7777
const binPath = join(require.resolve('@angular/cli/package.json'), '../bin/ng.js');
7878
const args = process.argv.slice(2).filter((a) => a !== '--create-application');
7979

@@ -87,16 +87,15 @@ if (!args.some((a) => a.startsWith('--preset'))) {
8787
args.push('--preset', 'basic');
8888
}
8989

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-
9790
args.push('--no-create-application');
9891

9992
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;
10099

101100
if (argv._.length === 0) {
102101
// eslint-disable-next-line no-console
@@ -113,7 +112,7 @@ const isNgNewOptions = (arg: string) => {
113112
if (arg.startsWith('--')) {
114113
return entries.some(([key]) => [`--${key}`, `--no-${key}`, `--${key.replaceAll(/([A-Z])/g, '-$1').toLowerCase()}`, `--no-${key.replaceAll(/([A-Z])/g, '-$1').toLowerCase()}`].includes(arg));
115114
} else if (arg.startsWith('-')) {
116-
return entries.some(([, {alias}]) => alias && arg === `-${alias}`);
115+
return entries.some(([, { alias }]) => alias && arg === `-${alias}`);
117116
}
118117

119118
return true;
@@ -131,7 +130,7 @@ const createNgProject = () => {
131130
const options = schematicsCliOptions
132131
.filter(([key]) => isNgNewOptions(key))
133132
.flat();
134-
const { error } = spawnSync(process.execPath, [binPath, 'new', ...argv._, ...options, '--skip-install'], {
133+
const { error } = spawnSync(process.execPath, [binPath, 'new', ...argv._, ...options], {
135134
stdio: 'inherit'
136135
});
137136

@@ -142,14 +141,31 @@ const createNgProject = () => {
142141
}
143142
};
144143

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+
];
146151
const cwd = resolve(process.cwd(), relativeDirectory);
147152
const options = schematicsCliOptions
148153
.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';
152160
});
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+
);
153169

154170
if (error) {
155171
// eslint-disable-next-line no-console
@@ -162,4 +178,4 @@ const projectFolder = argv._[0]?.replaceAll(' ', '-').toLowerCase() || '.';
162178

163179
console.info(logo);
164180
createNgProject();
165-
addOtterFramework(projectFolder);
181+
addOtterFramework(projectFolder, packageManager);

yarn.lock

+3
Original file line numberDiff line numberDiff line change
@@ -6869,6 +6869,9 @@ __metadata:
68696869
version: 0.0.0-use.local
68706870
resolution: "@o3r/create@workspace:packages/@o3r/create"
68716871
dependencies:
6872+
"@angular-devkit/architect": "npm:~0.1700.3"
6873+
"@angular-devkit/core": "npm:~17.0.3"
6874+
"@angular-devkit/schematics": "npm:~17.0.3"
68726875
"@angular-eslint/eslint-plugin": "npm:~17.2.0"
68736876
"@angular-eslint/eslint-plugin-template": "npm:~17.2.0"
68746877
"@angular/cli": "npm:~17.0.3"

0 commit comments

Comments
 (0)