-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathprocessFiles.js
More file actions
154 lines (132 loc) · 5.53 KB
/
processFiles.js
File metadata and controls
154 lines (132 loc) · 5.53 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
import fs from "fs";
import path from "path";
import { promisify } from "util";
import { execSync, exec as childExec } from "child_process";
const exec = promisify(childExec);
// Extensions defined in `BLUEHAWK_EXTENSIONS` can be snipped. Otherwise, treated as copy-only.
const BLUEHAWK_EXTENSIONS = new Set([
".c", ".cpp", ".cs", ".dart", ".go", ".gradle", ".groovy", ".gsh", ".gvy",
".gy", ".h", ".hpp", ".htm", ".html", ".java", ".js", ".json", ".jsx", ".kt",
".m", ".md", ".mm", ".php", ".py", ".rb", ".rs", ".rst", ".sc", ".scala", ".sh",
".svg", ".swift", ".ts", ".tsx", ".txt", ".uxml", ".xaml", ".xml", ".yaml",
]);
/**
* Resolves relative paths to absolute paths based on the Git repository root.
* @param {string} relativePath - The relative path to resolve.
* @returns {string} - The resolved absolute path.
*/
function resolvePathFromGitRoot(relativePath) {
let gitRoot;
try {
gitRoot = execSync("git rev-parse --show-toplevel", { encoding: "utf8" }).trim();
} catch (error) {
console.error("Error: Unable to determine the Git repository root. Ensure this script is run within a Git repository.");
throw error;
}
return path.resolve(gitRoot, relativePath);
}
/**
* Collects all files (recursively) in the given directory, ignoring specified patterns.
* @param {string} dirPath - Directory to scan for files.
* @param {Array<string>} arrayOfFiles - Array to hold the file paths.
* @param {Set<string>} ignorePatterns - Set of folder/file names to ignore.
* @returns {Array<string>} - List of absolute file paths.
*/
function getAllFiles(dirPath, arrayOfFiles = [], ignorePatterns = new Set()) {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const absolutePath = path.join(dirPath, file);
if (!ignorePatterns.has(file)) {
if (fs.statSync(absolutePath).isDirectory()) {
arrayOfFiles = getAllFiles(absolutePath, arrayOfFiles, ignorePatterns);
} else {
arrayOfFiles.push(absolutePath);
}
}
});
return arrayOfFiles;
}
/**
* Ensures the given directory exists, creating it if necessary.
* @param {string} directory - Directory path to create.
*/
async function ensureDirectoryExists(directory) {
try {
await fs.promises.mkdir(directory, { recursive: true });
} catch (error) {
console.error(`Failed to create directory: ${directory}`, error);
}
}
/**
* Processes a single file by snipping or copying based on its extension and contents.
* @param {string} filePath - Path of the file to process.
* @param {string} startDirectory - Root directory being processed.
* @param {string} outputDirectory - Directory to write processed files to.
*/
async function snip(filePath, startDirectory, outputDirectory) {
const fileExt = path.extname(filePath);
if (!fileExt) {
throw new Error(`File has no extension type. Please check file path: ${filePath}`);
}
const fileName = path.basename(filePath);
if (!BLUEHAWK_EXTENSIONS.has(fileExt)) {
return;
}
const relPath = path.relative(startDirectory, filePath);
const outputDir = path.join(outputDirectory, relPath.replace(fileName, ""));
await ensureDirectoryExists(outputDir);
const snipCommand = `bluehawk snip --output "${outputDir}" "${filePath}"`;
const copyCommand = `bluehawk copy --output "${outputDir}" "${filePath}"`;
let command;
const fileContents = fs.readFileSync(filePath, "utf8");
command = fileContents.includes("snippet-start") ? snipCommand : copyCommand;
try {
const { stdout, stderr } = await exec(command);
const lines = stdout.split("\n");
await handleOutput(lines, command);
// Bluehawk uses a library with a known bug that sometimes outputs this error
// The file still copies/snips, so we can ignore it
if (stderr && !stderr.startsWith('Error: Unable to use "first char" lexer optimizations:')) {
console.error(`Error encountered during processing: ${stderr}`);
}
} catch (error) {
console.error(`Failed to execute command: ${command}`, error);
}
}
let numFilesProcessedAndWritten = [0, 0];
/**
* Aggregates output statistics for processed and written files.
* @param {Array<string>} stdoutLines - Lines of output from the processing command.
*/
async function handleOutput(stdoutLines) {
const processedLine = stdoutLines.find((line) =>
line.toLowerCase().includes("processed")
);
if (processedLine) {
const processCountMatch = processedLine.match(/\d+/);
if (processCountMatch) {
numFilesProcessedAndWritten[0] += parseInt(processCountMatch[0], 10);
}
}
const writeCount = stdoutLines.filter((line) =>
line.toLowerCase().includes("wrote")
).length;
numFilesProcessedAndWritten[1] += writeCount;
}
/**
* Main driver function for processing files.
* @param {string} startDirectory - Root directory containing files to process.
* @param {string} outputDirectory - Directory where processed files should be written.
* @param {Set<string>} ignorePatterns - Set of folder/file names to ignore during processing.
*/
export async function processFiles(startDirectory, outputDirectory, ignorePatterns = new Set()) {
startDirectory = resolvePathFromGitRoot(startDirectory);
outputDirectory = resolvePathFromGitRoot(outputDirectory);
console.log(`Processing example files in ${startDirectory}`);
const files = getAllFiles(startDirectory, [], ignorePatterns);
for (const file of files) {
await snip(file, startDirectory, outputDirectory);
}
console.log(`Processed ${numFilesProcessedAndWritten[0]} file(s)`);
console.log(`Wrote ${numFilesProcessedAndWritten[1]} file(s) to ${outputDirectory}`);
}