Skip to content
This repository was archived by the owner on Dec 4, 2022. It is now read-only.

Commit 40e8226

Browse files
amitgilad3GiladShoham
authored andcommitted
Workspaces support (#43)
1 parent bde4c15 commit 40e8226

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/package-json/package-json.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,27 @@ export default class PackageJson {
179179
packageJson.dependencies = Object.assign({}, packageJson.dependencies, convertComponentsToValidPackageNames(registryPrefix, components));
180180
await saveRawObject(packageJson);
181181
}
182+
/*
183+
* For an existing package.json file of the root project, we don't want to do any change, other than what needed.
184+
* That's why this method doesn't use the 'load' and 'write' methods of this class. Otherwise, it'd write only the
185+
* PackageJsonPropsNames attributes.
186+
* Also, in case there is no package.json file in this project, it generates a new one with only the 'dependencies'
187+
* adds workspaces with private flag if dosent exist.
188+
*/
189+
static async addWorkspacesToPackageJson(rootDir: string, componentsDefaultDirectory: string, dependenciesDirectory: string, customImportPath: ?string ) {
190+
const getRawObject = () => fs.readJson(composePath(rootDir));
191+
const saveRawObject = obj => fs.outputJSON(composePath(rootDir), obj, { spaces: 2 });
192+
const getPackageJson = async () => {
193+
const exist = PackageJson.hasExisting(rootDir);
194+
return exist ? getRawObject() : { workspaces: {}, private: true };
195+
};
196+
const pkg = await getPackageJson();
197+
pkg.private = pkg.private || true;
198+
const workSpaces = pkg.workspaces || [];
199+
workSpaces.push(dependenciesDirectory);
200+
workSpaces.push(componentsDefaultDirectory);
201+
if(customImportPath) workSpaces.push(customImportPath);
202+
pkg.workspaces = R.uniq(workSpaces);
203+
await saveRawObject(pkg);
204+
}
182205
}

0 commit comments

Comments
 (0)