Skip to content

Commit 738ff8e

Browse files
jrainvilleiurimatias
authored andcommitted
feat(@cmd): add very basic embark init to add an embark.json file
1 parent 382a0b5 commit 738ff8e

File tree

10 files changed

+68
-52
lines changed

10 files changed

+68
-52
lines changed

packages/core/core/constants.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,24 @@
111111
"environments": {
112112
"development": "development"
113113
},
114-
"defaultMigrationsDir": "migrations"
114+
"defaultMigrationsDir": "migrations",
115+
"defaultEmbarkConfig": {
116+
"contracts": ["contracts/**"],
117+
"app": {},
118+
"buildDir": "dist/",
119+
"config": "config/",
120+
"migrations": "migrations",
121+
"versions": {
122+
"solc": "0.6.1"
123+
},
124+
"plugins": {
125+
},
126+
"options": {
127+
"solc": {
128+
"optimize": true,
129+
"optimize-runs": 200
130+
}
131+
},
132+
"generationDir": "embarkArtifacts"
133+
}
115134
}

packages/core/core/src/config.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { EmbarkEmitter as Events } from './events';
55
import { filesMatchingPattern, fileMatchesPattern } from './utils/utils';
66
const path = require('path');
77
const deepEqual = require('deep-equal');
8-
const web3 = require('web3');
98
import { __ } from 'embark-i18n';
109
import {
1110
buildUrlFromConfig,
@@ -23,13 +22,12 @@ import {
2322
getExternalContractUrl
2423
} from 'embark-utils';
2524
import { Logger } from 'embark-logger';
26-
import { readJsonSync } from 'fs-extra';
2725
const cloneDeep = require('lodash.clonedeep');
2826
const { replaceZeroAddressShorthand } = AddressUtils;
2927

30-
import { getBlockchainDefaults, getContractDefaults, embarkConfigDefaults } from './configDefaults';
28+
import { getBlockchainDefaults, getContractDefaults } from './configDefaults';
3129

32-
const constants = readJsonSync(path.join(__dirname, '../constants.json'));
30+
const constants = require('../constants.json');
3331

3432
const DEFAULT_CONFIG_PATH = 'config/';
3533

@@ -84,7 +82,7 @@ export class Config {
8482

8583
events: Events;
8684

87-
embarkConfig: EmbarkConfig = embarkConfigDefaults;
85+
embarkConfig: EmbarkConfig = constants.defaultEmbarkConfig;
8886

8987
context: any;
9088

@@ -629,7 +627,7 @@ export class Config {
629627
}
630628

631629
loadEmbarkConfigFile() {
632-
this.embarkConfig = recursiveMerge(embarkConfigDefaults, this.embarkConfig);
630+
this.embarkConfig = recursiveMerge(constants.defaultEmbarkConfig, this.embarkConfig);
633631

634632
const contracts = this.embarkConfig.contracts;
635633
// determine contract 'root' directories

packages/core/core/src/configDefaults.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ import { join } from "path";
44

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

7-
export const embarkConfigDefaults = {
8-
contracts: [],
9-
config: '',
10-
migrations: 'migrations',
11-
versions: {
12-
solc: "0.6.1"
13-
},
14-
options: {
15-
solc: {
16-
"optimize": true,
17-
"optimize-runs": 200
18-
}
19-
},
20-
generationDir: "embarkArtifacts"
21-
};
22-
237
export function getBlockchainDefaults(env) {
248
const defaults = {
259
clientConfig: {

packages/core/engine/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"embark-ethereum-blockchain-client": "^5.3.0-nightly.0",
6767
"embark-ganache": "^5.3.0-nightly.0",
6868
"embark-geth": "^5.3.0-nightly.0",
69+
"embark-i18n": "^5.2.3",
6970
"embark-library-manager": "^5.3.0-nightly.0",
7071
"embark-logger": "^5.3.0-nightly.0",
7172
"embark-mocha-tests": "^5.3.0-nightly.0",
@@ -87,7 +88,8 @@
8788
"embark-vyper": "^5.2.3",
8889
"embark-watcher": "^5.3.0-nightly.0",
8990
"embark-web3": "^5.3.0-nightly.0",
90-
"embark-webserver": "^5.3.0-nightly.0"
91+
"embark-webserver": "^5.3.0-nightly.0",
92+
"fs-extra": "8.1.0"
9193
},
9294
"devDependencies": {
9395
"embark-solo": "^5.2.3",

packages/core/engine/src/defaultEmbarkJson.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/core/engine/src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import {
66
ProcessManager,
77
ServicesMonitor
88
} from 'embark-core';
9+
import fs from 'fs-extra';
910
import { normalizeInput } from 'embark-utils';
1011
import { Logger } from 'embark-logger';
11-
import defaultEmbarkJson from './defaultEmbarkJson';
12+
import { __ } from 'embark-i18n';
13+
const constants = require('embark-core/constants');
1214
const EMBARK_PROCESS_NAME = 'embark';
1315

1416
export class Engine {
@@ -61,7 +63,7 @@ export class Engine {
6163
this.env = options.env;
6264
this.client = options.client;
6365
this.locale = options.locale;
64-
this.embarkConfig = options.embarkConfig || defaultEmbarkJson;
66+
this.embarkConfig = options.embarkConfig || constants.defaultEmbarkConfig;
6567
this.interceptLogs = options.interceptLogs;
6668
this.version = options.version;
6769
this.logFile = options.logFile;
@@ -118,6 +120,13 @@ export class Engine {
118120
callback();
119121
}
120122

123+
async generateEmbarkJSON() {
124+
if (fs.existsSync('embark.json')) {
125+
throw new Error(__('embark.json already there. Will not overwrite'));
126+
}
127+
return fs.writeFile('embark.json', JSON.stringify(constants.defaultEmbarkConfig, null, 2));
128+
}
129+
121130
loadDappPlugins() {
122131
if (this.config) {
123132
this.config.plugins.loadPlugins();

packages/core/engine/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@
127127
{
128128
"path": "../core"
129129
},
130+
{
131+
"path": "../i18n"
132+
},
130133
{
131134
"path": "../logger"
132135
},

packages/embark/src/bin/embark.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,9 @@ EmbarkJson.prototype.log = function () {
411411

412412
EmbarkJson.prototype.logMissingFile = function () {
413413
if (Json.prototype.logMissingFile.call(this, false)) {
414-
// Use default embark.json
415414
if (isDappCmd(this.cmd)) {
416-
// TODO add message about embark init once it's available
417-
embarklog['warn']('No embark.json file found.\n' +
418-
'You can find a basic embark.json file here: https://github.com/embarklabs/embark/blob/master/dapps/templates/boilerplate/embark.json');
415+
embarklog['error']('No embark.json file found.\n' +
416+
'Run `embark init` to generate one automatically.');
419417
exitWithError();
420418
}
421419
}
@@ -993,6 +991,7 @@ function isDappCmd(cmd) {
993991
'-h',
994992
'--help',
995993
'new',
994+
'init',
996995
'demo',
997996
'version',
998997
'help'

packages/embark/src/cmd/cmd.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Cmd {
1111
}
1212

1313
process(args) {
14+
this.init();
1415
this.newApp();
1516
this.demo();
1617
this.build();
@@ -37,6 +38,15 @@ class Cmd {
3738
program.parse(args);
3839
}
3940

41+
init() {
42+
program
43+
.command('init')
44+
.description(__('Creates a basic embark.json file'))
45+
.action(() => {
46+
this.embark.embarkInit();
47+
});
48+
}
49+
4050
newApp() {
4151

4252
let validateName = function (value) {
@@ -181,7 +191,7 @@ class Cmd {
181191
.option('-t, --track', __('Force tracking of migration script', false))
182192
.description(__("Executes specified scripts or all scripts in 'directory'"))
183193
.action((env, target, options) => {
184-
embark.exec({
194+
this.embark.exec({
185195
env,
186196
target,
187197
forceTracking: options.track

packages/embark/src/cmd/cmd_controller.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ const pkg = readJsonSync(join(__dirname, '../../package.json'));
2020
class EmbarkController {
2121

2222
constructor(options) {
23-
if (!options.embarkConfig) {
24-
throw new Error('No embarkConfig found in options');
25-
}
2623
this.embarkConfig = options.embarkConfig;
2724
this.version = pkg.version;
2825

@@ -39,6 +36,19 @@ class EmbarkController {
3936
this.plugins = this.config.plugins;
4037
}
4138

39+
async embarkInit() {
40+
const engine = new Engine({});
41+
try {
42+
await engine.generateEmbarkJSON();
43+
} catch (e) {
44+
console.error(__('Error generating embark.json file'));
45+
console.error(e.message);
46+
process.exit(1);
47+
}
48+
console.info(__('embark.json generated. You can now run all Embark commands.').green);
49+
process.exit();
50+
}
51+
4252
blockchain(options) {
4353
this.context = options.context || [constants.contexts.blockchain];
4454
const webServerConfig = {};

0 commit comments

Comments
 (0)