Skip to content

Helper functions

Kaspars Zuks edited this page Jun 9, 2020 · 3 revisions
Table of Contents

Paths
Config
Merge
Glob generator helper

Paths

const getPaths = require('@videinfra/example-website-builder/lib/get-path');

getPathConfig()

Return path configuration object

getPaths.getPathConfig(); // => {'src': '...', 'dest': '...', 'html': {'src': '...', 'dest': '...'}, ...}

getSourcePath(task, ...paths)

Returns task source path or paths, absolute paths.

If a task has a single source path, then returns a string, if a task has multiple source paths then returns an array of paths.

getPaths.getSourcePath('html');
// => '/www/my-project/html/src/html'

getPaths.getSourcePath('html', 'foo', 'bar', 'baz.html');
// => '/www/my-project/html/src/html/foo/bar/baz.html'

getPaths.getSourcePath('example', 'baz.html');
// => ['/www/my-project/html/src/example-1/baz.html', '/www/my-project/html/src/example-2/baz.html']

getSourcePaths(task, ...paths)

Returns task source paths. Same as getSourcePath but always returns an array of paths even if there is a single path.

getDestPath(task, ...paths)

Returns task destination path

getPaths.getDestPath('html'); // => 'public'
getPaths.getDestPath('html', 'foo', 'bar', 'baz.html'); // => '/www/my-project/public/foo/bar/baz.html'

getProjectPath(...paths)

Returns a path relative to the project folder. Project folder is a folder from which "builder" command is called.

getPaths.getProjectPath(); // => '/www/my-project'
getPaths.getProjectPath('example/hello', 'world.txt'); // => '/www/my-project/example/hello/world.txt'

getBuilderPath(...paths)

Returns a path relative to the builderfolder. Builder folder is a @videinfra/example-website-builder package folder.

getPaths.getBuilderPath();
// => '/www/my-project/node_modules/@videinfra/example-website-builder'

getPaths.getBuilderPath('example/hello', 'world.txt');
// => '/www/my-project/node_modules/@videinfra/example-website-builder/example/hello/world.txt'

Config

const getConfig = require('@videinfra/example-website-builder/lib/get-config');

getConfig.getConfig()

Returns full config, including all tasks, paths and preprocess functions

getConfig.getConfig(); // => {'paths': {...}, 'tasks': {...}, 'preprocess': {...}, 'html': {...}, 'stylesheets': {...}, ...}

getConfig.getTaskConfig(...paths)

Returns a value from config object by path

getConfig.getTaskConfig('javascripts', 'webpack' 'resolve'); // => {alias:{}}

Merge

const merge = require('@videinfra/example-website-builder/lib/merge');

merge(dest, ...src)

Merge source objects into dest objects recursively. Uses lodash/merge but with support for merging arrays

merge({'a': ['b'], 'c': {'d': 1}}, {'a': ['e'], 'c': {'e': 2}, 'f': 3);
// => {'a': ['b', 'e'], 'c': {'d': 1, 'e': 2}, 'f': 3}

Glob generator helper

Helper to create globs and normalize for different operating systems.

const glob = require('@videinfra/example-website-builder/lib/globs-helper');

glob.paths(paths)

Creates a helper object with paths

glob.paths(getConfig.getSourcePaths('html')); // => [GlobObject] instance

glob.generate(...globs)

Outputs paths from all GlobObjects as array

glob.generate(
  glob.paths(getConfig.getSourcePaths('html')).allFiles(),
  glob.paths(getConfig.getSourcePaths('data')).allFiles()
);
// => ['/www/my-project/html/src/html/**/*', '/www/my-project/html/src/data/**/*']

Glob object methods

GlobObjectInstance.generate()

Returns an array of paths

glob.paths(getConfig.getSourcePaths('html')).generate();
// => ['/www/my-project/html/src/html']

GlobObjectInstance.path(paths)

Add recursively additional paths

glob.paths(getConfig.getSourcePaths('html')).paths(['a', 'b']).generate();
// => ['/www/my-project/html/src/html/a', '/www/my-project/html/src/html/b']

GlobObjectInstance.allFiles()

Adds a wildcard to match all files for the paths

glob.paths(getConfig.getSourcePaths('html')).allFiles().generate();
// => ['/www/my-project/html/src/html/**/*']

GlobObjectInstance.filesWithExtensions(extensions)

Adds file extensions to the paths

glob.paths(getConfig.getSourcePaths('html')).filesWithExtensions(['jpg']).generate();
// => ['/www/my-project/html/src/html/**/*.jpg']

glob.paths(getConfig.getSourcePaths('html')).filesWithExtensions(['jpg', 'png']).generate();
// => ['/www/my-project/html/src/html/**/*.{jpg,png}']

GlobObjectInstance.ignore()

Adds all paths to the ignore list, meaning that gulp.src won't be matching those files

glob.paths(getConfig.getSourcePaths('html')).allFiles().ignore().generate();
// => ['!/www/my-project/html/src/html/**/*']

GlobObjectInstance.map(fn)

Runs array .map functions on all paths.

function addHelloWorld (path, index) {
  return path + '/hello-world';
}
glob.paths(getConfig.getSourcePaths('html')).map(addHelloWorld).generate();
// => ['/www/my-project/html/src/html/hello-world']

Clone this wiki locally