Only run my sails.js hook on `sails lift`, not `sails run` - sails.js

I have a custom sails.js hook which implements a Websocket server (we can't use the built-in socket.js hook because we needed to match an old API).
I also have a custom sails run script that does background processing.
Right now, when the sails run -name-of-my-script command is run, it also runs my hook which makes extra listeners for all of the events used by this hook.
I would like to make it that the hook only starts on a main application startup (sails lift or the equivalent node app.js), not on sails run ....
I have looked through the code and the documentation, but I can't seem to see any official way to do this.
I can think of two options:
In the hook, check for whether a script is being run and don't initialize.
In the script, disable the hook.
Is there any way to do either of those things?

Right now, there is no built-in way to do this because Sails scripts have no way to modify configuration - they use the default sails instance with its default settings.
Here is the hack that I used to make it work:
Add a check for a configuration option in the hook's initialize method:
if (app.config.notifier.active === false) {
sails.log.verbose('Notifier disabled');
return cb();
}
// Continue with notifier hook setup here
Add setting an environment variable to the top of the script (before the module.exports:
process.env.sails_notifier__active = false;
(Note: Step 1 is based on this answer but the package linked there no longer uses this technique. That question and answer are also extremely old -- at least as Sails.js answers go -- from before sails run existed.)

Related

VS Code API - remote calls

I am currently investigating VS code extensions in conjunction with a code generation project. While I understand that the VS code API is fairly full-fledged to allow in-extension manipulation of documents I can't figure out how to do this outside of the extension.
Are there ways of remotely executing the vs code JavaScript APIs outside of vscode which in turn will drive the GUI.
I am trying to figure out if I can do any of the following:
Execute the API JavaScript code when through the code cli
Run a web server/socket within an extension to listen to events from external systems and execute the JS API accordingly
If not socket/rest server, should I listen to filesystem changes and react accordingly?
Basically, what are the options for remote control driving of vs code?
I've faced a similar problem with an editor extension I'm working on.
I would highly recommend listening to file changes outside of VSCode APIs. This way the majority of your code is not bound to a specific editor. I also had much more difficulty using the VSCode API called createFileSystemWatcher as it didn't capture all changes.
Upon activation, run a child process using a file watcher library. I'd recommend using chokidar - its the same library VSCode uses internally but with more extensive access to the API.
// get your workspace uri & root uri
const workspaceRoots = vscode.workspace.workspaceFolders
if (!workspaceRoots || !workspaceRoots.length) {
throw new Error('No workspace root path')
}
const workspaceRoot: vscode.WorkspaceFolder = workspaceRoots[0]
const rootUri = vscode.workspace.getWorkspaceFolder(workspaceUri);
// the action you want to trigger
const command = vscode.commands.registerCommand('YOUR_CUSTOM_COMMAND', yourCustomFunction)
// file watcher
const fsWatcher = chokidar.watch('watcher_name', {
cwd: rootUri.uri.path,
interval: 1000,
})
// listen to any add/update/delete fs events
fsWatcher.on('change', (path, event) => {
vscode.commands.executeCommand('YOUR_CUSTOM_COMMAND')
})
On any file change, it can trigger the action of your choice.
If a VS Code extension can achieve everything you are after, then the simplest approach would be to let the extension be driven externally. VS Code extensions run in a node.js environment, so they can start a http server, listen on a named pipe, or communicate with the outside world using pretty much any other mechanism.
You can also take a look at VS Code support for testing extensions, since tests also just use standard VS Code extension apis: https://code.visualstudio.com/api/working-with-extensions/testing-extension
For the development of VS Code itself, there are some automation scripts that simulate user actions. If you really need to, I believe you could adopt these script to your use case, however I can't say how much work it would be. These script also break if the UI changes. Better to go the extension route if possible

How to configure Play for test environment

I have an app in Play 2.6 and Scala and I want to configure all my Action to return Future or not, or i could say, to be Action or Action.async by a config file. So I could configure my entire app to work in production or test environment.
I have no clue how to do that. Hpw can I start to study and implement it?
Thank you
I wonder if you're looking for something like this which would allow you to provide environment specific configuration values? Including how to use those values in your controller
That document also covers how to specify which application.conf to use via the command line.

How do get provideTasks method of registerTaskProvider working

I am trying to get the new registerTaskProvider method in the VSCode Task API working within my extension, and I have been unable to make this work so far.
I have used the npm extension as a basis. Here are the steps that I have followed:
Used yo to create a new extension
Updated package.json to include a new activationEvent onCommand:workbench.action.tasks.runTask
Updated package.json to include configuration and taskDefinitions sections in the contributes section
Added the following code to the extension.ts file
NOTE: I realise that the linked code won't actually provide any additional tasks, I am just using this code as a basis for testing.
Now, when I try to debug the extension, the provideTasks method is never invoked. What am I missing?
Also, the only way to have the activate method called is when I invoke the Hello World command. However, I might not have a command associated with the extension. How can I force the activation of the extension?
Is there any additional documentation on how to get started with the registerTaskProvider API?
I have added a sample repository that has the current work to date.
Turns out that this was a case of PEBKAC.
After a Twitter discussion with Erich Gamma, he showed me that the provideTasks method is only consulted when you begin running a task. As shown here:
https://twitter.com/ErichGamma/status/885823516293177346
I had assumed that the provideTasks method would be consulted on activation of the extension.

Bluemix DEA->Diego How can I handle the rename of VCAP_APP_PORT AND VCAP_APP_HOST without changing application code?

I have tested a few apps with enable-diego on Bluemix. They all broke because of this change. I could fix most, one I'm still struggling with. And none of which I actually wanted to make code changes in.
Instead of code changes being required to my apps, can I automate the mapping of the variables at container level (or any other solution)?
Not tried this but maybe you could create a user provided environment variable and set
VCAP_APP_PORT = $PORT
edit:
You can set user-provided environment variables using the Bluemix UI or using the cf set-env command.

sails.js blueprints override as hook

A while back I asked how to override sails.js blueprints (CRUD blueprint overriding in sails.js)
With v0.11 of sails.js we now have blueprints (and they are awesome :)).
Is it possible to turn off the current blueprints and install a new version of them as a hook?
This issue comment (https://github.com/balderdashy/sails/pull/2173#issuecomment-54165548) from #sgress454 seems to indicate it is/was in the works, but I can't find anything more specific about it.
I know I can override by creating an api/blueprints folder, but it would be easier for my users to consume via an npm install.
You are right, this is possible.
It is actually what happens in Sails's core here : https://github.com/balderdashy/sails/tree/master/lib/hooks/blueprints
Default blueprints are loaded like an Installable Hook would be.
You can just go ahead and copy that MIT licensed code and then replace the content of the actions directory with your own blueprints.
After that, update the object BlueprintController in index.js to make sure it points to your files.
Finally, all the magic happens in the extendControllerMiddleware method. You can see the code will go through each controller to inject your blueprints. The key point here is the replace the call to _.defaults with _.assign because your sails hook will want to overwrite defaults blueprints and not simply add them if they do not exist.