From 7cd569ee212bc29ee1d0a850d4dea5e39a491030 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Tue, 2 Jul 2019 18:41:48 +0200 Subject: [PATCH] Clean up logs, add debug option --- index.js | 19 ++++++++++++------- lib/ipfs-pinner.js | 21 ++++++++++++--------- package-lock.json | 2 +- package.json | 5 ++++- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 1ac7318..77969a5 100755 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ -#! /usr/bin/env node - +const debug = require('debug')('ipfs-pinner'); const Kredits = require('kredits-contracts'); const IpfsPinner = require('./lib/ipfs-pinner'); @@ -32,18 +31,24 @@ console.log(`Using IPFS:`, ipfsConfig); { network: argv.network, rpcUrl: argv.rpcUrl }, { apm: argv.apm, ipfsConfig: ipfsConfig, addresses: { Kernel: argv.daoAddress } } ).init(); - await kredits.ipfs._ipfsAPI.id(); // check the connection to the IPFS client TODO: redesign IPFS wrapper API and not use an internal attribute + + // check the connection to the IPFS client + // TODO redesign IPFS wrapper API and do not use an internal attribute + await kredits.ipfs._ipfsAPI.id(); + const ipfsPinner = new IpfsPinner(kredits); ipfsPinner.pinAll().then(pins => { - console.log('Pinned', JSON.stringify(pins, null, 2)); + console.log(`Pinned ${pins.length} existing documents`); }); - ipfsPinner.monitor((pin) => { - console.log('Pinned', JSON.stringify(pin)); + + ipfsPinner.monitor(pin => { + console.log('Pinned a new document:', pin[0]["hash"]); }); + console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`); } catch(e) { - console.log('Failed to start pinner'); + console.log('Failed to start'); console.log(e); process.exit(1); } diff --git a/lib/ipfs-pinner.js b/lib/ipfs-pinner.js index 3df2b75..48765f2 100644 --- a/lib/ipfs-pinner.js +++ b/lib/ipfs-pinner.js @@ -1,15 +1,18 @@ +const debug = require('debug')('ipfs-pinner'); + class IpfsPinner { constructor (kredits, ipfsApi) { this.kredits = kredits; this.ipfsApi = ipfsApi || this.kredits.ipfs; } - pinAll () { - return Promise.all([ - this._pinAllFromContract(this.kredits.Contributor), - this._pinAllFromContract(this.kredits.Contribution), - this._pinAllFromContract(this.kredits.Proposal) - ]); + async pinAll () { + const contributorHashes = await this._pinAllFromContract(this.kredits.Contributor); + const contributionHashes = await this._pinAllFromContract(this.kredits.Contribution); + const proposalHashes = await this._pinAllFromContract(this.kredits.Proposal); + + return contributorHashes.concat(contributionHashes) + .concat(proposalHashes); } monitor (callback) { @@ -31,13 +34,13 @@ class IpfsPinner { } _pinAllFromContract (contract) { - console.log(`Pinning data from ${contract.constructor.name}...`); + debug(`Pinning data from ${contract.constructor.name}...`); return contract.count.then(count => { let promises = [...Array(count).keys()].map(i => { let id = i + 1; // 0 => 1 - ids start with 1 and not with 0 - console.log(`Loading ${contract.constructor.name} #${id}`); + debug(`Loading ${contract.constructor.name} #${id}`); return contract.getData(id).then(data => { - console.log(`Pinning ${contract.constructor.name} #${id}`); + debug(`Pinning ${contract.constructor.name} #${id}`); return this.ipfsApi.pin(data); }); }); diff --git a/package-lock.json b/package-lock.json index 5e6f386..1a2c284 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "kredits-ipfs-pinner", + "name": "@67p/kredits-ipfs-pinner", "version": "1.0.0", "lockfileVersion": 1, "requires": true, diff --git a/package.json b/package.json index cd57abb..52408c3 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,11 @@ { "name": "@67p/kredits-ipfs-pinner", "version": "1.0.0", - "description": "Tool to pin IPFS data of a kredits organization on a connected IPFS node.", + "description": "Pins IPFS data of a Kredits organization on an IPFS node", "main": "index.js", "scripts": { + "start": "node index.js", + "debug": "DEBUG=ipfs-pinner node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "bin": { @@ -18,6 +20,7 @@ ], "license": "MIT", "dependencies": { + "debug": "^4.1.1", "kredits-contracts": "^5.4.0", "yargs": "^13.2.4" }