Skip to content
Merged
30 changes: 15 additions & 15 deletions packages/devkit/src/tasks/install-packages-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ import {
* `package.json` hasn't changed at all or it hasn't changed since the last invocation.
*
* @param tree - the file system tree
* @param alwaysRun - always run the command even if `package.json` hasn't changed.
* @param ensureInstall - ensure install runs even if `package.json` hasn't changed,
* unless install already ran this generator cycle.
*/
export function installPackagesTask(
tree: Tree,
alwaysRun: boolean = false,
ensureInstall: boolean = false,
cwd: string = '',
packageManager: PackageManager = detectPackageManager(join(tree.root, cwd))
): void {
if (
!tree
.listChanges()
.find((f) => f.path === joinPathFragments(cwd, 'package.json')) &&
!alwaysRun
) {
const packageJsonPath = joinPathFragments(cwd, 'package.json');
const packageJsonChanged = tree
.listChanges()
.some((f) => f.path === packageJsonPath);

if (!packageJsonChanged && !ensureInstall) {
return;
}

const packageJsonValue = tree.read(
joinPathFragments(cwd, 'package.json'),
'utf-8'
);
let storedPackageJsonValue: string = global['__packageJsonInstallCache__'];
// Don't install again if install was already executed with package.json
if (storedPackageJsonValue != packageJsonValue || alwaysRun) {
const packageJsonValue = tree.read(packageJsonPath, 'utf-8');
const storedPackageJsonValue: string = global['__packageJsonInstallCache__'];
const installAlreadyRan = storedPackageJsonValue != null;
const packageJsonDiffers = storedPackageJsonValue != packageJsonValue;

if (packageJsonDiffers || (ensureInstall && !installAlreadyRan)) {
global['__packageJsonInstallCache__'] = packageJsonValue;
const pmc = getPackageManagerCommand(packageManager);
const execSyncOptions: ExecSyncOptions = {
Expand Down
Loading