From a16a85310329338fa51f1f36afe8e0f2e7702245 Mon Sep 17 00:00:00 2001 From: Stephan Noel <34415120+stephan-noel@users.noreply.github.com> Date: Wed, 30 Dec 2020 17:11:49 +0000 Subject: [PATCH] fix: Error for rootDir only when necessary (#29) --- core/garment/__tests__/garment.test.ts | 9 ++++----- core/garment/package.json | 3 ++- core/garment/src/garment.ts | 23 ++++++++++------------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/core/garment/__tests__/garment.test.ts b/core/garment/__tests__/garment.test.ts index 4a80089..f87dea3 100644 --- a/core/garment/__tests__/garment.test.ts +++ b/core/garment/__tests__/garment.test.ts @@ -64,20 +64,19 @@ describe('createFileInput', () => { expect(filesCount).toBe(2); }); - test('should throw with a clear error message when rootDir does not exist', async () => { + test('should NOT throw when rootDir does not exist, allowing for input globs in garment.json whose rootDir may or may not exist', async () => { const testDir = await initFixture('basic'); const nonExistingDirectory = Path.join(testDir, 'nonExistingDirectory'); const generator = createFileInput({ - rootDir: nonExistingDirectory + rootDir: nonExistingDirectory, + include: ['*'] }); const createFileInputForNonExistingRootDirectory = () => { generator.next(); }; - expect(createFileInputForNonExistingRootDirectory).toThrow( - /nonExistingDirectory/ - ); + expect(createFileInputForNonExistingRootDirectory).not.toThrow(); }); }); diff --git a/core/garment/package.json b/core/garment/package.json index b127a3b..1419567 100644 --- a/core/garment/package.json +++ b/core/garment/package.json @@ -18,7 +18,8 @@ "multimatch": "^4.0.0", "normalize-path": "^3.0.0", "tempy": "0.3.0", - "unionfs": "^4.4.0" + "unionfs": "^4.4.0", + "globby": "10.0.1" }, "files": [ "lib", diff --git a/core/garment/src/garment.ts b/core/garment/src/garment.ts index 45ed216..755fb0e 100644 --- a/core/garment/src/garment.ts +++ b/core/garment/src/garment.ts @@ -1127,19 +1127,16 @@ export function* createFileInput( { rootDir, files = [], include, exclude = [] }: Input, fsInstance = fs ) { - if (!fsInstance.existsSync(rootDir)) { - throw new Error(`The path ${rootDir} does not exist, please check the input property - of your tasks in your garment.json file and verify that the "non-magical" part of your glob - is a path to an already existing directory`); - } - const filesFromGlob = include - ? globby.sync(include, { - cwd: rootDir, - absolute: true, - ignore: exclude, - dot: true - }) - : []; + const filesFromGlob = + fsInstance === fs && include && fsInstance.existsSync(rootDir) + ? globby.sync(include, { + cwd: rootDir, + absolute: true, + ignore: exclude, + dot: true + }) + : []; + const uniqueFiles = new Set([...files, ...filesFromGlob]); for (const absolutePath of uniqueFiles) { const content = fsInstance.readFileSync(absolutePath);