Skip to content

Commit

Permalink
feat(publish): split to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
h13i32maru committed Feb 26, 2017
1 parent 681af03 commit ad0a33d
Show file tree
Hide file tree
Showing 188 changed files with 259 additions and 8,729 deletions.
1 change: 0 additions & 1 deletion script/build.js
Expand Up @@ -5,7 +5,6 @@ sh.rm('./out/src');
sh.mkdir('./out/src');
sh.exec('./node_modules/.bin/babel --out-dir out/src src');
sh.chmod('./out/src/ESDocCLI.js', '755');
sh.cp('./src/Publisher/Builder/template/', './out/src/Publisher/Builder/template/');

// build test
sh.rm('./out/test/src');
Expand Down
2 changes: 1 addition & 1 deletion src/Doc/FileDoc.js
Expand Up @@ -31,7 +31,7 @@ export default class FileDoc extends AbstractDoc {

/** specify name to longname */
_$longname() {
this._value.longname = this._value.name;
this._value.longname = this._pathResolver.fileFullPath;
}

/** specify file content to value.content */
Expand Down
82 changes: 78 additions & 4 deletions src/ESDoc.js
Expand Up @@ -25,10 +25,8 @@ export default class ESDoc {
/**
* Generate documentation.
* @param {ESDocConfig} config - config for generation.
* @param {function(results: Object[], config: ESDocConfig)} publisher - callback for output html.
*/
static generate(config, publisher) {
assert(typeof publisher === 'function');
static generate(config) {
assert(config.source);
assert(config.destination);

Expand Down Expand Up @@ -92,6 +90,29 @@ export default class ESDoc {
if (tag.undocument === true) tag.ignore = true;
}

// config.index
if (config.index) {
try {
const indexContent = fs.readFileSync(config.index, {encode: 'utf8'}).toString();
const tag = {
kind: 'index',
content: indexContent,
longname: path.resolve(config.index),
name: config.index,
static: true,
access: 'public'
};
results.push(tag);
} catch (e) {
// ignore
}
}

// manual
if (config.manual) {
results.push(...this._generateForManual(config));
}

results = Plugin.onHandleTag(results);

// cleanup
Expand Down Expand Up @@ -138,7 +159,12 @@ export default class ESDoc {
fs.copySync(srcPath, _destPath);
};

Plugin.onPublish(write, copy);
const read = (filePath) => {
const _filePath = path.resolve(config.destination, filePath);
return fs.readFileSync(_filePath).toString();
};

Plugin.onPublish(write, copy, read);
} catch (e) {
InvalidCodeLogger.showError(e);
process.exit(1);
Expand Down Expand Up @@ -305,4 +331,52 @@ export default class ESDoc {

return {results: factory.results, ast: ast};
}

static _generateForManual(config) {
const results = [];

if (!config.manual) return results;

if (config.manual.index) {
results.push({
kind: 'manualIndex',
globalIndex: config.manual.globalIndex,
coverage: config.manual.coverage,
content: fs.readFileSync(config.manual.index).toString(),
longname: path.resolve(config.manual.index),
name: config.manual.index,
static: true,
access: 'public'
});
}

if (config.manual.asset) {
results.push({
kind: 'manualAsset',
longname: path.resolve(config.manual.asset),
name: config.manual.asset,
static: true,
access: 'public'
});
}

const names = ['overview', 'design', 'installation', 'usage', 'tutorial', 'configuration', 'example', 'advanced', 'faq', 'changelog'];
for (const name of names) {
if (!config.manual[name]) continue;

const kind = `manual${name.replace(/^./, c => c.toUpperCase())}`;
for (const filePath of config.manual[name]) {
results.push({
kind: kind,
longname: path.resolve(filePath),
name: filePath,
content: fs.readFileSync(filePath).toString(),
static: true,
access: 'public'
});
}
}

return results;
}
}
3 changes: 1 addition & 2 deletions src/ESDocCLI.js
Expand Up @@ -3,7 +3,6 @@ import fs from 'fs';
import path from 'path';
import minimist from 'minimist';
import ESDoc from './ESDoc.js';
import defaultPublisher from './Publisher/publish.js';
import NPMUtil from './Util/NPMUtil.js';

/**
Expand Down Expand Up @@ -47,7 +46,7 @@ export default class ESDocCLI {
}

if (config) {
ESDoc.generate(config, defaultPublisher);
ESDoc.generate(config);
} else {
this._showHelp();
process.exit(1);
Expand Down
4 changes: 3 additions & 1 deletion src/Plugin/Plugin.js
Expand Up @@ -124,13 +124,15 @@ class Plugin {
* handle publish
* @param {function(filePath: string, content: string)} writeFile - write content.
* @param {function(srcPath: string, destPath: string)} copyDir - copy directory.
* @param {function(filePath: string):string} readFile - read content.
*/
onPublish(writeFile, copyDir) {
onPublish(writeFile, copyDir, readFile) {
const ev = new PluginEvent({});

// hack: fixme
ev.data.writeFile = writeFile;
ev.data.copyDir = copyDir;
ev.data.readFile = readFile;

this._execHandler('onPublish', ev);
}
Expand Down

0 comments on commit ad0a33d

Please sign in to comment.