smonn / globcat Goto Github PK
View Code? Open in Web Editor NEWConcatenate files from command line with glob pattern.
Home Page: https://www.npmjs.com/package/globcat
License: MIT License
Concatenate files from command line with glob pattern.
Home Page: https://www.npmjs.com/package/globcat
License: MIT License
globcat makes combining files super easy, appreciate it
One feature I'd like to see added is sourcemap generation, since I'm using it to concat JS files currently.
Thanks again!
Hi, this is a really useful library, so thank you!
I've hit an issue when concatenating large files, whereby the file contents get split up and mixed in with the content of other files.
I've created a codesandbox to reproduce the issue:
https://codesandbox.io/p/sandbox/clever-heisenberg-fkfxrt
The two input files in ./data
are a.txt
(containing thousands of lines of "a") and b.txt
(containing a single "b"). There's a build
script which runs globcat
and outputs to ./dist/output.txt
.
Expected outcome - I expect output.txt
to contain all the content from a.txt
followed by the content from b.txt
.
Actual outcome - The content from b.txt
is in the middle if the output, with thousands of "a"s before and after it.
I have limited experience with Node streams but I think the issue might lie here: https://github.com/smonn/globcat/blob/main/src/lib/files-to-stream.ts. Specifically this bit (shortened for clarity):
for (const file of files) {
const stream = fs.createReadStream(file)
passthrough = stream.pipe(passthrough, { end: false })
}
As I understand it, because this happens within a loop, it means that files are being streamed to passthrough
simultaneously, so their order is not guaranteed. I imagine this is fine as long as each file is below the highWaterMark
(64KiB by default) and is therefore processed as a single chunk.
However, files larger than this will be broken into chunks, and those chunks may be delayed and processed after chunks from a different file, making it possible for file content to be jumbled up as in my reproduction.
I've fixed this locally by writing chunks synchronously. As I say, I'm no expert on Node streams so I don't know whether there are any issues with this solution, but I've included it in case it's helpful:
for (const file of files) {
const stats = await fsPromises.stat(file)
if (stats.isFile()) {
const stream = fs.createReadStream(file)
stream.once('end', () => {
queueSize--
if (queueSize === 0) {
passthrough.end()
}
})
for await (const chunk of stream) {
passthrough.write(chunk)
}
} else {
throw new Error('Not a file: ' + file)
}
}
Thanks again,
Pete
It's currently not pushing the updated package.json to the main branch. See build output for more details:
https://github.com/smonn/globcat/actions/runs/4209293690/jobs/7306163261
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.