From f0bb5959ee6f40c6d1a1f2edb084722540f45ab7 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 30 Jun 2019 15:01:58 +0900 Subject: [PATCH 1/4] Add test for glob-like file path --- .../_files/\345\255\227/(non-ascii).md" | 0 test/marp-cli.ts | 11 +++++++++++ 2 files changed, 11 insertions(+) rename "test/_files/\345\255\227/non-ascii.md" => "test/_files/\345\255\227/(non-ascii).md" (100%) diff --git "a/test/_files/\345\255\227/non-ascii.md" "b/test/_files/\345\255\227/(non-ascii).md" similarity index 100% rename from "test/_files/\345\255\227/non-ascii.md" rename to "test/_files/\345\255\227/(non-ascii).md" diff --git a/test/marp-cli.ts b/test/marp-cli.ts index 3a86a672..281feb85 100644 --- a/test/marp-cli.ts +++ b/test/marp-cli.ts @@ -730,6 +730,17 @@ describe('Marp CLI', () => { ) }) + context('when glob special chars are included in real file path', () => { + it('finds out a file correctly', async () => { + jest.spyOn(cli, 'info').mockImplementation() + jest + .spyOn(Converter.prototype, 'convertFiles') + .mockImplementation(() => []) + + expect(await marpCli([assetFn('_files/字/(non-ascii).md')])).toBe(0) + }) + }) + context('when non-ASCII code is included in directory name', () => { it('finds out markdown files correctly', async () => { jest.spyOn(cli, 'info').mockImplementation() From 76a96745f5a5238f1f1450cc474f8c04947d138e Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 30 Jun 2019 15:05:47 +0900 Subject: [PATCH 2/4] Improve File.find() to collect real files at first --- src/file.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/file.ts b/src/file.ts index 00aab2b1..710f818b 100644 --- a/src/file.ts +++ b/src/file.ts @@ -7,6 +7,7 @@ import * as url from 'url' import { tmpName } from 'tmp' import { promisify } from 'util' +const stat = promisify(fs.stat) const mkdirpPromise = promisify(mkdirp) const readFile = promisify(fs.readFile) const tmpNamePromise = promisify(tmpName) @@ -119,14 +120,34 @@ export class File { private static stdinBuffer?: Buffer static async find(...pathes: string[]): Promise { - return (await globby(pathes, { + const filepathes = new Set() + const globs: string[] = [] + + // Collect passed files that refers to a real path at first + for (const p of pathes) { + try { + const s: fs.Stats = await stat(p) + + if (s.isFile()) { + filepathes.add(path.resolve(p)) + continue + } + } catch (e) {} + + globs.push(p) + } + + // Find remaining path through globby + ;(await globby(globs, { absolute: true, expandDirectories: { extensions: [], files: markdownExtensions.map(ext => `*.${ext}`), }, ignore: ['**/node_modules'], - })).map(p => new File(p)) + })).forEach(p => filepathes.add(p)) + + return [...filepathes.values()].map(p => new File(p)) } static async findDir(directory: string): Promise { From 58c006ede3069b1b11cf15e9b12b385a258a8bbb Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 30 Jun 2019 15:23:37 +0900 Subject: [PATCH 3/4] [ci skip] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 889bffb1..f6d14ae4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixed - Fix that cannot find Markdown from directory that includes non-ASCII code ([#108](https://github.com/marp-team/marp-cli/issues/108), [#109](https://github.com/marp-team/marp-cli/pull/109)) +- Process glob-like path that refers to a real file correctly ([#95](https://github.com/marp-team/marp-cli/issues/95), [#117](https://github.com/marp-team/marp-cli/pull/117)) ## v0.11.1 - 2019-06-28 From b41ce0ef0f785c8699d5fdc233c3d1e4c59cf03e Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 30 Jun 2019 15:24:01 +0900 Subject: [PATCH 4/4] Fix misspelled `pathes` to `paths` --- src/config.ts | 6 +++--- src/file.ts | 12 ++++++------ test/theme.ts | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/config.ts b/src/config.ts index 9aed7882..fc53bdf9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -121,7 +121,7 @@ export class MarpCLIConfig { const theme = await this.loadTheme() const initialThemes = theme instanceof Theme ? [theme] : [] - const themeSetPathes = + const themeSetPaths = this.args.themeSet || (this.conf.themeSet ? (Array.isArray(this.conf.themeSet) @@ -131,13 +131,13 @@ export class MarpCLIConfig { : []) const themeSet = await ThemeSet.initialize( - (inputDir ? [inputDir] : []).concat(themeSetPathes), + (inputDir ? [inputDir] : []).concat(themeSetPaths), initialThemes ) if ( themeSet.themes.size <= initialThemes.length && - themeSetPathes.length > 0 + themeSetPaths.length > 0 ) warn('Not found additional theme CSS files.') diff --git a/src/file.ts b/src/file.ts index 710f818b..f8bc7fe1 100644 --- a/src/file.ts +++ b/src/file.ts @@ -119,17 +119,17 @@ export class File { private static stdinBuffer?: Buffer - static async find(...pathes: string[]): Promise { - const filepathes = new Set() + static async find(...paths: string[]): Promise { + const filepaths = new Set() const globs: string[] = [] // Collect passed files that refers to a real path at first - for (const p of pathes) { + for (const p of paths) { try { const s: fs.Stats = await stat(p) if (s.isFile()) { - filepathes.add(path.resolve(p)) + filepaths.add(path.resolve(p)) continue } } catch (e) {} @@ -145,9 +145,9 @@ export class File { files: markdownExtensions.map(ext => `*.${ext}`), }, ignore: ['**/node_modules'], - })).forEach(p => filepathes.add(p)) + })).forEach(p => filepaths.add(p)) - return [...filepathes.values()].map(p => new File(p)) + return [...filepaths.values()].map(p => new File(p)) } static async findDir(directory: string): Promise { diff --git a/test/theme.ts b/test/theme.ts index 5542dc2d..28012908 100644 --- a/test/theme.ts +++ b/test/theme.ts @@ -24,7 +24,7 @@ describe('ThemeSet', () => { }) describe('#findPath', () => { - it('returns the result of findings from original pathes', async () => { + it('returns the result of findings from original paths', async () => { const themeSet = await ThemeSet.initialize([themeDir]) const found = await themeSet.findPath()