Open Directory in Node.js and node-webkit - javascript

I have a function that should open a directory after it was created,
setTimeout(function()
{
var fs = require('fs');
console.log(newPath);
var open = fs.opensync(newPath, 'r');
}, 2500);
But this doesn't seem to work. I am getting the following errors
first is,
TypeError: undefined is not a function
at eval (eval at <anonymous> (file:///Users/proslav/Library/Developer/Xcode/DerivedData/trackingCore-ecxfviftqracjxhimcuhhhvyddso/Build/Products/Debug/trackingCore.app/Contents/Resources/timeBroFront.app/Contents/Resources/app.nw/js/jquery-1.10.2.min.js:3:4994), :43:18)
and second is,
Uncaught ReferenceError: require is not defined
I was thinking that it could be that my variable newpath is undefinded but the log shows me the right link.
The creation of the directory with var fs = require('fs'); works fine.
What am I doing wrong here?

I found out how it has to be done. Node-webkit offers a function for that. It is working on MAC and should also work on windows.
The function below is an example function. nw.gui and gui.Shell.showItemInFolder did the thing for me. Thx for the input.
/*---------
Open Folder
---------*/
function openFolder(path){
var gui = require('nw.gui');
gui.Shell.showItemInFolder(path);
}

In nw.js version 0.13 or later, use:
nw.Shell.showItemInFolder(fullpath);
Version < 0.13:
var gui = require('nw.gui');
gui.Shell.showItemInFolder(fullpath);
Note that the full path name is required. If it doesn't exist, it will fail silently.
If the path is something like c:\foo\bar.txt, it will open the folder foo and highlight the file bar.txt.
If the path is c:\foo\foo2, it will open the folder foo and highlight the folder foo2 (I expected it to open the folder foo2, but it will open the parent).
To find the fullpath of the running app, as we can't use node functions in our front-end (that's why you had the error trying to load the fs module), I've created a node module (utils.js) with the following:
exports.getFullPath = function(fileName) {
var path = require('path');
return path.resolve(__dirname, fileName);
}
In the front-end:
function openFolder(path) {
var utils = require('./utils');
var fullpath = utils.getFullPath(path);
nw.Shell.showItemInFolder(fullpath);
}

Related

Error when importing .js file in one folder to spec.js file in another folder

I am new to the protractor, and trying to create a project in cucumber using POM. Following is the structure of the project:
In the addCustomerPage.js, I have mentioned the locators as well as the functions to perform a test:
var addCustomerPage = function () {
var BankManagerButton = element(by.buttonText('Bank Manager Login'));
***Other locators*****
this.create = async function(fName,lName,pCode){
await BankManagerButton.click();
****rest of the steps*****
}
}
module.exports = new addCustomerPage();
But when in the spec.js, import the above class, on running the code, it throws the error:
E/launcher - Error: Error: Cannot find module '../pages/addCustomerPage'
Following is the spec.js file's code:
var {
setDefaultTimeout
} = require('cucumber');
const {
expect
} = require('chai');
setDefaultTimeout(10 * 1000);
var addCustomerPage = require('../pages/addCustomerPage');
Given('I open the application and click on create customer button', async function () {
**code*****
});
When('I enter {string}, {string}, {string}', async function (fname, lname, pcode) {
return await addCustomerPage.create(fname, lname, pcode);
});
However, this works fine if the pages folder is under the features folder. Can anyone help on what am I doing wrong here?
../
The symbol above is signalling to go up one file directory.
When the variable is declared like this...
var addCustomerPage = require('../pages/addCustomerPage');
... your computer will go up one folder level from the current directory and search for the pages folder and not find it.
When you copied the pages folder and put it under the feature folder, it can detect it because it falls under the directory that you were searching it for
The solution is to:
Just paste your pages folder under features
or
Modify the file path in the variable to where your pages folder is located
Im guessing you have to go up a directory or two, so use this command ../ to get to where your page folder is
var addCustomerPage = require('../../pages/addCustomerPage');
The idea is to modify the file path to wherever the page folder might be

Nodejs Browserify Uncaught TypeError: exists is not a function

I am new to Browserify and trying the following:
I created a node server and trying to get a package called 'openbci' running on the browser.
so I have the following file structure:
Myapp
-...
-public
--app.js
--index.html
--openBCI.js
--...
--javascript
---openBCI
----bundle.js
---...
-node_modules
--openbci
---openBCIBoard.js
--browserify
--...
my app.js file sets the server to serve the public folder
// app.js
var express = require('express');
var app = express();
app.use(express.static('public'));
app.listen(myPort);
then I created the following openBCI.js
// openBCI.js
var OpenBCIBoard = require('openbci').OpenBCIBoard;
exports.OpenBCIBoard = OpenBCIBoard;
and finally launched the browserify command:
$ browserify public/openBCI.js > public/javascript/openBCI/bundle.js
but once called in my index.html file, I got an Uncaught TypeError: exists is not a function at Function.getRoot:
exports.getRoot = function getRoot (file) {
var dir = dirname(file)
, prev
while (true) {
if (dir === '.') {
// Avoids an infinite loop in rare cases, like the REPL
dir = process.cwd()
}
**if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {**
// Found the 'package.json' file or 'node_modules' dir; we're done
return dir
}
if (prev === dir) {
// Got to the top
throw new Error('Could not find module root given file: "' + file
+ '". Do you have a `package.json` file? ')
}
// Try the parent dir next
prev = dir
dir = join(dir, '..')
}
}
It appears that it could not find the original path for the module.
Could you please tell me what is to change? Or if I understood at all how browserify works ? :)
I notice a few things that seem strange about the code.
exists is undefined in JavaScript or node. It appears to be an alias of fs.exists - is that right?
If so, fs.exists is deprecated. Per the documentation, you can achieve the same effect with fs.stat or fs.access. Note however that you should either supply a callback (preferable) or use the Sync version of these methods.
If you are trying to use file system tools in the browser you are going to run into problems because you are attempting to access the server's file system from the browser. There is a plugin, browserify-fs, that gives you an equivalent to fs in the browser. However, this seems to access the browser's local IndexedDB, not the storage on your server.
I would suggest running code that relies on server-side files on the server, rather than in the browser.

node.js Error: EISDIR, open

I tried to uplaod file and move to new directory already exists.
follow Writing files in Node.js but I got the error:
Error: EISDIR, open '/Users/name/Sites/project/app/assets/images/UploadTemporary/'
at Error (native)
and I found Using Node.js I get, "Error: EISDIR, read" and Node.js Error: EISDIR, open Error similar error message, my UploadTemporary folder already exists do I mess something wrong?
I don't get it, if its not a directory what else can be?
var multipart = require('connect-multiparty');
var fs = require('fs');
var path = require('path');
var appDir = path.dirname(require.main.filename);
...
var sourceFile = req.files.file[0].path;
var destinationFile = appDir + '/assets/images/UploadTemporary/';
var source = fs.createReadStream(sourceFile);
var destination = fs.createWriteStream(destinationFile);
source.pipe(destination);
source.on('end', function () {
fs.unlinkSync(sourceFile);
});
When you are writing a file to a specific directory, you need to give the actual destination file name as well. Unlike cp command, the destination filename will not be inferred by fs module.
In your case, you are trying to write to a directory, instead of a file. That is why you are getting EISDIR error. To fix this, as you mentioned in the comments,
var destinationFile = appDir + '/assets/images/UploadTemporary/' + newfilename;
include the file name as well.

Referencing non-exportable file in node module

I am trying to reference an html document in a "self containing" module. The module is comprised of two files:
app_root/node_module/my_module/main.js
app_root/node_module/my_module/header.html
main.js contains:
module.exports.test = function() {
var doc = fs.readFileSync('./header.html');
console.log(doc);
}
when i run my program in app_root/program.js
require('my_module').test();
When i start my app, the current working directory is set to app_root. When it tries to read ./header.html it breaks because the paths aren't correct.
How would I find directory of the installed module without knowing anything about what is running it?
You can use __dirname to refer to the path of the current script.
So main.js would become:
module.exports.test = function() {
var doc = fs.readFileSync(__dirname + '/header.html');
console.log(doc);
}

