How to add a config.js file into nwjs install directory? - node-webkit

I use node-webkit-builder to package my nwjs application.
Since the program running directory path is not same as the install directory path( the running directory path is like this: C:\Users[username]\AppData\Local\Temp\nw7388_7201 ). I don't know how to add my config.js file in the install directory, and read it in my nwjs app.
Best regards.

Checkout this: https://github.com/nwjs/nw.js/wiki/App#datapath
This is common folder for application data. With gui.App.dataPath you always know where is this folder.
If you need get know where is your executable lives, try:
var path = require('path');
var nwPath = process.execPath;
var nwDir = path.dirname(nwPath);
https://github.com/nwjs/nw.js/issues/1197

Related

Apache server cannot find local file [duplicate]

I'm working on a Flask extension from which I want to create a directory in the project's root path on the file system.
Suppose we have this directory structure
/project
/app
/tests
/my_folder
manage.py
my_folder should be created dynamically by the extension, which is a test utility and wraps the application under test in the /tests directory. However, I'm struggling to determine the project's root path within my extension.
For now, I am trying to guess the path from the run file:
def root_path(self):
# Infer the root path from the run file in the project root (e.g. manage.py)
fn = getattr(sys.modules['__main__'], '__file__')
root_path = os.path.abspath(os.path.dirname(fn))
return root_path
This obviously breaks as soon as the tests are run from within the IDE instead of the manage.py. I could simply infer the project's root relative to the app or tests directory, but I don't want to make any assumptions regarding the name or structure of these directories (since multiple apps might be hosted as subpackages in a single package).
I was wondering if there is a best practice for this type of problem or an undocumented method which the Flask object provides (such as get_root_path).
app.root_path contains the root path for the application. This is determined based on the name passed to Flask. Typically, you should use the instance path (app.instance_path) not the root path, as the instance path will not be within the package code.
filename = os.path.join(app.instance_path, 'my_folder', 'my_file.txt')
app.root_path is the absolute path to the root directory containing your app code.
app.instance_path is the absolute path to the instance folder. os.path.dirname(app.instance_path) is the directory above the instance folder. During development, this is next to or the same as the root path, depending on your project layout.

load local files outside NWJS app

When I work locally I want to share code among two or more (nwjs and other kinds of) projects. Folder structure:
-project 1
-project 2
-shared code
When releasing the apps I build the external files into a file inside each project app.
But I cannot access files outside the node-webkit/nwjs app folder.
I tried things like:
Setting "chromium-args": "--allow-file-access-from-files" in the manifest file but I think this is default now.
Using file:/// and chromium-extension:/// prepending the relative paths but I think this is only for absolute paths?
Load files dynamically and using path.relative( process.cwd(), "../shared_code/scripts/controllers/searchController.js" );
The user of the app will be able to put it anywhere on his computer.
Is it possible to load js and css files and images from outside the nwjs project folder locally?
nwjs sdk version 0.19.5
I had a similar problem trying to load images outside of the NW.js application. I found that this fixed my issues. Try adding this to your JSON manifest file. This worked for me...
"chromium-args": "--allow-file-access-from-files --allow-file-access --user-data-dir"
Just so you know, I had originally tried this...
"chromium-args": "--allow-file-access-from-files --allow-file-access --user-data-dir --mixed-context"
But it stopped jquery loading.
You can then access files with file:/// and use absolute paths anywhere on the machine.
I hope that helps.
For sharing code, when the code is (or can be) a node module, it's very helpful to be able to npm link it into your project. (It works the same with NW.js apps as for Node.js:)
cd shared-code
npm link
cd ../project-1
npm link shared-code
cd ../project-2
npm link shared-code

Where does intellij idea save the local storage and preferences of libgdx (pc)

I started working with files, a simple operation of writing and reading files.
But i had an error when writing a file and now i have to fix it by hand.
Thats the problem, i don't know where is my file.
Also i would like to see the file i'm writing.
I am working with intellij idea 2016 1.4, maybe the file is complied in a jar?
Yes, i know that clearing cache its an option.
nothing here: https://github.com/libgdx/libgdx/wiki/File-handling
on the wiki link only talk about where you can find the ablolute path file but thats not my case. I get the file this way:
this.resolver = Gdx.files.local(path + "item"+ String.valueOf(weaponNumber) + ".txt");
String description = this.resolver.readString();
So.. where is the file? thanks
In the desktop version it saves the file in the assets folder inside your android module.
FileHandle file = Gdx.files.local("myfile.txt");
file.writeString("Test libGDX", false);
System.out.println(Gdx.files.getLocalStoragePath());
Output: D:\Dropbox\Projetos\Outros\gdxTest\android\assets\
The project folder in my computer is gdxTest.
In your case you have the path var so probably will be a folder inside assets folder.
But when you pack the desktop game into a jar file, the file will be created in the same folder where your game jar file is located. Usually yourProject\desktop\build\libs.
The difference is because when we configure the desktop project we set the Working Directory in the yourProject\android\assets\ folder. So to Android Studio it is the local folder of your project.

Archive localResource in YARN : customize location

I wanted to deploy a zip file as a local resource in yarn. Hence I did:
packageResource.setResource(packageUrl);
packageResource.setSize(packageFile.length());
packageResource.setTimestamp(packageFile.lastModified());
packageResource.setType(LocalResourceType.ARCHIVE);
packageResource.setVisibility(LocalResourceVisibility.APPLICATION);
If my file name is "abc.zip", Yarn unpacks all the zip contents into a folder called "abc", not the current working directory.
For example, it creates something like:
/grid/5/tmp/yarn-local/usercache/(user)/appcache/application_1394223910537_2533883/container_1394223910537_2533883_01_000002/abc
Can I customize this behavior? How do I get Yarn to unzip a file in the current working directory ,instead of creating a new directory?
The use-case is: if my app has some scripts, it would be useful to have all scripts deployed to current directory, instead of having to change the code to reference them from within the folder that Yarn creates.

Get the path to the current application in node-webkit

In node-webkit, is there any way to find the path to the current application? In node.js, you can use __dirname to find the path to the current application, but in node-webkit, the variable __dirname appears to be undefined.
The following node.js script prints the file path correctly:
console.log(__dirname)
The following node-webkit script does not print the file path correctly:
<script type = "text/javascript">
alert(__dirname);
</script>
What is the correct way to find the path to the current application in node-webkit?
The accepted answer's link is no longer available, so here is a short answer:
nw.js extract the content of your app, to a temp directory, every time you run it.
If you want to access the path where nw.js extracted your app, use process.cwd()
In other causes, where you want to access the path of your executable app, use:
var path = require('path');
var nwDir = path.dirname(process.execPath);
The answer to this question was discussed here: https://groups.google.com/d/topic/node-webkit/IwGzluFC9iU/discussion
On Windows, use "process.execPath" to see the path of the executable that launched it. Then work from there, removing the executable's filename from the path to get the folder's path (assuming your app's .nw is relative to the executable or is combined with it).
This works for me whether it is running with the zipped 'app.nw' or where 'nw.exe' and 'app.nw' are combined into one executable file (app.exe).
If you are looking for the path to the App source (i.e. the folder that contains package.json) then you can use process.cwd().
No matter what the environment's true working directory is when the node executable is launched, it will set process.cwd() to the location of the App source. If the App is contained in an archive, cwd will point to the temporary folder where the source is extracted.
Importantly, note that process.cwd() can be changed during the application run by process.chdir(newPath) and potentially by other events as well, so you might want to store the initial value at application launch.
EDIT:
Just to clarify, process.cwd() is set to the folder that contains the actual package.json file that is used by the running app. So if you have packaged your app in an archive or executable (zip, exe, nwz, nw, etc), then nw.exe will extract the project files to a temporary directory before running the app. So process.cwd() will point to that temporary folder, not the location of the original archive or executable.
this should work:
var nw = require('nw.gui'); //This line is only required for NW.js 0.12.x and below
console.log(nw.__dirname)
window.location won't work if you're loaded an external uri, but the following seems reliable regardless:
var path = require('path');
,appPath = path.dirname(require.main.filename)
Not sure when this is added, but I believe the official way to get the start up path now is:
nw.App.startPath