From 738ff8e41d26b053981f414d5d2a24d4279fd551 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 27 Feb 2020 13:20:51 -0500 Subject: [PATCH] feat(@cmd): add very basic embark init to add an embark.json file --- packages/core/core/constants.json | 21 ++++++++++++++++++- packages/core/core/src/config.ts | 10 ++++----- packages/core/core/src/configDefaults.ts | 16 -------------- packages/core/engine/package.json | 4 +++- packages/core/engine/src/defaultEmbarkJson.js | 18 ---------------- packages/core/engine/src/index.ts | 13 ++++++++++-- packages/core/engine/tsconfig.json | 3 +++ packages/embark/src/bin/embark.js | 7 +++---- packages/embark/src/cmd/cmd.js | 12 ++++++++++- packages/embark/src/cmd/cmd_controller.js | 16 +++++++++++--- 10 files changed, 68 insertions(+), 52 deletions(-) delete mode 100644 packages/core/engine/src/defaultEmbarkJson.js diff --git a/packages/core/core/constants.json b/packages/core/core/constants.json index 92459807f6..b0d3854caa 100644 --- a/packages/core/core/constants.json +++ b/packages/core/core/constants.json @@ -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" + } } diff --git a/packages/core/core/src/config.ts b/packages/core/core/src/config.ts index 75e53d4c9c..3a60f54471 100644 --- a/packages/core/core/src/config.ts +++ b/packages/core/core/src/config.ts @@ -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, @@ -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/'; @@ -84,7 +82,7 @@ export class Config { events: Events; - embarkConfig: EmbarkConfig = embarkConfigDefaults; + embarkConfig: EmbarkConfig = constants.defaultEmbarkConfig; context: any; @@ -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 diff --git a/packages/core/core/src/configDefaults.ts b/packages/core/core/src/configDefaults.ts index ec020996ee..d03fee8f8c 100644 --- a/packages/core/core/src/configDefaults.ts +++ b/packages/core/core/src/configDefaults.ts @@ -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: { diff --git a/packages/core/engine/package.json b/packages/core/engine/package.json index 2a030f132d..c25828d412 100644 --- a/packages/core/engine/package.json +++ b/packages/core/engine/package.json @@ -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", @@ -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", diff --git a/packages/core/engine/src/defaultEmbarkJson.js b/packages/core/engine/src/defaultEmbarkJson.js deleted file mode 100644 index f101ca1f53..0000000000 --- a/packages/core/engine/src/defaultEmbarkJson.js +++ /dev/null @@ -1,18 +0,0 @@ -module.exports = { - contracts: ["contracts/**"], - app: {}, - buildDir: "dist/", - config: "config/", - versions: { - solc: "0.6.1" - }, - plugins: { - }, - options: { - solc: { - "optimize": true, - "optimize-runs": 200 - } - }, - generationDir: "embarkArtifacts" -}; diff --git a/packages/core/engine/src/index.ts b/packages/core/engine/src/index.ts index d8e6a5a246..24bf32313d 100644 --- a/packages/core/engine/src/index.ts +++ b/packages/core/engine/src/index.ts @@ -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 { @@ -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; @@ -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(); diff --git a/packages/core/engine/tsconfig.json b/packages/core/engine/tsconfig.json index c98b6c439c..5245050746 100644 --- a/packages/core/engine/tsconfig.json +++ b/packages/core/engine/tsconfig.json @@ -127,6 +127,9 @@ { "path": "../core" }, + { + "path": "../i18n" + }, { "path": "../logger" }, diff --git a/packages/embark/src/bin/embark.js b/packages/embark/src/bin/embark.js index 4bdca9d66b..14b545b46e 100755 --- a/packages/embark/src/bin/embark.js +++ b/packages/embark/src/bin/embark.js @@ -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(); } } @@ -993,6 +991,7 @@ function isDappCmd(cmd) { '-h', '--help', 'new', + 'init', 'demo', 'version', 'help' diff --git a/packages/embark/src/cmd/cmd.js b/packages/embark/src/cmd/cmd.js index 61b1d069be..2f613ac63e 100644 --- a/packages/embark/src/cmd/cmd.js +++ b/packages/embark/src/cmd/cmd.js @@ -11,6 +11,7 @@ class Cmd { } process(args) { + this.init(); this.newApp(); this.demo(); this.build(); @@ -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) { @@ -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 diff --git a/packages/embark/src/cmd/cmd_controller.js b/packages/embark/src/cmd/cmd_controller.js index 79f082eb54..93b60a4553 100644 --- a/packages/embark/src/cmd/cmd_controller.js +++ b/packages/embark/src/cmd/cmd_controller.js @@ -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; @@ -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 = {};