Skip to content

Commit

Permalink
Remove modular-template dependencies after modular add (#2365)
Browse files Browse the repository at this point in the history
* Remove modular-template dependencies after modular add
  • Loading branch information
AlbertoBrusa authored Apr 27, 2023
1 parent d770c80 commit 89097fc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/cool-pumpkins-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'modular-scripts': minor
---

`modular add` now cleans up after itself by removing the added template
dependency
47 changes: 33 additions & 14 deletions packages/modular-scripts/src/addPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,8 @@ async function addPackage({
yarnAddArgs.push('--cached');
}

const templateInstallSubprocess = execAsync('yarnpkg', yarnAddArgs, {
cwd: modularRoot,
stderr: 'pipe',
stdout: 'ignore',
});

// Remove warnings
templateInstallSubprocess.stderr?.pipe(
new LineFilterOutStream(/.*warning.*/),
);
if (verbose) {
templateInstallSubprocess.stderr?.pipe(process.stderr);
}
await runYarnCommand(yarnAddArgs, verbose);

await templateInstallSubprocess;
templatePackageJsonPath = require.resolve(installedPackageJsonPath, {
paths: [modularRoot],
});
Expand Down Expand Up @@ -304,6 +291,20 @@ async function addPackage({
}

await subprocess;

// Clean up package.json dependencies - the template isn't needed anymore
const rootPackageJson = fs.readJSONSync(
path.join(modularRoot, 'package.json'),
) as ModularPackageJson;

if (rootPackageJson.dependencies?.[templateName]) {
const removeTemplateArgs = ['remove', templateName];
if (isYarnV1) {
removeTemplateArgs.push('-W');
}

await runYarnCommand(removeTemplateArgs, verbose);
}
}

function getNewPackageDetails({
Expand Down Expand Up @@ -381,4 +382,22 @@ async function validatePackageDetails(
}
}

async function runYarnCommand(args: string[], verbose: boolean) {
const templateInstallSubprocess = execAsync('yarnpkg', args, {
cwd: getModularRoot(),
stderr: 'pipe',
stdout: 'ignore',
});

// Remove warnings
templateInstallSubprocess.stderr?.pipe(
new LineFilterOutStream(/.*warning.*/),
);
if (verbose) {
templateInstallSubprocess.stderr?.pipe(process.stderr);
}

await templateInstallSubprocess;
}

export default actionPreflightCheck(addPackage);

0 comments on commit 89097fc

Please sign in to comment.