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

Commit 01eef57

Browse files
authored
fix error when Yarn workspaces uses nohoist (#121)
1 parent a436ba6 commit 01eef57

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/).
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8-
## [unreleased]
8+
## [2.1.3-dev.3] - 2019-10-22
9+
10+
- [#2079](https://github.com/teambit/bit/issues/2079) fix error when Yarn workspaces uses nohoist
911

1012
## [2.1.3-dev.2] - 2019-10-17
1113

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bit-javascript",
3-
"version": "2.1.3-dev.2",
3+
"version": "2.1.3-dev.3",
44
"scripts": {
55
"lint": "tsc && eslint \"src/**/*.ts\"",
66
"lint-circle": "eslint \"src/**/*.ts\" --format junit -o junit/eslint-results.xml",

src/package-json/package-json.ts

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ function composePath(componentRootFolder: string) {
1010
return path.join(componentRootFolder, PACKAGE_JSON);
1111
}
1212
function convertComponentsIdToValidPackageName(registryPrefix: string, id: string): string {
13-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
1413
return `${registryPrefix}/${id.replace(/\//g, '.')}`;
1514
}
1615
function convertComponentsToValidPackageNames(
@@ -21,7 +20,6 @@ function convertComponentsToValidPackageNames(
2120
if (R.isEmpty(bitDependencies) || R.isNil(bitDependencies)) return obj;
2221
Object.keys(bitDependencies).forEach(key => {
2322
const name = convertComponentsIdToValidPackageName(registryPrefix, key);
24-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
2523
obj[name] = bitDependencies[key];
2624
});
2725
return obj;
@@ -51,7 +49,7 @@ export type PackageJsonProps = {
5149
peerDependencies?: Record<string, any>;
5250
license?: string;
5351
scripts?: Record<string, any>;
54-
workspaces: string[];
52+
workspaces?: string[];
5553
private?: boolean;
5654
};
5755

@@ -129,8 +127,7 @@ export default class PackageJson {
129127
);
130128
}
131129

132-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
133-
static hasExisting(componentRootFolder: string, throws? = false): boolean {
130+
static hasExisting(componentRootFolder: string, throws = false): boolean {
134131
const packageJsonPath = composePath(componentRootFolder);
135132
const exists = fs.pathExistsSync(packageJsonPath);
136133
if (!exists && throws) {
@@ -148,7 +145,6 @@ export default class PackageJson {
148145
}
149146

150147
static create(componentRootFolder: string): PackageJson {
151-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
152148
return new PackageJson(componentRootFolder, {});
153149
}
154150

@@ -157,7 +153,6 @@ export default class PackageJson {
157153
}
158154

159155
static fromPlainObject(componentRootFolder: string, object: Record<string, any>) {
160-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
161156
return new PackageJson(componentRootFolder, object);
162157
}
163158

@@ -242,8 +237,11 @@ export default class PackageJson {
242237
* Also, in case there is no package.json file in this project, it generates a new one with only the 'dependencies'
243238
* attribute. Nothing more, nothing less.
244239
*/
245-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
246-
static async addComponentsIntoExistingPackageJson(rootDir: string, components: Array, registryPrefix: string) {
240+
static async addComponentsIntoExistingPackageJson(
241+
rootDir: string,
242+
components: Record<string, any>,
243+
registryPrefix: string
244+
) {
247245
const packageJson = (await PackageJson.getPackageJson(rootDir)) || { dependencies: {} };
248246
packageJson.dependencies = Object.assign(
249247
{},
@@ -266,11 +264,12 @@ export default class PackageJson {
266264
customImportPath: string | null | undefined
267265
) {
268266
const pkg = (await PackageJson.getPackageJson(rootDir)) || {};
269-
const workSpaces = pkg.workspaces || [];
267+
const workSpaces = PackageJson.extractWorkspacesPackages(pkg) || [];
270268
workSpaces.push(dependenciesDirectory);
271269
workSpaces.push(componentsDefaultDirectory);
272270
if (customImportPath) workSpaces.push(customImportPath);
273-
pkg.workspaces = R.uniq(workSpaces);
271+
if (!pkg.workspaces) pkg.workspaces = [];
272+
this.updateWorkspacesPackages(pkg, R.uniq(workSpaces));
274273
pkg.private = !!pkg.workspaces;
275274
await PackageJson.saveRawObject(rootDir, pkg);
276275
}
@@ -280,8 +279,10 @@ export default class PackageJson {
280279
*/
281280
static async removeComponentsFromWorkspaces(rootDir: string, pathsTOoRemove: string[]) {
282281
const pkg = (await PackageJson.getPackageJson(rootDir)) || {};
283-
const workSpaces = pkg.workspaces || [];
284-
pkg.workspaces = workSpaces.filter(folder => !pathsTOoRemove.includes(folder));
282+
const workspaces = this.extractWorkspacesPackages(pkg);
283+
if (!workspaces) return;
284+
const updatedWorkspaces = workspaces.filter(folder => !pathsTOoRemove.includes(folder));
285+
this.updateWorkspacesPackages(pkg, updatedWorkspaces);
285286
await PackageJson.saveRawObject(rootDir, pkg);
286287
}
287288

@@ -297,4 +298,44 @@ export default class PackageJson {
297298
await PackageJson.saveRawObject(rootDir, pkg);
298299
}
299300
}
301+
302+
static extractWorkspacesPackages(packageJson: { [k: string]: any }): string[] | null {
303+
if (!packageJson.workspaces) return null;
304+
this.throwForInvalidWorkspacesConfig(packageJson);
305+
if (Array.isArray(packageJson.workspaces)) {
306+
return packageJson.workspaces;
307+
}
308+
if (Array.isArray(packageJson.workspaces.packages)) {
309+
return packageJson.workspaces.packages;
310+
}
311+
return null;
312+
}
313+
314+
static updateWorkspacesPackages(packageJson, workspacesPackages): void {
315+
if (!packageJson.workspaces) return;
316+
this.throwForInvalidWorkspacesConfig(packageJson);
317+
if (Array.isArray(packageJson.workspaces)) {
318+
packageJson.workspaces = workspacesPackages;
319+
}
320+
if (Array.isArray(packageJson.workspaces.packages)) {
321+
packageJson.workspaces.packages = workspacesPackages;
322+
}
323+
}
324+
325+
/**
326+
* according to Yarn Git repo, the workspaces type configured as the following
327+
* `workspaces?: Array<string> | WorkspacesConfig`
328+
* and `WorkspacesConfig` is:
329+
* `export type WorkspacesConfig = { packages?: Array<string>, nohoist?: Array<string> };`
330+
* see https://github.com/yarnpkg/yarn/blob/master/src/types.js
331+
*/
332+
static throwForInvalidWorkspacesConfig(packageJson) {
333+
if (!packageJson.workspaces) return;
334+
if (
335+
typeof packageJson.workspaces !== 'object' ||
336+
(!Array.isArray(packageJson.workspaces) && !Array.isArray(packageJson.workspaces.packages))
337+
) {
338+
throw new Error('workspaces property does not have the correct format, please refer to Yarn documentation');
339+
}
340+
}
300341
}

0 commit comments

Comments
 (0)