diff --git a/packages/core/utils/src/getRootDir.js b/packages/core/utils/src/getRootDir.js index ac05ccfafda..352317318cf 100644 --- a/packages/core/utils/src/getRootDir.js +++ b/packages/core/utils/src/getRootDir.js @@ -4,6 +4,8 @@ import type {FilePath} from '@parcel/types'; import {isGlob} from './glob'; import path from 'path'; +// Returns the common root of the given paths. +// If there is no common root, returns the current working directory. export default function getRootDir(files: Array): FilePath { let cur = null; @@ -27,10 +29,16 @@ export default function getRootDir(files: Array): FilePath { } cur.dir = i > 1 ? curParts.slice(0, i).join(path.sep) : cur.root; + cur.name = ''; + cur.base = ''; } } - return cur ? cur.dir : process.cwd(); + if (!cur) { + return process.cwd(); + } + + return path.join(cur.dir, cur.name); } // Transforms a path like `packages/*/src/index.js` to the root of the glob, `packages/` diff --git a/packages/core/utils/test/getRootDir.test.js b/packages/core/utils/test/getRootDir.test.js new file mode 100644 index 00000000000..93e3c84c69d --- /dev/null +++ b/packages/core/utils/test/getRootDir.test.js @@ -0,0 +1,19 @@ +import assert from 'assert'; +import getRootDir from '../src/getRootDir'; +import path from 'path'; + +describe('getRootDir', () => { + it('Should return the common parts if provided a file list', () => { + const rootPath = process.cwd(); + const fileList = [ + path.join(rootPath, 'foo', 'bar'), + path.join(rootPath, 'foo', 'bar', 'baz', 'qux'), + path.join(rootPath, 'foo.js'), + ]; + assert.equal(getRootDir(fileList), rootPath); + }); + it('Should return the passsed path if its a directory', () => { + const rootPath = process.cwd(); + assert.equal(getRootDir([rootPath]), rootPath); + }); +});