UNPKG

2 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5
6const DEFAULT_CONFIG_IGNORE_PATHS = [];
7const DEFAULT_CONFIG_PARSER = 'dox';
8const DEFAULT_CONFIG_PLUGIN = 'markdown';
9
10/**
11 * Finds package.json file from either the directory the script was called from or a supplied path.
12 *
13 * console.log(findPackageFileInPath());
14 * console.log(findPackageFileInPath('./package.json'));
15 * console.log(findPackageFileInPath('~/git/github/doxdox/'));
16 *
17 * @param {String} [input] Directory or file.
18 * @return {String} Path to package.json file.
19 * @public
20 */
21
22const findPackageFileInPath = input => {
23
24 if (!input) {
25
26 input = process.cwd();
27
28 }
29
30 try {
31
32 const stat = fs.statSync(input);
33
34 if (stat.isFile()) {
35
36 return path.resolve(path.join(path.dirname(input), 'package.json'));
37
38 } else if (stat.isDirectory()) {
39
40 return path.resolve(path.join(input, 'package.json'));
41
42 }
43
44 } catch (err) {
45
46 process.stderr.write(`${err.toString()}\n`);
47
48 }
49
50 return null;
51
52};
53
54/**
55 * Format an array of directories and/or files to be ignored by globby by adding a "!" at the beginning of each path.
56 *
57 * console.log(formatPathsArrayToIgnore(['./src']));
58 *
59 * @param {Array} paths Array of directories and/or files.
60 * @return {Array} Modified array of directories and/or files.
61 * @public
62 */
63
64const formatPathsArrayToIgnore = paths =>
65 paths.filter(path => path).map(path => `!${path.replace(/^!/u, '')}`);
66
67/**
68 * Sets default configuration values.
69 *
70 * console.log(setConfigDefaults({}));
71 *
72 * @param {Object} config Custom configuration object.
73 * @return {Object} Modified configuration object.
74 * @public
75 */
76
77const setConfigDefaults = config => ({
78 'ignore': DEFAULT_CONFIG_IGNORE_PATHS,
79 'parser': DEFAULT_CONFIG_PARSER,
80 'plugin': DEFAULT_CONFIG_PLUGIN,
81 ...config
82});
83
84module.exports = {
85 findPackageFileInPath,
86 formatPathsArrayToIgnore,
87 setConfigDefaults
88};