|
| 1 | +import { addDependenciesToPackageJson, formatFiles, installPackagesTask, logger, readJson, Tree } from '@nx/devkit'; |
| 2 | +import { prompt } from 'enquirer'; |
| 3 | +import { PEER_DEPENDENCIES } from '../../versions'; |
| 4 | + |
| 5 | +export async function auxGenerator(tree: Tree) { |
| 6 | + logger.info('[NGT] Initialize auxiliary packages'); |
| 7 | + |
| 8 | + const packageJson = readJson(tree, 'package.json'); |
| 9 | + if (!packageJson) { |
| 10 | + logger.warn(`[NGT] PackageJson not found in: ${tree.root}`); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + const ngtVersion = |
| 15 | + packageJson['dependencies']?.['angular-three'] || packageJson['devDependencies']?.['angular-three']; |
| 16 | + |
| 17 | + if (!ngtVersion) { |
| 18 | + logger.warn(`[NGT] angular-three is not installed.`); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const { packages } = await prompt<{ packages: string[] }>({ |
| 23 | + type: 'multiselect', |
| 24 | + name: 'packages', |
| 25 | + message: 'Which packages to add?', |
| 26 | + choices: ['angular-three-soba', 'angular-three-rapier', 'angular-three-postprocessing', 'angular-three-cannon'], |
| 27 | + }); |
| 28 | + |
| 29 | + const packagesToAdd = {}; |
| 30 | + |
| 31 | + for (const pkg of packages) { |
| 32 | + const exist = packageJson['dependencies']?.[pkg] || packageJson['devDependencies']?.[pkg]; |
| 33 | + if (exist) { |
| 34 | + logger.warn(`[NGT] ${pkg} is already installed. Skipping`); |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + packagesToAdd[pkg] = ngtVersion; |
| 39 | + |
| 40 | + if (PEER_DEPENDENCIES[pkg]) { |
| 41 | + for (const depName in PEER_DEPENDENCIES[pkg]) { |
| 42 | + packagesToAdd[depName] = PEER_DEPENDENCIES[pkg][depName]; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (Object.keys(packagesToAdd).length === 0) { |
| 48 | + console.warn(`[NGT] No packages to add`); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + addDependenciesToPackageJson(tree, packagesToAdd, {}); |
| 53 | + |
| 54 | + await formatFiles(tree); |
| 55 | + |
| 56 | + return () => { |
| 57 | + installPackagesTask(tree); |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +export default auxGenerator; |
0 commit comments