Copy contents of image file to the clipboard using gulp - node.js

I'm writing a gulp task to do the following:
Watch an image file for changes
If image file has changed, copy the contents of the image to the clipboard, ready for me to paste.
Note: I'm on Windows and am using nircmd.exe to do the copy of the image contents. The following command line works for me:
nircmd.exe clipboard copyimage "G:\IMG\pic.png"
I've put this into a .bat file so I can run the file via commandline.
My gulpfile (so far):
var gulp = require('gulp'),
watch = require('gulp-watch'),
shell = require('gulp-shell'),
run = require('gulp-run'),
clipboard = require("gulp-clipboard"),
myTerminal = require("child_process").exec,
commandToBeExecuted = "./copy-to-clipboard.bat";
gulp.task('copy-to-clipboard', function () {
require('child_process').spawn('cmd', ['/s', '/c', '"G:\\Git\\copy-to-clipboard\\copy-to-clipboard.bat"'], {
windowsVerbatimArguments: true
});
});
gulp.task('default', function () {
watch('watch/*.png','copy-to-clipboard');
});
I've tried gulp-shell but since this opens a node terminal, nircmd.exe is not recognised as an internal or external command.
I've also tried child_process (both spawn and exec), but while I get no errors, the contents are still not in the clipboard.
Is there an easier way or is this just not possible?
Thanks in advance!

It sounds like its trying to run it if you are getting a
nircmd.exe is not recognised as an internal or external command.
error.
I would add nircmd.exe to your path to fix it.
or in your bat file you could do something like:
c:\path\to\program\nircmd.exe
that might fix it to.
If you are going to use nircmd that much I would just add it to your path.

Related

Interract with a .exe program with js instead of typing in the program

Hello I'm tryna make a Skyrim server Dashboard.
The server look like this =>
On this server i can type some command like this =>
when I manualy wrote /help and it show the output.
I tried to run the executable in node js, the server is working, I can join it, And I can see the output on my VSCode Terminal
But I can't input some text or command
Hope you can help me thanks in advance.
##Its my first ask
Add shell: true to the spawn method so you can still pass commands to the process.
const child = require('child_process').spawn("C:/SkyrimTogetherServer.exe", {
shell: true
});

How to run the node.js script again if there is change in a folder?

I have created a Node.js script which pick up some images from a directory and do the work and after doing all the process it moves the image to another folder and my script ends but did not exist, so when I add new images in the folder, I need to close the script first and rerun the script so that it will again pick up image and do the work.
fs.readdirSync(Imagefolder).forEach((file) => {
ImagesNames.push("public/" + file);
});
ImagesNames.forEach((element) => {
//do something
});
after this as there is no image code won't run and sit idea, but I am thinking if there is any change in the direcotry it should re run this code.
How will I automate this so that whenever a change in folder/directory automatically node.js script rerun again

My cli engine(npm package) cant find path to read file when install on another folder

I am building an app that auto completes some type of file. When i run node index.js in the program folder i get the correct i results.
Although i want to make it an npm package that can work as a cli engine. For example i want to write the command generate and the code to produce the results.
In order to auto complete the file i have to read some data that i have stored in a .csv file that comes along with my program.
When i try to run generate command under another folder it can read that file.
I am very new to cli and i don't understand yet quite well how thing work.
Thanks in advance for the help.
Here is the code of my cli conversion.
#!/usr/bin/env node
const program = require("commander");
const {
predict
} = require("./classifier");
program.version("1.0.0").description("ESLint Rules Generator");
program
.command("generate")
.description("Generate ESLint Rules")
.action(() => {
predict();
});
program.parse(process.argv);
Here is the problematic line:
let str = fs.readFileSync("data_files/rules.csv", "utf-8");
This is the error i get: ENOENT: no such file or directory, open 'data_files/rules.csv'

Opening an excel file with opn while another excel file is open, will execute code immediately without waiting

I'm using a library called opn. This makes it easier to spawn a child process to open a file in the main process of electron. I could use shell but I need to be able to detect when the file is closed which shell API doesn't provide.
Whenever I open an excel file and there are no other excel files opened, it will work fine and it will wait until I close the file before it resolve the promise. However, if there are other excel files open then it won't wait for the file to be closed and it will resolve the promise immediately.
I did some digging in the source code of the library and the command used to open the file is this for windows: start "" /wait name_of_file where name_of_file is a place holder for the file. The actual command the library executes is different but this one is enough for my use case. I ran into the same problem even when doing it manually without using the library. Maybe I should post this under super user instead of stackoverflow.
Here is the code that opens the file, wait for the file then executes callback.
opn(file, { app: 'EXCEL', wait: true }).then(() => {
const stream = fs.createReadStream(file);
papa.parse(stream, {
delimiter: ',',
complete: (results) => {
mainWindow.webContents.send('uploadNewIndexFileRen', key,
results.data);
},
error: (err) => {
throw err;
}
});
}).catch((error) => { throw error; });
UPDATE:
Here is a quick nodejs repo. All you need to do is clone/fork to check the problem or make a pull request then do npm install to download needed packages. To reproduce the exact problem that I have you might need to be in the following software versions:
Window 10
npm 5.5.1
node v8.9.1
excel 2013
opn 5.2.0 as seen in package.json
This is probably due to the way Excel handles files. I'm guessing Excel handles files with a single process so subsequent opening of files simply signals to the existing process that another files needs opening before closing.
You may be able to force Excel to open a new instance for each file by following these instructions.
Apparently using the /x command line switch will also start a new process:
excel.exe /x

Run a Cakefile Programmatically

I spent a lot of time building this wonderful Cakefile in Coffeescript that builds everything, and now I'd like to be able to run the command cake build from outside of that directory using another alias thats relevant to my program.
Is there any way to run cake build from within a executable file? Something I can have executed by npm under '/bin'?
As Noli says, the only way to do it is to either reverse-engineer cake.js or—more simply—run the cake command from the target directory. Under Node, you can do that using child_process.spawn by setting the cwd option to the desired working directory.
It looks like there's no command line option to do that
https://github.com/jashkenas/coffee-script/blob/master/lib/cake.js#L38
exports.run = function() {
return path.exists('Cakefile', function(exists) {
var arg, args, _i, _len, _ref, _results;
if (!exists) {
throw new Error("Cakefile not found in " + (process.cwd()));
}
So your process will probably need to 'cd' to the directory of your Cakefile first, in order to run it. (Or you can patch coffescript to take an argument)

Resources