Skip to content

Commit

Permalink
Adam/dispose and expose feature (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Yahid authored Oct 1, 2020
1 parent fec302c commit 3e315fb
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 93 deletions.
2 changes: 1 addition & 1 deletion examples/electron-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@example/electron-app",
"version": "15.0.0",
"version": "15.1.0-rc.0",
"main": "index.js",
"scripts": {
"start": "electron ."
Expand Down
2 changes: 1 addition & 1 deletion examples/file-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@example/file-server",
"version": "15.0.0",
"version": "15.1.0-rc.0",
"scripts": {
"start": "engine start",
"test": "yarn test:ix && yarn test:unit",
Expand Down
2 changes: 1 addition & 1 deletion examples/playground/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@example/playground",
"version": "15.0.0",
"version": "15.1.0-rc.0",
"scripts": {
"start": "engine start"
},
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "15.0.0",
"version": "15.1.0-rc.0",
"npmClient": "yarn",
"useWorkspaces": true
}
4 changes: 2 additions & 2 deletions packages/core-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wixc3/engine-core-node",
"version": "15.0.0",
"version": "15.1.0-rc.0",
"main": "cjs/index.js",
"types": "cjs/index.d.ts",
"scripts": {
Expand All @@ -12,7 +12,7 @@
"prepack": "yarn build"
},
"dependencies": {
"@wixc3/engine-core": "^15.0.0",
"@wixc3/engine-core": "^15.1.0-rc.0",
"create-listening-server": "^1.0.0",
"express": "^4.17.1",
"socket.io": "^2.3.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wixc3/engine-core",
"version": "15.0.0",
"version": "15.1.0-rc.0",
"main": "cjs/index.js",
"types": "cjs/index.d.ts",
"scripts": {
Expand Down
14 changes: 7 additions & 7 deletions packages/engineer/feature/dev-server.dev-server.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ const attachWSHost = (socketServer: SocketIO.Server, envName: string, communicat

devServerFeature.setup(
devServerEnv,
({ run, devServerConfig, engineerWebpackConfigs, serverListeningHandlerSlot }, { COM: { communication } }) => {
(
{ run, devServerConfig, engineerWebpackConfigs, serverListeningHandlerSlot, onDispose },
{ COM: { communication } }
) => {
const {
httpServerPort,
featureName,
Expand All @@ -57,12 +60,7 @@ devServerFeature.setup(
defaultRuntimeOptions,
outputPath,
} = devServerConfig;

const appStartArgs = outputPath
? { basePath, nodeEnvironmentsMode, outputPath }
: { basePath, nodeEnvironmentsMode };

const application = new TargetApplication(appStartArgs);
const application = new TargetApplication({ basePath, nodeEnvironmentsMode, outputPath });
const disposables = new Set<() => unknown>();

// Extract these into a service
Expand All @@ -76,6 +74,8 @@ devServerFeature.setup(
disposables.clear();
};

onDispose(close);

run(async () => {
// Should engine config be part of the dev experience of the engine????
const engineConfig = await application.getEngineConfig();
Expand Down
8 changes: 4 additions & 4 deletions packages/engineer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wixc3/engineer",
"version": "15.0.0",
"version": "15.1.0-rc.0",
"license": "MIT",
"main": "cjs/src/index.js",
"types": "cjs/src/index.d.ts",
Expand All @@ -18,9 +18,9 @@
},
"dependencies": {
"@file-services/node": "^4.1.2",
"@wixc3/engine-core": "^15.0.0",
"@wixc3/engine-core-node": "^15.0.0",
"@wixc3/engine-scripts": "^15.0.0",
"@wixc3/engine-core": "^15.1.0-rc.0",
"@wixc3/engine-core-node": "^15.1.0-rc.0",
"@wixc3/engine-scripts": "^15.1.0-rc.0",
"commander": "^6.1.0",
"open": "^7.3.0",
"react": "^16.13.1",
Expand Down
20 changes: 15 additions & 5 deletions packages/engineer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
runNodeEnvironment,
TopLevelConfigProvider,
} from '@wixc3/engine-scripts';
import { RuntimeEngine, BaseHost, TopLevelConfig } from '@wixc3/engine-core';
import { RuntimeEngine, BaseHost, TopLevelConfig, RuntimeFeature } from '@wixc3/engine-core';

import devServerFeature, { devServerEnv } from '../feature/dev-server.feature';
import guiFeature from '../feature/gui.feature';

export interface IStartOptions {
publicPath: string;
publicPath?: string;
targetApplicationPath: string;
outputPath?: string;
featureName?: string;
Expand All @@ -27,16 +27,17 @@ export interface IStartOptions {
autoLaunch?: boolean;
engineerEntry?: string;
overrideConfig?: TopLevelConfig | TopLevelConfigProvider;
inspect?: boolean;
}

export function startDevServer({
export async function startDevServer({
featureName,
configName,
httpServerPort = 3000,
singleRun,
singleFeature,
pathsToRequire = [],
publicPath,
publicPath = '/',
mode = 'development',
title,
publicConfigsRoute = 'configs/',
Expand All @@ -45,9 +46,11 @@ export function startDevServer({
engineerEntry = 'engineer/gui',
overrideConfig = [],
outputPath,
inspect,
}: IStartOptions): Promise<{
dispose: () => Promise<void>;
engine: RuntimeEngine;
devServerFeature: RuntimeFeature;
}> {
const basePath = resolve(__dirname, '../feature');
const featurePaths = fs.findFilesSync(basePath, {
Expand All @@ -57,7 +60,7 @@ export function startDevServer({

const features = loadFeaturesFromPaths(new Set(featurePaths), new Set([basePath]), fs).features;

return runNodeEnvironment({
const { engine, dispose } = await runNodeEnvironment({
featureName: engineerEntry,
features: [...features],
name: devServerEnv.env,
Expand All @@ -79,6 +82,7 @@ export function startDevServer({
basePath: targetApplicationPath,
overrideConfig,
outputPath,
inspect,
},
}),
guiFeature.use({
Expand All @@ -88,6 +92,12 @@ export function startDevServer({
}),
],
});

return {
engine,
dispose,
devServerFeature: engine.features.get(features.get('engineer/dev-server')!.exportedFeature) as RuntimeFeature,
};
}

function preRequire(pathsToRequire: string[], basePath: string) {
Expand Down
56 changes: 18 additions & 38 deletions packages/engineer/test/dev-server.unit.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { expect } from 'chai';
import type { Page } from 'puppeteer';
import { createBrowserProvider } from '@wixc3/engine-test-kit';
import devServerFeature, { devServerEnv } from '../feature/dev-server.feature';
import fs from '@file-services/node';
import { createDisposables, BaseHost, RuntimeFeature, TopLevelConfig, RuntimeEngine } from '@wixc3/engine-core';
import type { Page } from 'puppeteer';
import { expect } from 'chai';
import {
runNodeEnvironment,
loadFeaturesFromPackages,
resolvePackages,
TopLevelConfigProvider,
} from '@wixc3/engine-scripts';
import { createDisposables, RuntimeFeature, TopLevelConfig, RuntimeEngine } from '@wixc3/engine-core';
import type { TopLevelConfigProvider } from '@wixc3/engine-scripts';
import { startDevServer } from '../src/utils';

const engineFeatureFixturePath = fs.join(__dirname, '../fixtures/engine-feature');
const multiFeatureFixturePath = fs.join(__dirname, '../fixtures/engine-multi-feature');
Expand Down Expand Up @@ -51,43 +46,28 @@ describe('engineer:dev-server', function () {
runtimeFeature: RuntimeFeature | undefined;
config: { featureName: string | undefined; port: number };
}> => {
const { dispose, engine } = await runNodeEnvironment({
featureName: 'engineer/dev-server',
features: [...loadFeaturesFromPackages(resolvePackages('../'), fs).features],
name: devServerEnv.env,
type: 'node',
host: new BaseHost(),
config: [
devServerFeature.use({
devServerConfig: {
basePath,
httpServerPort: port,
singleRun: true,
featureName,
autoLaunch,
configName,
inspect,
overrideConfig,
outputPath,
},
}),
],
const { dispose, engine, devServerFeature } = await startDevServer({
engineerEntry: 'engineer/dev-server',
targetApplicationPath: basePath,
httpServerPort: port,
featureName,
autoLaunch,
configName,
inspect,
overrideConfig,
outputPath,
singleRun: true,
});

const runtimeFeature: RuntimeFeature | undefined = engine.features.get(devServerFeature);
runtimeFeature?.addOnDisposeHandler(async () => {
await runtimeFeature.api.devServerActions.close();
}, devServerEnv.env);

const runningPort: number = await new Promise((resolve) => {
runtimeFeature!.api.serverListeningHandlerSlot.register(({ port }: { port: number }) => {
devServerFeature.api.serverListeningHandlerSlot.register(({ port }: { port: number }) => {
resolve(port);
});
});

disposables.add(() => dispose());

return { dispose, engine, runtimeFeature, config: { featureName, port: runningPort } };
return { dispose, engine, runtimeFeature: devServerFeature, config: { featureName, port: runningPort } };
};

const loadPage = async (url: string) => {
Expand Down
33 changes: 6 additions & 27 deletions packages/engineer/test/gui.unit.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { createDisposables, RuntimeFeature, BaseHost } from '@wixc3/engine-core';
import { createDisposables, RuntimeFeature } from '@wixc3/engine-core';
import { createBrowserProvider } from '@wixc3/engine-test-kit';
import { runNodeEnvironment, loadFeaturesFromPackages, resolvePackages } from '@wixc3/engine-scripts';
import fs from '@file-services/node';
import devServerFeature, { devServerEnv } from '../feature/dev-server.feature';
import guiFeature from '../feature/gui.feature';
import { expect } from 'chai';
import type { Page } from 'puppeteer';
import { startDevServer } from '../src';

function getBodyContent(page: Page) {
return page.evaluate(() => document.body.textContent!.trim());
Expand All @@ -17,35 +16,15 @@ describe('engineer:gui', function () {
const browserProvider = createBrowserProvider();

const setup = async ({ featureName, basePath }: { featureName?: string; basePath: string }) => {
const features = loadFeaturesFromPackages(resolvePackages(__dirname + '../'), fs).features;
const { dispose, engine } = await runNodeEnvironment({
featureName: 'engineer/gui',
features: [...features],
name: devServerEnv.env,
type: 'node',
host: new BaseHost(),
config: [
devServerFeature.use({
devServerConfig: {
basePath,
},
}),
guiFeature.use({
engineerConfig: {
features,
},
}),
],
const { dispose, engine, devServerFeature } = await startDevServer({
engineerEntry: 'engineer/gui',
targetApplicationPath: basePath,
});

const runtimeFeature = engine.features.get(guiFeature) as RuntimeFeature;
const devServerRuntime = engine.features.get(devServerFeature) as RuntimeFeature;
runtimeFeature.addOnDisposeHandler(async () => {
await devServerRuntime.api.devServerActions.close();
}, devServerEnv.env);

const runningPort: number = await new Promise((resolve) => {
devServerRuntime.api.serverListeningHandlerSlot.register(({ port }: { port: number }) => {
devServerFeature.api.serverListeningHandlerSlot.register(({ port }: { port: number }) => {
resolve(port);
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wixc3/engine-scripts",
"version": "15.0.0",
"version": "15.1.0-rc.0",
"main": "cjs/index.js",
"types": "cjs/index.d.ts",
"scripts": {
Expand All @@ -19,8 +19,8 @@
"@file-services/node": "^4.1.2",
"@file-services/types": "^4.1.0",
"@wixc3/cross-performance": "^14.3.1",
"@wixc3/engine-core": "^15.0.0",
"@wixc3/engine-core-node": "^15.0.0",
"@wixc3/engine-core": "^15.1.0-rc.0",
"@wixc3/engine-core-node": "^15.1.0-rc.0",
"commander": "^6.1.0",
"cors": "^2.8.5",
"create-listening-server": "^1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/test-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wixc3/engine-test-kit",
"version": "15.0.0",
"version": "15.1.0-rc.0",
"main": "cjs/index.js",
"types": "cjs/index.d.ts",
"scripts": {
Expand All @@ -14,7 +14,7 @@
"typescript": ">=3.0.0"
},
"dependencies": {
"@wixc3/engine-scripts": "^15.0.0",
"@wixc3/engine-scripts": "^15.1.0-rc.0",
"is-ci": "^2.0.0",
"promise-assist": "^1.3.0",
"sinon": "^9.1.0"
Expand Down

0 comments on commit 3e315fb

Please sign in to comment.