Skip to content

Commit

Permalink
Allow passing custom port number in cli
Browse files Browse the repository at this point in the history
#24

Also converting all commands into functions that are passed argv
  • Loading branch information
andrew committed Jun 2, 2021
1 parent b1317eb commit aca5463
Show file tree
Hide file tree
Showing 22 changed files with 120 additions and 73 deletions.
35 changes: 18 additions & 17 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,55 @@ const { hideBin } = require('yargs/helpers')

yargs(hideBin(process.argv))
.command(['server', '$0'], 'start the forage proxy server', () => {}, (argv) => {
require('./lib/commands/server')
require('./lib/commands/server')(argv)
})
.command('browse', 'open the forage UI', () => {}, (argv) => {
require('./lib/commands/browse')
require('./lib/commands/browse')(argv)
})
.command('seed', 'reseed any packages announced on IPFS', () => {}, (argv) => {
require('./lib/commands/seed')
require('./lib/commands/seed')(argv)
})
.command('import', 'load packages listed in forage.lock from IPFS', () => {}, (argv) => {
require('./lib/commands/import')
require('./lib/commands/import')(argv)
})
.command('republish', 'add local packages to IPFS and write to forage.lock', () => {}, (argv) => {
require('./lib/commands/republish')
require('./lib/commands/republish')(argv)
})
.command('watch', 'watch for new packages published upstream', () => {}, (argv) => {
require('./lib/commands/watch')
require('./lib/commands/watch')(argv)
})
.command('packages', 'list all cached packages', () => {}, (argv) => {
require('./lib/commands/packages')
require('./lib/commands/packages')(argv)
})
.command('config', 'set package managers proxy config', () => {}, (argv) => {
require('./lib/commands/config')
require('./lib/commands/config')(argv)
})
.command('unconfig', 'remove package managers proxy config', () => {}, (argv) => {
require('./lib/commands/unconfig')
require('./lib/commands/unconfig')(argv)
})
.command('preload', 'import packages from all package-lock.json files', () => {}, (argv) => {
require('./lib/commands/preload')
require('./lib/commands/preload')(argv)
})
.command('update', 'check for updates to all cached packages', () => {}, (argv) => {
require('./lib/commands/update')
require('./lib/commands/update')(argv)
})
.command('verify', 'validate cids of all cached packages', () => {}, (argv) => {
require('./lib/commands/verify')
require('./lib/commands/verify')(argv)
})
.command('reset', 'empty the forage database', () => {}, (argv) => {
require('./lib/commands/reset')
require('./lib/commands/reset')(argv)
})
.command('sizes', 'calculate sizes of tarballs', () => {}, (argv) => {
require('./lib/commands/sizes')
require('./lib/commands/sizes')(argv)
})
.command('peers', 'list peers sharing similar packages to you', () => {}, (argv) => {
require('./lib/commands/peers')
require('./lib/commands/peers')(argv)
})
.command('export', 'export all packages as a single IPFS directory', () => {}, (argv) => {
require('./lib/commands/export')
require('./lib/commands/export')(argv)
})
.command('id', 'find your IPFS peer ID and public key', () => {}, (argv) => {
require('./lib/commands/id')
require('./lib/commands/id')(argv)
})
.command('search query', 'search packages by name', () => {}, (argv) => {
require('./lib/commands/search')(argv)
Expand All @@ -72,4 +72,5 @@ yargs(hideBin(process.argv))
.command('trusted', 'list trusted public keys', () => {}, (argv) => {
require('./lib/commands/trusted')(argv)
})
.default('port', 8005)
.argv
6 changes: 5 additions & 1 deletion lib/commands/browse.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
require('open')('http://localhost:8005')
async function browse(argv) {
require('open')(`http://localhost:${argv.port}`)
}

module.exports = browse
8 changes: 6 additions & 2 deletions lib/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@

const forage = require('../forage');

forage.setConfig()
console.log('Set forage configs')
async function config(argv) {
forage.setConfig(argv.port)
console.log('Set forage configs')
}

module.exports = config
7 changes: 4 additions & 3 deletions lib/commands/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Export all packages as a single IPFS directory

const forage = require('../forage');
const async = require('async');

(async () => {
async function export(argv) {
var db = forage.connectDB()
await forage.connectIPFS(db);

var stats = await forage.core.exportPackages(db)
console.log(stats)
await db.close()
process.exit(0)
})();
}

module.exports = export
6 changes: 4 additions & 2 deletions lib/commands/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const forage = require('../forage');

(async () => {
async function id(argv) {
var db = forage.connectDB()
var ipfsID = await forage.connectIPFS(db);
var key = await forage.signing.fetchPrivateKey(db)
Expand All @@ -13,4 +13,6 @@ const forage = require('../forage');
console.log('IPFS Peer ID:',ipfsID.id)
await db.close()
process.exit(0)
})()
}

module.exports = id
6 changes: 4 additions & 2 deletions lib/commands/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const forageLock = JSON.parse(fs.readFileSync('forage.lock', 'utf8')); // TODO a
const forage = require('../forage');
const async = require('async');

(async () => {
async function import(argv) {
var db = forage.connectDB()
await forage.connectIPFS(db);

Expand All @@ -24,4 +24,6 @@ const async = require('async');
await db.close()
process.exit(0)
})
})();
}

