Skip to content

Commit

Permalink
Properly support workspaces (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
job13er authored Sep 27, 2023
1 parent 89c1030 commit c8f5466
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 72 deletions.
22 changes: 7 additions & 15 deletions src/bumpr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
})
}

/**
Expand Down Expand Up @@ -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})
)
)
)
Expand Down Expand Up @@ -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)
}
Expand Down
73 changes: 16 additions & 57 deletions src/tests/bumpr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

/**
Expand All @@ -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)
Expand Down Expand Up @@ -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
})
Expand All @@ -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')
})
Expand All @@ -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
})
Expand All @@ -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')
})
Expand All @@ -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', () => {
Expand Down Expand Up @@ -1618,6 +1576,7 @@ describe('Bumpr', () => {
beforeEach(() => {
bumpr.config.files = ['_package.json']
info = {scope: 'foo'}
setupPkgs()
return bumpr.maybeBumpVersion(info).catch((e) => {
err = e
})
Expand Down

0 comments on commit c8f5466

Please sign in to comment.