Unable to add tasks from the provideTasks function - visual-studio-code

Following on from this question I have another question about how to implement the provideTasks method of the registerTaskProvider.
Using the npm extension as an example I have tried to implement a basic function, to simply return a single, hard-coded, task. This would then be extended to actually parse a file, ready to add dynamic tasks. However, I have been unable to get this to work.
The code that I am trying is:
vscode.workspace.findFiles('**/*.cake').then((files) => {
if (files.length === 0) {
return emptyTasks;
}
try {
const result: vscode.Task[] = [];
result.push(new vscode.Task({type: 'cake', script: 'NuGet-Restore'} as CakeTaskDefinition, 'Nuget-Restore', 'cake', new vscode.ShellExecution('npm install'), []));
console.log(result);
return Promise.resolve(result);
} catch (e) {
return Promise.resolve(emptyTasks);
}
});
Even though I can see that result contains a Task, I don't see it populated in the Task drop down list.
Can anyone offer any help in why this is not working?
A repository with the current code can be found here.
UPDATE
I have just edited the above code to be the following:
try {
const result: vscode.Task[] = [];
result.push(new vscode.Task({ type: 'cake', script: 'NuGet-Restore' } as CakeTaskDefinition, 'Nuget-Restore', 'cake', new vscode.ShellExecution('npm install'), []));
console.log(result);
return Promise.resolve(result);
} catch (e) {
return Promise.resolve(emptyTasks);
}
By not including the initial findFiles function, it correctly populates the Task Menu with the single hard-coded Task. Why is it that I can't do the return from within the findFiles method? I now suspect that this is a TypeScript/JavaScript problem, rather than one with the provideTasks function, but I am still looking for some help on this.
What is the recommended approach for what I am trying to do? Thanks in advance!

You need to return findFiles to ensure the tasks are actually returned from the task provider:
return vscode.workspace.findFiles('**/*.cake').then((files) => {
...
});
Using "noImplicitReturns": true in your tsconfig.json—or, even better, "strict": true—can help catch bugs like this

Related

How to handle non explicit errors inside sails.js helpers?

I am trying to figure out how the Error handling in Sails.js works. Unfortunatley the code examples in the docs do not cover this use case.
The problem is I keep getting this error:
UsageError: `.intercept()` handler returned `undefined`, but this should never happen.
Regardless, here is a summary of the original underlying error:
Now all I am trying to do is call a helper and if it fails, then I want to catch the error (any), log it and run some code. If I wouldn't be using Sails but normal promises I would have handled it like this:
await helper().catch((err) => { // run some code }
In Sails I should be able to use .intercept() instead of .catch()
My code looks like this:
// ExportController.js
const csv = await sails.helpers.files.convertToCsv(data)
.intercept((err) => {
sails.log.error(err)
req.addFlash('error_messages', 'Error parsing data to csv!')
return res.redirect(`/`);
})
// convert-to-csv.js
if (!Array.isArray(inputs.data)) {
throw new Error('invalid inputs.data type: ' + typeof inputs.data)
};
Now how can I avoid getting this error?
The code examples show only cases where errors that are explicitly added to the exits object are handled, but not for general error handling.
In the docs it says that if the filter argument is
not provided, ALL errors will be intercepted.
Or is that only true for db queries? Because the .intercept() doc section is in that subcategory.
You could use “throw ‘errorCode’;” for example:
Set the exits:
exits {
errorWithCsvFile: {
responseType: 'badRequest'
}
}
const csv = await sails.helpers.files.convertToCsv(data)
.intercept(‘somethingWrongCode’, ‘errorWithCsvFile’)
... // Other handles
.intercept(err => new Error(err))
Alternative:
try {
...
const csv = await sails.helpers.files.convertToCsv(data)
.intercept((err) => {
sails.log.error(err)
req.addFlash('error_messages', 'Error parsing data to csv!')
throw 'badRequest';
})
...
} catch (err) {
sails.log.err(err);
return res.redirect(`/`);
}

protractor browser.executescript does not return a value

I'm trying to run the following scripts in Protractor
browser.executeScript("return window.localStorage.getItem('access_token');").then((accessToken) => {
console.log("AccessToken Obtained ", accessToken);
});
However the code never goes into the .then section. It just gets blocked. I tried executeAsyncScript and still the same result.
I went through a lot of online examples and I think this should work, however not sure whats going wrong here.
Could you try this one:
function getAccessToken() {
return window.localStorage.getItem('access_token');
}
browser.executeScript(getAccessToken).then((accessToken) => {
console.log("AccessToken Obtained ", accessToken);
});

Protractor- automate the error message on tab out when input field is empty

I have an angular2 application where I am trying to write end to end test cases to automate things.I have just begun with learning Protractor for this and trying to implement a negative test case for a form field where if any field is empty, the error message should be shown. I have tried something like below to automate the form and its working fine.
In my spec.ts-
import userDetailsPage from './userDetails.e2e-po;
it('should fill out User Details', () => {
const userDetail: IUserDetail = {
firstName: 'Lorem',
lastName: 'Ipsum'
};
userDetailsPage.populateUserDetails(userDetail);
});
In userDetails.e2e-po-
populateUserDetails(details: IUserDetail) {
this.fillFirstName(details.firstName)
.fillLastName(details.lastName)
return this;
}
I am writing the below code which automatically inputs the firstName and lastName field.
fillLastName(last: string) {
let el = element(by.css('input[name="lastName'));
el.clear().then(() => {
el.sendKeys(last);
});
return this;
}
The above scenario works fine. But I am also trying to achieve a scenario where I do not input either first name or last name field, should throw me an error message.Can someone let me know what else should I add to achieve this.
I am already handling the validation in my HTML.
Any help is much appreciated.
Instead of details.firstname and details.lastname put empty strings and then validate the error that occurs on the page.
I think you can try the following method as a reusable function
function formValidate(donefn){
newProjBtn.click().then(async function () {
var lastName_fld = element(by.css('input[name="lastName'));
await lastName_fld.sendKeys("", protractor.Key.TAB);
//browser.sleep(2000);
var elm = element(by.css(".error-message"));
elm.isPresent().then(function(result){
if(result){
console.log("Error message displayed")
//some more code to do like selecting the field and enter the test
return result;
}else{
console.log("Error message not displayed")
return result;
}
})
donefn();
})
I solved it in this way:
await input.sendKeys(protractor.Key.CONTROL, 'a');
await input.sendKeys(protractor.Key.BACK_SPACE);
await input.sendKeys(protractor.Key.TAB);
//then the error-message will appear

Returning multiple server operations in Meteor in one call. How to?

I've very new to this but essentially I want to create a cleanup function that runs on the server that I can call at any time to reset various things like collections and sessions in one call.
I'm really very new but this is what I have so far. Can someone please help fill me in where I'm going wrong?
I am trying essentially to return two things (and many more in the future) at once. I've done some research on this but it's as far as I can fathom with my skill level at the moment.
It would be much appreciated. Thank you.
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
return Meteor.methods({
//Use this to emplty the form data
cleanUpForms: function() {
var cleanUpPhoneNumbers = orgPhoneNumbers.remove({});
var cleanUpEmailAddresses = orgEmailAddresses.remove({});
return {
cleanUpPhoneNumbers : cleanUpPhoneNumbers;
cleanUpEmailAddresses : cleanUpEmailAddresses;
}
}
});
});
}
By the way, the current error is for line :
cleanUpPhoneNumbers : cleanUpPhoneNumbers;
It states:
Unexpected token
I'm not sure if I'm doing this correctly. I essentially want it to run multiple cleanups in one go, all called from the client to the server with the above method. I hope that makes sense.
The unexpected token is likely for the ; at the end of the line. When building a JSON object, use a comma between the elements...
return {
cleanUpPhoneNumbers : cleanUpPhoneNumbers,
cleanUpEmailAddresses : cleanUpEmailAddresses
}
I think this will return the number of items that where removed. Is that what you are expecting?
Also, just in case you didn't know, you can run 'meteor reset' from the command line to erase ALL collections.
This is the fully adjusted code for any future reference which may help others. Thanks so much to FloatingCoder for the help.
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
return Meteor.methods({
removeAllNewOrgs: function() {
var PhoneNumbers = newOrgPhoneNumbers.remove({});
var Organsations = newOrgansations.remove({});
//If we want to return the data, to get around only being able to return one thing at a time we're return via an array. CLEVS!
return {
PhoneNumbers : PhoneNumbers,
Organsations : Organsations
}
}
});
});
}

