Documentationcurrent version
Help us improve the docs by fixing typos and proposing enhancements.


Action "fs.glob"

Search for files in a directory hierarchy.


The action use the POXIX find command to fetch all files and filter the paths locally using the Minimatch package.


  • files
    List of files matching the globing expression.


Short notation:

const {files} = await nikita.fs.glob(`${process.cwd()}/*`)
for(const file of files){`Found: ${file}`)

Extended notation:

const {files} = await nikita.fs.glob({
  dot: true,
  target: `${process.cwd()}/*`
for(const file of files){`Found: ${file}`)

Schema definitions

definitions =
    type: 'object'
        type: 'boolean'
        description: '''
        Minimatch option to handle files starting with a ".".
        type: 'string'
        description: '''
        Globbing expression of the directory tree to match.
        type: 'boolean'
        default: false
        description: '''
        Leave a slash at the end of directories.
        type: 'object'
        description: '''
        Pass any additionnal config to Minimatch.
    required: ['target']


handler = ({config, tools: {path}}) ->
  config.minimatch ?= {} ?= if = path.normalize
  minimatch = new Minimatch, config.minimatch
  {stdout, exit_code} = await @execute
    command: [
      ...(getprefix s for s in minimatch.set)
      # trailing slash
      '-type d -exec sh -c \'printf "%s/\\n" "$0"\' {} \\; -or -print'
    ].join ' '
    $relax: true
    trim: true
  # Find returns exit code 1 when no match is found, treat it as an empty output
  stdout ?= ''
  # Filter each entries
  files = utils.string.lines(stdout).filter (file) ->
    minimatch.match file
  # Remove the trailing slash introduced by the find command
  unless config.trailing
    files = (file) ->
      if file.slice(-1) is '/'
      then file.slice 0, -1
      else file
  files: files


module.exports =
  handler: handler
    argument_to_config: 'target'
    definitions: definitions
    shy: true


{Minimatch} = require 'minimatch'
utils = require '../../utils'


getprefix = (pattern) ->
  prefix = null
  n = 0
  while typeof pattern[n] is "string" then n++
  # now n is the index of the first one that is *not* a string.
  # see if there's anything else
  switch n
    # if not, then this is rather simple
    when pattern.length
      prefix = pattern.join '/'
      return prefix
    when 0
      # pattern *starts* with some non-trivial item.
      # going to readdir(cwd), but not include the prefix in matches.
      return null
      # pattern has some string bits in the front.
      # whatever it starts with, whether that's "absolute" like /foo/bar,
      # or "relative" like "../baz"
      prefix = pattern.slice 0, n
      prefix = prefix.join '/'
      return prefix
Edit on GitHub

Nikita is an open source project hosted on GitHub and developed by Adaltas.