Skip to content
Permalink
Browse files

feat(config): support .esdoc.json, .esdoc.js and package.json

  • Loading branch information...
h13i32maru committed Dec 4, 2016
1 parent 2ebb2c6 commit 08fa2bc296b552168f86538561174e010a3a2f14
@@ -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);
}

/**
@@ -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');
}

/**
@@ -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.
@@ -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.
@@ -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"
}
@@ -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"
};
@@ -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"
}
}
@@ -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');
});
});
@@ -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);

0 comments on commit 08fa2bc

Please sign in to comment.
You can’t perform that action at this time.