Skip to content

Commit 08fa2bc

Browse files
committed
feat(config): support .esdoc.json, .esdoc.js and package.json
1 parent 2ebb2c6 commit 08fa2bc

File tree

6 files changed

+130
-9
lines changed

6 files changed

+130
-9
lines changed

src/ESDocCLI.js

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,20 @@ export default class ESDocCLI {
3838
*/
3939
exec() {
4040
let config;
41-
if (this._argv.c) {
42-
config = this._createConfigFromJSONFile(this._argv.c);
41+
42+
const configPath = this._findConfigFilePath();
43+
if (configPath) {
44+
config = this._createConfigFromJSONFile(configPath);
45+
} else {
46+
config = this._createConfigFromPackageJSON();
47+
}
48+
49+
if (config) {
50+
ESDoc.generate(config, defaultPublisher);
4351
} else {
4452
this._showHelp();
4553
process.exit(1);
4654
}
47-
48-
ESDoc.generate(config, defaultPublisher);
4955
}
5056

5157
/**
@@ -60,6 +66,11 @@ export default class ESDocCLI {
6066
console.log(' -h', 'output usage information');
6167
console.log(' -v', 'output the version number');
6268
console.log('');
69+
console.log('ESDoc finds configuration:');
70+
console.log(' 1. -c option');
71+
console.log(' 2. .esdoc.json in working directory');
72+
console.log(' 3. .esdoc.js in working directory');
73+
console.log(' 4. `esdoc` property in package.json');
6374
}
6475

6576
/**
@@ -75,6 +86,35 @@ export default class ESDocCLI {
7586
}
7687
}
7788

89+
/**
90+
* find ESDoc config file.
91+
* @returns {string|null} config file path.
92+
* @private
93+
*/
94+
_findConfigFilePath() {
95+
if (this._argv.c) {
96+
return this._argv.c;
97+
}
98+
99+
try {
100+
const filePath = path.resolve('./.esdoc.json');
101+
fs.readFileSync(filePath);
102+
return filePath;
103+
} catch (e) {
104+
// ignore
105+
}
106+
107+
try {
108+
const filePath = path.resolve('./.esdoc.js');
109+
fs.readFileSync(filePath);
110+
return filePath;
111+
} catch (e) {
112+
// ignore
113+
}
114+
115+
return null;
116+
}
117+
78118
/**
79119
* create config object from config file.
80120
* @param {string} configFilePath - config file path.
@@ -93,6 +133,24 @@ export default class ESDocCLI {
93133
return config;
94134
}
95135
}
136+
137+
/**
138+
* create config object from package.json.
139+
* @return {ESDocConfig|null} config object.
140+
* @private
141+
*/
142+
_createConfigFromPackageJSON() {
143+
try {
144+
const filePath = path.resolve('./package.json');
145+
const packageJSON = fs.readFileSync(filePath, 'utf8').toString();
146+
const packageObj = JSON.parse(packageJSON);
147+
return packageObj.esdoc;
148+
} catch (e) {
149+
// ignore
150+
}
151+
152+
return null;
153+
}
96154
}
97155

98156
// if this file is directory executed, work as CLI.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"source": "../../package/src",
3+
"destination": "../../dest/find-.esdoc.json",
4+
"access": ["public", "protected"],
5+
"includes": ["Access/.*\\.js"],
6+
"index": "../../package/README.md",
7+
"package": ".../../package/package.json"
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
"source": "../../package/src",
3+
"destination": "../../dest/find-.esdoc.js",
4+
"access": ["public", "protected"],
5+
"includes": ["Access/.*\\.js"],
6+
"index": "../../package/README.md",
7+
"package": ".../../package/package.json"
8+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"esdoc": {
3+
"source": "../../package/src",
4+
"destination": "../../dest/find-package.json",
5+
"access": ["public", "protected"],
6+
"includes": ["Access/.*\\.js"],
7+
"index": "../../package/README.md",
8+
"package": ".../../package/package.json"
9+
}
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import process from 'process';
2+
import {readDoc, assert, cli} from '../util.js';
3+
4+
describe('test finding config path:', ()=>{
5+
const cwd = process.cwd();
6+
7+
process.chdir('./test/fixture/config/find-.esdoc.json/');
8+
cli();
9+
process.chdir(cwd);
10+
11+
process.chdir('./test/fixture/config/find-.esdoc.js/');
12+
cli();
13+
process.chdir(cwd);
14+
15+
process.chdir('./test/fixture/config/find-package.json/');
16+
cli();
17+
process.chdir(cwd);
18+
19+
it('can find .esdoc.json', ()=>{
20+
const doc = readDoc('class/src/Access/Class.js~TestAccessClassPublic.html', './test/fixture/dest/find-.esdoc.json');
21+
assert.includes(doc, '.self-detail [data-ice="name"]', 'TestAccessClassPublic');
22+
});
23+
24+
it('can find .esdoc.js', ()=>{
25+
const doc = readDoc('class/src/Access/Class.js~TestAccessClassPublic.html', './test/fixture/dest/find-.esdoc.js');
26+
assert.includes(doc, '.self-detail [data-ice="name"]', 'TestAccessClassPublic');
27+
});
28+
29+
it('can find package.js', ()=>{
30+
const doc = readDoc('class/src/Access/Class.js~TestAccessClassPublic.html', './test/fixture/dest/find-package.json');
31+
assert.includes(doc, '.self-detail [data-ice="name"]', 'TestAccessClassPublic');
32+
});
33+
});

test/src/util.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import cheerio from 'cheerio';
44
import path from 'path';
55
import ESDocCLI from '../../src/ESDocCLI.js';
66

7-
export function cli(configPath) {
7+
export function cli(configPath = null) {
88
const cliPath = path.resolve('./src/cli.js');
9-
configPath = path.resolve(configPath);
10-
const argv = ['node', cliPath, '-c', configPath];
11-
const cli = new ESDocCLI(argv);
9+
const argv = ['node', cliPath];
10+
11+
if (configPath) {
12+
configPath = path.resolve(configPath);
13+
argv.push('-c', configPath);
14+
console.log(`process: ${configPath}`);
15+
}
1216

13-
console.log(`process: ${configPath}`);
17+
const cli = new ESDocCLI(argv);
1418
consoleLogSwitch(false);
1519
cli.exec();
1620
consoleLogSwitch(true);

0 commit comments

Comments
 (0)