From 62856ff57a6bf70c2173386e103fd33b9c825703 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Sun, 14 Aug 2016 18:12:59 +0100 Subject: [PATCH] start work on documentation generator --- .eslintrc.js | 1 + docs/custom/getting_started.js | 8 +++++ docs/custom/index.js | 14 +++++++++ docs/docs.json | 1 + docs/gen/index.js | 53 ++++++++++++++++++++++++++++++++++ package.json | 7 +++-- 6 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 docs/custom/getting_started.js create mode 100644 docs/custom/index.js create mode 100644 docs/docs.json create mode 100644 docs/gen/index.js diff --git a/.eslintrc.js b/.eslintrc.js index a721fe464..d4ac453f8 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -13,5 +13,6 @@ module.exports = { "no-restricted-syntax": 0, "no-param-reassign": 0, "consistent-return": 0, + "import/no-extraneous-dependencies": 0, } }; \ No newline at end of file diff --git a/docs/custom/getting_started.js b/docs/custom/getting_started.js new file mode 100644 index 000000000..fcf5e1e0a --- /dev/null +++ b/docs/custom/getting_started.js @@ -0,0 +1,8 @@ +module.exports = { + category: 'General', + name: 'Getting Started', + data: +`# Welcome! +discord.js is an easy-to-use and intuitive JavaScript library that wraps around the Discord API. +`, +}; diff --git a/docs/custom/index.js b/docs/custom/index.js new file mode 100644 index 000000000..15fe93bb7 --- /dev/null +++ b/docs/custom/index.js @@ -0,0 +1,14 @@ +const files = [ + require('./getting_started'), +]; + +const categories = {}; +for (const file of files) { + file.category = file.category.toLowerCase(); + if (!categories[file.category]) { + categories[file.category] = {}; + } + categories[file.category][file.name] = file.data; +} + +module.exports = categories; diff --git a/docs/docs.json b/docs/docs.json new file mode 100644 index 000000000..e79ce6b00 --- /dev/null +++ b/docs/docs.json @@ -0,0 +1 @@ +{"custom":{"general":{"Getting Started":"# Welcome!\ndiscord.js is an easy-to-use and intuitive JavaScript library that wraps around the Discord API.\n"}},"json":[{"id":"Client","longname":"Client","name":"Client","scope":"global","kind":"class","description":"Creates a new Discord Client\n```js\nconst Discord = require(\"discord.js\");\nconst client = new Discord.Client();\n```","meta":{"lineno":18,"filename":"Client.js","path":"src/client"},"order":0},{"id":"User","longname":"User","name":"User","scope":"global","kind":"class","description":"Represents a User on Discord.","meta":{"lineno":6,"filename":"User.js","path":"src/structures"},"order":3},{"id":"Client#user","longname":"Client#user","name":"user","scope":"instance","kind":"member","description":"The User of the logged in Client, only available after `READY` has been fired.","memberof":"Client","params":[],"returns":[{"type":{"names":["ClientUser"]},"description":"[description]"}],"meta":{"lineno":55,"filename":"Client.js","path":"src/client"},"order":2},{"id":"User#username","longname":"User#username","name":"username","scope":"instance","kind":"member","description":"The username of the User","memberof":"User","type":{"names":["String"]},"meta":{"lineno":19,"filename":"User.js","path":"src/structures"},"order":4},{"id":"User#id","longname":"User#id","name":"id","scope":"instance","kind":"member","description":"The ID of the User","memberof":"User","type":{"names":["String"]},"meta":{"lineno":24,"filename":"User.js","path":"src/structures"},"order":5},{"id":"User#discriminator","longname":"User#discriminator","name":"discriminator","scope":"instance","kind":"member","description":"A discriminator based on username for the User","memberof":"User","type":{"names":["String"]},"meta":{"lineno":29,"filename":"User.js","path":"src/structures"},"order":6},{"id":"User#avatar","longname":"User#avatar","name":"avatar","scope":"instance","kind":"member","description":"The ID of the user's avatar","memberof":"User","type":{"names":["String"]},"meta":{"lineno":34,"filename":"User.js","path":"src/structures"},"order":7},{"id":"User#bot","longname":"User#bot","name":"bot","scope":"instance","kind":"member","description":"Whether or not the User is a Bot.","memberof":"User","type":{"names":["Boolean"]},"meta":{"lineno":39,"filename":"User.js","path":"src/structures"},"order":8},{"id":"User#status","longname":"User#status","name":"status","scope":"instance","kind":"member","description":"The status of the user:\n\n* **`online`** - user is online\n* **`offline`** - user is offline\n* **`idle`** - user is AFK","memberof":"User","type":{"names":["String"]},"meta":{"lineno":48,"filename":"User.js","path":"src/structures"},"order":9},{"id":"Client#login","longname":"Client#login","name":"login","scope":"instance","kind":"function","description":"Logs the client in. If successful, resolves with the account's token.","memberof":"Client","params":[{"type":{"names":["string"]},"description":"The email or token used for the account. If it is an email, a password _must_ be\nprovided.","name":"emailOrToken"},{"type":{"names":["string"]},"optional":true,"description":"The password for the account, only needed if an email was provided.","name":"password"}],"examples":["client.login(\"token\");\n// or\nclient.login(\"email\", \"password\");"],"returns":[{"type":{"names":["Promise."]}}],"meta":{"lineno":42,"filename":"Client.js","path":"src/client"},"order":1},{"id":"User#deleteDM","longname":"User#deleteDM","name":"deleteDM","scope":"instance","kind":"function","description":"Deletes a DM Channel (if one exists) between the Client and the User. Resolves with the Channel if successful.","memberof":"User","params":[],"returns":[{"type":{"names":["Promise."]}}],"meta":{"lineno":60,"filename":"User.js","path":"src/structures"},"order":10}]} \ No newline at end of file diff --git a/docs/gen/index.js b/docs/gen/index.js new file mode 100644 index 000000000..ddd4ef9c7 --- /dev/null +++ b/docs/gen/index.js @@ -0,0 +1,53 @@ +let fs; +/* eslint no-console:0 no-return-assign:0 */ +let parse; + +const customDocs = require('../custom/index'); + +const GEN_VERSION = 1; + +try { + fs = require('fs-extra'); + parse = require('jsdoc-parse'); +} catch (e) { + console.log('Error loading fs-extra or jsdoc-parse:'); + console.log(e); + process.exit(); +} + +console.log('Starting...'); + +let json = ''; + +const stream = parse({ + src: ['./src/*.js', './src/*/*.js'], +}); + +const cwd = (`${process.cwd()}\\`).replace(/\\/g, '/'); + +function cleanPaths() { + for (const item of json) { + if (item.meta && item.meta.path) { + item.meta.path = item.meta.path.replace(/\\/g, '/').replace(cwd, ''); + } + } +} + +function next() { + json = JSON.parse(json); + cleanPaths(); + console.log('parsed inline code'); + json = { + custom: customDocs, + json, + }; + fs.writeFile('./docs/docs.json', JSON.stringify(json, null, 0), err => { + if (err) { + throw err; + } + console.log('done'); + }); +} + +stream.on('data', chunk => json += chunk.toString('utf-8')); +stream.on('end', () => next()); diff --git a/package.json b/package.json index 09f090512..64bc6e480 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "A way to interface with the Discord API", "main": "./src/index", "scripts": { - "test": "node test/random" + "test": "node test/random", + "docs": "node docs/gen/index.js" }, "repository": { "type": "git", @@ -45,7 +46,9 @@ "grunt-contrib-uglify": "^0.11.0", "grunt-jscs": "^2.8.0", "jscs": "^2.11.0", - "load-grunt-tasks": "^3.3.0" + "load-grunt-tasks": "^3.3.0", + "fs-extra": "^0.30.0", + "jsdoc-parse": "^1.2.7" }, "optionalDependencies": { "node-opus": "^0.1.11"