-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack.js
More file actions
157 lines (142 loc) · 4.35 KB
/
pack.js
File metadata and controls
157 lines (142 loc) · 4.35 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const fs = require('fs');
const path = require('path');
// Replace with the desired root directory
const rootDir = __dirname;
// source directory
const srcDir = path.join(rootDir, 'src');
// Directory for storing the generated JavaScript files
const distDir = path.join(rootDir, 'dist');
// Configuration file path
const configFile = path.join(rootDir, 'conf.json');
/**
* Traverses the directories recursively starting from the specified directory.
*
* @param {string} directory - The current directory to traverse.
*/
function traverseDirectories(directory) {
const files = fs.readdirSync(directory);
files.forEach((file) => {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory() && !file.startsWith('.') && directory !== distDir) {
processFilesInDirectory(filePath);
}
});
}
/**
* Processes the files within a directory.
*
* @param {string} directory - The directory to process.
*/
function processFilesInDirectory(directory) {
const files = fs.readdirSync(directory);
const functions = [];
files.forEach((file) => {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isFile() && path.extname(file) === '.md') {
const fileFunctions = processMarkdownFile(filePath);
functions.push(...fileFunctions);
}
});
if (functions.length > 0) {
const dirName = path.basename(directory);
const jsFilePath = path.join(distDir, `${dirName}.js`);
const jsContent = functions.join('\n\n');
fs.writeFileSync(jsFilePath, jsContent);
console.log(`Created ${jsFilePath}`);
updateConfigFile(jsFilePath);
} else {
const dirName = path.basename(directory);
const jsFilePath = path.join(distDir, `${dirName}.js`);
const jsContent = '// No scripts found';
fs.writeFileSync(jsFilePath, jsContent);
console.log(`Created ${jsFilePath}`);
updateConfigFile(jsFilePath);
}
}
/**
* Processes a markdown file and extracts functions from code blocks.
*
* @param {string} filePath - The path to the markdown file.
* @returns {string[]} - An array of extracted functions.
*/
function processMarkdownFile(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
const regex = /### Code(?:.|[\r\n])*?```(?:js|javascript|JavaScript)([\s\S]*?)```/g;
const matches = [...content.matchAll(regex)];
const functions = matches.map((match) => match[1].trim());
return functions;
}
/**
* Updates the configuration file with the path of the generated JavaScript file.
*
* @param {string} jsFilePath - The path of the generated JavaScript file.
*/
function updateConfigFile(jsFilePath) {
const config = require(configFile);
const relativePath = `./${path.basename(jsFilePath)}`;
if (!config.source.include.includes(relativePath)) {
config.source.include.push(relativePath);
fs.writeFileSync(configFile, JSON.stringify(config, null, 2));
console.log(`Updated ${configFile}`);
}
}
/**
* Creates the configuration file if it doesn't exist.
*/
function createConfigFileIfNotExists() {
if (!fs.existsSync(configFile)) {
const initialConfig = {
source: {
include: []
},
plugins: [
'plugins/markdown',
'plugins/summarize'
],
markdown: {
parser: 'gfm',
hardwrap: false,
idInHeadings: true
},
templates: {
cleverLinks: false,
monospaceLinks: false,
name: 'library-gs',
footerText: 'PhobiaCide',
tabNames: {
api: 'API',
tutorials: 'Examples'
}
},
metadata: {
title: 'library-gs'
},
opts: {
'prism-theme': 'prism-custom',
encoding: 'utf8',
recurse: true,
package: 'package.json',
readme: 'README.md',
destination: './.docs/',
verbose: true,
template: './node_modules/docdash',
theme_opts: {
default_theme: 'dark'
}
}
};
fs.writeFileSync(configFile, JSON.stringify(initialConfig, null, 2));
console.log(`Created ${configFile}`);
}
}
// Create the .dist directory if it doesn't exist
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir);
console.log(`Created ${distDir}`);
}
// Create the config file if it doesn't exist
createConfigFileIfNotExists();
// Start traversing the directories from the root
traverseDirectories(srcDir);