Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 25ec5fb into 22e779a
Browse files Browse the repository at this point in the history
imsnif authored Dec 5, 2019
2 parents 22e779a + 25ec5fb commit 7e2351d
Showing 11 changed files with 622 additions and 147 deletions.
51 changes: 34 additions & 17 deletions components/core/capsule/capsule.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import State from "./state";
import State from './state';
import Container from './container';
// @ts-ignore
import { Volume } from "memfs/lib/volume";
import Console from "./console";
import librarian from 'librarian';
import { Volume } from 'memfs/lib/volume';
import Console from './console';
// @ts-ignore
import { Union } from 'unionfs';
import { ContainerFS } from './container';
import { Exec } from "./container";
import { Exec } from './container';

import loader from '../../../src/cli/loader'; // TODO: better (have the capsule accept the loader as an arg?)

export class ContainerFactoryOptions {
image: string = '';
config: object = {};
};
}

export default class Capsule {
constructor(
@@ -41,6 +44,8 @@ export default class Capsule {
*/
static image = 'ubuntu';

componentName?: string;

/**
* default capsule config.
*/
@@ -63,15 +68,29 @@ export default class Capsule {
this.container.on(event, fn);
}

updateFs(fs: {[path: string]: string}, fn: Function): void {
Object.keys(fs).forEach((path) => {
updateFs(fs: { [path: string]: string }, fn: Function): void {
Object.keys(fs).forEach(path => {
// @ts-ignorex
this.fs.writeFile(path, fs[path], () => {
if (Object.keys(fs).length === 1) fn();
});
});
}

async execNode(executable: string, args: any) {
// TODO: better
const loaderPrefix = this.componentName ? `isolating ${this.componentName}` : '';
const log = message => (this.componentName ? loader.setText(`${loaderPrefix}: ${message}`) : {});
const { patchFileSystem } = librarian.api();
const onScriptRun = () =>
this.componentName ? loader.setText(`running build for ${this.componentName} in an isolated environment`) : {}; // TODO: do this from the compiler/tester so we can customize the message
await patchFileSystem(executable, { args, cwd: this.container.path, log, onScriptRun });
}

setComponentName(componentName: string) {
this.componentName = componentName;
}

outputFile(file: string, data: any, options: Object): Promise<any> {
return this.container.outputFile(file, data, options);
}
@@ -89,7 +108,7 @@ export default class Capsule {
}

resume() {
return this.container.resume()
return this.container.resume();
}

stop() {
@@ -117,21 +136,19 @@ export default class Capsule {

private static buildFs(memFs: Volume, containerFs: ContainerFS): Volume {
const fs = new Union();
fs
.use(memFs)
.use(containerFs);
fs.use(memFs).use(containerFs);

return fs;
}

static async create<T extends Capsule>(
containerFactory: (options: ContainerFactoryOptions) => Promise<Container>,
volume: Volume = new Volume(),
initialState: State = new State(),
console: Console = new Console()
): Promise<T> {
containerFactory: (options: ContainerFactoryOptions) => Promise<Container>,
volume: Volume = new Volume(),
initialState: State = new State(),
console: Console = new Console()
): Promise<T> {
const container = await containerFactory({ image: this.image, config: this.config });
const fs = await ContainerFS.fromJSON(container, {});
return (new this(container, this.buildFs(volume, fs), console, initialState) as T);
return new this(container, this.buildFs(volume, fs), console, initialState) as T;
}
}
25 changes: 13 additions & 12 deletions components/core/capsule/container/container.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ export default interface Container {
* container id
*/
id: string;
path: string;

/**
* execute a command on the container
@@ -20,13 +21,13 @@ export default interface Container {
/**
* put a file or a directory to the container.
*/
put(files: {[path: string]: string}, options: { overwrite?: boolean, path: string }): Promise<void>;
put(files: { [path: string]: string }, options: { overwrite?: boolean; path: string }): Promise<void>;

on(event: string, fn: (data: any) => void): void;

on(event: string, fn: (data: any) => void): void;

/**
* start a container.
*/
*/
start(): Promise<void>;

/**
@@ -69,37 +70,37 @@ export type ContainerStatus = {
/**
* array of open container ports
*/
ports: number[],
ports: number[];

/**
* container host
*/
host: string,
host: string;
};

export type CommitOptions = {
/**
* repository name for the created image
*/
repo: string,
repo: string;

/**
* tag name for the create image
*/
tag: string,
tag: string;

/**
* commit message
*/
comment: string,
comment: string;

/**
* author of the image.
*/
author: string,
author: string;

/**
* whether to pause the container before committing
*/
pause: boolean,
pause: boolean;
};
2 changes: 1 addition & 1 deletion e2e/commands/isolate.e2e.1.ts
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ describe('run bit isolate', function() {
describe('with the same parameters as pack is using', () => {
let isolatePath;
before(() => {
isolatePath = helper.command.isolateComponent('bar/foo', '-olw');
isolatePath = helper.command.isolateComponent('bar/foo', '-owls');
});
it('should be able to generate the links correctly and require the dependencies', () => {
const appJsFixture = `const barFoo = require('./');
2 changes: 1 addition & 1 deletion e2e/functionalities/custom-module-resolutions.e2e.2.ts
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ describe('custom module resolutions', function() {
describe('importing the component using isolated environment', () => {
let isolatePath;
before(() => {
isolatePath = helper.command.isolateComponent('bar/foo', '-olw');
isolatePath = helper.command.isolateComponent('bar/foo', '-olws');
});
it('should be able to generate the links correctly and require the dependencies', () => {
const appJsFixture = `const barFoo = require('./');
Loading

0 comments on commit 7e2351d

Please sign in to comment.