-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (100 loc) · 3.22 KB
/
index.js
File metadata and controls
114 lines (100 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';
const fs = require('fs');
const path = require('path');
const findup = require('findup');
/**
* Return the package.json's path or undefined if not found
*
* Note: The search is always done in the ancestors of the file tree
*
* @param {string} [dir=process.cwd()] Path where the file search starts
* @returns {string} Package.json's path or undefined if not found
*/
const findPathPkg = (dir = process.cwd()) => {
try {
return path.join(findup.sync(path.resolve(process.cwd(), dir), 'package.json'), 'package.json');
} catch {
return undefined;
}
};
/**
* Return the JSON content of package.json or empty JSON if not found the file package.json
*
* @param {string} [pathSearchStart] Path where the file search starts
* @returns {Object} JSON content of package.json
*/
const getJsonPkg = pathSearchStart => {
const pkgPath = findPathPkg(pathSearchStart);
let contentJson = {};
if (pkgPath) {
contentJson = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
}
return contentJson;
};
/**
* Return the property value from object via a string key
*
* For example:
*
* const obj = {
* "a": "Value a",
* "child": {
* "b": "Value b"
* }
* }
*
* getDeepAccessProp(obj, 'child.b'); // -> 'Value b'
*
* @param {Object} obj A object
* @param {string} key A key
* @returns {Object} The property value of key in obj
*/
const getDeepAccessProp = (object, key) => {
// eslint-disable-next-line unicorn/no-array-reduce
return key.split('.').reduce((nestedObject, key) => {
if (nestedObject && key in nestedObject) {
return nestedObject[key];
}
return undefined;
}, object);
};
/**
* Plugin markdown-magic-package-json
*
* @param {string} content Content before transformation
* @param {Object} options Options passed to the transform
* @param {string} [options.template] String with placeholders like Template literals. For example, if we want to retrieve the `name` property in the package.json, we can write this: `${name}`
* @param {string} [options.unknownTxt] String to add if the property is unknown
* @param {string} [options.pkg] `package.json` path. If the path is incorrect, the plugin find `package.json` in ancestor's dir
* @param {Object} config markdown-magic configuration object
* @returns {string} Content modified
*/
module.exports = (content, options, config) => {
if (!options) {
options = {};
}
let updatedContent = 'undefined';
if (options.unknownTxt) {
updatedContent = options.unknownTxt;
}
if (options.template) {
updatedContent = options.template;
}
let result;
const reg = /(?:\${(\w+(?:\.\w+)?)})/gi;
const jsonPkg = getJsonPkg(options.pkg, config);
while ((result = reg.exec(options.template)) !== null) {
const placeholder = result[0];
const match = result[1];
updatedContent = options.unknownTxt && getDeepAccessProp(jsonPkg, match) === undefined ? updatedContent.replace(placeholder, options.unknownTxt) : updatedContent.replace(placeholder, getDeepAccessProp(jsonPkg, match));
}
return updatedContent
.replace(/\\n/g, '\n')
.replace(/\\t/g, '\t')
.replace(/\\r/g, '\r')
.replace(/\\b/g, '\b')
.replace(/\\f/g, '\f');
};
module.exports.findPathPkg = findPathPkg;
module.exports.getJsonPkg = getJsonPkg;
module.exports.getDeepAccessProp = getDeepAccessProp;