Skip to content

Commit

Permalink
Merge 79e07ad into d0571ed
Browse files Browse the repository at this point in the history
  • Loading branch information
amitgilad3 authored Jan 11, 2018
2 parents d0571ed + 79e07ad commit 437aa94
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 7 deletions.
59 changes: 58 additions & 1 deletion e2e/commands/import.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,64 @@ describe('bit import', function () {
});
});
});

describe('import component with dependencies with yarn workspaces', () => {
let dependencies;
before(() => {
helper.setNewLocalAndRemoteScopes();
dependencies = path.join(
helper.localScopePath,
'components',
'.dependencies',
'global',
'simple',
helper.remoteScope,
'0.0.1'
);
helper.addNpmPackage('lodash.isboolean', '3.0.0');
const simpleFixture = 'import a from "lodash.isboolean"; ';
helper.createFile('global', 'simple.js', simpleFixture);
helper.addComponentWithOptions('global/simple.js', { i: 'global/simple' });
helper.commitComponent('simple');
helper.exportComponent('simple');
helper.addNpmPackage('lodash.isstring', '4.0.0');
const withDepsFixture = 'import a from "./global/simple.js"; import c from "lodash.isstring"';
helper.createFile('', 'with-deps.js', withDepsFixture);
helper.addComponentWithOptions('with-deps.js', { i: 'comp/with-deps' });
helper.commitAllComponents();
helper.exportComponent('comp/with-deps');
helper.reInitLocalScope();
helper.createPackageJson();
helper.addRemoteScope(helper.remoteScopePath);
helper.changePackageManagerToYarn();
helper.importComponent('comp/with-deps');
});
it('should install component dependencie as separate packages with yarn workspaces', () => {
expect(dependencies).to.be.a.directory('should not be empty').and.not.empty;
});
it('Should contain yarn lock file', () => {
expect(path.join(helper.localScopePath, 'yarn.lock')).to.be.a.file('no yarn lock file');
});
it('should install global/simple package dependencies with yarn', () => {
expect(path.join(helper.localScopePath, 'node_modules')).to.be.a.directory('should not be empty').and.not.empty;
expect(path.join(helper.localScopePath, 'node_modules', 'lodash.isboolean')).to.be.a.directory(
'should contain lodash.isboolean'
).and.not.empty;
});
it('should contain workspaces array in package.json and private true', () => {
const pkgJson = helper.readPackageJson(helper.localScopePath);
expect(pkgJson.workspaces).to.include('components/.dependencies/*/*/*/*', 'components/*/*');
expect(pkgJson.private).to.be.true;
});
it('component dep should be install as npm package', () => {
const modulePath = path.join(
helper.localScopePath,
'node_modules',
'@bit',
`${helper.remoteScope}.global.simple`
);
expect(modulePath).to.be.a.directory('should contain component dep as npm package dep').and.not.empty;
});
});
describe.skip('Import compiler', () => {
before(() => {
helper.reInitLocalScope();
Expand Down
18 changes: 18 additions & 0 deletions e2e/e2e-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ export default class Helper {
return fs.readJSONSync(bitJsonPath) || {};
}

createPackageJson(
name: string = 'test',
version: string = '0.0.1',
packageJsonPath: string = path.join(this.localScopePath, 'package.json')
) {
const packageJson = { name, version };
fs.writeJSONSync(packageJsonPath, packageJson);
}
changePackageManagerToYarn(
withWorkspaces: boolean = true,
bitJsonPath: string = path.join(this.localScopePath, 'bit.json')
) {
const bitJson = this.readBitJson(bitJsonPath);
bitJson.packageManager = 'yarn';
bitJson.manageWorkspaces = withWorkspaces;
this.writeBitJson(bitJson);
}

readPackageJson(packageJsonFolder: string = this.localScopePath) {
const packageJsonPath = path.join(packageJsonFolder, 'package.json');
return fs.readJSONSync(packageJsonPath) || {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"dependencies": {
"array-difference": "^0.0.1",
"babel-runtime": "^6.23.0",
"bit-javascript": "0.10.8-dev.10",
"bit-javascript": "0.10.8-dev.11",
"buffer-from": "^0.1.1",
"chalk": "^2.1.0",
"chokidar": "^1.7.0",
Expand Down
4 changes: 4 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ export const BIT_CACHE_DIRNAME = 'cache';

export const LATEST_TESTED_MARK = '*';

export const BIT_DEPENDECIES_REGEX = '/*/*/*/*';

export const YARN_WORKSPACES_REGEX = '*';

export const SCOPE_JSON = 'scope.json';

export const DEFAULT_RESOLVER = () => '';
Expand Down
15 changes: 11 additions & 4 deletions src/consumer/bit-json/consumer-bit-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type consumerBitJsonProps = {
packageManager?: 'npm' | 'yarn',
packageManagerArgs?: string[],
packageManagerProcessOptions?: Object,
useWorkspaces?: boolean
useWorkspaces?: boolean,
manageWorkspaces?: boolean
};

export default class ConsumerBitJson extends AbstractBitJson {
Expand All @@ -51,6 +52,7 @@ export default class ConsumerBitJson extends AbstractBitJson {
packageManagerArgs: ?(string[]); // package manager client to use
packageManagerProcessOptions: ?Object; // package manager process options
useWorkspaces: boolean; // Enables integration with Yarn Workspaces
manageWorkspaces: boolean; // manage workspaces with yarn

constructor({
impl,
Expand All @@ -69,7 +71,8 @@ export default class ConsumerBitJson extends AbstractBitJson {
packageManager = DEFAULT_PACKAGE_MANAGER,
packageManagerArgs,
packageManagerProcessOptions,
useWorkspaces = false
useWorkspaces = false,
manageWorkspaces = true
}: consumerBitJsonProps) {
super({ impl, spec, compiler, tester, dependencies, lang, bindingPrefix, extensions });
this.distTarget = distTarget;
Expand All @@ -81,6 +84,7 @@ export default class ConsumerBitJson extends AbstractBitJson {
this.packageManagerArgs = packageManagerArgs;
this.packageManagerProcessOptions = packageManagerProcessOptions;
this.useWorkspaces = useWorkspaces;
this.manageWorkspaces = manageWorkspaces;
}

toPlainObject() {
Expand All @@ -92,7 +96,8 @@ export default class ConsumerBitJson extends AbstractBitJson {
packageManager: this.packageManager,
packageManagerArgs: this.packageManagerArgs,
packageManagerProcessOptions: this.packageManagerProcessOptions,
useWorkspaces: this.useWorkspaces
useWorkspaces: this.useWorkspaces,
manageWorkspaces: this.manageWorkspaces
});
if (this.distEntry || this.distTarget) {
const dist = {};
Expand Down Expand Up @@ -145,7 +150,8 @@ export default class ConsumerBitJson extends AbstractBitJson {
packageManager,
packageManagerArgs,
packageManagerProcessOptions,
useWorkspaces
useWorkspaces,
manageWorkspaces
} = object;

return new ConsumerBitJson({
Expand All @@ -164,6 +170,7 @@ export default class ConsumerBitJson extends AbstractBitJson {
packageManagerArgs,
packageManagerProcessOptions,
useWorkspaces,
manageWorkspaces,
distTarget: R.propOr(undefined, 'target', dist),
distEntry: R.propOr(undefined, 'entry', dist)
});
Expand Down
33 changes: 32 additions & 1 deletion src/consumer/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
NODE_PATH_SEPARATOR,
LATEST_BIT_VERSION,
CFG_REGISTRY_DOMAIN_PREFIX,
DEFAULT_REGISTRY_DOMAIN_PREFIX
DEFAULT_REGISTRY_DOMAIN_PREFIX,
DEFAULT_SEPARATOR,
BIT_DEPENDECIES_REGEX,
YARN_WORKSPACES_REGEX
} from '../constants';
import { Scope, ComponentWithDependencies } from '../scope';
import migratonManifest from './migrations/consumer-migrator-manifest';
Expand Down Expand Up @@ -480,6 +483,30 @@ export default class Consumer {
});
}

/**
* Adds workspace array to package.json - only if user wants to work with yarn workspaces
*/
async addWorkspacesToPackageJson() {
const driver = await this.driver.getDriver();
const PackageJson = driver.PackageJson;
const pkg = await PackageJson.load(this.getPath(), false);
if (pkg) {
const workSpaces = pkg.workspaces || [];
workSpaces.push(this.bitJson.dependenciesDirectory + BIT_DEPENDECIES_REGEX);
const formatedComponentsPath = format(this.bitJson.componentsDefaultDirectory, {
name: YARN_WORKSPACES_REGEX,
scope: YARN_WORKSPACES_REGEX,
namespace: YARN_WORKSPACES_REGEX
});
const formatedRegexPath = formatedComponentsPath
.split(DEFAULT_SEPARATOR)
.map(part => (R.contains(YARN_WORKSPACES_REGEX, part) ? YARN_WORKSPACES_REGEX : part))
.join(DEFAULT_SEPARATOR);
workSpaces.push(formatedRegexPath);
pkg.workspaces = R.uniq(workSpaces);
await pkg.write({ override: true });
}
}
/**
* write the components into '/components' dir (or according to the bit.map) and its dependencies in the
* '/components/.dependencies' dir. Both directories are configurable in bit.json
Expand Down Expand Up @@ -603,6 +630,10 @@ export default class Consumer {
}
});
}

// add workspaces if flag is true
if (this.bitJson.manageWorkspaces) await this.addWorkspacesToPackageJson();

await bitMap.write();
if (installNpmPackages) await this.installNpmPackages(componentsWithDependencies, verbose);
await this.addComponentsToRootPackageJson(writtenComponents, bitMap);
Expand Down

0 comments on commit 437aa94

Please sign in to comment.