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 20, 2017
1 parent c0740cf commit 681af03
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/ESDoc.js
Expand Up @@ -94,6 +94,7 @@ export default class ESDoc {

results = Plugin.onHandleTag(results);

// cleanup
fs.removeSync(config.destination);

// dump.json
Expand All @@ -109,8 +110,35 @@ export default class ESDoc {
fs.outputFileSync(filePath, json);
}

// package.json
try {
publisher(results, config);
const json = fs.readFileSync(config.package, {encoding: 'utf-8'});
const filePath = path.resolve(config.destination, 'package.json');
fs.outputFileSync(filePath, json, {encoding: 'utf8'});
} catch (e) {
// ignore
}

try {
const write = (content, filePath) =>{
const _filePath = path.resolve(config.destination, filePath);
content = Plugin.onHandleContent(content, _filePath);

// todo: remove deprecated code
const ext = path.extname(filePath).toLowerCase();
if (ext === '.html') content = Plugin.onHandleHTML(content, _filePath);

console.log(`output: ${_filePath}`);
fs.outputFileSync(_filePath, content);
};

const copy = (srcPath, destPath) => {
const _destPath = path.resolve(config.destination, destPath);
console.log(`output: ${_destPath}`);
fs.copySync(srcPath, _destPath);
};

Plugin.onPublish(write, copy);
} catch (e) {
InvalidCodeLogger.showError(e);
process.exit(1);
Expand Down
22 changes: 22 additions & 0 deletions src/Plugin/Plugin.js
Expand Up @@ -120,18 +120,40 @@ class Plugin {
return ev.data.tag;
}

/**
* handle publish
* @param {function(filePath: string, content: string)} writeFile - write content.
* @param {function(srcPath: string, destPath: string)} copyDir - copy directory.
*/
onPublish(writeFile, copyDir) {
const ev = new PluginEvent({});

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

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

/**
* handle HTML.
* @param {string} html - original HTML.
* @param {string} fileName - the fileName of the HTML file.
* @returns {string} handled HTML.
* @deprecated please use onHandleContent
*/
onHandleHTML(html, fileName) {
const ev = new PluginEvent({html, fileName});
this._execHandler('onHandleHTML', ev);
return ev.data.html;
}

onHandleContent(content, fileName) {
const ev = new PluginEvent({content, fileName});
this._execHandler('onHandleContent', ev);
return ev.data.content;
}

/**
* handle complete
*/
Expand Down

0 comments on commit 681af03

Please sign in to comment.