How to automatically run .sh after a file is uploaded - node.js

I am new to Sails/nodeJS. I am trying to create a system that will automatically run a .sh after a video is uploaded to the server (local file system). I understand from Sails documentation that file uploading is done in Controller file, and what I want to do is to trigger a .sh after that. I need to constantly read the output from .sh and return it to the backend.
Anyone has any idea how to implement this?
Thanks!

You always can use https://www.npmjs.com/package/exec
Simple implementation:
var exec = require('exec');
exec('PATH TO YOUR .SH FILE', function(err, out, code) {
// Process to the next action within the callback function.
});

Related

Possible to create file in sources directory on Azure DevOps during build

I have a node script which needs to create a file in the root directory of my application before it builds the file.
The data this file will contain is specific to each build that gets triggered, however, I'm having no luck on Azure DevOps in this regards.
For the writing of the file I'm using fs.writeFile(...), something similar to this:
fs.writeFile(targetPath, file, function(err) { // removed for brevity });
however, this throws an expection:
[Error: ENOENT: no such file or directory, open '/home/vsts/work/1/s/data-file.json']
Locally this works, I'm assuming this has got to do with permissions, however, I tried adding a blank version of this file to my project, however, it still throws this exception.
Possible to create file in sources directory on Azure DevOps during
build
The answer is Yes. This is fully supported scenario in Azure Devops Service if you're using Microsoft ubuntu-hosted agent.
If you met this issue when using microsoft-hosted agent, I think this issue is more related to one path issue. Please check:
The function where the error no such file or directory comes. Apart from the fs.writeFile function, do you also use fs.readFile in the xx.js file? If so, you should make sure the two paths are same.
The structure of your source files and your real requirements. According to your question you want to create it in Source directory /home/vsts/work/1/s, but the first line indicates that you actually want to create file in root directory of my application.
1).If you want to create file in source directory /home/vsts/work/1/s:
In your js file: Use something targetpath like './data-file.json'. And make sure you're running command node xx.js from source directory. (Leaving CMD task/PS task/Bash task's working directory blank!!!)
2).If you want to do that in root of application folder like /home/vsts/work/1/s/MyApp:
In your js file: Use __dirname like fs.writeFile(__dirname + '/data-file.json', file, function(err) { // removed for brevity }); and fs.readFile(__dirname + '/data-file.json',...).

How to automigrate when needed in loopback 3?

I created an automigrate script under /bin in my loopback app and added its path in the package.json file so that I can run this script to automigrate whenever I want from the terminal.
I also have a boot script "createUsers.js" which creates some default users in a model. The problem is, whenever I run this script it calls the boot script and it tries to create the users while automigration is still not finished, resulting in a failed automigration. I don't understand why the boot scripts are called when I only run automigrate script specifically. I could call automigrate in the boot scripts and wrap the createUsers.js code in its callback (as shown here), but that would automigrate every time the app is started which is undesirable since the data is lost on automigration. Where should I call automigrate() so that it can be called whenever required? Any help is greatly appreciated.
What I normally do, is create a script called util.js inside boot
util.js
class util{
static _automigrate(){
//Whatever you need to do
}
}
module.exports = function (server) {
global.util = util;
};
This way your script is available across the entire application. And you can call it whenever you need to.
You could call it with
util._automigrate();
I normally use this pattern to store all my input validations etc since I might need those across different models.

Upload action using Nightwatch.js

I am trying to create an action for nightwatch that will upload files to a given upload form. Is it possible to create that kind of action in nightwatch? The nightwatch is integrated into a nodejs application and is running in a selenium webdriver.
Use AutoIT to create an executable for the upload action. Execute the executable using the exec function of the Node. Upload.exe is the executable file compililed from the AutoIt script.
exec('START "" .\\src\\Resources\\Upload.exe', function(err) {
console.log('err: ' + err);})
This question is already asked here.
The approved answer gives this solution:
.setValue('input#fileUpload', require('path').resolve(__dirname + '/testfile.txt'))
You can take a look here at the discussion that led to this fix.

express-formidable access to events

So being novice in Express and Node, I fear I must be not understanding something fundamental about middleware. I am trying to upload a file using express-formidable, and I've got it to work (as far as taking the file and uploading it to the directory of my choice). However, I would love a progress bar or to be able to do something at the start of the upload such as choose the file name. I see a lot of examples of regular formidable using the 'progress' or 'file' event. But for the life of me I can't figure out how to access this with express-formidable. Since I can't create an IncomingForm, I don't know where the events are or how to bind them.
What am I missing about how express-formidable works? Does it cut out everything about formidable and just stuff a file where you tell it? Or do you have access to everything formidable?
Here's an example of a route I have called ingest, which is where I receive an uploaded file and process it.
app.post('/ingest', function(req, res) {
/*I want to be able to show progress here, and do other things
like set the file name before it's saved, but by the time I get here
the file is already processed and saved, and I can't figure out how
to access events using '.on' if there's no form object*/
});

Running a jar file in bluemix, inside a dialog app

I'm currently trying to create an application like a Virtual Agent, using Watson Dialog. I have to use Node.js with this Watson service, but I have never used it before, so I took my time.
For now, I can use Java to call the dialog service to simulate an user. But I want to use Node.js to call Java to simulate the Agent.
In Watson Dialog, the Agent has a number of sentences written in a file like dialog.xml. But instead I want my Agent to ask specific questions according to the user's profile.
That's why I'm using a BRMS tool, written in Java. I created a .jar and want to call it in /public/demo.js to fill the variable response:
var texts = dialog.conversation.response;
var response = texts.join('<br/>');
I tried this in /public/demo.js:
var exec = require('child_process').exec;
var child = exec('java -jar C:\\PATH\\Example.jar',
function (error, stdout, stderr){
response += stdout;
if(error !== null){
console.log("Error -> "+error);
}
});
Using that code in another application, it works without any problem, I can run my .jar. I'm sure of it. But once written in my Bluemix app, the first line make it crash. Am I missing something in the manifest.yml file? Do I need to change a config? Or maybe it comes from the demo.js file?
Thank you for helping.

Resources