[ANN] `df` now available on npm

150 views
Skip to first unread message

AJ ONeal

unread,
Jan 22, 2012, 10:16:37 PM1/22/12
to node.js mailing list
I know I've seen discussion of it on the node list and stackoverflow, so I was surprised that no one has yet put their implementation on npm yet. Well, too late! I beat you all!

I learned the `require.main === module` trick today (like the python main trick), so I created a single file for both the commandline and module versions. W00T!


Command-line:

    npm install -g df
    df-json
      [   
        {   
          "filesystem": "/dev/disk0s2",
          "blocks": 488555536,
          "used": 472631704,
          "available": 15411832,
          "percent": 97, 
          "mountpoint": "/" 
        }   
      ]   
    
Module:

    npm install df
    npm test df


Github:



AJ ONeal

P.S. Yes, I realize that my disk is 97% full. Yes, I'm going to fix it.

Arunoda Susiripala

unread,
Jan 22, 2012, 10:18:53 PM1/22/12
to nod...@googlegroups.com
nice

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
Arunoda Susiripala


Karl Tiedt

unread,
Jan 22, 2012, 10:24:11 PM1/22/12
to nod...@googlegroups.com
now if only this was network statistics... (interface throughput) it
would have been exactly what I was looking for this week... on topic
though... this solves another problem I was going to tackle this week
:)

Thanks!

-Karl Tiedt

Nuno Job

unread,
Jan 22, 2012, 10:43:44 PM1/22/12
to nod...@googlegroups.com
>> I learned the `require.main === module` trick today (like the python main trick), 
>> so I created a single file for both the commandline and module versions. W00T!

Looks like a terrible practice. It just obfuscates code for no apparent reason other than doing just that.

Am I missing something? Is there something you can solve with this that you cannot solve by just creating a bin file that requires your library?

AJ ONeal

unread,
Jan 22, 2012, 11:45:45 PM1/22/12
to nod...@googlegroups.com
Looks like a terrible practice. It just obfuscates code for no apparent reason other than doing just that.

Perhaps you didn't understand my snippet outside of the proper context.

Look at the module (link in prior post) to see the context, and the nodejs docs:

It's common practice and python and I think it's very clear.
 
Am I missing something? Is there something you can solve with this that you cannot solve by just creating a bin file that requires your library?

Yes,

I can have one npm module instead of two.

If your module is installed globally with npm, you can't require it unless also installed locally. If your module is installed locally, it can't have a cli.

let's say your module is called `foo` (I know someone's is)

  `lib/index.js` is your module
  `bin/foo-cli.js` is your cli, which installs a symlink to `/usr/local/bin/`

in `foo-cli` you have

   var foo = require('foo');
   foo();

You would have to publish both `foo` and `foo-cli` and list `foo` as a dependency.

Now, it may work using relative paths such as `require('../lib/index')`, but I didn't actually test that.

AJ ONeal 

Isaac Schlueter

unread,
Jan 23, 2012, 1:57:30 AM1/23/12
to nod...@googlegroups.com
You can have a bin and install something locally as well. You can
also have more than one javascript file in your npm package. Just
list one as a bin and the other as the main.

But sniffing `require.main === module` is fine, too.

Is there any way to get this information other than shelling out to
the `df` executable?

AJ ONeal

unread,
Jan 23, 2012, 2:31:02 AM1/23/12
to nod...@googlegroups.com, Isaac Schlueter
You can have a bin and install something locally as well.  You can
also have more than one javascript file in your npm package.  Just
list one as a bin and the other as the main.

I'm aware of this. However, my experience has been that modules installed globally only work for the `bin` directive, and not for the `main` directive.


Test Case
===

    npm --version
    1.1.0-beta-10

    node --version
    v0.6.7

    npm install -g highlight

    echo '(function () {
      "use strict";

      var df = require("highlight")
        ;
    }());' > /tmp/test.js

    node /tmp/test.js
    Error: Cannot find module 'highlight'


    npm install highlight
    node /tmp/test.js

    npm install -g highlight-cli
    highlight /tmp/test.js --languages=javascript

 
Is there any way to get this information other than shelling out to
the `df` executable?

On linux I'm pretty sure you can read stuff in /proc and /sys to get the same information.

On OS X... not sure.

Could I look at the C code for df and write a compiled node module?
Yes.

Am I going to?
No.
The exec slowness doesn't affect my use case (polling every few minutes).

Would I use it if someone wrote it?
Maybe. Maybe not.

Would I accept a patch for it?
Definitely.
How do we specify "use module x if compiler can compile it and the os is the correct os, else use module x.js"?

Isaac Schlueter

unread,
Jan 23, 2012, 5:16:31 AM1/23/12
to AJ ONeal, nod...@googlegroups.com
On Sun, Jan 22, 2012 at 23:31, AJ ONeal <cool...@gmail.com> wrote:
>> You can have a bin and install something locally as well.  You can
>> also have more than one javascript file in your npm package.  Just
>> list one as a bin and the other as the main.
>
> I'm aware of this. However, my experience has been that modules installed
> globally only work for the `bin` directive, and not for the `main`
> directive.

Well, you can't require() stuff that's installed globally, that's
true. You'd just install it in the appropriate place based on what
you intend to do with it, or install/link it in both places if you
want to do both things.

David

unread,
Jan 23, 2012, 2:47:10 PM1/23/12
to nodejs
Hi Karl,

> now if only this was network statistics... (interface throughput) it
> would have been exactly what I was looking for this week...

Did you look at the sigar binding? It does df, network statistics and
much more:
https://github.com/wdavidw/node-sigar

d.

Karl Tiedt

unread,
Jan 23, 2012, 3:10:42 PM1/23/12
to nod...@googlegroups.com

Nice, thanks David, I googled but nothing came up, I'll definitely check it out

Reply all
Reply to author
Forward
0 new messages