diff --git a/src/bumpr.js b/src/bumpr.js index 1522b7c..49aeabc 100644 --- a/src/bumpr.js +++ b/src/bumpr.js @@ -9,7 +9,7 @@ import Promise from 'promise' import replace from 'replace-in-file' import semverInc from 'semver/functions/inc.js' import semverPrerelease from 'semver/functions/prerelease.js' -import {createReadStream, exec, existsSync, readdir, statSync, writeFile} from './node-wrappers.js' +import {createReadStream, exec, readdir, statSync, writeFile} from './node-wrappers.js' import MissingKeyError from './errors/missing-key.js' import NoLogFileError from './errors/no-log-file.js' import {Logger} from './logger.js' @@ -23,15 +23,10 @@ const {cloneDeep, get} = _ * @returns {Promise} a promise - resolved with an array of package names or rejected on error */ function getPackages() { - if (existsSync('packages') && statSync('packages').isDirectory()) { - return readdir('packages', {withFileTypes: true}).then((entries) => - // prettier formats it this way (@job13er 2022-06-02) - // eslint-disable-next-line implicit-arrow-linebreak - entries.filter((e) => e.isDirectory()).map((e) => e.name) - ) - } - - return Promise.resolve(['.']) + return exec('npm query .workspace', {cwd: '.', maxBuffer: 1024 * 1024}).then((output) => { + const packages = JSON.parse(output).map((entry) => entry.location) + return packages.length === 0 ? ['.'] : packages + }) } /** @@ -203,10 +198,7 @@ class Bumpr { packages.map((pkg) => // prettier formats it this way (@job13er 2022-06-02) // eslint-disable-next-line implicit-arrow-linebreak - exec('npm publish .', { - cwd: pkg === '.' ? pkg : path.join('packages', pkg), - maxBuffer: 1024 * 1024, - }) + exec('npm publish .', {cwd: pkg, maxBuffer: 1024 * 1024}) ) ) ) @@ -432,7 +424,7 @@ class Bumpr { const allFiles = [] pkgs.forEach((pkg) => { files.forEach((filename) => { - const fullPath = pkg === '.' ? filename : path.join('packages', pkg, filename) + const fullPath = path.join(pkg, filename) if (fsExistsSync(fullPath)) { allFiles.push(fullPath) } diff --git a/src/tests/bumpr.test.js b/src/tests/bumpr.test.js index d8e8710..30a42d8 100644 --- a/src/tests/bumpr.test.js +++ b/src/tests/bumpr.test.js @@ -34,18 +34,8 @@ class FileNotFoundError extends Error { } } -function setupPkgs({exists, isDir, pkgs}) { - existsSync.mockReturnValueOnce(exists) - if (exists) { - const stat = {isDirectory: jest.fn().mockReturnValue(Boolean(isDir))} - statSync.mockReturnValueOnce(stat) - } - - if (pkgs) { - readdir.mockReturnValueOnce( - Promise.resolve(pkgs.map(({dir, name}) => ({isDirectory: jest.fn().mockReturnValue(dir), name}))) - ) - } +function setupPkgs(pkgs = []) { + exec.mockReturnValueOnce(Promise.resolve(JSON.stringify(pkgs))) } /** @@ -68,13 +58,9 @@ function itShouldBumpVersion(bumprFn, filename, scope, expectedVersion, dirs) { bumpr.config.files = [filename, 'missing-file.json'] if (dirs) { - setupPkgs({ - exists: true, - isDir: true, - pkgs: dirs.map((name) => ({dir: true, name})), - }) + setupPkgs(dirs.map((name) => ({location: `packages/${name}`}))) } else { - setupPkgs({exists: false}) + setupPkgs() } existsSync.mockReturnValue(true) @@ -667,9 +653,9 @@ describe('Bumpr', () => { beforeEach(() => { const log = {scope: 'minor'} jest.spyOn(bumpr, 'getLog').mockReturnValue(Promise.resolve({log})) - setupPkgs({exists: false}) + setupPkgs() writeFile.mockReturnValue(Promise.resolve()) - exec.mockReturnValue(Promise.resolve()) + exec.mockReturnValueOnce(Promise.resolve()) return bumpr.publish().then((r) => { result = r }) @@ -679,14 +665,6 @@ describe('Bumpr', () => { bumpr.getLog.mockReset() }) - it('should check for packages', () => { - expect(existsSync).toHaveBeenCalledWith('packages') - }) - - it('should not get stats for packages', () => { - expect(statSync).not.toHaveBeenCalled() - }) - it('should log publishing', () => { expect(Logger.log).toHaveBeenCalledWith('Publishing to npm') }) @@ -709,14 +687,14 @@ describe('Bumpr', () => { }) }) - describe('when scope is not "none" (and packages exists but is not a directory)', () => { + describe('when scope is not "none"', () => { let result beforeEach(() => { const log = {scope: 'minor'} jest.spyOn(bumpr, 'getLog').mockReturnValue(Promise.resolve({log})) - setupPkgs({exists: true, isDir: false}) + setupPkgs() writeFile.mockReturnValue(Promise.resolve()) - exec.mockReturnValue(Promise.resolve()) + exec.mockReturnValueOnce(Promise.resolve()) return bumpr.publish().then((r) => { result = r }) @@ -726,14 +704,6 @@ describe('Bumpr', () => { bumpr.getLog.mockReset() }) - it('should check for packages', () => { - expect(existsSync).toHaveBeenCalledWith('packages') - }) - - it('should get stats for packages', () => { - expect(statSync).toHaveBeenCalledWith('packages') - }) - it('should log publishing', () => { expect(Logger.log).toHaveBeenCalledWith('Publishing to npm') }) @@ -756,39 +726,27 @@ describe('Bumpr', () => { }) }) - describe('when scope is not "none" (and packages exists as a directory)', () => { + describe('when scope is not "none"', () => { let result beforeEach(() => { const log = {scope: 'minor'} jest.spyOn(bumpr, 'getLog').mockReturnValue(Promise.resolve({log})) - setupPkgs({ - exists: true, - isDir: true, - pkgs: [ - {dir: true, name: 'pkg1'}, - {dir: false, name: 'file1'}, - {dir: false, name: 'file2'}, - {dir: true, name: 'pkg2'}, - {dir: false, name: 'file3'}, - ], - }) + setupPkgs([{location: 'packages/pkg1'}, {location: 'packages/pkg2'}]) writeFile.mockReturnValue(Promise.resolve()) - exec.mockReturnValue(Promise.resolve()) + exec.mockReturnValueOnce(Promise.resolve()) // publish pkg1 + exec.mockReturnValueOnce(Promise.resolve()) // publish pkg2 return bumpr.publish().then((r) => { result = r }) }) afterEach(() => { + exec.mockReset() bumpr.getLog.mockReset() }) it('should check for packages', () => { - expect(existsSync).toHaveBeenCalledWith('packages') - }) - - it('should get stats for packages', () => { - expect(statSync).toHaveBeenCalledWith('packages') + expect(exec).toHaveBeenCalledWith('npm query .workspace', {cwd: '.', maxBuffer: 1024 * 1024}) }) it('should log publishing', () => { @@ -1618,6 +1576,7 @@ describe('Bumpr', () => { beforeEach(() => { bumpr.config.files = ['_package.json'] info = {scope: 'foo'} + setupPkgs() return bumpr.maybeBumpVersion(info).catch((e) => { err = e })