rollup.js: How to disable output? - rollupjs

I'm trying to use the rollup JS API. rollup.rollup doesn't require me to specify the output option but it appears rollup.watch does for some reason.
Here's what I've got:
#!/usr/bin/env node
import * as rollup from 'rollup';
async function main() {
const watcher = rollup.watch({
input: 'src/main.ts',
// output: {
// dir: 'dist'
// }
})
watcher.on('event', event => {
console.log(event);
})
}
main().catch(err => {
console.error(err);
process.exit(1);
})
It's saying:
{
code: 'ERROR',
error: Error: You must specify "output.file" or "output.dir" for the build.
at error (file:///somepath/screeps/node_modules/rollup/dist/es/shared/rollup.js:10380:30
)
at Object.write (file:///somepath/screeps/node_modules/rollup/dist/es/shared/rollup.js:1
8594:24)
at file:///somepath/screeps/node_modules/rollup/dist/es/shared/watch.js:7083:86
at Array.map (<anonymous>)
at Task.run (file:///somepath/screeps/node_modules/rollup/dist/es/shared/watch.js:7083:6
3)
at async Watcher.run (file:///somepath/screeps/node_modules/rollup/dist/es/shared/watch.
js:7003:17) {
code: 'MISSING_OPTION'
}
}
I should be able to get what I need out of the event emitter though -- I don't want to write it to disk. How do I disable that?

Turns out there's a watch.skipWrite option.

Related

Electron ipcMain how to gracefully handle throwing an error

In Electron if I throw an error anywhere on the backend it goes to a custom window. Trying to find a way to catch that to push to a custom area in my app I've found that I can detect the process with process.on('uncaughtException'). However I'm stuck trying to run a sender to send either the error or the report. What I've tried:
ipcMain.on('main', async (e, data) => {
try {
await someModule(data)
process.on('uncaughtException', err => e.sender.send('error', err.message))
return e.sender.send('audit', 'No issues found')
} catch (err) {
console.log(err)
}
})
module.js:
module.export = data => {
throw Error('this is a test')
}
In the above I'm sending both get both errorandaudit` to renderer. I've researched for a way to pass 'uncaughtException' to a ternary but I'm not able to find any docs on how to condition for 'uncaughtException' but I did try:
process.on('uncaughtException', err => {
if (err) return e.sender.send('error', err.message)
return e.sender.send('audit', 'test complete')
})
and the above only works if an error is present, research:
Catch all uncaughtException for Node js app
Nodejs uncaught exception handling
Node.js Uncaught Exception - Passing more details
In Electron how can I intercept the error to pass it to renderer from main without throwing the default error window?
If you use ipcMain.handle you will be able to handle errors in the renderer process like this
// Main process
ipcMain.handle('my-invokable-ipc', async (event, data) => {
await someModule(data)
return 'No issues found'
})
// Renderer process
async () => {
try {
const result = await ipcRenderer.invoke('my-invokable-ipc', data)
console.log(result) // 'No issues found' if someModule did not throw an error
} catch (err) {
// you can handle someModule errors here
}
}
Update: An issue with this approach is that the error emitted to the renderer process is serialized and it gets printed even though it's handled with a try/catch.
To fix this, you can also handle the errors in the main process
// Main process
ipcMain.handle('my-invokable-ipc', async (event, data) => {
try {
await someModule(data)
return 'No issues found'
} catch (err) {
// handle someModule errors and notify renderer process
// return err.message or any other way you see fit
}
})
// Renderer process
async () => {
const result = await ipcRenderer.invoke('my-invokable-ipc', data)
console.log(result) // 'No issues found' if someModule did not throw an error
}

Debugging Worker thread in electron

I'm testing out worker_thread on an electron application. I'm currently using version 11.0.2.
The code is simple and is working and returning the sample data but I cant seem to step into the code.
Main Process:
import { Worker, isMainThread, workerData } from 'worker_threads';
config.ipcMain.on('entries:search', (evt: any, opts: any) => {
if (isMainThread) {
const pathWorker = path.join(__dirname, '../data/entries_worker.js');
const worker = new Worker(pathWorker, {
workerData: opts.value,
});
worker.on('message', (data) => {
debugger;
const d = 1;
});
worker.on('error', (data) => {
debugger;
const d = 1;
});
worker.on('exit', (data) => {
debugger;
const d = 1;
});
}
});
The worker file code:
import { workerData, parentPort } from 'worker_threads';
debugger;
parentPort.postMessage({ status: 'Done' });
I'm using Visual Studio Code and I do put breakpoints and event the debugger statement but it never seems to break into the worker file.
The message event does receive the response from the script { status: 'Done' } and the exit event returns 0.
Any ideas on how I can stop at the breakpoint in the worker file entries_worker.js?
Update
Found the following link about how it's not available right now. I'm not 100% sure if it has changed
ndb allow debugger worker thread. run in develop env like this:
"electron-dev": "ndb electron ."
When you use worker thread, you can found it easy:
You can also add breakpoints debug your code:

