Hi,
with <http://clojure.java.io|clojure.java.io>
you can read files under resources
like:
(-> "config.edn"
io/resource
do-something-with-the-file)
What would be the equivalent to io/resource
approach in Lumo,
something that looks under project’s resources
directory?
I get that you can do <http://lumo.io/slurp|lumo.io/slurp>
, but for it to work you have to
specify the full path to it. I’m looking for a way to read a file, relative
to the project it’s being run from. Is there something like that?
Thanks@dotemacs http://lumo.io/slurp should not need full path. Im not at the computer but http://lumo.io/resource and http://lumo.io/slurp is what you want to use more than any fs require or macchiato/fs
Yes you need http://lumo.io/respurce before your slurp it for the relative path to work
<http://lumo.io/slurp|lumo.io/slurp>
, is that new? it’s not available in my version of lumo (1.8.0-beta)
yes it is
it’s been there since 1.2
but I have never spoken about these, since they are internal helpers for now
use at your own risk
ok, I forgot a require, since it’s not loaded at startup obviously - sorry 🙂
good thing about http://lumo.io/resources is that it takes bundles data into account. So if lumo ever supports bundled data, you wont need to change your code.
Thanks for the pointers
:)
See this:
cljs.user=> (require '[<http://lumo.io|lumo.io> :refer [resource slurp]])
nil
cljs.user=> (slurp "resources/bar")
"this is bar"
cljs.user=> (slurp (resource "bar"))
should never happen!
(new)
Function.cljs.core.ex_info.cljs$core$IFn$_invoke$arity$3 (NO_SOURCE_FILE <embedded>:1923:72)
Function.cljs.core.ex_info.cljs$core$IFn$_invoke$arity$2 (NO_SOURCE_FILE <embedded>:1922:449)
lumo$io$slurp (evalmachine.<anonymous>:49:25)
(evalmachine.<anonymous>:1:15)
ContextifyScript.Script.runInThisContext (vm.cljs:44:33)
Object.runInThisContext (vm.cljs:116:38)
(<http://Object.lt|Object.lt>)
(Object.lumo.repl.caching_node_eval)
(NO_SOURCE_FILE <embedded>:5720:273)
Am I misunderstanding how resource
should be used or … ?
What version are you running, I used resource just few days ago in this project http://://github.com/hlolli/lumo-quiz-game/blob/master/src/lumo_quiz_game/main.cljs questions-db symbol.
@dotemacs <http://lumo.io/resource|lumo.io/resource>
doesn’t really behave like Clojure’s
it looks for something in the classpath
but doesn’t add a resources
folder to the classpath automatically
@anmonteiro isn't that exactly how clojure's behaves?
It's lein that adds resources folder to the classpath.
well then
it doesn’t behave exactly like Lein
🙂
@hlolli on 1.7.0
@dotemacs try 1.8.0-alpha
@anmonteiro the root folder is always on classpath? So useing io/resource with full path relative to the root folder should work?
yes
thanks for that
The main reason I even started to ask about this is because I have a script that is invoked via: npm run foo
, where foo
is a task in package.json
. So using io/resource
would look for a relative path from where the npm run foo
was invoked.
But I figure that by adding resources
dir to the class path explicitly, I’ll be able to pick up the script that I’m after
You wouldnt run lein repl from different folder than the root foler, then why would you run npm run from other dir than project root. Nodejs also wont find childrents node_modules directorires, only parents
But ok as scriot goot question, dont know what the variable process.cwd() returns from foreign dirs
Does (.pwd js/process) return the same value wherever you call this script? Dont know myself
Does (.cwd js/process) return the same value wherever you call this script? Dont know myself
Wtf edit on mobile phone causes replied :)
I’m trying something out
don’t worry
this is one of many joys of being always connected
(.cwd js/process)
does return the correct pwd…
but I desperately wanted this:
(-> "foo.edn"
resource
(js->clj :keywordize-keys true)
:src
…
to workno luck
wait, the above will work, if you add the project’s resources to the class path