How do I get a simple tree of files to concat using broccoli.js

I am trying to get my head around broccoli.js but am running into an error. If you look in my GitHub repo, you will see that I have a brocfile.js that looks like this:
brocfile.js
var compileES6 = require('broccoli-es6-concatenator')
var pickFiles = require('broccoli-static-compiler');
var mergeTrees = require('broccoli-merge-trees')
var app = 'app'
app = pickFiles(app,{
srcDir: '/',
destDir: 'app-kit'
})
var appJS = compileES6(app, {
inputFiles: ['/app-kit/**/*.js'],
outputFile: '/dist/app.js'
})
module.exports = mergeTrees([appJS]);
and I am getting this error:
Built with error:
Error: ENOENT, no such file or directory 'tmp/static_compiler-tmp_dest_dir-JjclDVEm.tmp/undefined'
at Object.fs.statSync (fs.js:684:18)
at addLegacyFile (/Users/cully/Sites/ember-todo/node_modules/broccoli-es6-concatenator/index.js:130:44)
at /Users/cully/Sites/ember-todo/node_modules/broccoli-es6-concatenator/index.js:47:5
at invokeCallback (/Users/cully/Sites/ember-todo/node_modules/broccoli/node_modules/rsvp/dist/commonjs/rsvp/promise.js:228:21)
at publish (/Users/cully/Sites/ember-todo/node_modules/broccoli/node_modules/rsvp/dist/commonjs/rsvp/promise.js:176:9)
at publishFulfillment (/Users/cully/Sites/ember-todo/node_modules/broccoli/node_modules/rsvp/dist/commonjs/rsvp/promise.js:312:5)
at flush (/Users/cully/Sites/ember-todo/node_modules/broccoli/node_modules/rsvp/dist/commonjs/rsvp/asap.js:41:9)
at process._tickCallback (node.js:415:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
In the long run I would like to use this as a starting point for building ember.js apps so I created a github repo. If you go there you can see my file structure. Any thoughts on why I am getting this error?
I've been wrestling with Broccoli myself for the last few days. It's new to me as well, but I cloned your repo and got broccoli serve to work by doing the following:
1) I created a "loader.js" file at loader/loader.js. The loader.js file contains ES6 Module Loader polyfill code to allow ES6 modules to load in non-ES6 browsers. I grabbed the file from the Broccoli sample app: https://github.com/joliss/broccoli-sample-app/blob/master/vendor/loader.js
2) I injected the loader into the app tree and referenced it in the compileES6 config. (This option was the undefined value in your error.)
Here's the updated brocfile.js that successfully builds for me.
//brocfile.js
var compileES6 = require('broccoli-es6-concatenator');
var pickFiles = require('broccoli-static-compiler');
var mergeTrees = require('broccoli-merge-trees');
var app = 'app';
app = pickFiles(app,{
srcDir: '/',
destDir: 'app-kit'
});
// Reference to ES6 loader
var loader = "loader";
// Squash the loader and app trees into one
var sourceTrees = [app, loader];
sourceTrees = new mergeTrees(sourceTrees);
var appJS = compileES6(sourceTrees, {
loaderFile: 'loader.js', // This required option was undefined before
inputFiles: ['app-kit/**/*.js'],
outputFile: '/dist/app.js'
});
module.exports = mergeTrees([appJS]);
I hope that helps you get to the next step on your Broccoli adventure.
You can use broccoli-concat, then:
emberTests = concatFilter(emberTests, {inputFiles: ['**/*.js'], outputFile:'/tests.js'});
templates = concatFilter(templates, {inputFiles: ['**/*.handlebars'], outputFile:'/templates.js'});
trees = concatFilter(trees, {inputFiles: ['**/*.js'], outputFile:'/app.js'});

Categories