Call yeoman generator from code with options

I created a yeoman generator with user interaction, that can be called in the terminal like (after running npm link):
yo mygenerator --name test --path /test/path --project testproject
Now I want to include this generator in my vscode extension.
How can I call the yo generator from my typescript code when the generator when the generator is added as a package.json dependency?
So something like (pseudo code)
import { yo } from 'yeoman';
import mygenerator; // added as a dependency via package.json
const options = {
name: 'test',
path: '/test/path',
project: 'testproject',
};
yo.exec(mygenerator, options, () => {
console.log('yeoman finished')
});
Is something like this possible?
Here is a solution for that:
const env = yeoman.createEnv();
const generatorPath = '../node_modules/generator-name/generators/app/index.js';
env.getByPath(generatorDir);
env.on('error', (err: any) => {
// handle error
});
const options = {
env,
'option1': option1,
'option2': option2,
};
try {
await env.run('name', options);
} catch (err) {
// handle error
}

How to capture responsebody from newman

I want to capture the responsebody in Newman.
const newman = require('newman');
newman.run({
collection: require('./xxx.json'),
iterationData: './data.jsp',
reporters: 'cli'
}, function (err, summary) {
if (err) { throw err; }
console.log('collection run complete!');
console.log(summary);
});
I use the code above. it works fine but I want to capture the json output here from the call. How can I achieve it?
Perhaps you used a wrong term for retrieving json response body. If you want to just get the response body you need to parse JSON returned and to save it to a variable.
If you would use newman to run through command line there are everything is super simple:
let body = JSON.parse(responseBody)
console.log(body)
and after test where you need to see the response you put this 2 lines of code.
But with your case perhaps you need that:
1) Callback option
const newman = require('newman');
newman.run({
collection: require('./xxx.json'),
iterationData: './data.jsp',
reporters: 'cli'
}, function (err, summary) {
if (err) { throw err; }
console.log('collection run complete!');
console.log(summary);
})
.on('request', function (err, data) {
// err, data can be used to write to files using the node fs module.
});
or the better and modern option:
let response = await newman.run({
collection: 'collection',
environment: 'env',
})
.on('request', async function (err, data) {
// err, data can be used to write to files using the node fs module.
});
console.log(response)
Not sure I will be working as expected, but at least try.
Btw, where do you run these tests? just in clear env or use some runner framework.
Postman return execution summary in Callback function. after execution if you save the summary in callback and return it. you can access request/response/ headers.
function runcollection(callback){
newman.run({
collection: 'C:\\newman\\PMP Dependency latest collection\\Updated\\TestCollection.postman_collection.json',
environment: 'C:\\newman\\PMP Dependency latest collection\\Updated\\Test.postman_environment.json',
iterationCount :1
},function(error, summary){
callback(summary)
});
}
runcollection(result => {console.log(result.run.executions[0].response.stream.toString())});

How do I get yeoman to auto overwrite files?

I have a custom generator and am writing some tests for it. Before the app.run() call I already have a app.options['skip-install'] = true to prevent npm from running. But I need it to auto-overwrite files too.
Part way through the install I get a [?] Overwrite client/file.js? (Ynaxdh) and need it to auto-answer it for me.
I have tried app.options.force = true but that doesn't seem to do anything.
I'm running in app install with this:
function installApp(obj, opts, done) {
helpers.mockPrompt(obj.app, opts);
obj.app.options['skip-install'] = true;
obj.app.run({}, function () {
async.series([
function (cb) {
if (opts._extras.addPage) {
installAppPage(obj, cb);
} else {
cb();
}
}
], done);
});
}
Then I want to run a sub-generator with this:
function installAppPage(obj, done) {
helpers.mockPrompt(obj.page, {
pageName: 'webPage',
pageType: 'web'
});
obj.page.args = ['--force']; // This isn't working
obj.page.options.force; // This isn't working either
obj.page.run([], function () {
helpers.mockPrompt(obj.page, {
pageName: 'mobilePage',
pageType: 'mobile'
});
obj.page.run({}, function () {
done();
});
});
}
The sub-generator for the page modifies a file. I need to to just overwrite it so I can test it. How do I force it? I can't be prompted while running the tests, it needs to be automated.
I think you're looking for conflicter.force
obj.page.conflicter.force = true;

Resources