UNPKG

3.17 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const logger = require('./logger');
3const dom = require('./parse/dom.js');
4const url = require('url');
5const path = require('path');
6
7exports.defaultDocsPath = './docs';
8exports.defaultBuildPath = './_site'
9exports.projectPath = process.cwd();
10exports.defaultTplHookPrefix = 'tpl:'
11/**
12 * 复制一个对象的属性到另一个对象
13 *
14 * @param {any} obj
15 * @param {any} props
16 * @returns
17 */
18exports.extend = function extend(obj, props) {
19 for (let i in props) {
20 if (props.hasOwnProperty(i)) {
21 obj[i] = props[i];
22 }
23 }
24 return obj;
25}
26
27exports.distinct = function(arr, fn){
28 let data = [], existValue = []
29 arr.forEach(item=>{
30 let value = fn(item)
31 if(existValue.indexOf(value) === -1){
32 existValue.push(value)
33 data.push(item)
34 }
35 })
36 return data;
37}
38
39
40exports.clearArray = function clearArray(a) {
41 return a.splice(0, a.length);
42}
43
44function fileExist(filePath){
45 try {
46 return fs.statSync(filePath).isFile();
47 } catch (err) {
48 return false;
49 }
50}
51exports.fileExist = fileExist;
52
53
54function dirExist(filePath){
55 try {
56 return fs.statSync(filePath).isDirectory();
57 } catch (err) {
58 return false;
59 }
60}
61exports.dirExist = dirExist;
62
63
64/**
65 * log 输出,一共四个 api:
66 * log.debug(msg)
67 * log.info(msg)
68 * log.warn(msg)
69 * log.error(msg)
70 */
71exports.log = new logger('info');
72
73exports.hashEncode = function hashEncode(text){
74 return text.replace(/[\~\:\#\@\/\(\)]/g, '').replace(/\s+/g, '-');
75}
76
77exports.handleMdUrl = (findTransactionBySrcPath) => (content, filepath) => {
78 if(!/\.md/.test(content)){
79 return content;
80 }
81 let $ = dom.parse(content);
82 let urls = $('a');
83 urls.each(function(){
84 let item = $(this);
85 let href = item.attr('href');
86 if(!href) return;
87 let urlObj = url.parse(href);
88
89 if(urlObj.hostname){
90 return;
91 }
92 if(!urlObj.path){
93 return;
94 }
95
96 if(path.extname(urlObj.pathname) === '.md'){
97 let srcPath = path.resolve(filepath, urlObj.pathname);
98 let findTransaction = findTransactionBySrcPath(srcPath);
99 if(findTransaction){
100 item.attr('href', href.replace('.md', '.html'))
101 }
102 }
103 })
104 return $.html()
105}
106
107exports.getConfigPath = function getConfigPath(dirname){
108 let allowFilename = ['ydoc.json', 'ydoc.js'];
109 for(let i =0; i< allowFilename.length; i++){
110 let configFilepath = path.resolve(dirname, allowFilename[i]);
111 if(fileExist(configFilepath)) return configFilepath;
112 }
113 return null;
114}
115
116exports.getConfig = function getConfig(filepath){
117 if(fileExist(filepath)){
118 return require(filepath)
119 }
120 return {};
121}
122
123exports.isUrl = function(url){
124 return /^https?:\/\//.test(url)
125}
126
127/**
128 * 递归合并文件
129 * @param {*} src 源文件目录
130 * @param {*} dist 目标文件目录
131 */
132function mergeCopyFiles(src, dist){
133 let files = fs.readdirSync(src);
134 files.forEach(item=>{
135 let distPath = path.resolve(dist, item);
136 let srcPath = path.resolve(src, item)
137 if(fileExist(srcPath)){
138 fs.copySync(srcPath, distPath)
139 }else if(dirExist(srcPath)){
140 mergeCopyFiles(srcPath, distPath)
141 }
142 })
143}
144
145exports.mergeCopyFiles = mergeCopyFiles;
\No newline at end of file