Browse Source

Clean up logs, add debug option

pull/2/head
raucao 3 years ago
parent
commit
7cd569ee21
Signed by: raucao
GPG Key ID: BE4634D632D39B67
  1. 19
      index.js
  2. 21
      lib/ipfs-pinner.js
  3. 2
      package-lock.json
  4. 5
      package.json

19
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);
}

21
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);
});
});

2
package-lock.json generated

@ -1,5 +1,5 @@
{
"name": "kredits-ipfs-pinner",
"name": "@67p/kredits-ipfs-pinner",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,

5
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"
}

Loading…
Cancel
Save