module.exports = import
6 changes: 4 additions & 2 deletions lib/commands/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

const forage = require('../forage');

(async () => {
async function packages(argv) {
var db = forage.connectDB()
forage.listPackages().then(async function(packages) {
packages.forEach(pkg => console.log(pkg.manager, pkg.name, pkg.version));
console.log('Total: ' + packages.length, 'packages')
await db.close()
process.exit(0)
})
})()
}

module.exports = packages
6 changes: 4 additions & 2 deletions lib/commands/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const forage = require('../forage');

(async () => {
async function peers(argv) {
var db = forage.connectDB()
var ipfsID = await forage.connectIPFS(db)

Expand All @@ -21,4 +21,6 @@ const forage = require('../forage');
await db.close()
process.exit(0)
})
})();
}

module.exports = peers
6 changes: 4 additions & 2 deletions lib/commands/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const forage = require('../forage');
const path = require("path");
const async = require('async');

(async () => {
async function preload(argv) {
var db = forage.connectDB()
await forage.connectIPFS(db);

Expand Down Expand Up @@ -67,4 +67,6 @@ const async = require('async');
})

});
})()
}

module.exports = preload
8 changes: 5 additions & 3 deletions lib/commands/republish.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs');
const forage = require('../forage');
const async = require('async');

(async () => {
async function republish(argv) {
var db = forage.connectDB()
await forage.connectIPFS(db);

Expand All @@ -27,7 +27,7 @@ const async = require('async');
}, forage.concurrency())

q.error(function(err, task) {
console.error("Failed to import", task, err);
console.error("Failed to import", task, err);
});

// TODO accept file argument
Expand All @@ -51,4 +51,6 @@ const async = require('async');

await db.close()
process.exit(0)
})();
}

module.exports = republish
6 changes: 4 additions & 2 deletions lib/commands/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

const forage = require('../forage');

(async () => {
async function reset(argv) {
await forage.reset()
console.log('Forage has been reset')
})()
}

module.exports = reset
6 changes: 4 additions & 2 deletions lib/commands/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

const forage = require('../forage');

(async () => {
async function seed(argv) {
var db = forage.connectDB()
await forage.connectIPFS(db);

forage.seed()
})();
}

module.exports = seed
10 changes: 8 additions & 2 deletions lib/commands/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ const createServer = require('../server');
const forage = require('../forage');

(async () => {

})()

async function server(argv) {
var db = forage.connectDB()
var ipfsID = await forage.connectIPFS(db);
if(ipfsID){
var server = createServer(db)
server.listen(8005)
server.listen(argv.port)
forage.watchKnown()
forage.periodicUpdate()
}
})()
}

module.exports = server
36 changes: 20 additions & 16 deletions lib/commands/sizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@

const forage = require('../forage');

var total = 0

function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}

var db = forage.connectDB()
forage.core.filteredReadStream(db, 'size:').on('data', function (data) {
parts = data.key.split(':')
console.log(parts[1], `${parts[2]}@${parts[3]}:`, bytesToSize(data.value))
total += parseInt(data.value)
}).on('end', function () {
async function sizes(argv) {
var total = 0

var db = forage.connectDB()
forage.core.filteredReadStream(db, 'size:').on('data', function (data) {
parts = data.key.split(':')
console.log(parts[1], `${parts[2]}@${parts[3]}:`, bytesToSize(data.value))
total += parseInt(data.value)
}).on('end', async function () {

console.log('Total:', bytesToSize(total))
await db.close()
process.exit(0)
})
}

console.log('Total:', bytesToSize(total))
db.close()
process.exit(0)
})
module.exports = sizes
8 changes: 6 additions & 2 deletions lib/commands/unconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@

const forage = require('../forage');

forage.unsetConfig()
console.log('Removed forage config')
async function unconfig(argv) {
await forage.unsetConfig()
console.log('Removed forage config')
}

module.exports = unconfig
6 changes: 4 additions & 2 deletions lib/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const forage = require('../forage');

(async () => {
async function update(argv) {
var db = forage.connectDB()
await forage.connectIPFS(db);

Expand All @@ -17,4 +17,6 @@ const forage = require('../forage');

await db.close()
process.exit(0)
})();
}

module.exports = update
6 changes: 4 additions & 2 deletions lib/commands/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const forage = require('../forage');
const async = require('async');

(async () => {
async function verify(argv) {
var db = forage.connectDB()
await forage.connectIPFS(db);

Expand Down Expand Up @@ -32,4 +32,6 @@ const async = require('async');
process.exit(0)
})
})
})();
}

module.exports = verify
6 changes: 4 additions & 2 deletions lib/commands/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

const forage = require('../forage');

(async () => {
async function watch(argv) {
var db = forage.connectDB()
var ipfsID = await forage.connectIPFS(db);
if(ipfsID){
forage.watchAll();
}
})()
}

module.exports = watch
4 changes: 2 additions & 2 deletions lib/forage.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ async function connectIPFS(db){
}
}

async function setConfig() {
async function setConfig(port) {
for (const [name, manager] of Object.entries(managers)) {
manager.setConfig()
manager.setConfig(port)
}
}

Expand Down
Loading

0 comments on commit aca5463

Please sign in to comment.