This repository was archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathreader.js
167 lines (151 loc) · 5.01 KB
/
reader.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* apidoc
* https://apidocjs.com
*
* Authors:
* Peter Rottmann <rottmann@inveris.de>
* Nicolas CARPi @ Deltablot
* Copyright (c) 2013 inveris OHG
* Licensed under the MIT license.
*/
const _ = require('lodash');
const defaultConfig = {
name: 'Acme project',
version: '0.0.0',
description: 'REST Api',
};
/**
* Read information about the source code we are parsing from the config file
*/
class Reader {
constructor (app) {
this.app = app;
this.log = app.log;
this.opt = app.options;
this.fs = require('fs-extra');
this.path = require('path');
}
// read the apidoc.json file, or apidoc.config.js or from the package.json
read () {
let config = {};
// if the config file is provided, we use this and do no try to read other files
if (this.opt.config) {
this.log.debug('Config file provided, reading this.');
config = require(this.path.resolve(this.opt.config));
} else {
config = this.search();
}
// replace header footer with file contents
return Object.assign(config, this.getHeaderFooter(config));
}
/**
* Look for config files in input folder
*/
search () {
this.log.debug('Now looking for apidoc config files');
// possible sources of information
const sources = [
'package.json',
'apidoc.json',
'apidoc.config.js',
];
// create a new object because javascript will not assign value
const config = Object.assign({}, defaultConfig);
// loop the three possible source of information to try and find packageInfo
sources.forEach(configFile => {
this.log.debug(`Now looking for ${configFile}`);
// first look in cwd dir
Object.assign(config, this.findConfigFileInDir(configFile, process.cwd()));
// scan each source dir to find a valid config file
this.opt.src.forEach(dir => {
Object.assign(config, this.findConfigFileInDir(configFile, dir));
});
});
if (_.isEqual(config, defaultConfig)) {
this.log.warn('No config files found.');
}
return config;
}
/**
* Get json.header / json.footer title and markdown content (from file)
*
* @param {Object} config
* @returns {Object}
*/
getHeaderFooter (config) {
const result = {};
['header', 'footer'].forEach(key => {
if (config[key] && config[key].filename) {
this.log.debug('Now looking for ' + key);
// note that markdown files path is taken from first input value
let filePath = this.path.join(config.input ? config.input[0] : './', config[key].filename);
// try again to find it in current dir
if (!this.fs.existsSync(filePath)) { filePath = this.path.join(process.cwd(), config[key].filename); }
// try again to find it in input folders
if (!this.fs.existsSync(filePath)) { filePath = this.findFileInSrc(config[key].filename); }
// try again to find it in dir with the config file
if (!this.fs.existsSync(filePath) && typeof this.opt.config === 'string') {
filePath = this.path.join(this.path.dirname(this.opt.config), config[key].filename);
}
try {
this.log.debug(`Reading ${key} file: ${filePath}`);
const content = this.fs.readFileSync(filePath, 'utf8');
result[key] = {
title: config[key].title,
content: this.app.markdownParser ? this.app.markdownParser.render(content) : content,
};
} catch (e) {
throw new Error('Can not read: ' + filePath);
}
}
});
return result;
}
/**
* Scan a directory for config files
*/
findConfigFileInDir (filename, dir) {
let foundConfig;
const target = this.path.resolve(this.path.join(dir, filename));
if (this.fs.existsSync(target)) {
this.log.debug(`Found file: ${target}`);
foundConfig = require(target);
// if it has an apidoc key, read that
if (foundConfig.apidoc) {
this.log.verbose(`Using apidoc key of ${filename}`);
// pull any missing config from root
['version', 'name', 'description'].forEach(key => {
if (!foundConfig.apidoc[key] && foundConfig[key]) {
this.log.verbose(`Using ${key} from root of ${filename}`);
foundConfig.apidoc[key] = foundConfig[key];
}
});
return foundConfig.apidoc;
}
// for package.json we don't want to read it if it has no apidoc key
if (filename !== 'package.json') {
return foundConfig;
}
}
return {};
}
/**
* Look for a file in each of the input folders
*/
findFileInSrc (filename) {
// scan each source dir to find a valid config file
// note that any file found here will supersede a previously found file
for (const dir of this.opt.src) {
const target = this.path.join(dir, filename);
if (this.fs.existsSync(target)) {
this.log.debug('Found file: ' + target);
return this.path.resolve(target);
}
}
return '';
}
}
module.exports = {
Reader: Reader,
defaultConfig: defaultConfig,
};