-
Notifications
You must be signed in to change notification settings - Fork 537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
-require local script file vs. npm package #646
Comments
Hi! I should be able to make some changes to support your use cases. Part of the problem may be that mapshaper is using I'll look into the global context issues you reported. |
I investigated a bit further and i think the last bullet (no access to context) is just caused because the function is enforced to be a nested property of the alias and not on the global level. with this script...
mapshaper -require "./index.js" -run "{fnA()}" -run "{nested.fnB()}" ... At the moment functions in npm modules are always a nested properties. This make the difference?! |
Just to give you some context. The functions I use most often generate geojson output based on geojson input. And the template I use looks something like this: const fnName = "myFn"
module.exports = {
[fnName]: function (arg1, arg2, arg3) {
const { $, target } = this;
if ($) {
const geojson = $.geojson;
// generate modified geojson
return geojsonMOD;
} else if (target) {
const fnName_ = `[+ ${fnName}]`;
console.info(`${fnName_} The target gets cloned and renamed - use '-each ${fnName}(...)' to alter it in place`)
let args = [...arguments].map(JSON.stringify)
let name = [target.layer_name, `_${fnName}`, ...args]
return `-filter "true" target=${target.layer_name} + name=${name.join('_')}
-each this.geojson=${fnName}(${args.join(',')})`;
}
}
} This allows the function to be called with (maybe I should use the io api of |
I've just tested how to make the context thing work. Turns out that If npm modules would also be asigned to global like so the context would be available with this. This would fix this issue as well. Maybe the functions in the module should be assigned to |
I'm struggling with -require (again ;-)
I'm having a bunch of scripts that extend mapshaper functionality that I use from time to time. To make it easier to use them on other systems I wanted to put them in a registry so I would be able to load them with npm. I was surprised that this makes quite a difference.
Some of my findings:
this
when called with-run
. This gave a lot of flexibility.There are probably good reasons for those differences between those two types of "-require" but maybe you could give me a hint what the best-practice is to create a plugin-library that combines multiple commands - that is easy to distribute, load and call...
The text was updated successfully, but these errors were encountered: