Skip to content

Instantly share code, notes, and snippets.

@jaszhix
Created May 8, 2017 10:28
Show Gist options
  • Save jaszhix/11872fc1ec4b68d4ca23581242aeebff to your computer and use it in GitHub Desktop.
Save jaszhix/11872fc1ec4b68d4ca23581242aeebff to your computer and use it in GitHub Desktop.
JSGTK-Cinnamon integration
const Applet = imports.ui.applet;
function main(metadata, orientation, panel_height, instance_id) {
try{
global[metadata.uuid] = [metadata, orientation, panel_height, instance_id];
const
runtime = Function,
// basic dependencies
gi = imports.gi,
//GLib = gi.GLib,
//GFile = gi.Gio.File,
// scoped global + shortcuts
//global = window,
replace = String.prototype.replace,
// folders bootstrap
CURRENT_DIR = metadata.path,
DIR_SEPARATOR = /\//.test(CURRENT_DIR) ? '/' : '\\',
PROGRAM_NAME = 'applet.js', //imports.system.programInvocationName,
PROGRAM_DIR = CURRENT_DIR
/*PROGRAM_DIR = ((exe) => {
let
dir = exe.slice(0, -(1 + GLib.path_get_basename(exe).length)),
path = dir.split(DIR_SEPARATOR)
;
switch (path[path.length - 1]) {
// global case
case 'bin':
path.pop();
path.push('lib', 'node_modules', 'jsgtk');
dir = path.join(DIR_SEPARATOR);
break;
// local module
case '.bin':
path.pop();
path.push('jsgtk');
dir = path.join(DIR_SEPARATOR);
break;
}
return dir;
})(GFile.new_for_path(PROGRAM_NAME).get_path())*/
;
window.ARGV = [metadata.path];
// inject the jsgtk folder to import at runtime internal helpers
imports.searchPath.push([PROGRAM_DIR, 'jsgtk_modules'].join(DIR_SEPARATOR));
print(PROGRAM_DIR)
// populate the constants file
Object.defineProperties(
imports.jsgtk.constants,
{
CURRENT_DIR: {enumerable: true, value: CURRENT_DIR},
DEBUG: {enumerable: true, value: ARGV.some(arg => arg === '--debug')},
DIR_SEPARATOR: {enumerable: true, value: DIR_SEPARATOR},
PROGRAM_NAME: {enumerable: true, value: PROGRAM_NAME},
PROGRAM_DIR: {enumerable: true, value: PROGRAM_DIR},
TRANSFORM: {enumerable: true, value: !ARGV.some(arg => arg === '--no-transform')}
}
);
// module handler
function evaluateModule(sanitize, nmsp, unique, id, fd) {
const
dir = id.slice(0, -1 -fd.get_basename().length),
exports = {},
module = {exports: exports, id: id},
content = (sanitize ? transform : String)(
replace.call(fd.load_contents(null)[1], /^#![^\n\r]*/, '')
)
;
// sanitize && print(transform(content));
nmsp[unique] = exports;
runtime(
'require',
'exports',
'module',
'__dirname',
'__filename',
content
).call(
exports,
function require(module) {
return requireWithPath(module, dir);
},
exports,
module,
dir,
id
);
return (nmsp[unique] = module.exports);
}
// bring in polyfills and all modules loaders + process and timers
const
polyfills = imports.jsgtk.polyfills,
mainloop = imports.jsgtk.mainloop,
gtk = imports.jsgtk.gtk_modules.withRuntime(),
core = imports.jsgtk.core_modules.withRuntime(evaluateModule),
modules = imports.jsgtk.node_modules.withRuntime(evaluateModule),
Babel = imports.jsgtk.babel.Babel,
BabelTransformer = {plugins: [
'transform-decorators-legacy',
'transform-class-properties',
'transform-flow-strip-types',
'transform-es2015-classes',
'transform-es2015-literals',
'transform-es2015-object-super',
'transform-es2015-parameters',
'transform-es2015-shorthand-properties',
'transform-es2015-unicode-regex',
'transform-exponentiation-operator',
'transform-es2015-template-literals',
]}
;
/*if (typeof Promise === 'undefined') {
Object.defineProperty(
global,
'Promise',
{value: imports.jsgtk.promise.ES6Promise.Promise}
);
}*/
/*// env normalization
Object.defineProperties(
global,
{
global: {enumerable: true, value: global},
// used as ES6 classes properties decorator:
// https://github.com/WebReflection/jsgtk/blob/master/examples/lang.js
GObjectProperties: {value: imports.jsgtk.extended.GObjectProperties},
// access to modules even internally
require: {value: function require(module) {
return requireWithPath(module, CURRENT_DIR);
}}
}
);*/
if (typeof window.Promise === 'undefined') {
global.Promise = imports.jsgtk.promise.ES6Promise.Promise;
window.Promise = global.Promise;
}
global.GObjectProperties = imports.jsgtk.extended.GObjectProperties;
window.GObjectProperties = global.GObjectProperties;
global.require = function require(module) {
return requireWithPath(module, CURRENT_DIR);
}
window.require = global.require;
// the actual require
function requireWithPath(module, dir) {
switch (true) {
case core.has(module):
return core.get(module);
case gtk.has(module):
return gtk.get(module);
default:
return modules.get(module) || modules.load(module, dir);
}
}
// makes most ES6 compatible with GJS
function transform(code) {
return Babel.transform(code, BabelTransformer).code;
}
// initialize basic global modules
ARGV.core = core;
window.process = core.get('process');
window.timers = core.get('timers');
window.console = core.get('console');
;
print(typeof window.console.log)
delete ARGV.core;
window.process = process;
window.console = console;
// TODO: implement a better logic for this
let applet = requireWithPath('./__applet.js', CURRENT_DIR);
return applet
} catch (e) {
if (!e.stack) e.stack = '';
e.stack += (constructor ? (constructor.name + ': ') : '') + String(e) + '\n';
print(e.stack)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment