Skip to content

Commit

Permalink
feat(config): support .esdoc.json, .esdoc.js and package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
h13i32maru committed Dec 4, 2016
1 parent 2ebb2c6 commit 08fa2bc
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 9 deletions.
66 changes: 62 additions & 4 deletions src/ESDocCLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ export default class ESDocCLI {
*/
exec() {
let config;
if (this._argv.c) {
config = this._createConfigFromJSONFile(this._argv.c);

const configPath = this._findConfigFilePath();
if (configPath) {
config = this._createConfigFromJSONFile(configPath);
} else {
config = this._createConfigFromPackageJSON();
}

if (config) {
ESDoc.generate(config, defaultPublisher);
} else {
this._showHelp();
process.exit(1);
}

ESDoc.generate(config, defaultPublisher);
}

/**
Expand All @@ -60,6 +66,11 @@ export default class ESDocCLI {
console.log(' -h', 'output usage information');
console.log(' -v', 'output the version number');
console.log('');
console.log('ESDoc finds configuration:');
console.log(' 1. -c option');
console.log(' 2. .esdoc.json in working directory');
console.log(' 3. .esdoc.js in working directory');
console.log(' 4. `esdoc` property in package.json');
}

/**
Expand All @@ -75,6 +86,35 @@ export default class ESDocCLI {
}
}

/**
* find ESDoc config file.
* @returns {string|null} config file path.
* @private
*/
_findConfigFilePath() {
if (this._argv.c) {
return this._argv.c;
}

try {
const filePath = path.resolve('./.esdoc.json');
fs.readFileSync(filePath);
return filePath;
} catch (e) {
// ignore
}

try {
const filePath = path.resolve('./.esdoc.js');
fs.readFileSync(filePath);
return filePath;
} catch (e) {
// ignore
}

return null;
}

/**
* create config object from config file.
* @param {string} configFilePath - config file path.
Expand All @@ -93,6 +133,24 @@ export default class ESDocCLI {
return config;
}
}

/**
* create config object from package.json.
* @return {ESDocConfig|null} config object.
* @private
*/
_createConfigFromPackageJSON() {
try {
const filePath = path.resolve('./package.json');
const packageJSON = fs.readFileSync(filePath, 'utf8').toString();
const packageObj = JSON.parse(packageJSON);
return packageObj.esdoc;
} catch (e) {
// ignore
}

return null;
}
}

// if this file is directory executed, work as CLI.
Expand Down
8 changes: 8 additions & 0 deletions test/fixture/config/find-.esdoc.js/.esdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"source": "../../package/src",
"destination": "../../dest/find-.esdoc.json",
"access": ["public", "protected"],
"includes": ["Access/.*\\.js"],
"index": "../../package/README.md",
"package": ".../../package/package.json"
}
8 changes: 8 additions & 0 deletions test/fixture/config/find-.esdoc.json/.esdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
"source": "../../package/src",
"destination": "../../dest/find-.esdoc.js",
"access": ["public", "protected"],
"includes": ["Access/.*\\.js"],
"index": "../../package/README.md",
"package": ".../../package/package.json"
};
10 changes: 10 additions & 0 deletions test/fixture/config/find-package.json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"esdoc": {
"source": "../../package/src",
"destination": "../../dest/find-package.json",
"access": ["public", "protected"],
"includes": ["Access/.*\\.js"],
"index": "../../package/README.md",
"package": ".../../package/package.json"
}
}
33 changes: 33 additions & 0 deletions test/src/ConfigTest/FindConfigPathTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import process from 'process';
import {readDoc, assert, cli} from '../util.js';

describe('test finding config path:', ()=>{
const cwd = process.cwd();

process.chdir('./test/fixture/config/find-.esdoc.json/');
cli();
process.chdir(cwd);

process.chdir('./test/fixture/config/find-.esdoc.js/');
cli();
process.chdir(cwd);

process.chdir('./test/fixture/config/find-package.json/');
cli();
process.chdir(cwd);

it('can find .esdoc.json', ()=>{
const doc = readDoc('class/src/Access/Class.js~TestAccessClassPublic.html', './test/fixture/dest/find-.esdoc.json');
assert.includes(doc, '.self-detail [data-ice="name"]', 'TestAccessClassPublic');
});

it('can find .esdoc.js', ()=>{
const doc = readDoc('class/src/Access/Class.js~TestAccessClassPublic.html', './test/fixture/dest/find-.esdoc.js');
assert.includes(doc, '.self-detail [data-ice="name"]', 'TestAccessClassPublic');
});

it('can find package.js', ()=>{
const doc = readDoc('class/src/Access/Class.js~TestAccessClassPublic.html', './test/fixture/dest/find-package.json');
assert.includes(doc, '.self-detail [data-ice="name"]', 'TestAccessClassPublic');
});
});
14 changes: 9 additions & 5 deletions test/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import cheerio from 'cheerio';
import path from 'path';
import ESDocCLI from '../../src/ESDocCLI.js';

export function cli(configPath) {
export function cli(configPath = null) {
const cliPath = path.resolve('./src/cli.js');
configPath = path.resolve(configPath);
const argv = ['node', cliPath, '-c', configPath];
const cli = new ESDocCLI(argv);
const argv = ['node', cliPath];

if (configPath) {
configPath = path.resolve(configPath);
argv.push('-c', configPath);
console.log(`process: ${configPath}`);
}

console.log(`process: ${configPath}`);
const cli = new ESDocCLI(argv);
consoleLogSwitch(false);
cli.exec();
consoleLogSwitch(true);
Expand Down

0 comments on commit 08fa2bc

Please sign in to comment.