-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (32 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env node
const { readFileSync, writeFileSync } = require('fs');
const { join } = require('path');
const { render } = require('ejs');
const pkg = require(join(process.cwd(), 'package.json'));
if (!/^@use-hooks\//.test(pkg.name)) {
console.error('ERROR: Only for @use-hooks packages.');
process.exit(0);
}
const jsdocRegex = /[ \t]*\/\*\*\s*\n([^*]*(\*[^/])?)*\*\//g;
const sourceCode = readFileSync(join(process.cwd(), 'src/index.js'), 'utf8');
const jsdocs = sourceCode.match(jsdocRegex);
if (
!jsdocs
|| jsdocs.length !== 2
|| !jsdocs[0].includes('Params')
|| !jsdocs[1].includes('Returns')
) {
console.error('ERROR: Invalid format of JSDoc.');
process.exit(0);
}
const example = readFileSync(join(process.cwd(), 'example/App.jsx'), 'utf8');
const readmeTpl = readFileSync(join(__dirname, 'README.TEMPLATE.md'), 'utf8');
const readme = render(readmeTpl, {
name: pkg.name.split('/')[1],
desc: pkg.description,
params: jsdocs[0],
returns: jsdocs[1],
example: example.replace('../src', pkg.name),
});
writeFileSync(join(process.cwd(), 'README.md'), readme, 'utf8');
console.log('README.md is updated.')