-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildReadme.js
More file actions
60 lines (55 loc) · 2.22 KB
/
buildReadme.js
File metadata and controls
60 lines (55 loc) · 2.22 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
53
54
55
56
57
58
59
60
const fs = require('fs');
const buildReadme = (file, currentPath, title, figlet, licenseBadge, description, repositoryUrl, prefix = '', extraData) => {
if (currentPath) {
let files = fs.readdirSync(currentPath);
// Detect if a .gitignore file exists
if (files.includes('.gitignore') && extraData.ignore_gitIgnoreFiles) {
// List the files and patterns in the .gitignore file
const gitignore = fs.readFileSync('.gitignore', 'utf8');
// filter out the files in the current directory that are in the .gitignore file
files = files.filter((file) => !gitignore.includes(file));
}
if (extraData.ignore_gitFiles) {
// ignore any file that starts with a .git
files = files.filter((file) => !file.startsWith('.git'));
}
const directory = [];
const currentFiles = [];
let directoryFileList = '';
let currentFilesList = '';
let directoryTree = '';
// identify if the files are directories or files
files.map((file) => {
const filePath = currentPath + '/' + file;
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
directory.push(file);
directoryFileList += ` - [${file}/](./${file}/)\r`;
} else {
currentFiles.push(file);
currentFilesList += ` - [${file}](./${file})\r`;
}
});
currentFiles.forEach((element) => {
directoryTree += ' ' + `│ ${element}\n`;
});
directory.forEach((element) => {
directoryTree += ' ' + `└─── ${element}/\n`;
});
const directoryTreePrefix = `\`\`\`\n${file}/\n`;
const directoryTreeSuffix = `\`\`\``;
const buildReadme = `
${licenseBadge ? licenseBadge + '\n\n' : ''}
# ${title ? title : 'Awesome-Readme'}
${figlet}
${description ? description : ''}
${directoryFileList ? '## Directories\n' + directoryFileList + '\n' : ''}
${currentFilesList ? currentFilesList : ''}
${directoryTree ? '## Directory Tree\n[<- Previous](' + repositoryUrl + ')\n' + directoryTreePrefix + directoryTree + directoryTreeSuffix : ''}
`;
fs.writeFileSync(currentPath + '/README.md', buildReadme);
console.log('\x1b[32m%s\x1b[0m', 'README.md created in ' + currentPath, '\x1b[0m');
return directoryTree;
}
};
module.exports = buildReadme;