file-flattener
Advanced tools
Comparing version 1.0.2 to 1.0.3
33
index.js
@@ -12,2 +12,3 @@ #!/usr/bin/env node | ||
.option('-i, --ignore [ignore]', 'File extensions to ignore, comman-separated') | ||
.option('-o, --output [output]', 'Output format') | ||
.option('-v, --version', 'Output the version number'); | ||
@@ -31,2 +32,3 @@ | ||
// Function to check if the file should be ignored | ||
function isIgnored(filePath, ignore) { | ||
@@ -36,4 +38,7 @@ return ignore && ignore.some(ignoredFile => filePath.endsWith(ignoredFile)); | ||
// JSON output array used when output format is JSON | ||
const jsonOutput = []; | ||
// Function to flatten files | ||
async function flattenFiles(baseDir, dir, fileEndings, blacklist, ignore) { | ||
async function flattenFiles(baseDir, dir, fileEndings, blacklist, ignore, outputFormat) { | ||
const fullDirPath = path.join(baseDir, dir); | ||
@@ -45,3 +50,3 @@ const files = fs.readdirSync(fullDirPath, { withFileTypes: true }); | ||
if (file.isDirectory() && !isBlacklisted(filePath, blacklist)) { | ||
await flattenFiles(baseDir, relativeFilePath, fileEndings, blacklist, ignore); // Pass relativeFilePath as the new dir | ||
await flattenFiles(baseDir, relativeFilePath, fileEndings, blacklist, ignore, outputFormat); // Pass relativeFilePath as the new dir | ||
} else if (file.isFile()) { | ||
@@ -52,4 +57,8 @@ if (isIgnored(filePath, ignore)) { | ||
if ((!fileEndings && isTextFile(filePath)) || (fileEndings && fileEndings.some(ext => file.name.endsWith(ext)) && isTextFile(filePath))) { | ||
const contents = fs.readFileSync(filePath, 'utf8'); | ||
console.log(`---\n${relativeFilePath}\n---\n${contents}\n`); // Output to console including the relative path | ||
const content = fs.readFileSync(filePath, 'utf8'); | ||
if (outputFormat === 'text') { | ||
console.log(`---\n${relativeFilePath}\n---\n${content}\n`); // Output to console including the relative path | ||
} else if (outputFormat === 'json') { | ||
jsonOutput.push({ metadata: { path: relativeFilePath }, content }); | ||
} | ||
} | ||
@@ -85,4 +94,18 @@ } | ||
// Parse output format | ||
let outputFormat = 'text'; | ||
if (options.output) { | ||
outputFormat = options.output; | ||
if (!['text', 'json'].includes(outputFormat)) { | ||
return console.log('Error: Invalid output format. Valid options are "text" and "json"'); | ||
} | ||
} | ||
// Start flattening files from the current directory | ||
await flattenFiles('.', '', fileEndings, blacklist, ignore); | ||
await flattenFiles('.', '', fileEndings, blacklist, ignore, outputFormat); | ||
// Output the JSON if requested | ||
if (outputFormat === 'json') { | ||
console.log(JSON.stringify(jsonOutput, null, 2)); | ||
} | ||
} | ||
@@ -89,0 +112,0 @@ |
{ | ||
"name": "file-flattener", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "File flattener", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5422
90