Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@embark/cmd-controller): re add cargo for file watcher #1855

Merged
merged 2 commits into from
Sep 5, 2019
Merged
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
84 changes: 36 additions & 48 deletions packages/embark/src/cmd/cmd_controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BlockchainClient, Simulator} from 'embark-blockchain-process';
import {Simulator} from 'embark-blockchain-process';
import {__} from 'embark-i18n';
import {dappPath, embarkPath} from 'embark-utils';
import findUp from 'find-up';
Expand Down Expand Up @@ -174,12 +174,6 @@ class EmbarkController {
engine.registerModuleGroup("namesystem");
engine.registerModulePackage('embark-deploy-tracker', {plugins: engine.plugins});

engine.events.on('deployment:deployContracts:afterAll', () => {
engine.events.request('pipeline:generateAll', () => {
engine.events.emit('outputDone');
});
});

const plugin = engine.plugins.createPlugin('cmdcontrollerplugin', {});
plugin.registerActionForEvent("embark:engine:started", async (_params, cb) => {
try {
Expand All @@ -196,8 +190,6 @@ class EmbarkController {
cb();
});

engine.events.request('watcher:start');

engine.events.on('check:backOnline:Ethereum', function () {
engine.logger.info(__('Ethereum node detected') + '..');
// TODO: deploy contracts, etc...
Expand All @@ -209,36 +201,14 @@ class EmbarkController {
engine.events.emit("status", __("Ready").green);
});

engine.events.on('file-event', async ({fileType, path}) => {
// TODO: re-add async.cargo / or use rxjs to use latest request in the queue

if (fileType === 'contract' || fileType === 'config') {
try {
await compileAndDeploySmartContracts(engine);
} catch (err) {
engine.logger.error(err);
}
} else if (fileType === 'asset') {
engine.events.request('pipeline:generateAll', () => {
engine.events.emit('outputDone');
});
}
});

engine.startEngine(async () => {
callback();

engine.events.request("webserver:start")
engine.events.request("webserver:start");

try {
let contractsFiles = await engine.events.request2("config:contractsFiles");
let compiledContracts = await engine.events.request2("compiler:contracts:compile", contractsFiles);
let _contractsConfig = await engine.events.request2("config:contractsConfig");
let contractsConfig = cloneDeep(_contractsConfig);
let [contractsList, contractDependencies] = await engine.events.request2("contracts:build", contractsConfig, compiledContracts);
await engine.events.request2("deployment:contracts:deploy", contractsList, contractDependencies);

await engine.events.request2("watcher:start");
await compileAndDeploySmartContracts(engine);
await setupCargoAndWatcher(engine);
} catch (e) {
engine.logger.error(__('Error building and deploying'), e);
}
Expand Down Expand Up @@ -570,25 +540,14 @@ class EmbarkController {
engine.events.emit("status", __("Ready").green);
});

engine.events.on('file-event', async ({ fileType, path }) => {
if (fileType === 'contract' || fileType === 'config') {
try {
await compileAndDeploySmartContracts(engine);
} catch (e) {
engine.logger.error(e);
}
} else if (fileType === 'asset') {
engine.events.request('pipeline:generateAll', () => engine.events.emit('outputDone'));
}
});

engine.startEngine(async () => {
try {
await compileAndDeploySmartContracts(engine);
await setupCargoAndWatcher(engine);
} catch (e) {
return callback(e);
}
engine.events.request2("watcher:start")

callback();
});
},
Expand Down Expand Up @@ -955,5 +914,34 @@ async function compileAndDeploySmartContracts(engine) {
let _contractsConfig = await engine.events.request2("config:contractsConfig");
let contractsConfig = cloneDeep(_contractsConfig);
let [contractsList, contractDependencies] = await engine.events.request2("contracts:build", contractsConfig, compiledContracts);
return await engine.events.request2("deployment:contracts:deploy", contractsList, contractDependencies);
await engine.events.request2("deployment:contracts:deploy", contractsList, contractDependencies);
await engine.events.request2('pipeline:generateAll');
engine.events.emit('outputDone');
}

async function setupCargoAndWatcher(engine) {
const cargo = async.cargo(async (tasks) => {
if (tasks.includes('contract') || tasks.includes('config')) {
try {
return await compileAndDeploySmartContracts(engine);
} catch (err) {
engine.logger.error(err);
return;
}
}

await engine.events.request2('pipeline:generateAll');
engine.events.emit('outputDone');
});

let fileTimeout;
engine.events.on('file-event', ({fileType, _path}) => {
clearTimeout(fileTimeout);
// Throttle file changes so we re-write only once for all files
fileTimeout = setTimeout(async () => {
cargo.push(fileType);
}, 50);
});

await engine.events.request2("watcher:start");
}