apidoc 監視檔案變動自動生成工具
阿新 • • 發佈:2019-02-02
之前介紹了apiDoc這個生成Restful api文件的工具,可是還是不爽,每次寫完了還要手動去執行命令,所以自己寫了一個監視檔案變化,然後自動執行生成文件的工具。
先上程式碼:
/**
* Created by lincoln on 16-1-6.
*/
var gaze = require('gaze');
var exec = require('child_process').exec;
var fs = require('fs')
function init(){
fs.mkdirSync('./api');
fs.mkdirSync('./doc');
createConfigureFile();
beginWatch();
}
/**
{
"name": "測試",
"version": "0.0.1",
"description": "API文件測試",
"title": "API文件測試",
"url" : "http://121.41.44.218",
"sampleUrl" : "http://121.41.44.218",
"template":{
"forceLanguage":"zh-cn"
}
}
*/
function createConfigureFile(){
var configure = {
"name": "測試",
"version": "0.0.1",
"description": "API文件測試",
"title": "API文件測試",
"url" : "http://xxxxxx",
"sampleUrl" : "http://xxx",
"template":{
"forceLanguage":"zh-cn"
}
}
fs.writeFileSync('./api/apidoc.json' ,JSON.stringify(configure));
}
function beginWatch(){
gaze('./api/*.*',function(error,watcher){
this.on('all', function(event, filepath) {
console.log(filepath + ' was ' + event);
runGeneartion();
})
});
}
function runGeneartion(){
var com = exec('apidoc -i ./api -o ./doc ' )
com.stdout.on('data', function (data) {
console.log("生成Api->"+data);
});
com.stderr.on('data', function (data) {
console.log('生成錯誤啦->' + data);
});
}
if(fs.existsSync('./api') && fs.existsSync('./doc')){
beginWatch();
}else{
init();
}
工具說明:
1、第一次執行,生成兩個資料夾api和doc,並在api中生成apidoc.json配置檔案,然後監測在api資料夾中的檔案變化,如果發現變化,就生成文件。
2、如果存在api和doc,則直接監視api資料夾中的變動