Skip to content

Latest commit

 

History

History
334 lines (291 loc) · 10.7 KB

api.md

File metadata and controls

334 lines (291 loc) · 10.7 KB

fsInheritanceLib

This Module collects files by given patterns and can create. Arrays or even Files contianing found files. You can Also pass Inheritance paths to find a file.

fsInheritanceLib~findSingleFile(cfg, file) ⇒ Object

Takes inherit array and single file to search for the file in given inheritance. It then selects the first appearance and returns an object containong origin and path of file found

Kind: inner method of fsInheritanceLib
Returns: Object - containing origin and path

Param Type Description
cfg Object config
file String filename

Example

var fsIn = require('fs-inheritance-lib')
var config = {
  inheritFrom: ['../parent', '../neighbour', '../../ancestor'],
  root: 'foo/bar/src'
}
var files = 'file.js'
var result = fsIn.findSingleFile(cfg, files)
// returns an object like this, depending on where the file was found.
// local means found in current working directory.
console.log(result)
// {
//  origin: 'local',
//  path: 'foo/bar/src/file.js'
// }
// or
// {
//  origin: 'neighbour',
//  path: 'foo/bar/src/file.js'
// }

fsInheritanceLib~findSinglePath(cfg, fpath) ⇒ Array

This Function returns an array of matching paths for one given pattern

Kind: inner method of fsInheritanceLib
Returns: Array - containing found paths

Param Type Description
cfg Object config
fpath String path

Example

var fsIn = require('fs-inheritance-lib')
var cfg = {
  inheritFrom: ['../parent', '../neighbour', '../../ancestor'],
  root: 'foo/bar/baz'
}
var paths = ['client-vars']
var result = fsIn.findSingleFile(conig, paths[1])
console.log(result)
// [
//  '../parent/foo/bar/baz/client-vars',
//  '../neighbour/foo/bar/baz/client-vars'
//  '../ancestor/foo/bar/baz/client-vars'
// ]

fsInheritanceLib~findFiles(cfg, files) ⇒ Array

Needs a config and an array of files. The config must contain inheritFrom attribute with an array of folders to inherit from. The order of this array defines the priority of the folders to be searched in. First appearance in first inheritance is taken.

Kind: inner method of fsInheritanceLib
Returns: Array - of files found by given config

Param Type Description
cfg Object config
files Array

Example

var fsIn = require('fs-inheritance-lib')
var config = {
  inheritFrom: ['../parent', '../neighbour', '../../ancestor'],
  root: 'foo/bar/src/'
}
var files = [
  'file1.js',
  'sub/sub1.js',
  'file2.js',
  'file3.js',
  'file.js',
  'file4.js'
]
var result = fsIn.findSingleFile(cfg, files)
// returns an object like this, depending on where the file was found.
// local means found in current working directory.
console.log(result)
// [
//  '../../ancestor/foo/bar/src/file1.js',
//  '../../ancestor/foo/bar/src/sub/sub1.js',
//  '../parent/foo/bar/src/file2.js',
//  '../neighbour/foo/bar/src/file3.js',
//  'foo/bar/src/file.js',
//  'foo/bar/src/file4.js'
// ]

fsInheritanceLib~findPaths(cfg, paths) ⇒ Array

Needs a config and an array of paths. The config must contain inheritFrom attribute with an array of folders to inherit from. The order of this array defines the priority of the folders to be searched in. First appearance in first inheritance is taken.

Kind: inner method of fsInheritanceLib
Returns: Array - of paths found

Param Type Description
cfg Object config
paths Array

Example

var fsIn = require('fs-inheritance-lib')
var config = {
  inheritFrom: ['../parent', '../neighbour', '../../ancestor'],
  root: 'foo/bar/src/'
}
var paths = [
  'client-vars',
  'fonts',
  'framework',
  'mixins',
  'module',
  'additional'
]
var result = fsIn.fsInheritanceLib.findPaths(config, paths)
console.log(result)
// [
//  '../parent/foo/bar/src/client-vars',
//  '../neighbour/foo/bar/src/client-vars',
//  '../../ancestor/foo/bar/src/client-vars',
//  '../../ancestor/foo/bar/src/fonts',
//  '../../ancestor/foo/bar/src/framework',
//  '../../ancestor/foo/bar/src/mixins',
//  '../../ancestor/foo/bar/src/module',
//  '../parent/foo/bar/src/additional',
//  '../neighbour/foo/bar/src/additional'
// ]

