Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Input, Output } from 'web-ext-native-msg';
- import { homedir } from 'os';
- import * as process from 'process';
- import * as patch from 'path';
- import * as fs from 'fs/promises';
- const handleReject = e => {
- e = (new Output()).encode(e);
- e && process.stdout.write(e);
- console.log(`there was an error: ${e}`);
- return false;
- };
- const writeStdout = async msg => {
- msg = await (new Output()).encode(msg);
- return msg && process.stdout.write(msg);
- };
- const handleMsg = async msg => {
- const dir = patch.resolve(homedir(), './.better-vulcan');
- const file = patch.resolve(dir, 'data.json');
- console.log(`read message: ${msg.type}`);
- try {
- console.log(`creating new directory: ${dir}`);
- await fs.mkdir(dir);
- }
- catch {}
- console.log(`saving message to: ${file}`);
- await fs.writeFile(file, JSON.stringify(msg));
- console.log(`message handled!`);
- writeStdout({ message: 'done!' });
- };
- const input = new Input();
- const readStdin = chunk => {
- const arr = input.decode(chunk);
- const func = [];
- Array.isArray(arr) && arr.length && arr.forEach(msg => {
- msg && func.push(handleMsg(msg));
- });
- console.log(`read ${arr.length} messages`);
- return Promise.all(func).catch(handleReject);
- };
- console.log(`start!`);
- process.stdin.on('data', readStdin);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement