How to set the path to include in Gjs code?

As I could see, Gjs imports , by default, only loads /usr/share/gjs-1.0 and /usr/lib/gjs-1.0 . I want to modulate the application, as we can do with node, but I have to find the modules relative to the script file.

I found two ways to add include paths:

  • gjs --include-path=my-modules my-script.js
  • GJS_PATH=my-modules gjs my-script.js

... but both of them are associated with the current directory, and not with the file (invisibly), and they must be declared on the command line, which makes this unnecessarily complicated.

How can I set the path to include in Gjs code? (So ​​that I can do it regarding the file)

Or ... Is there another way to import files from anywhere, for example, in python?

(Please do not suggest using a shellscript launcher to solve the --include-path and GJS_PATH . This is obvious, but less effective. If we do not have a better solution, we survive with that.)

+6
source share
2 answers

You need to set or modify imports.searchPath (which is not obvious because it is not displayed using for (x in imports)print(x) ). So:

 imports.searchPath.unshift('.'); var foo = imports.foo; 

imports the file "foo.js" as an object foo .

This is compatible with Seed , although imports knows that it has searchPath .

(Earlier versions of this answer were significantly less accurate and more inflammatory. Sorry.)

+8
source

As Douglas says, you need to change imports.searchPath to include the location of your library. Use . simple, but depends on files that always run from the same directory. Unfortunately, finding the directory of the current executable script is a huge hack. Here Gnome Shell does this for extension APIs

I applied this in the following function for general use:

 const Gio = imports.gi.Gio; function getCurrentFile() { let stack = (new Error()).stack; // Assuming we're importing this directly from an extension (and we shouldn't // ever not be), its UUID should be directly in the path here. let stackLine = stack.split('\n')[1]; if (!stackLine) throw new Error('Could not find current file'); // The stack line is like: // init([object Object])@/home/user/data/gnome-shell/extensions/ u@u.id /prefs.js:8 // // In the case that we're importing from // module scope, the first field is blank: // @/home/user/data/gnome-shell/extensions/ u@u.id /prefs.js:8 let match = new RegExp('@(.+):\\d+').exec(stackLine); if (!match) throw new Error('Could not find current file'); let path = match[1]; let file = Gio.File.new_for_path(path); return [file.get_path(), file.get_parent().get_path(), file.get_basename()]; } 

Here you can use it from your app.js entry point file after defining the getCurrentFile function:

 let file_info = getCurrentFile(); // define library location relative to entry point file const LIB_PATH = file_info[1] + '/lib'; // then add it to the imports search path imports.searchPath.unshift(LIB_PATH); 

In and! Now importing our libraries is very simple:

 // import your app libraries (if they were in lib/app_name) const Core = imports.app_name.core; 
+5
source

Source: https://habr.com/ru/post/912866/


All Articles