stdin-glob 1.0.7 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -8
- package/dist/index.js +43 -6
- package/dist/utils/binary.d.ts +6 -0
- package/dist/utils/binary.js +20 -0
- package/dist/utils/pipes.d.ts +9 -0
- package/dist/utils/pipes.js +28 -0
- package/package.json +5 -1
- package/.github/workflows/npm-publish.yml +0 -26
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/src/index.ts +0 -103
- package/tsconfig.json +0 -19
package/README.md
CHANGED
|
@@ -25,9 +25,12 @@ This pipes all relevant TypeScript/TSX files directly into my clipboard, ready t
|
|
|
25
25
|
|
|
26
26
|
- Expand glob patterns to find matching files
|
|
27
27
|
- Output file contents with syntax highlighting markers
|
|
28
|
-
-
|
|
28
|
+
- **Limit output to a specific number of lines per file** with `--max-lines`
|
|
29
|
+
- **Show line numbers** with `--line-numbers` for better code reference
|
|
30
|
+
- **Auto-copy output** directly to clipboard with `--copy` flag
|
|
29
31
|
- Support for absolute or relative paths
|
|
30
32
|
- Option to show only file paths without content
|
|
33
|
+
- **Intelligent handling of binary files** - shows metadata instead of attempting to display unreadable content
|
|
31
34
|
- Written in TypeScript
|
|
32
35
|
|
|
33
36
|
## Installation
|
|
@@ -44,13 +47,15 @@ stdin-glob [options] [patterns...]
|
|
|
44
47
|
|
|
45
48
|
### Options
|
|
46
49
|
|
|
47
|
-
| Option
|
|
48
|
-
|
|
|
49
|
-
| `--no-content`
|
|
50
|
-
| `--absolute`
|
|
51
|
-
| `-c, --copy`
|
|
52
|
-
| `-
|
|
53
|
-
| `-
|
|
50
|
+
| Option | Description |
|
|
51
|
+
| --------------------- | --------------------------------------------------------------------- |
|
|
52
|
+
| `--no-content` | Do not show file contents, only list matching paths |
|
|
53
|
+
| `--absolute` | Show absolute paths for entries |
|
|
54
|
+
| `-c, --copy` | Copy the output to clipboard instead of printing to console |
|
|
55
|
+
| `-m, --max-lines <n>` | Show only the first N lines of each file (shows full file if omitted) |
|
|
56
|
+
| `-n, --line-numbers` | Display line numbers next to each line, like in IDE sidebars |
|
|
57
|
+
| `-V, --version` | Output the version number |
|
|
58
|
+
| `-h, --help` | Display help information |
|
|
54
59
|
|
|
55
60
|
### Arguments
|
|
56
61
|
|
|
@@ -92,6 +97,75 @@ function add(a, b) {
|
|
|
92
97
|
```
|
|
93
98
|
````
|
|
94
99
|
|
|
100
|
+
### Limit lines per file
|
|
101
|
+
|
|
102
|
+
Show only the first 10 lines of each TypeScript file - perfect for quick overviews:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
stdin-glob "src/**/*.ts" --max-lines 10
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Output:
|
|
109
|
+
|
|
110
|
+
````
|
|
111
|
+
```ts
|
|
112
|
+
// src/index.ts
|
|
113
|
+
|
|
114
|
+
import { Command } from 'commander';
|
|
115
|
+
import { version } from '../package.json';
|
|
116
|
+
import { readFile } from 'fs/promises';
|
|
117
|
+
import glob from 'fast-glob';
|
|
118
|
+
import path from 'path';
|
|
119
|
+
import clipboard from 'clipboardy';
|
|
120
|
+
// ... (23 more lines truncated)
|
|
121
|
+
```
|
|
122
|
+
````
|
|
123
|
+
|
|
124
|
+
### Show line numbers
|
|
125
|
+
|
|
126
|
+
Display line numbers alongside the code, just like in your editor:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
stdin-glob "src/**/*.js" --line-numbers
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Output:
|
|
133
|
+
|
|
134
|
+
````
|
|
135
|
+
```js
|
|
136
|
+
// src/index.js
|
|
137
|
+
|
|
138
|
+
1 | import { Command } from 'commander';
|
|
139
|
+
2 | import { version } from '../package.json';
|
|
140
|
+
3 | import { readFile } from 'fs/promises';
|
|
141
|
+
4 | import glob from 'fast-glob';
|
|
142
|
+
5 | import path from 'path';
|
|
143
|
+
```
|
|
144
|
+
````
|
|
145
|
+
|
|
146
|
+
### Combine with line limits
|
|
147
|
+
|
|
148
|
+
Get a preview with line numbers for better context:
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
stdin-glob "src/**/*.ts" --max-lines 5 --line-numbers
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Output:
|
|
155
|
+
|
|
156
|
+
````
|
|
157
|
+
```ts
|
|
158
|
+
// src/index.ts
|
|
159
|
+
|
|
160
|
+
1 | import { Command } from 'commander';
|
|
161
|
+
2 | import { version } from '../package.json';
|
|
162
|
+
3 | import { readFile } from 'fs/promises';
|
|
163
|
+
4 | import glob from 'fast-glob';
|
|
164
|
+
5 | import path from 'path';
|
|
165
|
+
// ... (23 more lines truncated)
|
|
166
|
+
```
|
|
167
|
+
````
|
|
168
|
+
|
|
95
169
|
### Copy to clipboard
|
|
96
170
|
|
|
97
171
|
Copy all TypeScript file contents directly to clipboard:
|
|
@@ -148,6 +222,17 @@ Output:
|
|
|
148
222
|
/home/pedrito/project/src/types/index.ts
|
|
149
223
|
```
|
|
150
224
|
|
|
225
|
+
### Binary file handling
|
|
226
|
+
|
|
227
|
+
When encountering binary files (like images, compiled binaries, etc.), the tool safely displays metadata instead of attempting to show unreadable content:
|
|
228
|
+
|
|
229
|
+
````
|
|
230
|
+
```png
|
|
231
|
+
// assets/logo.png
|
|
232
|
+
// [BINARY FILE] - Size: 0.024 MB
|
|
233
|
+
```
|
|
234
|
+
````
|
|
235
|
+
|
|
151
236
|
### Integration with other commands
|
|
152
237
|
|
|
153
238
|
Use with grep to search for specific content:
|
package/dist/index.js
CHANGED
|
@@ -9,14 +9,22 @@ const promises_1 = require("fs/promises");
|
|
|
9
9
|
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
10
10
|
const path_1 = __importDefault(require("path"));
|
|
11
11
|
const clipboardy_1 = __importDefault(require("clipboardy"));
|
|
12
|
+
const binary_1 = require("./utils/binary");
|
|
13
|
+
const pipes_1 = require("./utils/pipes");
|
|
12
14
|
const program = new commander_1.Command();
|
|
13
15
|
program
|
|
14
16
|
.name('stdin-glob')
|
|
15
17
|
.description('Expand glob patterns and output file contents and paths')
|
|
16
18
|
.version(package_json_1.version)
|
|
17
19
|
.option('--no-content', 'Do not show file contents, only list matching paths')
|
|
18
|
-
.option('--absolute', 'Show the absolute path for entries')
|
|
19
|
-
.option('-c, --copy', 'Copy the output to clipboard instead of printing to console')
|
|
20
|
+
.option('--absolute', 'Show the absolute path for entries', false)
|
|
21
|
+
.option('-c, --copy', 'Copy the output to clipboard instead of printing to console', false)
|
|
22
|
+
.option('-m, --max-lines <int>', 'Show a limited number of lines in the file. If you not provide a number of lines it will show the full file content.', (value) => {
|
|
23
|
+
if (isNaN(parseInt(value)))
|
|
24
|
+
throw new Error('Lines must be a number');
|
|
25
|
+
return parseInt(value);
|
|
26
|
+
})
|
|
27
|
+
.option('-n, --line-numbers', 'Show line numbers next to each line, like in IDE sidebars', false)
|
|
20
28
|
.argument('[patterns...]', 'Glob patterns to match files')
|
|
21
29
|
.action(async (patterns, options) => {
|
|
22
30
|
if (patterns.length === 0) {
|
|
@@ -35,7 +43,7 @@ program
|
|
|
35
43
|
let output = '';
|
|
36
44
|
for (const file of files) {
|
|
37
45
|
if (options.content) {
|
|
38
|
-
const fileOutput = await getFileContent(file);
|
|
46
|
+
const fileOutput = await getFileContent(file, options.maxLines ?? undefined, options.lineNumbers ?? false);
|
|
39
47
|
output += fileOutput;
|
|
40
48
|
}
|
|
41
49
|
else {
|
|
@@ -68,18 +76,47 @@ const findMaxConsecutiveBackticks = (str) => {
|
|
|
68
76
|
};
|
|
69
77
|
/**
|
|
70
78
|
* Get file content with markdown format
|
|
79
|
+
* @param filePath - The path to the file
|
|
80
|
+
* @param maxLines - The number of lines to show. If you not provide a number of lines it will show the full file content.
|
|
81
|
+
* @param showLineNumbers - Whether to show line numbers
|
|
82
|
+
* @returns The file content with markdown format
|
|
71
83
|
*/
|
|
72
|
-
const getFileContent = async (filePath) => {
|
|
84
|
+
const getFileContent = async (filePath, maxLines, showLineNumbers) => {
|
|
73
85
|
try {
|
|
74
|
-
const
|
|
86
|
+
const fileBuffer = await (0, promises_1.readFile)(filePath);
|
|
87
|
+
if ((0, binary_1.isBinaryFile)(fileBuffer)) {
|
|
88
|
+
// is binary!! not show content
|
|
89
|
+
const stats = await (0, promises_1.stat)(filePath);
|
|
90
|
+
const fileSize = stats.size;
|
|
91
|
+
const extension = path_1.default.extname(filePath).replace('.', '');
|
|
92
|
+
const sizeFormatted = (0, pipes_1.formatSizeInMB)(fileSize);
|
|
93
|
+
return ('```' +
|
|
94
|
+
extension +
|
|
95
|
+
'\n' +
|
|
96
|
+
`// ${filePath}\n` +
|
|
97
|
+
`// [BINARY FILE] - Size: ${sizeFormatted}\n` +
|
|
98
|
+
'```\n\n');
|
|
99
|
+
}
|
|
100
|
+
// is text file!! proceed with normal processing
|
|
101
|
+
const content = fileBuffer.toString('utf-8');
|
|
102
|
+
const lines = content.split('\n');
|
|
103
|
+
// maxLines if exists
|
|
104
|
+
const linesToShow = maxLines ? lines.slice(0, maxLines) : lines;
|
|
105
|
+
let contentToShow = linesToShow.join('\n');
|
|
106
|
+
if (showLineNumbers)
|
|
107
|
+
contentToShow = (0, pipes_1.addLineNumbers)(linesToShow.join('\n'), 1);
|
|
75
108
|
const extension = path_1.default.extname(filePath).replace('.', '');
|
|
76
109
|
const maxBackticks = findMaxConsecutiveBackticks(content);
|
|
77
110
|
const wrapper = '`'.repeat(Math.max(3, maxBackticks + 1));
|
|
111
|
+
const truncation = maxLines && lines.length > maxLines
|
|
112
|
+
? `\n// ... (${lines.length - maxLines} more lines truncated)`
|
|
113
|
+
: '';
|
|
78
114
|
return (wrapper +
|
|
79
115
|
extension +
|
|
80
116
|
'\n' +
|
|
81
117
|
`// ${filePath}\n\n` +
|
|
82
|
-
|
|
118
|
+
contentToShow +
|
|
119
|
+
truncation +
|
|
83
120
|
'\n' +
|
|
84
121
|
wrapper +
|
|
85
122
|
'\n\n');
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBinaryFile = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Check if a file is binary
|
|
6
|
+
* @see https://gist.github.com/magnetikonline/7a21ec5f5bcdbf7adb92f9d617e6198f
|
|
7
|
+
*/
|
|
8
|
+
const isBinaryFile = (buffer) => {
|
|
9
|
+
// check for null bytes or control characters that indicate binary
|
|
10
|
+
for (let i = 0; i < Math.min(buffer.length, 512); i++) {
|
|
11
|
+
const byte = buffer[i];
|
|
12
|
+
// Null byte or control character
|
|
13
|
+
if (byte === 0 || (byte < 32 && byte !== 9 && byte !== 10 && byte !== 13)) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
};
|
|
19
|
+
exports.isBinaryFile = isBinaryFile;
|
|
20
|
+
//# sourceMappingURL=binary.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add line numbers to content
|
|
3
|
+
*/
|
|
4
|
+
export declare const addLineNumbers: (content: string, startLine?: number) => string;
|
|
5
|
+
/**
|
|
6
|
+
* Format file size in MB with 3 decimal places
|
|
7
|
+
*/
|
|
8
|
+
export declare const formatSizeInMB: (bytes: number) => string;
|
|
9
|
+
//# sourceMappingURL=pipes.d.ts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatSizeInMB = exports.addLineNumbers = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Add line numbers to content
|
|
6
|
+
*/
|
|
7
|
+
const addLineNumbers = (content, startLine = 1) => {
|
|
8
|
+
const lines = content.split('\n');
|
|
9
|
+
// calculate width of bar
|
|
10
|
+
const paddingWidth = (startLine + lines.length - 1).toString().length;
|
|
11
|
+
return lines
|
|
12
|
+
.map((line, index) => {
|
|
13
|
+
const lineNumber = startLine + index;
|
|
14
|
+
const paddedNumber = lineNumber.toString().padStart(paddingWidth, ' ');
|
|
15
|
+
return `${paddedNumber} | ${line}`;
|
|
16
|
+
})
|
|
17
|
+
.join('\n');
|
|
18
|
+
};
|
|
19
|
+
exports.addLineNumbers = addLineNumbers;
|
|
20
|
+
/**
|
|
21
|
+
* Format file size in MB with 3 decimal places
|
|
22
|
+
*/
|
|
23
|
+
const formatSizeInMB = (bytes) => {
|
|
24
|
+
const mb = bytes / (1024 * 1024);
|
|
25
|
+
return mb.toFixed(3) + ' MB';
|
|
26
|
+
};
|
|
27
|
+
exports.formatSizeInMB = formatSizeInMB;
|
|
28
|
+
//# sourceMappingURL=pipes.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stdin-glob",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Expand glob patterns and output file contents.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
"prepare": "npm run build",
|
|
13
13
|
"prepublishOnly": "npm run build"
|
|
14
14
|
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/**/*.js",
|
|
17
|
+
"dist/**/*.d.ts"
|
|
18
|
+
],
|
|
15
19
|
"keywords": [
|
|
16
20
|
"glob",
|
|
17
21
|
"stdin",
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
-
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
3
|
-
|
|
4
|
-
name: NPM Package
|
|
5
|
-
|
|
6
|
-
on:
|
|
7
|
-
push:
|
|
8
|
-
tags:
|
|
9
|
-
- 'v*'
|
|
10
|
-
|
|
11
|
-
permissions:
|
|
12
|
-
id-token: write # Required for OIDC
|
|
13
|
-
contents: read
|
|
14
|
-
|
|
15
|
-
jobs:
|
|
16
|
-
publish:
|
|
17
|
-
runs-on: ubuntu-latest
|
|
18
|
-
steps:
|
|
19
|
-
- uses: actions/checkout@v4
|
|
20
|
-
- uses: actions/setup-node@v4
|
|
21
|
-
with:
|
|
22
|
-
node-version: '24'
|
|
23
|
-
registry-url: 'https://registry.npmjs.org'
|
|
24
|
-
|
|
25
|
-
- run: npm install
|
|
26
|
-
- run: npm publish
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
|
package/src/index.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { Command } from 'commander';
|
|
2
|
-
import { version } from '../package.json';
|
|
3
|
-
import { readFile } from 'fs/promises';
|
|
4
|
-
import glob from 'fast-glob';
|
|
5
|
-
import path from 'path';
|
|
6
|
-
import clipboard from 'clipboardy';
|
|
7
|
-
|
|
8
|
-
interface Options {
|
|
9
|
-
content?: boolean;
|
|
10
|
-
absolute?: boolean;
|
|
11
|
-
copy?: boolean;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const program = new Command();
|
|
15
|
-
|
|
16
|
-
program
|
|
17
|
-
.name('stdin-glob')
|
|
18
|
-
.description('Expand glob patterns and output file contents and paths')
|
|
19
|
-
.version(version)
|
|
20
|
-
.option('--no-content', 'Do not show file contents, only list matching paths')
|
|
21
|
-
.option('--absolute', 'Show the absolute path for entries')
|
|
22
|
-
.option(
|
|
23
|
-
'-c, --copy',
|
|
24
|
-
'Copy the output to clipboard instead of printing to console',
|
|
25
|
-
)
|
|
26
|
-
.argument('[patterns...]', 'Glob patterns to match files')
|
|
27
|
-
.action(async (patterns: string[], options: Options) => {
|
|
28
|
-
if (patterns.length === 0) {
|
|
29
|
-
console.error('Error: No patterns provided.');
|
|
30
|
-
process.exit(1);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
//expand glob
|
|
34
|
-
const files = await glob(patterns, {
|
|
35
|
-
onlyFiles: true,
|
|
36
|
-
absolute: options.absolute ?? false,
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
if (files.length === 0) {
|
|
40
|
-
console.error('No files matched the given patterns.');
|
|
41
|
-
process.exit(1);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
let output = '';
|
|
45
|
-
|
|
46
|
-
for (const file of files) {
|
|
47
|
-
if (options.content) {
|
|
48
|
-
const fileOutput = await getFileContent(file);
|
|
49
|
-
output += fileOutput;
|
|
50
|
-
} else {
|
|
51
|
-
output += file + '\n';
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (options.copy) {
|
|
56
|
-
try {
|
|
57
|
-
await clipboard.write(output.trim());
|
|
58
|
-
console.log('-> Output copied to clipboard successfully!');
|
|
59
|
-
} catch (error) {
|
|
60
|
-
console.error('-X Error copying to clipboard:', error);
|
|
61
|
-
process.exit(1);
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
console.log(output.trim());
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
program.parse(process.argv);
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Find the maximum number of consecutive backticks in a string
|
|
72
|
-
*/
|
|
73
|
-
const findMaxConsecutiveBackticks = (str: string): number => {
|
|
74
|
-
const matches = str.match(/`+/g);
|
|
75
|
-
if (!matches) return 0;
|
|
76
|
-
return Math.max(...matches.map((m) => m.length));
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Get file content with markdown format
|
|
81
|
-
*/
|
|
82
|
-
const getFileContent = async (filePath: string): Promise<string> => {
|
|
83
|
-
try {
|
|
84
|
-
const content = await readFile(filePath, 'utf-8');
|
|
85
|
-
const extension = path.extname(filePath).replace('.', '');
|
|
86
|
-
const maxBackticks = findMaxConsecutiveBackticks(content);
|
|
87
|
-
const wrapper = '`'.repeat(Math.max(3, maxBackticks + 1));
|
|
88
|
-
|
|
89
|
-
return (
|
|
90
|
-
wrapper +
|
|
91
|
-
extension +
|
|
92
|
-
'\n' +
|
|
93
|
-
`// ${filePath}\n\n` +
|
|
94
|
-
content +
|
|
95
|
-
'\n' +
|
|
96
|
-
wrapper +
|
|
97
|
-
'\n\n'
|
|
98
|
-
);
|
|
99
|
-
} catch (e) {
|
|
100
|
-
console.error(`Error reading file ${filePath}:`, e);
|
|
101
|
-
return '';
|
|
102
|
-
}
|
|
103
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"lib": ["ES2020"],
|
|
6
|
-
"outDir": "./dist",
|
|
7
|
-
"rootDir": "./src",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"declaration": true,
|
|
13
|
-
"declarationMap": true,
|
|
14
|
-
"sourceMap": true,
|
|
15
|
-
"resolveJsonModule": true
|
|
16
|
-
},
|
|
17
|
-
"include": ["src/**/*"],
|
|
18
|
-
"exclude": ["node_modules", "dist"]
|
|
19
|
-
}
|