npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lumo-npm-example

v0.1.4

Published

Example How to Call Non-Trivial NPM Libraries in lumo / clojurescript

Downloads

5

Readme

Example How to Call Non-Trivial NPM Libraries in lumo / clojurescript

Example of how to call a couple of NPM Libraries with different sets of function signatures from Clojurescript in a Lumo program.

NPM Libraries Used

The two npm libraries are:

git-latest-semver-tag has a very simple calling signature. It just returns the latest tag of the git repot that it is run in. In Javascript:

var gitLatestSemverTag = require('git-latest-semver-tag');
 
gitLatestSemverTag(function(err, tag) {
  console.log(tag);
  //=> 'v1.0.0' 
});

The equivalent in Lumo / Clojurescript is:

(def git-latest (js/require "git-latest-semver-tag"))
(git-latest (fn [err,tag] (js/console.log tag)))

simple-git is a much more complex library with many functions for doing git operations on a repo. You can specify the repo path as an argument to the base function of the library.

It does require that you have the git program installed on the computer that runs it.

This example just uses the .tags function. The "constructor" takes an argument:

workingDirPath, is optional defaulting to the current directory.

Our example uses the current directory In Javascript:

var simpleGit = require('simple-git')('.');
simpleGit.tags(function(err, tags) {
            console.log("Latest available tag: %s", tags.latest);
            });

The lumo / clojurescript equivalent:

(def simple-git ((js/require "simple-git") ".")) 
(.tags simple-git  (fn [err,tags] (js/console.log "Latest available tag: " tags.latest)))

You can also specify the directory after the require in your code if you initialize simple-git without calling the result of the require as a function:

(def simple-git (js/require "simple-git"))
(.tags (simple-git ".")  (fn [err,tags] (js/console.log "Latest available tag: " tags.latest)))

Many (most?) NPM libraries are asynchronous which use callback functions to return results. This is great for web apps, but generally a bummer for writing scripts. The example program (src/core.cljs) uses core-async to get the asynchronous results back into the main program thread.

Setting Up

Clone the Repo

Of course you need to:

git clone [email protected]:rberger/lumo-npm-example.git
cd lumo-npm-example

Install the NPM dependencies in your local repo

npm install

This will pull the npm libraries and dependencies into node_modules

Install Clojure / Clojurescript dependencies

We need to use the customized version of clojure/core.async called andare that works with bootstrapped ClojureScript environments like lumo.

The easiest way is to get andare into your local maven repo. The easiest way I know to do that is to have a project.clj and use lein deps to get it and any potential dependencies. This technique will work for any clojure or clojurescript dependencies you may need in your lumo scripts.

This repo has a project.clj that is only used for getting these dependencies. It is not used by lumo itself.

So you should run:

lein deps

Executing the program

To execute the program:

lumo -K -D andare:0.7.0 -c src -m lumo-npm-example.core
  • -K will cache any dependencies in .lumo_cache This will speed up running the command after the first time.
  • -D andare:0.7.0 tells lumo to pull in the dependency andre:0.7.0 from your local maven repo (~/.m2)
  • -c src specifies the classpath for the lumo run
  • -m lumo-npm-example.core Call the -main function lumo-npm-example.core

Assuming that there are the tags 1.0.0, foo, bar in the current repo, you should see:

Simple-Git RESULTS:  [1.0.0 bar foo]
Git-Latest RESULTS:  1.0.0

The repo is also set up to be a deployable npm package itself and has an executable in bin/lumo-npm-example. The package.json includes lumo-cljs as a dependency so its loaded into node_modules when you did the npm install earlier.

This means you can also execute the program as:

bin/lumo-npm-example

Things to know to do this on your own

If you use the repo you don't need to do the following since the package.json already exists

But if you want to know how to do something similar from scratch for your own project in a fresh new directory/repo you would have to:

  • Get the NPM libraries into the repo so they are able to be required, you need to do the following:
    npm init
    • You can accept the defaults
    • This will create an empty package.json
  • Do a local npm install for the npm packages you want to use. To use the two git libraries in this example you would:
    npm install git-latest-semver-tag --save
    npm install simple-git --save
    • The --save makes it install them locally to the current directory in npm_modules and updates the package.json
  • To make your repo into a deployable npm package itself
    • Include lumo-cljs in the package.json under "dependencies" as is done in this repo
    • Have some kind of bin/<executable>
      • Have an entry in package.json under "bin" similar to how its done in this repo
      • Have any clojure/clojurescript libraries (jars) in lib/
      • The example bin/lumo-npm-example shows how to set up the paths to the embedded lumo-cljs and any clojure libs
        • It will work calling from within the repo with a relative path, or from the globally installed npm package location
    • Make sure the "name" parameter in package.json is set to something appropriate for npmjs.com
    • See Publishing npm packages for more details on actually pushing the package to npmjs.com

Thanks To

Copyright & License

Copyright © Robert J. Berger

Licensed under the MIT License