Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit c068d8d

Browse files
authored
Merge pull request #218 from microsoft/Fix-for-input-dir-without-glob
Allow scanning of dir without blob expression
2 parents c90e87e + 53e8a22 commit c068d8d

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

packages/chatdown/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ EXAMPLES
4242
$ bf chatdown --in=./path/to/file/sample.chat --out=./
4343
$ bf chatdown -i ./path/to/file/*.sample.chat -o ./
4444
$ bf chatdown -i=./path/to/file/*.sample.chat -o=./
45+
$ bf chatdown --in ./path/to/directory
46+
$ bf chatdown --in ./path/to/directory/*
4547
$ (echo user=Joe && [ConversationUpdate=MembersAdded=Joe]) | bf chatdown --static
4648
```
4749

packages/chatdown/src/commands/chatdown.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export default class Chatdown extends Command {
4141

4242
if (inputIsDirectory) {
4343
let inputDir = flags.in ? flags.in.trim() : ''
44-
4544
const len = await this.processFiles(inputDir, outputDir)
4645
if (len === 0) {
4746
throw new CLIError('No chat files found at: ' + flags.in)
@@ -93,14 +92,36 @@ export default class Chatdown extends Command {
9392
}
9493
}
9594

95+
private getFiles(directoryPath: any) {
96+
return new Promise((resolve, reject) => {
97+
let fileList: any = []
98+
fs.readdir(directoryPath, (err: any, files: any) => {
99+
if (err) {
100+
reject('Error scanning directory' + err)
101+
}
102+
fileList = files.map((file: any) => path.join(directoryPath, file))
103+
resolve(fileList)
104+
})
105+
})
106+
}
107+
96108
private getFileName(file: any) {
97109
let fileName = path.basename(file, path.extname(file))
98110
return fileName
99111
}
100112

101113
private async processFiles(inputDir: any, outputDir: any) {
102114
return new Promise(async (resolve, reject) => {
103-
let files = glob.sync(inputDir, {ignore: ['**/node_modules/**']})
115+
let files: any = []
116+
if (inputDir.indexOf('*') > -1) {
117+
files = glob.sync(inputDir, {ignore: ['**/node_modules/**']})
118+
} else {
119+
try {
120+
files = await this.getFiles(inputDir)
121+
} catch (err) {
122+
reject(new CLIError(`Failed to scan directory ${err}`))
123+
}
124+
}
104125
/* tslint:disable:prefer-for-of */
105126
for (let i = 0; i < files.length; i++) {
106127
try {

packages/chatdown/test/commands/chatdown.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@ describe('chatdown', () => {
4444
});
4545

4646
it('should read from file when chat file is passed as an argument', done => {
47-
cp.exec(`node ./bin/run chatdown --in ./test/utils/cli.sample.chat`, (error, stdout) => {
47+
cp.exec(`node ./bin/run chatdown --in "./test/utils/cli.sample.chat"`, (error, stdout) => {
4848
assert.doesNotThrow(() => JSON.parse(stdout));
4949
done();
5050
});
5151
});
5252

5353
it('should process all files when a glob is passed in with the -i argument, and the -o is passed in for the output directory', done => {
54-
cp.exec(`node ./bin/run chatdown -i ./test/utils/*.sample.chat -o ./`, (error, stdout, stderr) => {
54+
cp.exec(`node ./bin/run chatdown -i "./test/utils/*.sample.chat" -o ./`, (error, stdout, stderr) => {
5555
assert(stdout.includes('Successfully wrote'));
5656
done();
5757
});
5858
});
5959

6060
it('should process all files when a glob is passed in with the -i argument', done => {
61-
cp.exec(`node ./bin/run chatdown -i ./test/utils/*.sample.chat`, (error, stdout, stderr) => {
61+
cp.exec(`node ./bin/run chatdown -i "./test/utils/*.sample.chat"`, (error, stdout, stderr) => {
6262
assert(stdout.includes('Successfully wrote'));
6363
done();
6464
});
@@ -79,7 +79,7 @@ describe('chatdown', () => {
7979
});
8080

8181
it('throw error if invalid path in argument', done => {
82-
cp.exec(`node ./bin/run chatdown --in aaaaa`, (error, stdout, stderr) => {
82+
cp.exec(`node ./bin/run chatdown --in 'aaaaa'`, (error, stdout, stderr) => {
8383
assert(stderr.includes('no such file or directory') || stderr.includes('error'));
8484
done();
8585
});

0 commit comments

Comments
 (0)