forked from TeamWertarbyte/react-props-md-table
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite-readme.js
More file actions
52 lines (46 loc) · 1.7 KB
/
write-readme.js
File metadata and controls
52 lines (46 loc) · 1.7 KB
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
44
45
46
47
48
49
50
51
52
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const writeReadme = (readme, propsTable, regex, force) => {
try {
let readmeContent = '';
if(!fs.existsSync(readme)) {
console.warn(chalk.yellow(`${readme} not found.`));
} else {
readmeContent = fs.readFileSync(path.resolve(process.cwd(), readme), 'utf-8');
}
if (readmeContent.match(regex)) {
const result = readmeContent.replace(
regex,
'<!-- props-table-start -->\n' + propsTable + '<!-- props-table-end -->'
);
fs.writeFile(readme, result, 'utf-8', (e) => {
if (e) {
return console.error(chalk.red(`Error updating propsTable in ${readme}: ${e}`));
} else {
return console.log(chalk.green(`${readme} updated successfully.`));
}
});
} else {
if (force) {
const result = readmeContent + '\n\n<!-- props-table-start -->\n' + propsTable + '<!-- props-table-end -->';
fs.writeFile(readme, result, 'utf-8', (e) => {
if (e) {
return console.error(chalk.red(`Error updating propsTable in ${readme}: ${e}`));
} else {
return console.log(chalk.green(`${readme} force updated successfully.`));
}
});
} else {
console.warn(
chalk.yellow(`Could not find propsTable to replace in ${readme}\n`),
'The props table needs to be wrapped between <!-- props-table-start --> and <!-- props-table-end -->\n',
'or use with --force to add to the end of the file'
)
}
}
} catch (e) {
console.error(chalk.red(`Error updating propsTable in ${readme}: ${e}`));
}
}
module.exports = writeReadme;