@fanyinghao/crust-cli
Version:
The Crust command-line interface (Crust CLI) is a set of commands used to access Crust Network resources
40 lines (36 loc) • 1.27 kB
JavaScript
const fs = require('fs');
const IpfsHttpClient = require('ipfs-http-client');
const { globSource } = IpfsHttpClient;
const { ipfsTimeout } = require('./consts');
module.exports = {
default: async (path) => {
let host = process.env.HOST || 'localhost';
let port = process.env.PORT || 5001;
let protocol = process.env.PROTOCOL || 'http';
let timeout = process.env.TTL || ipfsTimeout;
try {
// 1. Check local ipfs is alive
const ipfs = IpfsHttpClient({
host,
port,
protocol,
timeout
});
// 2. Check legality of path
if (!fs.existsSync(path)) {
console.error(`File/directory is not exists: ${path}`);
return;
}
// 3. Pin it
const { cid } = await ipfs.add(globSource(path, { recursive: true }));
// 4. Check local pin
if (cid) {
console.log(`Pin success: ${cid}`);
} else {
console.error(`Pin failed, please try it again`);
}
} catch (e) {
console.error(`IPFS is offline, please start it over, error detail: ${e}`);
}
}
}