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

fix error when Yarn workspaces uses nohoist #121

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/).
and this project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased]
## [2.1.3-dev.3] - 2019-10-22

- [#2079](https://github.com/teambit/bit/issues/2079) fix error when Yarn workspaces uses nohoist

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

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bit-javascript",
"version": "2.1.3-dev.2",
"version": "2.1.3-dev.3",
"scripts": {
"lint": "tsc && eslint \"src/**/*.ts\"",
"lint-circle": "eslint \"src/**/*.ts\" --format junit -o junit/eslint-results.xml",
Expand Down
67 changes: 54 additions & 13 deletions src/package-json/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function composePath(componentRootFolder: string) {
return path.join(componentRootFolder, PACKAGE_JSON);
}
function convertComponentsIdToValidPackageName(registryPrefix: string, id: string): string {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
return `${registryPrefix}/${id.replace(/\//g, '.')}`;
}
function convertComponentsToValidPackageNames(
Expand All @@ -21,7 +20,6 @@ function convertComponentsToValidPackageNames(
if (R.isEmpty(bitDependencies) || R.isNil(bitDependencies)) return obj;
Object.keys(bitDependencies).forEach(key => {
const name = convertComponentsIdToValidPackageName(registryPrefix, key);
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
obj[name] = bitDependencies[key];
});
return obj;
Expand Down Expand Up @@ -51,7 +49,7 @@ export type PackageJsonProps = {
peerDependencies?: Record<string, any>;
license?: string;
scripts?: Record<string, any>;
workspaces: string[];
workspaces?: string[];
private?: boolean;
};

Expand Down Expand Up @@ -129,8 +127,7 @@ export default class PackageJson {
);
}

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

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

Expand All @@ -157,7 +153,6 @@ export default class PackageJson {
}

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

Expand Down Expand Up @@ -242,8 +237,11 @@ export default class PackageJson {
* Also, in case there is no package.json file in this project, it generates a new one with only the 'dependencies'
* attribute. Nothing more, nothing less.
*/
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
static async addComponentsIntoExistingPackageJson(rootDir: string, components: Array, registryPrefix: string) {
static async addComponentsIntoExistingPackageJson(
rootDir: string,
components: Record<string, any>,
registryPrefix: string
) {
const packageJson = (await PackageJson.getPackageJson(rootDir)) || { dependencies: {} };
packageJson.dependencies = Object.assign(
{},
Expand All @@ -266,11 +264,12 @@ export default class PackageJson {
customImportPath: string | null | undefined
) {
const pkg = (await PackageJson.getPackageJson(rootDir)) || {};
const workSpaces = pkg.workspaces || [];
const workSpaces = PackageJson.extractWorkspacesPackages(pkg) || [];
workSpaces.push(dependenciesDirectory);
workSpaces.push(componentsDefaultDirectory);
if (customImportPath) workSpaces.push(customImportPath);
pkg.workspaces = R.uniq(workSpaces);
if (!pkg.workspaces) pkg.workspaces = [];
this.updateWorkspacesPackages(pkg, R.uniq(workSpaces));
pkg.private = !!pkg.workspaces;
await PackageJson.saveRawObject(rootDir, pkg);
}
Expand All @@ -280,8 +279,10 @@ export default class PackageJson {
*/
static async removeComponentsFromWorkspaces(rootDir: string, pathsTOoRemove: string[]) {
const pkg = (await PackageJson.getPackageJson(rootDir)) || {};
const workSpaces = pkg.workspaces || [];
pkg.workspaces = workSpaces.filter(folder => !pathsTOoRemove.includes(folder));
const workspaces = this.extractWorkspacesPackages(pkg);
if (!workspaces) return;
const updatedWorkspaces = workspaces.filter(folder => !pathsTOoRemove.includes(folder));
this.updateWorkspacesPackages(pkg, updatedWorkspaces);
await PackageJson.saveRawObject(rootDir, pkg);
}

Expand All @@ -297,4 +298,44 @@ export default class PackageJson {
await PackageJson.saveRawObject(rootDir, pkg);
}
}

static extractWorkspacesPackages(packageJson: { [k: string]: any }): string[] | null {
if (!packageJson.workspaces) return null;
this.throwForInvalidWorkspacesConfig(packageJson);
if (Array.isArray(packageJson.workspaces)) {
return packageJson.workspaces;
}
if (Array.isArray(packageJson.workspaces.packages)) {
return packageJson.workspaces.packages;
}
return null;
}

static updateWorkspacesPackages(packageJson, workspacesPackages): void {
if (!packageJson.workspaces) return;
this.throwForInvalidWorkspacesConfig(packageJson);
if (Array.isArray(packageJson.workspaces)) {
packageJson.workspaces = workspacesPackages;
}
if (Array.isArray(packageJson.workspaces.packages)) {
packageJson.workspaces.packages = workspacesPackages;
}
}

/**
* according to Yarn Git repo, the workspaces type configured as the following
* `workspaces?: Array<string> | WorkspacesConfig`
* and `WorkspacesConfig` is:
* `export type WorkspacesConfig = { packages?: Array<string>, nohoist?: Array<string> };`
* see https://github.com/yarnpkg/yarn/blob/master/src/types.js
*/
static throwForInvalidWorkspacesConfig(packageJson) {
if (!packageJson.workspaces) return;
if (
typeof packageJson.workspaces !== 'object' ||
(!Array.isArray(packageJson.workspaces) && !Array.isArray(packageJson.workspaces.packages))
) {
throw new Error('workspaces property does not have the correct format, please refer to Yarn documentation');
}
}
}