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

glob-exec

v0.1.1

Published

Execute processes using output from glob.

Downloads

4,515

Readme

glob-exec

Yet another command-line wrapper for glob!

Usage

glob-exec [--foreach [--parallel]] [--glob [glob-options]] glob-pattern command

glob-pattern is a glob pattern as detailed in the glob documentation.

foreach specifies that the command should be executed for each file found.
If not specified, the default is to run a single command with all files.

parallel specifies that commands should be executed in parallel.
It only applies when foreach is specified.
If not specified, the default is to run commands in sequence.

glob-options are a list of (subarg)[https://www.npmjs.com/package/subarg] formatted options that will be passed to the glob function.

command is the command that will be executed. See the Details section below for additional information.

Details

glob-exec runs in two different modes of operation that have different semantics. The first one is used when foreach is not specified on the command-line. It is the default mode and is called the all mode. The second one is used when foreach is specified on the command-line. It is called the foreach mode.

In both modes, the command argument can use basic JavaScript expressions by surrounding them with two sets of curly brackets (like this: {{...}}). The result of the expression will be converted to string and replaced in the command (for example: {{new Date().toISOString()}}).

This JavaScript expression can use all Node.js global objects as well as some mode-specific variables. This allows for quite complex yet familiar usage of the globbing expression result (for example: {{files.join(' ** ')}} or {{file.relative('./src').substr(0, 3)}}). See the Examples section below for more complex examples.

Any expression found which is not valid JavaScript (or which throws an exception) will not be replaced in the resulting command.

all mode

This mode is used when foreach is not specified on the command-line.

With this mode, a single command will be run. It may/should process all the files found by the globbing expression.

In addition to Node.js global objects, the command may use the following variable:

foreach mode

This mode is used when foreach is specified on the command-line.

With this mode, the command will be run for each file found by the globbing expression.

In addition to Node.js global objects, the command may use the following variable:

Examples

all mode

# prints the number of files found (by accessing the length property) and the files themselves
glob-exec "./src/**/*.ts" -- "echo found {{files.length}} files: {{files}}!"

> found 2 files: ./src/cli.ts,./src/glob-exec.ts!
# invokes browserify on all files to create a single bundle
# passes the output to exorcist to extract the source-map
glob-exec "./lib/**/*.js" -- "browserify --debug {{files.join(' ')}} | exorcist ./browser/index.js.map > ./browser/index.js"
# DO NOT DO THAT! -- TAKE A STEP BACK BEFORE WRITING SUCH CODE
# shows how complex JavaScript can be done
glob-exec "./lib/**/*.js" -- "echo {{(function() { var fs = require('fs'); return files.map(function(f) { return f + ': ' + fs.statSync(f).mtime.toISOString(); }).join(', '); })()}}"

> ./lib/cli.js: 2017-04-01T10:42:12.359Z, ./lib/glob-exec.js: 2017-04-01T10:42:12.339Z

foreach mode

# prints each file found in foreach mode
glob-exec --foreach "./src/**/*.ts" -- "echo found {{file}}"

> found ./src/cli.ts
> found ./src/glob-exec.ts
# shows that commands are executed in sequence when used without --parallel
glob-exec --foreach "./src/**/*.ts" -- "node -e \"setTimeout(function() { console.log(new Date().toTimeString() + ': {{file}}'); }, 2000);\""

> 10:42:02 GMT+0200 (Romance Daylight Time): ./src/cli.ts
> 10:42:04 GMT+0200 (Romance Daylight Time): ./src/glob-exec.ts
# shows that commands are executed simultaneously when used with --parallel
glob-exec --foreach --parallel "./src/**/*.ts" -- "node -e \"setTimeout(function() { console.log(new Date().toTimeString() + ': {{file}}'); }, 2000);\""

> 11:42:04 GMT+0200 (Romance Daylight Time): ./src/cli.ts
> 11:42:04 GMT+0200 (Romance Daylight Time): ./src/glob-exec.ts
# invokes browserify on each file to create a bundle per file
# passes the output to exorcist to extract the source-map
glob-exec --foreach --parallel "./lib/**/*.js" -- "browserify --debug {{file}} | exorcist ./browser/{{file.relative('./lib')}}.map > ./browser/{{file.relative('./lib')}}"

Copyright & License

Copyright Franck Pascutti 2017.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)