Skip to content

Instantly share code, notes, and snippets.

@b-m-9
Created December 1, 2018 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-m-9/543921871d40d9b857d7dc9d8accc783 to your computer and use it in GitHub Desktop.
Save b-m-9/543921871d40d9b857d7dc9d8accc783 to your computer and use it in GitHub Desktop.
buildAPI.js for api-nodejs
const fs = require('fs');
const path = require('path');
const pathProject = path.normalize(__dirname + '/..');
const config = require('../modules/config');
function getPathAPI(apiPath) {
return apiPath.replace(path.normalize(pathProject + '/api/'), '').replace('.js', '')
}
function requireAPI(apiPath) {
console.log("API start:" + getPathAPI(apiPath));
let file = fs.readFileSync(apiPath);
let lines = file.toString().split('\n');
for (let number = 0; lines.length > number; number++) {
if (lines[number].indexOf('API.register(') !== -1) {
let str = lines[number];
let name_method = str.substring(str.indexOf('(') + 1, str.indexOf('=>')).split(',')[0].replace('async','').replace(new RegExp(' ', 'g'), '').replace(new RegExp('\'', 'g'), '');
let spases = str.substring(0, str.indexOf('API'));
if (name_method[0] === '(') name_method = '';
if (name_method[0] + name_method[1] === ' (') name_method = '';
if (name_method !== '') name_method = "/" + name_method;
console.log('JSdoc method:', getPathAPI(apiPath) + name_method);
if (lines[number - 1].indexOf('/**') === -1)
lines.splice(number, 0, '\n' + spases + '/** @method ' + getPathAPI(apiPath) + name_method + ' */');
else
lines[number - 1] = spases + '/** @method ' + getPathAPI(apiPath) + name_method + ' */'
}
}
let readyFile = lines.join('\n');
fs.writeFileSync(apiPath, readyFile);
}
function readDirApi(dir) {
fs.readdir(dir, (err, items) => {
for (let i = 0; i < items.length; i++) {
if (fs.statSync(dir + '/' + items[i]).isDirectory())
readDirApi(dir + '/' + items[i]);
else
requireAPI(dir + '/' + items[i]);
}
});
}
readDirApi(pathProject + '/api');
const RestapiDocs = require('api-nodejs-docs');
setTimeout(() => {
console.log('\n\nStart build docs project (nuxt.js)\n');
RestapiDocs({
"baseUrl": config.get('shema') + '://' + config.get('domain'),
"API_URL": config.get('api_url'),
"projectName": config.get('project_name'),
"hideDeveloper": false
});
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment