Skip to content

Commit d2cf649

Browse files
author
Vahid Alimohamadi
authored
Merge pull request #1 from cybercoder/develop
Develop
2 parents 0bf30ef + 990edd2 commit d2cf649

14 files changed

+1366
-287
lines changed

.eslintrc.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"extends": "standard",
33
"env": {
4-
"browser": true,
54
"node": true,
65
"mocha": true
76
},

.gitignore

+11-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
node_modules
1+
node_modules
2+
.idea
3+
dump.rdb
4+
.DS_Store
5+
.vscode
6+
yarn-error.log
7+
build
8+
dist
9+
doc
10+
docs
11+
public

README.MD

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# socketio-doc
2+
A socket.io document generator.
3+
4+
## Installation and Usage
5+
You can install ```socketio-doc``` globally or in your project's ```node_modules``` folder.
6+
7+
To install the latest version on npm globally:
8+
9+
```
10+
npm install -g socketio-doc
11+
```
12+
13+
To install the latest version on npm locally and save it in your package's ```package.json``` file:
14+
15+
```
16+
npm install --save-dev socketio-doc
17+
```
18+
19+
## Configuration
20+
By default needs a file named ```socketio-doc.conf.json``` in your project root folder with contents like these:
21+
22+
```
23+
{
24+
"version" : "2.3.1",
25+
"source": ["./src/io/*.js"],
26+
"destination" : "./socketio-doc"
27+
}
28+
```
29+
or you can simply pass custom file: ```socketio-doc -c <filename>```.
30+
31+
## Inline comments
32+
This is a example for commenting ```emits```, and ```listens```:
33+
34+
```
35+
/**
36+
* Singleline or multiline description text. Line breaks are preserved.
37+
* @socket.io-doc
38+
* @listen exampleListen description
39+
* @tag Messaging
40+
* @data {object}
41+
* @example
42+
* {
43+
* "id": 1,
44+
* "title": 5
45+
* }
46+
*/
47+
socket.on('exampleListen', data => {
48+
/**
49+
* Singleline or multiline description text. Line breaks are preserved.
50+
* @socket.io-doc
51+
* @emit exampleEmit description
52+
* @tag Messaging
53+
* @data {object}
54+
* @example { id: 5, data: [2, 3] }
55+
*/
56+
socket.emit('exampleEmit', { id: 5, data: [2, 3] });
57+
});
58+
```
59+
60+
to generate document in interactive ```HTML```, just try:
61+
62+
```
63+
socketio-doc
64+
```
65+
if it is installed globally
66+
or
67+
```
68+
./node_modules/.bin/socketio-doc
69+
```
70+
if installed locally.
71+
It will genetate folders which determined in configuration file as destination.

cli.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('./index')

index.js

+71-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,72 @@
1-
const commentParser = require('comment-parser');
2-
3-
const parseFile = file => new Promise((resolve,reject)=>{
4-
commentParser.file(file, (err,result)=>err ? reject(err) : resolve(result));
5-
})
6-
7-
parseFile('./sample.js').then(c=> {
8-
let all = Object.values(c).reduce((result,item)=>{
9-
if (!item.tags.some(i=>i.tag==='socket.io-doc')) return result;
10-
let category = item.tags.find(i=>i.tag==='tag');
11-
category=category ? category.name : 'uncategorized';
12-
13-
let transformed = item.tags.reduce((r,t)=>{
14-
(t.tag === 'listen' || t.tag ==='emit') && (r={...r,action:t.tag, event: t.name});
15-
t.tag==='example' && (r={...r, example: t.name.replace(/\n|\r/g, "")+t.description.replace(/\n|\r/g, "")});
16-
return r;
17-
},{
18-
category,
19-
description:item.description || null
20-
});
21-
result.push(transformed);
22-
return result;
23-
},[]);
24-
25-
console.log(all);
26-
}).catch(e=>console.error(e));
1+
#!/usr/bin/env node
2+
const {get_files, parse_file, createLinks, generate_html_template} = require('./lib');
3+
const fs = require('fs');
4+
const path = require('path');
275

6+
const socketioDocCli = require("commander");
7+
const chalk = require("chalk");
8+
9+
socketioDocCli.option('-c, --config <string>', 'Specify config path.').action( cmd=> {
10+
let {config} = cmd;
11+
console.log(chalk.white("\n\tconfig file: "), chalk.gray(config || 'config file not specified, using default.','\n'));
12+
13+
// check config file flag or default config file existance.
14+
if (!config && !fs.existsSync(path.resolve('./socketio-doc.conf.json'))) {
15+
return console.log(chalk.red('config file not found.\n'));
16+
}
17+
18+
if (config && !fs.existsSync(path.resolve(config))) {
19+
return console.log(chalk.red('specified config file, not found.\n'));
20+
}
21+
22+
let { source, destination, version } = config ? require(path.resolve(`${config}`)) : require(path.resolve('./socketio-doc.conf.json'));
23+
24+
destination = path.resolve(destination);
25+
26+
if (!source) {
27+
return console.log(chalk.red('source not determined.\n'));
28+
}
29+
30+
if (!destination) {
31+
return console.log(chalk.red('destination not determined.\n'));
32+
}
33+
34+
console.log(chalk.green("Start creating documents...\n"));
35+
36+
return get_files(source)
37+
.then(async files=>{
38+
let results = await Promise.all(files.flat().map(async file=> {
39+
let c = await parse_file(file);
40+
return Object.values(c).reduce((result,item)=>{
41+
if (!item.tags.some(i=>i.tag==='socket.io-doc')) return result;
42+
let tag = item.tags.find(i=>i.tag==='tag');
43+
tag=tag ? tag.name : 'uncategorized';
44+
45+
let transformed = item.tags.reduce((r,t)=>{
46+
(t.tag === 'listen' || t.tag ==='emit') && (r={...r,action:t.tag, event: t.name});
47+
t.tag==='example' && (r={...r, example: t.name.replace(/\n|\r/g, "")+t.description.replace(/\n|\r/g, "")});
48+
return r;
49+
},{
50+
tag,
51+
description:item.description || null
52+
});
53+
result.push(transformed);
54+
return result;
55+
},[]);
56+
}));
57+
58+
results=results.flat();
59+
60+
let emitLinks = createLinks(results.filter(r=>r.action==='listen'));
61+
62+
!fs.existsSync(`${destination}`) && fs.mkdirSync(`${destination}`);
63+
!fs.existsSync(`${destination}/css`) && fs.mkdirSync(`${destination}/css`);
64+
65+
fs.createReadStream(`${__dirname}/templates/default/css/style.css`).pipe(fs.createWriteStream(`${destination}/css/style.css`));
66+
fs.writeFileSync(`${destination}/index.html`, generate_html_template(emitLinks,results,version));
67+
console.log(chalk.green(`\tBuild compelete : ${destination}`))
68+
})
69+
.catch(e=>console.error(chalk.red(e)));
70+
});
71+
72+
socketioDocCli.parse(process.argv);

0 commit comments

Comments
 (0)