node-webkit windows file system seperator? - javascript

Im currently working on a simple file-explorer with NW.js
Im developing on a linux-machine and there it works fine until now, but i tested it on a windows-system at work and there is a problem with listing all files in a directory. I developed it to work on both systems (i thought) here´s a link to my repo please note the file: js/main.js where i set the seperator-variable to be "\" on windows platforms (in function: getRootDir()).
In JS this:
alert("\\");
gives me: "\"
isnt that the seperator for windows?
Any help would be appreciated.

I was playing around a bit and found out that the async one works and the sync one doesnt - here the async one:
fs.lstat(rootElement.path + seperator + file, function(err, stats) {
if (err) {throw err;}
if (stats.isDirectory()) {
createFolderView(rootElement, file);
} else {
createFileView(rootElement, file);
}
});
and here the sync one:
if (fs.lstatSync(rootElement.path + seperator + file).isDirectory()) {
createFolderView(rootElement, file);
} else {
createFileView(rootElement, file);
}
but shouldnt work both the same way - or am i missing something?

Related

Using Javascript as hook for cordova project in Visual Studio 2017

I'm having a cordova project running in Visual Studio. Before the build process I want to use the hook functionality in order to copy an additional gradle file to the corresponding platforms folder. For copying this file I'm using a simple javascript file. I want to use js because it is independent of the OS.
I use the following code from here:
source = "D:\\myProject\\config\\build-extras.gradle";
target = "D:\\myProject\\platforms\\android\\build-extras.gradle";
var fs = require('fs');
var cbCalled = false;
var rd = fs.createReadStream(source);
rd.on("error", function(err) {
done(err);
});
var wr = fs.createWriteStream(target);
wr.on("error", function(err) {
done(err);
});
wr.on("close", function(ex) {
done();
});
rd.pipe(wr);
function done(err) {
if (!cbCalled) {
console.log("Something wrong with copy of build-extras.gradle: " + err);
cbCalled = true;
}
}
When I run this code in a console with node, then the script is copying the file as expected.
However, when I include the same script in my config.xml file of cordova the script is doing something different: It creates a file with the defined name but the file is always empty.
Does anybody know what's wrong here?
Thanks, Peter

Ionic 3 Prod Build With Version Number

I use the following command when building an ionic project for desktop
ionic cordova build browser --prod
Which results in the following file being generated
build/main.js
However I would like to be able to add a version number to the generated file automatically as part of the build process. So would end up with something like
build/main.js?version=1.00
as to avoid needing to clear the browser cache after every prod build.
Is there a flag for this, or is it something I must do manually?
Any advice would be great!
EDIT:
My solution is on GitHub for anyone interested!
https://github.com/RichardM99/ionic-3-version-build-file-hook
Here's some advice - You can create a cordova hook.
Hooks are scripts that you want to be executed at different stages of the build process. In your case, you are looking at a script which renames the main.js file after the build event is finished, or in other words a 'after_build' type hook.
The script will usually be a Node.js file, although you can have other types of scripts executed as well.
One more thing. Since you want to get around cache, you wont be renaming the file itself. What you will want to do is rather replace the reference to "main.js" in you "index.html" to include a random or maybe your actual version number.
I have pointed you in a direction, but won't spoonfeed. Look up documentation on cordova hooks. They are super simple if you understand Javascript/Node
Something like this should get the job done:
var index_orig = fs.readFileSync(path-to-index.html, 'utf8');
var index_new = index_orig.replace("main.js", "main.js?version="+version_num);
fs.writeFileSync(path-to-index.html, index_new, 'utf8');
If you want the actual build number, you can read your config.xml and parse it to get it's value.
Hope it helps.
I wrote blog long time ago
In my build pipeline i have command to set version
version "$(app.versionPrefix)$(Build.BuildNumber)"
$(app.versionPrefix) - is a prefix version such as 0.1.
$(Build.BuildNumber) - is build version
Then I have environment file
export const environment = {
apiUrl: 'https://....',
production: true,
version: '0.0.57'
}
Then i have js script to update version in environment and config.xml
var replace = require('replace-in-file');
var package = require("./package.json");
var buildVersion = package.version;
const options = {
files: ['config.xml'],
from: /" version="([0-9]*.[0-9]*.[0-9]*)"/g,
to: "\" version=\""+ buildVersion + "\"",
allowEmptyPaths: false,
};
const optionsEnv = {
files: ['src/environments/environment.prod.ts'],
from: /version: '(.*)'/g,
to: "version: '"+ buildVersion + "' ",
allowEmptyPaths: false,
};
try {
let changedFiles = replace.sync(options);
if (changedFiles == 0) {
throw "Please make sure that file '" + options.files + "' has \"version: ''\"";
}
changedFiles = replace.sync(optionsEnv);
if (changedFiles == 0) {
throw "Please make sure that file '" + optionsEnv.files + "' has \"version: ''\"";
}
console.log('Build version set: "' + options.to + '"');
}
catch (error) {
console.error('Error occurred:', error);
throw error
}
NOTE: you need to install plugin replace-in-file
Then in build pipe line I am running this script
node ./replace.build.js
In your case if you need only for browser you can tune script.

FileError {code: 5} trying to use absolute file paths with Meteor 1.4 Cordova

I'm trying to use Meteor 1.4 with Cordova (mobile support) to read files on my android phone.
I've got a small Meteor app running on my phone (via meteor run android-device). I have also created a small text file in the app's private storage using adb shell and run-as.
I can read the file if I call getFile('a'), but I can't find any way to use cordova.file.dataDirectory (even with WebAppLocalServer.localFileSystemUrl) as recommended in the Meteor guide. I get FileError {code: 5} (ENCODING_ERR).
Here's the JS console output from the Chrome inspector when I click the button in my app:
This repros on both Android 5 and 6.
My dev environment is 64-bit Ubuntu 14.04 LTS.
App Info for my app reports it has these permissions:
modify or delete the contents of your SD card
read the contents of your SD card
The app uses the cordova file plugin version 4.2.0.
Dup? How to read/write a file in Meteor-Cordova?
This does work in the chrome://inspect console via USB debugging: HTTP.call('GET', 'http://localhost:12640/local-filesystem/data/user/0/com.adammonsen.app/files/a', {}, function (e,r) {console.error(e); console.log(r)});
output is Object {statusCode: 200, content: "in external dir↵", headers: Object, data: null}
Got it. The trick is to use window.resolveLocalFileSystemURL() to get at the filesystem. Here's how to get at a file on external storage. My original question was about app-private storage. If you're looking for that, just use the cordova.file.dataDirectory instead of cordova.file.externalDataDirectory.
function reportError(err) {
console.error(err);
}
function readFile(file) {
var reader = new FileReader();
reader.onloadend = function() {
console.log('read file! contents: ' + this.result);
};
reader.readAsText(file);
}
function processFileEntry(fileEntry) {
fileEntry.file(file => {
readFile(file);
}, reportError);
}
Template.hello.events({
'click button'(event, instance) {
// increment the counter when button is clicked
instance.counter.set(instance.counter.get() + 1);
if (Meteor.isCordova) {
// aaaaaand do some other stuff
const path = cordova.file.externalDataDirectory + 'a';
window.resolveLocalFileSystemURL(path, fileEntry => {
console.log('fileEntry: ', fileEntry);
processFileEntry(fileEntry);
}, reportError);
}
},
});
See https://github.com/meonkeys/cordova-file-test/tree/solution for a full working example and instructions.
Make sure AndroidExtraFilesystems includes files-external. I used this line in my mobile-config.js:
App.setPreference('AndroidExtraFilesystems', 'files,files-external');
I'd love to understand the difference between the APIs, but for now I'm just happy I found something that works reliably.

I tried to use browserify with 'temp' and 'fs' modules, and all I got was this lousy error

I'm trying to migrate a node project to run in the browser. The project (nevermind the reason for the moment) writes to a temporary file, using temp with code similar to this:
var temp = require('temp');
var fs = require('fs');
temp.open('helloworld.txt', function (err, info) {
if (err) {
console.log('Failed to open a temp file:', err);
return;
}
console.log('temp: ', info.path);
fs.writeFile(info.path, 'Hello World!', function (err2) {
if (err2) {
console.log('Failed to write to a temp file:', err2);
return;
}
});
});
When running the browserified code I get the following error:
Uncaught TypeError: undefined is not a function
for the following line:
fs.open(filePath, RDWR_EXCL, 0600, function(err, fd) {
which is called by temp.open.
I'm very much aware you cannot write files using plain ol' javascript in the browser, but I was wondering if browserify have some replacement for that, especially since we're talking about a temp file.
I found browserify-fs, and tried:
browserify -r fs:browserify-fs my-example.js -o bfied.js
but I get the exact same error. I tried to replace require('fs') with require('browserify-fs') but this resulted in a similar error as well (I of course installed browserify-fs module..)
Any suggested solution would be appreciated :) If this type of migration is not possible, I'd also be glad to know..
Thanks!
I stumbled across this post as I think I was trying to do pretty much the exact same thing as you. I am not sure where you got to with this, but I found that when using browserify-fs, you cannot access files outside the sandboxed state of the browser.
So when trying...
var fs = require('broserify-fs);
fs.read('hello.txt', 'utf8' ....
I got the same error as you, but when doing this...
fs.mkdir('/home', function() {
fs.writeFile('/home/hello-world.txt', 'Hello world!\n', function() {
fs.readFile('/home/hello-world.txt', 'utf-8', function(err, data) {
console.log(data);
});
});
});
Everything worked fine (using just temporary files I think).
I am super new to this so I'm not sure what the best approach is. I imagine you either want to run a Node.js server locally and issue ajax commands to it to perform read/write operations, or investigate the HTML5 file system stuff - this looks like a good article - http://www.html5rocks.com/en/tutorials/file/dndfiles/
Hope that's some help (sorry you've had to wait 2 months for an answer ;) It was serendipitous I happened to stumble across your question and my heart sank when there were no answers)
Tom

fileUpload phonegap plugin does not work in iOS

I am using fileupload phonegap plugin to upload jpeg file to my server from iPad.
It is not working for me.
Here is my code:
window.plugins.fileUploader.uploadByUri('http://192.168.1.54:8080/POC/fileUploader', 'file://Documents/flower.jpg', null, 'myPhoto', 'flower.jpg', 'image/jpeg',
function(result) {
console.log('Done: ' + result);
},
function(result) {
console.log("Error: " + result);
}
);
In fileUploader.js file, I put an alert below the uploadbyuri method. But its not displaying. That means that method is not calling.
What mistake I have done?
Please help me.
Please check this plugins its works for me.
https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/FileUploader
and if its not working please specify proper error that you are getting for upload image.
Me too had the same situation. FileUploader worked fine in cordova1.9.0. When i changed to cordova2.7.0 face the same situation. After a long time messing with the issue, I made it work by remnaming "Phonegap" to "cordova" in fileuploader.js file which is in www folder.
PhoneGap.addConstructor(function() {....
to
cordova.addConstructor(function() {....
and
return PhoneGap.exec('FileUploader.' + method, callback + '.success', callback + '.fail', callback + '.progress', server, file, fileKey, fileName, mimeType, params);
to
return cordova.exec(null,null,"FileUploader", "uploadByUri",[ callback + '.success', callback + '.fail', callback + '.progress', server, file, fileKey, fileName, mimeType, params]);

Categories

Resources