refactor(@schematics/angular): update server schematic to use new dependency utility

This commit updates the server schematic to use the new dependency utility.
This commit is contained in:
Alan Agius 2024-01-05 09:16:51 +00:00 committed by Alan Agius
parent 481358618b
commit 94082a7ca4

View File

@ -20,14 +20,9 @@ import {
strings, strings,
url, url,
} from '@angular-devkit/schematics'; } from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { posix } from 'node:path'; import { posix } from 'node:path';
import { addRootProvider } from '../utility'; import { DependencyType, InstallBehavior, addDependency, addRootProvider } from '../utility';
import { import { getPackageJsonDependency } from '../utility/dependencies';
NodeDependencyType,
addPackageJsonDependency,
getPackageJsonDependency,
} from '../utility/dependencies';
import { JSONFile } from '../utility/json-file'; import { JSONFile } from '../utility/json-file';
import { latestVersions } from '../utility/latest-versions'; import { latestVersions } from '../utility/latest-versions';
import { isStandaloneApp } from '../utility/ng-ast-utils'; import { isStandaloneApp } from '../utility/ng-ast-utils';
@ -136,23 +131,25 @@ function updateTsConfigFile(tsConfigPath: string): Rule {
}; };
} }
function addDependencies(): Rule { function addDependencies(skipInstall: boolean | undefined): Rule {
return (host: Tree) => { return (host: Tree) => {
const coreDep = getPackageJsonDependency(host, '@angular/core'); const coreDep = getPackageJsonDependency(host, '@angular/core');
if (coreDep === null) { if (coreDep === null) {
throw new SchematicsException('Could not find version.'); throw new SchematicsException('Could not find version.');
} }
const platformServerDep = {
...coreDep,
name: '@angular/platform-server',
};
addPackageJsonDependency(host, platformServerDep);
addPackageJsonDependency(host, { const install = skipInstall ? InstallBehavior.None : InstallBehavior.Auto;
type: NodeDependencyType.Dev,
name: '@types/node', return chain([
version: latestVersions['@types/node'], addDependency('@angular/platform-server', coreDep.version, {
}); type: DependencyType.Default,
install,
}),
addDependency('@types/node', latestVersions['@types/node'], {
type: DependencyType.Dev,
install,
}),
]);
}; };
} }
@ -178,9 +175,6 @@ export default function (options: ServerOptions): Rule {
return; return;
} }
if (!options.skipInstall) {
context.addTask(new NodePackageInstallTask());
}
const clientBuildOptions = clientBuildTarget.options as Record<string, string>; const clientBuildOptions = clientBuildTarget.options as Record<string, string>;
const browserEntryPoint = await getMainFilePath(host, options.project); const browserEntryPoint = await getMainFilePath(host, options.project);
const isStandalone = isStandaloneApp(host, browserEntryPoint); const isStandalone = isStandaloneApp(host, browserEntryPoint);
@ -220,7 +214,7 @@ export default function (options: ServerOptions): Rule {
), ),
updateConfigFileBrowserBuilder(options, tsConfigDirectory), updateConfigFileBrowserBuilder(options, tsConfigDirectory),
]), ]),
addDependencies(), addDependencies(options.skipInstall),
addRootProvider( addRootProvider(
options.project, options.project,
({ code, external }) => ({ code, external }) =>