Skip to content

Commit

Permalink
feat(@cmd): add very basic embark init to add an embark.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
jrainville authored and iurimatias committed Feb 28, 2020
1 parent 382a0b5 commit 738ff8e
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 52 deletions.
21 changes: 20 additions & 1 deletion packages/core/core/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,24 @@
"environments": {
"development": "development"
},
"defaultMigrationsDir": "migrations"
"defaultMigrationsDir": "migrations",
"defaultEmbarkConfig": {
"contracts": ["contracts/**"],
"app": {},
"buildDir": "dist/",
"config": "config/",
"migrations": "migrations",
"versions": {
"solc": "0.6.1"
},
"plugins": {
},
"options": {
"solc": {
"optimize": true,
"optimize-runs": 200
}
},
"generationDir": "embarkArtifacts"
}
}
10 changes: 4 additions & 6 deletions packages/core/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { EmbarkEmitter as Events } from './events';
import { filesMatchingPattern, fileMatchesPattern } from './utils/utils';
const path = require('path');
const deepEqual = require('deep-equal');
const web3 = require('web3');
import { __ } from 'embark-i18n';
import {
buildUrlFromConfig,
Expand All @@ -23,13 +22,12 @@ import {
getExternalContractUrl
} from 'embark-utils';
import { Logger } from 'embark-logger';
import { readJsonSync } from 'fs-extra';
const cloneDeep = require('lodash.clonedeep');
const { replaceZeroAddressShorthand } = AddressUtils;

import { getBlockchainDefaults, getContractDefaults, embarkConfigDefaults } from './configDefaults';
import { getBlockchainDefaults, getContractDefaults } from './configDefaults';

const constants = readJsonSync(path.join(__dirname, '../constants.json'));
const constants = require('../constants.json');

const DEFAULT_CONFIG_PATH = 'config/';

Expand Down Expand Up @@ -84,7 +82,7 @@ export class Config {

events: Events;

embarkConfig: EmbarkConfig = embarkConfigDefaults;
embarkConfig: EmbarkConfig = constants.defaultEmbarkConfig;

context: any;

Expand Down Expand Up @@ -629,7 +627,7 @@ export class Config {
}

loadEmbarkConfigFile() {
this.embarkConfig = recursiveMerge(embarkConfigDefaults, this.embarkConfig);
this.embarkConfig = recursiveMerge(constants.defaultEmbarkConfig, this.embarkConfig);

const contracts = this.embarkConfig.contracts;
// determine contract 'root' directories
Expand Down
16 changes: 0 additions & 16 deletions packages/core/core/src/configDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ import { join } from "path";

const constants = readJsonSync(join(__dirname, '../constants.json'));

export const embarkConfigDefaults = {
contracts: [],
config: '',
migrations: 'migrations',
versions: {
solc: "0.6.1"
},
options: {
solc: {
"optimize": true,
"optimize-runs": 200
}
},
generationDir: "embarkArtifacts"
};

export function getBlockchainDefaults(env) {
const defaults = {
clientConfig: {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"embark-ethereum-blockchain-client": "^5.3.0-nightly.0",
"embark-ganache": "^5.3.0-nightly.0",
"embark-geth": "^5.3.0-nightly.0",
"embark-i18n": "^5.2.3",
"embark-library-manager": "^5.3.0-nightly.0",
"embark-logger": "^5.3.0-nightly.0",
"embark-mocha-tests": "^5.3.0-nightly.0",
Expand All @@ -87,7 +88,8 @@
"embark-vyper": "^5.2.3",
"embark-watcher": "^5.3.0-nightly.0",
"embark-web3": "^5.3.0-nightly.0",
"embark-webserver": "^5.3.0-nightly.0"
"embark-webserver": "^5.3.0-nightly.0",
"fs-extra": "8.1.0"
},
"devDependencies": {
"embark-solo": "^5.2.3",
Expand Down
18 changes: 0 additions & 18 deletions packages/core/engine/src/defaultEmbarkJson.js

This file was deleted.

13 changes: 11 additions & 2 deletions packages/core/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
ProcessManager,
ServicesMonitor
} from 'embark-core';
import fs from 'fs-extra';
import { normalizeInput } from 'embark-utils';
import { Logger } from 'embark-logger';
import defaultEmbarkJson from './defaultEmbarkJson';
import { __ } from 'embark-i18n';
const constants = require('embark-core/constants');
const EMBARK_PROCESS_NAME = 'embark';

export class Engine {
Expand Down Expand Up @@ -61,7 +63,7 @@ export class Engine {
this.env = options.env;
this.client = options.client;
this.locale = options.locale;
this.embarkConfig = options.embarkConfig || defaultEmbarkJson;
this.embarkConfig = options.embarkConfig || constants.defaultEmbarkConfig;
this.interceptLogs = options.interceptLogs;
this.version = options.version;
this.logFile = options.logFile;
Expand Down Expand Up @@ -118,6 +120,13 @@ export class Engine {
callback();
}

async generateEmbarkJSON() {
if (fs.existsSync('embark.json')) {
throw new Error(__('embark.json already there. Will not overwrite'));
}
return fs.writeFile('embark.json', JSON.stringify(constants.defaultEmbarkConfig, null, 2));
}

loadDappPlugins() {
if (this.config) {
this.config.plugins.loadPlugins();
Expand Down
3 changes: 3 additions & 0 deletions packages/core/engine/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
{
"path": "../core"
},
{
"path": "../i18n"
},
{
"path": "../logger"
},
Expand Down
7 changes: 3 additions & 4 deletions packages/embark/src/bin/embark.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,9 @@ EmbarkJson.prototype.log = function () {

EmbarkJson.prototype.logMissingFile = function () {
if (Json.prototype.logMissingFile.call(this, false)) {
// Use default embark.json
if (isDappCmd(this.cmd)) {
// TODO add message about embark init once it's available
embarklog['warn']('No embark.json file found.\n' +
'You can find a basic embark.json file here: https://github.com/embarklabs/embark/blob/master/dapps/templates/boilerplate/embark.json');
embarklog['error']('No embark.json file found.\n' +
'Run `embark init` to generate one automatically.');
exitWithError();
}
}
Expand Down Expand Up @@ -993,6 +991,7 @@ function isDappCmd(cmd) {
'-h',
'--help',
'new',
'init',
'demo',
'version',
'help'
Expand Down
12 changes: 11 additions & 1 deletion packages/embark/src/cmd/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Cmd {
}

process(args) {
this.init();
this.newApp();
this.demo();
this.build();
Expand All @@ -37,6 +38,15 @@ class Cmd {
program.parse(args);
}

init() {
program
.command('init')
.description(__('Creates a basic embark.json file'))
.action(() => {
this.embark.embarkInit();
});
}

newApp() {

let validateName = function (value) {
Expand Down Expand Up @@ -181,7 +191,7 @@ class Cmd {
.option('-t, --track', __('Force tracking of migration script', false))
.description(__("Executes specified scripts or all scripts in 'directory'"))
.action((env, target, options) => {
embark.exec({
this.embark.exec({
env,
target,
forceTracking: options.track
Expand Down
16 changes: 13 additions & 3 deletions packages/embark/src/cmd/cmd_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ const pkg = readJsonSync(join(__dirname, '../../package.json'));
class EmbarkController {

constructor(options) {
if (!options.embarkConfig) {
throw new Error('No embarkConfig found in options');
}
this.embarkConfig = options.embarkConfig;
this.version = pkg.version;

Expand All @@ -39,6 +36,19 @@ class EmbarkController {
this.plugins = this.config.plugins;
}

async embarkInit() {
const engine = new Engine({});
try {
await engine.generateEmbarkJSON();
} catch (e) {
console.error(__('Error generating embark.json file'));
console.error(e.message);
process.exit(1);
}
console.info(__('embark.json generated. You can now run all Embark commands.').green);
process.exit();
}

blockchain(options) {
this.context = options.context || [constants.contexts.blockchain];
const webServerConfig = {};
Expand Down

0 comments on commit 738ff8e

Please sign in to comment.