Meteor onRendered function and access to Collections

When user refresh a certain page, I want to set some initial values from the mongoDB database.
I tried using the onRendered method, which in the documentation states will run when the template that it is run on is inserted into the DOM. However, the database is not available at that instance?
When I try to access the database from the function:
Template.scienceMC.onRendered(function() {
var currentRad = radiationCollection.find().fetch()[0].rad;
}
I get the following error messages:
Exception from Tracker afterFlush function:
TypeError: Cannot read property 'rad' of undefined
However, when I run the line radiationCollection.find().fetch()[0].rad; in the console I can access the value?
How can I make sure that the copy of the mongoDB is available?
The best way for me was to use the waitOn function in the router. Thanks to #David Weldon for the tip.
Router.route('/templateName', {
waitOn: function () {
return Meteor.subscribe('collectionName');
},
action: function () {
// render all templates and regions for this route
this.render();
}
});
You need to setup a proper publication (it seems you did) and subscribe in the route parameters. If you want to make sure that you effectively have your data in the onRendered function, you need to add an extra step.
Here is an example of how to make it in your route definition:
this.templateController = RouteController.extend({
template: "YourTemplate",
action: function() {
if(this.isReady()) { this.render(); } else { this.render("yourTemplate"); this.render("loading");}
/*ACTION_FUNCTION*/
},
isReady: function() {
var subs = [
Meteor.subscribe("yoursubscription1"),
Meteor.subscribe("yoursubscription2")
];
var ready = true;
_.each(subs, function(sub) {
if(!sub.ready())
ready = false;
});
return ready;
},
data: function() {
return {
params: this.params || {}, //if you have params
yourData: radiationCollection.find()
};
}
});
In this example you get,in the onRendered function, your data both using this.data.yourData or radiationCollection.find()
EDIT: as #David Weldon stated in comment, you could also use an easier alternative: waitOn
I can't see your collection, so I can't guarantee that rad is a key in your collection, that said I believe your problem is that you collection isn't available yet. As #David Weldon says, you need to guard or wait on your subscription to be available (remember it has to load).
What I do in ironrouter is this:
data:function(){
var currentRad = radiationCollection.find().fetch()[0].rad;
if (typeof currentRad != 'undefined') {
// if typeof currentRad is not undefined
return currentRad;
}
}