fsInheritanceLib~findGlobPatterns(cfg, pat) ⇒ Array

Find Files that match wildcards

Kind: inner method of fsInheritanceLib
Returns: Array - of files found via wildcards | duplicates are removed

Param Type Description
cfg Object config
pat String pattern

Example

var fsIn = require('fs-inheritance-lib')
var config = {
  inheritFrom: ['../parent', '../neighbour', '../../ancestor'],
  root: 'foo/bar/src',
  loglevel: []
}
var files = [
  '** /*.js' // i had to put the sapce before the backslash because of jsdoc issues
]
var result = fsIn.fsInheritanceLib.findGlobPatterns(config, files[0])
console.log(result)
// [
//  'foo/bar/src/file.js',
//  'foo/bar/src/file4.js',
//  '../parent/foo/bar/src/file2.js',
//  '../neighbour/foo/bar/src/file3.js',
//  '../../ancestor/foo/bar/src/file1.js',
//  '../../ancestor/foo/bar/src/sub/sub1.js'
// ]

fsInheritanceLib~findGlobPath(cfg, pat) ⇒ Array

Find global paths by widlcards

Kind: inner method of fsInheritanceLib
Returns: Array - of paths found mathing pattern

Param Type Description
cfg Object config
pat String pattern

Example

var fsIn = require('fs-inheritance-lib')
var config = {
  inheritFrom: ['./', '../parent', '../neighbour', '../../ancestor'],
  root: 'foo/bar/src',
  loglevel: []
}
var paths = [
  '*',
]
var result = fsIn.fsInheritanceLib.findGlobPatterns(config, paths[0])
console.log(result)
// [
//  'foo/bar/src/foo',
//  'foo/bar/src/bar',
//  'foo/bar/src/baz',
//  '../parent/foo/bar/src/foo',
//  '../parent/foo/bar/src/bar',
//  '../parent/foo/bar/src/baz',
//  '../neighbour/foo/bar/src/foo',
//  '../neighbour/foo/bar/src/bar',
//  '../neighbour/foo/bar/src/baz',
//  '../../ancestor/foo/bar/src/foo',
//  '../../ancestor/foo/bar/src/bar',
//  '../../ancestor/foo/bar/src/baz',
// ]

fsInheritanceLib~removeDuplicates(arr) ⇒ Array

remove duplicates in array

Kind: inner method of fsInheritanceLib
Returns: Array - without duplicates

Param Type Description
arr Array with duplicates

Example

var result = fsInheritanceLib.removeDuplicates(['foo', 'foo', 'bar', 'bar', 'bar', 'baz'])
console.log(result)
// ['foo', 'bar', 'baz']

fsInheritanceLib~filterRegex(arr, regex) ⇒ Array

filter array of strings by matching regex

Kind: inner method of fsInheritanceLib
Returns: Array - array without matching regex

Param Type Description
arr Array with matching regex
regex String that should be used to filter

Example

var regex = new RegExp('foo', 'g')
var array = ['foofoo', 'barfoo', 'bar', 'baz']
var result = fsInheritanceLib.filterRegex()
console.log(result)
// ['foofoo', 'barfoo']

fsInheritanceLib~writeAssetLibrary(input, name, dest)

write a file

Kind: inner method of fsInheritanceLib

Param Type Description
input any string/object/array that should be written
name any name of the output file (with extension)
dest any destination of file

Example

var string = 'someString'
fsInheritanceLib.writeAssetLibrary(string, 'bar.txt', './')
// creates ./bar.txt with content 'someString'

fsInheritanceLib~mkLib(cfg, cb) ⇒ Object | Array | function

Kind: inner method of fsInheritanceLib
Descriptiondescription: Make Library collects files or paths and writes a file it can also have a callback function which returns the data found

Param Type Description
cfg Object config
cb function takes optional callback function

Example

var config = {
  root: 'foo/bar/baz',
  files: ['foo.js', 'bar.js', 'baz.js'],
  outputPath: 'bar/foo',
  outputName: 'baz.json'
}
fsInheritanceLib.writeAssetLibrary(config, function(result){
  console.log('result')
  // ['./foo/bar/baz/foo.js', './foo/bar/baz/bar.js', './foo/bar/baz/baz.js']
  // and a file bar/foo/baz.json is created (if the path exists!)
})