Skip to content

Commit 6b79dcf

Browse files
committed
feat(File Matching): Allows use of node-glob syntax for portable file matching
Closes #35
1 parent ea69e95 commit 6b79dcf

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,22 @@ Will result in the following output
150150
If you want to run `embedme` over multiple files, you can use glob matching, i.e.
151151

152152
```bash
153-
embedme src/**/*.md
153+
embedme "src/**/*.md"
154154
```
155155

156-
Note that this globbing is shell expansion, not internally handled so _don't_ quote the glob expression. You can test
157-
you output with `ls`, i.e. `ls src/**/*.md`
156+
Note embedme supports both quoted globbing and unquoted. Be careful using unquoted
157+
globbing as this can lead to behaviour that is not portable between different
158+
operating systems.
159+
160+
If you're using Windows, you must use forward slashes (`/`) to denote path separators.
161+
162+
You can also pass multiple separate glob patterns to match multiple sets of files
163+
164+
example:
165+
166+
```bash
167+
embedme "src/**/*.md" "docs/**/*.markdown"
168+
```
158169

159170
### CI Checks
160171

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"dependencies": {
4141
"chalk": "~2.4.2",
4242
"commander": "~2.20.0",
43-
"gitignore-parser": "~0.0.2"
43+
"gitignore-parser": "~0.0.2",
44+
"node-glob": "~1.2.0"
4445
}
4546
}

src/embedme.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { embedme, EmbedmeOptions, logBuilder } from './embedme.lib';
66
import { compile } from 'gitignore-parser';
77
import program from 'commander';
88
import chalk from 'chalk';
9+
import glob from 'glob';
910
const pkg = require('../package.json');
1011

1112
program
@@ -22,12 +23,22 @@ program
2223
.option('--strip-embed-comment', `Remove the comments from the code fence. *Must* be run with --stdout flag`)
2324
.parse(process.argv);
2425

25-
let { args: sourceFiles } = program;
26+
const { args: sourceFilesInput } = program;
2627

2728
const options: EmbedmeOptions = (program as unknown) as EmbedmeOptions;
2829

2930
const log = logBuilder(options);
3031

32+
let sourceFiles = sourceFilesInput.reduce<string[]>((files, file) => {
33+
if (glob.hasMagic(file)) {
34+
files.push(...glob.sync(file));
35+
} else {
36+
files.push(file);
37+
}
38+
39+
return files;
40+
}, []);
41+
3142
if (sourceFiles.length > 1) {
3243
log(chalk.yellow(`More than one file matched your input, results will be concatenated in stdout`));
3344
} else if (sourceFiles.length === 0) {

yarn.lock

+18
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ assign-symbols@^1.0.0:
376376
resolved "https://registry.yarnpkg.com/assign-symbols/-/assign-symbols-1.0.0.tgz#59667f41fadd4f20ccbc2bb96b8d4f7f78ec0367"
377377
integrity sha1-WWZ/QfrdTyDMvCu5a41Pf3jsA2c=
378378

379+
async@^1.3.0:
380+
version "1.5.2"
381+
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
382+
integrity sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=
383+
379384
asynckit@^0.4.0:
380385
version "0.4.0"
381386
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
@@ -1605,6 +1610,11 @@ glob-parent@^3.1.0:
16051610
is-glob "^3.1.0"
16061611
path-dirname "^1.0.0"
16071612

1613+
glob-to-regexp@^0.1.0:
1614+
version "0.1.0"
1615+
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.1.0.tgz#e0369d426578fd456d47dc23b09de05c1da9ea5d"
1616+
integrity sha1-4DadQmV4/UVtR9wjsJ3gXB2p6l0=
1617+
16081618
glob-to-regexp@^0.3.0:
16091619
version "0.3.0"
16101620
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab"
@@ -2891,6 +2901,14 @@ node-fetch@^2.3.0:
28912901
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
28922902
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==
28932903

2904+
node-glob@~1.2.0:
2905+
version "1.2.0"
2906+
resolved "https://registry.yarnpkg.com/node-glob/-/node-glob-1.2.0.tgz#5240ffedefc6d663ce8515e5796a4d47a750c0d5"
2907+
integrity sha1-UkD/7e/G1mPOhRXleWpNR6dQwNU=
2908+
dependencies:
2909+
async "^1.3.0"
2910+
glob-to-regexp "^0.1.0"
2911+
28942912
node-gyp@^3.8.0:
28952913
version "3.8.0"
28962914
resolved "https://registry.yarnpkg.com/node-gyp/-/node-gyp-3.8.0.tgz#540304261c330e80d0d5edce253a68cb3964218c"

0 commit comments

Comments
 (0)