diff --git a/package.json b/package.json index 1025ee97..87cba930 100644 --- a/package.json +++ b/package.json @@ -75,9 +75,11 @@ "lodash.clone": "^4.5.0", "lodash.defaults": "^4.2.0", "lodash.defaultsdeep": "^4.6.1", + "merge-options": "^1.0.1", "multiaddr": "^6.1.0", "safe-json-stringify": "^1.2.0", - "superagent": "^5.0.5" + "superagent": "^5.0.5", + "temp-write": "^4.0.0" }, "devDependencies": { "aegir": "^20.0.0", @@ -87,7 +89,7 @@ "dirty-chai": "^2.0.1", "go-ipfs-dep": "~0.4.22", "husky": "^3.0.4", - "ipfs": "~0.37.1", + "ipfs": "github:ipfs/js-ipfs#feat/daemon-start-init", "is-running": "^2.1.0", "lint-staged": "^9.2.5", "proxyquire": "^2.1.3", diff --git a/src/defaults/options.json b/src/defaults/options.json index ea8e9331..e31a081c 100644 --- a/src/defaults/options.json +++ b/src/defaults/options.json @@ -2,5 +2,6 @@ "type": "go", "disposable": true, "start": true, - "init": true + "init": true, + "args": [] } diff --git a/src/factory-daemon.js b/src/factory-daemon.js index 13f386f1..4975eff5 100644 --- a/src/factory-daemon.js +++ b/src/factory-daemon.js @@ -7,6 +7,7 @@ const tmpDir = require('./utils/tmp-dir') const Daemon = require('./ipfsd-daemon') const defaultConfig = require('./defaults/config.json') const defaultOptions = require('./defaults/options.json') +const { configFile } = require('./utils/config-file') /** @ignore @typedef {import("./index").SpawnOptions} SpawnOptions */ @@ -112,6 +113,17 @@ class FactoryDaemon { options.initOptions = defaultsDeep({}, this.options.initOptions, options.initOptions) const node = new Daemon(options) + if (options.init && options.start && options.type === 'js') { + const configPath = await configFile(options.type, options.config) + const args = options.args.concat([ + '--init', + '--init-config', configPath, + '--init-profile', options.initOptions.profile + ]) + await node.start(args) + + return node + } if (options.init) { await node.init(options.initOptions) diff --git a/src/ipfsd-daemon.js b/src/ipfsd-daemon.js index c5e51dff..3f0ca307 100644 --- a/src/ipfsd-daemon.js +++ b/src/ipfsd-daemon.js @@ -20,6 +20,7 @@ const tmpDir = require('./utils/tmp-dir') const findIpfsExecutable = require('./utils/find-ipfs-executable') const setConfigValue = require('./utils/set-config-value') const run = require('./utils/run') +const { configFile } = require('./utils/config-file') // amount of ms to wait before sigkill const GRACE_PERIOD = 10500 @@ -180,12 +181,19 @@ class Daemon { } } + if (this.opts.type === 'js') { + const configPath = await configFile('js', this.opts.config) + args.push(`--init-config`, configPath) + } + await run(this, args, { env: this.env }) .catch(translateError) - const conf = await this.getConfig() + if (this.opts.type === 'go') { + const conf = await this.getConfig() - await this.replaceConfig(defaults({}, this.opts.config, conf)) + await this.replaceConfig(defaults({}, this.opts.config, conf)) + } this.clean = false this.initialized = true @@ -215,7 +223,7 @@ class Daemon { * @return {Promise} */ start (flags = []) { - const args = ['daemon'].concat(flags || []) + const args = ['daemon'].concat(flags) const setApiAddr = (addr) => { this._apiAddr = multiaddr(addr) diff --git a/src/utils/config-file.js b/src/utils/config-file.js new file mode 100644 index 00000000..d107fdbe --- /dev/null +++ b/src/utils/config-file.js @@ -0,0 +1,11 @@ +'use strict' +const tempWrite = require('temp-write') +const merge = require('merge-options') + +function configFile (type, config) { + return tempWrite(JSON.stringify(merge({}, config)), 'config.json') +} + +module.exports = { + configFile +}