watcher-ts/packages/peer/src/cli/create-peer.ts
prathamesh0 a56ade96fa
Refactor peer package for usage in watchers (#324)
* Refactor protocol change handler

* Refactor protocol stream creation

* Refactor Peer constructor

* Refactor peer CLIs to a separate cli folder

* Move peer node setup to a separate file

* Add a getter method for primary relay node mutiaddr

* Close new relay peer connection if limit already reached
2023-02-17 16:05:20 +05:30

50 lines
1.1 KiB
TypeScript

//
// Copyright 2022 Vulcanize, Inc.
//
import fs from 'fs';
import path from 'path';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs';
import { createPeerId } from '../peer.js';
interface Arguments {
file: string;
}
async function main (): Promise<void> {
const obj = await createPeerId();
const argv: Arguments = _getArgv();
if (argv.file) {
const exportFilePath = path.resolve(argv.file);
const exportFileDir = path.dirname(exportFilePath);
if (!fs.existsSync(exportFileDir)) {
fs.mkdirSync(exportFileDir, { recursive: true });
}
fs.writeFileSync(exportFilePath, JSON.stringify(obj, null, 2));
console.log(`Peer id ${obj.id.toString()} exported to file ${exportFilePath}`);
} else {
console.log(obj);
}
}
function _getArgv (): any {
return yargs(hideBin(process.argv)).parserConfiguration({
'parse-numbers': false
}).options({
file: {
type: 'string',
alias: 'f',
describe: 'Peer Id export file path (json)'
}
}).argv;
}
main().catch(err => {
console.log(err);
});