Run `ipfs init` for the case when user has installed ipfs, but not initialised it.

Runs `ipfs daemon` first, reads stdout for message regarding ipfs not initialised, then runs `ipfs init`, then re-runs `ipfs daemon`.
This commit is contained in:
emizzle 2018-06-18 22:34:42 +10:00
parent 7706e276bf
commit bb4fdbe369
2 changed files with 221 additions and 191 deletions

View File

@ -33,23 +33,34 @@ class IPFSProcess extends ProcessWrapper {
}); });
} }
startIPFSDaemon() { _bindChildEvents(childProcess){
const self = this; const self = this;
// spawn the daemon (muhaha) childProcess.on('error', (err) => {
this.child = child_process.spawn(this.command, ['daemon']);
this.child.on('error', (err) => {
err = err.toString(); err = err.toString();
console.error('IPFS error: ', err); console.error('IPFS error: ', err);
}); });
this.child.stderr.on('data', (data) => {
childProcess.stderr.on('data', (data) => {
data = data.toString(); data = data.toString();
console.log(`IPFS error: ${data}`); console.log(`IPFS error: ${data}`);
// `ipfs daemon called`, but `ipfs init` had not been run yet
if(!self.initCalled && data.indexOf('no IPFS repo found') > -1) {
self.initCalled = true;
let ipfsInitChild = child_process.spawn(this.command, ['init']);
self._bindChildEvents(ipfsInitChild);
}
}); });
this.child.stdout.on('data', (data) => {
childProcess.stdout.on('data', (data) => {
data = data.toString(); data = data.toString();
if (!self.readyCalled && data.indexOf('Daemon is ready') > -1) {
// ipfs init just run, and we have a successful result
// re-run `ipfs daemon`
if(self.initCalled && !self.readyCalled && data.indexOf('peer identity:') > -1) {
self.startIPFSDaemon();
}
else if (!self.readyCalled && data.indexOf('Daemon is ready') > -1) {
self.readyCalled = true; self.readyCalled = true;
// update IPFS cors before spawning a daemon (muhaha) // update IPFS cors before spawning a daemon (muhaha)
@ -90,13 +101,20 @@ class IPFSProcess extends ProcessWrapper {
} }
console.log('IPFS: ' + data); console.log('IPFS: ' + data);
}); });
this.child.on('exit', (code) => { childProcess.on('exit', (code) => {
if (code) { if (code) {
console.error('IPFS exited with error code ' + code); console.error('IPFS exited with error code ' + code);
} }
}); });
}
startIPFSDaemon() {
const self = this;
// spawn the daemon (muhaha)
this.child = child_process.spawn(this.command, ['daemon']);
self._bindChildEvents(this.child);
} }
kill() { kill() {

376
package-lock.json generated

File diff suppressed because it is too large Load Diff