Repository navigation
Expand file tree
/
Copy pathpackager.js
More file actions
134 lines (112 loc) · 3.61 KB
/
Copy pathpackager.js
File metadata and controls
134 lines (112 loc) · 3.61 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
const fs = require('fs');
const path = require('path');
const ignore = require('ignore');
const yargs = require('yargs');
const { minify } = require('html-minifier');
// Define command line arguments
const argv = yargs
.option('appDir', {
alias: 'a',
description: 'The directory of the React application',
type: 'string',
demandOption: true,
})
.option('outputFile', {
alias: 'o',
description: 'The output file path',
type: 'string',
default: 'combinedApp.txt',
})
.help()
.alias('help', 'h')
.argv;
// Define the directory containing your React application
const appDirectory = path.resolve(argv.appDir);
// Define the output file path
const outputFilePath = path.resolve(argv.outputFile);
// Read the .gitignore file
const gitignorePath = path.join(appDirectory, '.gitignore');
const ig = ignore();
// Custom ignore list
const customIgnoreList = ['mockServiceWorker.js'];
// Load the .gitignore file
async function loadGitignore() {
try {
const gitignoreContent = await fs.promises.readFile(gitignorePath, 'utf8');
ig.add(gitignoreContent);
} catch (error) {
if (error.code === 'ENOENT') {
console.warn('.gitignore file not found, proceeding without ignoring any files.');
} else {
console.error('Error reading .gitignore file:', error.message);
throw error;
}
}
// Add custom ignore list
ig.add(customIgnoreList);
}
// Helper function to read file content
async function readFileContent(filePath) {
try {
return await fs.promises.readFile(filePath, 'utf8');
} catch (error) {
console.error(`Error reading file ${filePath}:`, error.message);
throw error;
}
}
// Function to recursively get all files in a directory, filtering by .gitignore
async function getAllFiles(dirPath, arrayOfFiles = []) {
try {
const files = await fs.promises.readdir(dirPath);
for (const file of files) {
const fullPath = path.join(dirPath, file);
const relativePath = path.relative(appDirectory, fullPath);
if (ig.ignores(relativePath) || relativePath.includes('node_modules')) {
continue;
}
const stat = await fs.promises.stat(fullPath);
if (stat.isDirectory()) {
await getAllFiles(fullPath, arrayOfFiles);
} else if (/\.(js|jsx|ts|tsx|css|html|py)$/i.test(file)) { // Case-insensitive matching
arrayOfFiles.push(fullPath);
}
}
return arrayOfFiles;
} catch (error) {
console.error(`Error accessing directory ${dirPath}:`, error.message);
throw error;
}
}
// Minify HTML content
function minifyHtml(content) {
return minify(content, {
collapseWhitespace: true,
removeComments: true,
minifyJS: true,
minifyCSS: true,
});
}
// Main function to create the combined file
async function createCombinedFile() {
try {
await loadGitignore();
const allFiles = await getAllFiles(appDirectory);
// Create a write stream
const writeStream = fs.createWriteStream(outputFilePath);
for (const [index, absoluteFilePath] of allFiles.entries()) {
let fileContent = await readFileContent(absoluteFilePath);
// Minify HTML files
if (absoluteFilePath.endsWith('.html')) {
fileContent = minifyHtml(fileContent);
}
const separator = index > 0 ? '\n/* ' + '='.repeat(80) + ' */\n' : ''; // Add comment separator except before the first file
writeStream.write(separator + fileContent);
}
writeStream.end();
console.log(`Combined file created at ${outputFilePath}`);
} catch (error) {
console.error('Error creating combined file:', error.message);
}
}
// Run the main function
createCombinedFile();