Skip to content

Commit

Permalink
fix(cli): Update workspaces in root package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu96 authored and adrians5j committed Oct 19, 2021
1 parent 291d915 commit 9e7f782
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/cli/commands/upgrade/upgrades/5.16.0/index.js
Expand Up @@ -2,7 +2,12 @@
* A new type of upgrade where we take the files from cwp-template-aws and copy them into required locations.
* Old files are always backed up.
*/
const { prettierFormat, yarnInstall } = require("../utils");
const {
prettierFormat,
yarnInstall,
addWorkspaceToRootPackageJson,
removeWorkspaceToRootPackageJson
} = require("../utils");
const path = require("path");
const fs = require("fs");
const fsExtra = require("fs-extra");
Expand Down Expand Up @@ -284,6 +289,17 @@ module.exports = {
*/
assignPackageVersions(context, targets);

/**
* Update workspaces in root package.json.
*/
context.info(" Update workspaces in root package.json...");
const rootPackageJson = path.join(context.project.root, "package.json");
await addWorkspaceToRootPackageJson(rootPackageJson, [
"api/code/pageBuilder/importPages/*",
"api/code/pageBuilder/exportPages/*"
]);
await removeWorkspaceToRootPackageJson(rootPackageJson, ["api/code/pageBuilder/*"]);

await prettierFormat(
targets.map(t => t.destination),
context
Expand Down
41 changes: 40 additions & 1 deletion packages/cli/commands/upgrade/upgrades/utils.js
Expand Up @@ -311,6 +311,43 @@ const addImportsToSource = ({ context, source, imports, file }) => {
}
};

/**
* @param packageJsonPath {String}
* @param pathsToAdd {String[]}
*/
const addWorkspaceToRootPackageJson = async (packageJsonPath, pathsToAdd) => {
const rootPackageJson = await loadJson(packageJsonPath);

pathsToAdd.forEach(pathToAdd => {
// Ensure forward slashes are used.
pathToAdd = pathToAdd.replace(/\\/g, "/");
// Add it to workspaces packages if not already
if (!rootPackageJson.workspaces.packages.includes(pathToAdd)) {
rootPackageJson.workspaces.packages.push(pathToAdd);
}
});

await writeJson(packageJsonPath, rootPackageJson);
};

/**
* @param packageJsonPath {String}
* @param pathsToRemove {String[]}
*/
const removeWorkspaceToRootPackageJson = async (packageJsonPath, pathsToRemove) => {
const rootPackageJson = await loadJson(packageJsonPath);

pathsToRemove.forEach(pathToRemove => {
// Remove it from workspaces packages if present
const index = rootPackageJson.workspaces.packages.indexOf(pathToRemove);
if (index !== -1) {
rootPackageJson.workspaces.packages.splice(index, 1);
}
});

await writeJson(packageJsonPath, rootPackageJson);
};

module.exports = {
insertImport,
addPackagesToDependencies,
Expand All @@ -319,5 +356,7 @@ module.exports = {
createMorphProject,
prettierFormat,
yarnInstall,
addImportsToSource
addImportsToSource,
addWorkspaceToRootPackageJson,
removeWorkspaceToRootPackageJson
};

0 comments on commit 9e7f782

Please sign in to comment.