Skip to content

Commit

Permalink
add map-workspaces to dependencies and fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
timothybonci committed Sep 19, 2022
1 parent 5c47cab commit 101dc1c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
2 changes: 2 additions & 0 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ graph LR;
libnpmteam-->npmcli-template-oss["@npmcli/template-oss"];
libnpmversion-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmversion-->npmcli-git["@npmcli/git"];
libnpmversion-->npmcli-map-workspaces["@npmcli/map-workspaces"];
libnpmversion-->npmcli-run-script["@npmcli/run-script"];
libnpmversion-->npmcli-template-oss["@npmcli/template-oss"];
libnpmversion-->proc-log;
Expand Down Expand Up @@ -409,6 +410,7 @@ graph LR;
libnpmversion-->json-parse-even-better-errors;
libnpmversion-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmversion-->npmcli-git["@npmcli/git"];
libnpmversion-->npmcli-map-workspaces["@npmcli/map-workspaces"];
libnpmversion-->npmcli-run-script["@npmcli/run-script"];
libnpmversion-->npmcli-template-oss["@npmcli/template-oss"];
libnpmversion-->proc-log;
Expand Down
1 change: 1 addition & 0 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -14111,6 +14111,7 @@
"license": "ISC",
"dependencies": {
"@npmcli/git": "^3.0.0",
"@npmcli/map-workspaces": "^2.0.4",
"@npmcli/run-script": "^4.1.3",
"json-parse-even-better-errors": "^2.3.1",
"proc-log": "^2.0.0",
Expand Down
10 changes: 6 additions & 4 deletions workspaces/libnpmversion/lib/is-workspace-safe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = async (gitDir, path, cwd = process.cwd()) => {
if (cwd === gitDir) {
return true
}
// if your path's package.json contains workspaces, you are in a top level package, not a workspace
// if your path's package.json contains workspaces you are in a top level package, not a workspace
var rpj = await readJson(`${path}/package.json`)
if (rpj.workspaces) {
return true
Expand All @@ -22,11 +22,13 @@ module.exports = async (gitDir, path, cwd = process.cwd()) => {
cwd = dirname(cwd)
try {
rpj = await readJson(`${cwd}/package.json`)
} catch(er) { continue }
const workspaceMap = await mapWorkspaces({cwd, pkg: rpj})
} catch (er) {
continue
}
const workspaceMap = await mapWorkspaces({ cwd, pkg: rpj })
const mapValues = [...workspaceMap.values()]
if (mapValues.includes(path)) {
return false
return false
}
}
return true
Expand Down
6 changes: 5 additions & 1 deletion workspaces/libnpmversion/lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ module.exports = async (newversion, opts) => {

// - check if git dir is clean
// returns false if we should not keep doing git stuff
const doGit = gitTagVersion && Boolean(gitDir) && await enforceClean(opts) && await isWorkspaceSafe(gitDir, path)
const doGit = (gitTagVersion
&& Boolean(gitDir)
&& await enforceClean(opts)
&& await isWorkspaceSafe(gitDir, path)
)

if (!ignoreScripts) {
await runScript({
Expand Down
1 change: 1 addition & 0 deletions workspaces/libnpmversion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"dependencies": {
"@npmcli/git": "^3.0.0",
"@npmcli/map-workspaces": "^2.0.4",
"@npmcli/run-script": "^4.1.3",
"json-parse-even-better-errors": "^2.3.1",
"proc-log": "^2.0.0",
Expand Down
43 changes: 31 additions & 12 deletions workspaces/libnpmversion/test/is-workspace-safe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,65 @@ t.test('all the potential states', async t => {
name: 'foo'
}
const dir = t.testdir({
'git': {
git: {
'package.json': JSON.stringify({
...pkg,
workspaces: [
"packages/a"
]
'packages/a',
],
}, null, 2),
},
'git/packages/a': {
'package.json': JSON.stringify({
...pkg
...pkg,
}, null, 2),
},
'git/other': {},
'not-git': {},
'git-too': {
'package.json': JSON.stringify({
...pkg
...pkg,
}, null, 2),
},
'git-too/subdir': {}
'git-too/subdir': {},
})

await t.test('no git', async t => {
t.ok(await isWorkspaceSafe(null, `${dir}/not-git`), 'should be safe because there is no git')
t.ok(await isWorkspaceSafe(
null,
`${dir}/not-git`
), 'should be safe because there is no git')
})

await t.test('is git root', async t => {
t.ok(await isWorkspaceSafe(`${dir}/git`, `${dir}/git`, `${dir}/git`), 'should be safe because cwd is git root')
t.ok(await isWorkspaceSafe(
`${dir}/git`,
`${dir}/git`,
`${dir}/git`
), 'should be safe because cwd is git root')
})

await t.test('top level package has workspaces', async t => {
t.ok(await isWorkspaceSafe(`${dir}/git`, `${dir}/git`, `${dir}/git/other`), 'should be safe because we see the workspaces')
t.ok(await isWorkspaceSafe(
`${dir}/git`,
`${dir}/git`,
`${dir}/git/other`
), 'should be safe because we see the workspaces')
})

await t.test('in workspace', async t => {
t.notOk(await isWorkspaceSafe(`${dir}/git`, `${dir}${path.sep}git${path.sep}packages${path.sep}a`, `${dir}${path.sep}git${path.sep}packages${path.sep}a`), 'should return false for being in a workspace')
t.notOk(await isWorkspaceSafe(
`${dir}/git`,
`${dir}${path.sep}git${path.sep}packages${path.sep}a`,
`${dir}${path.sep}git${path.sep}packages${path.sep}a`
), 'should return false for being in a workspace')
})

await t.test('no workspaces', async t=> {
t.ok(await isWorkspaceSafe(`${dir}/git-too`, `${dir}/git-too`, `${dir}/git-too/subdir`), 'should be safe because of no workspaces')
await t.test('no workspaces', async t => {
t.ok(await isWorkspaceSafe(
`${dir}/git-too`,
`${dir}/git-too`,
`${dir}/git-too/subdir`
), 'should be safe because of no workspaces')
})
})
6 changes: 3 additions & 3 deletions workspaces/libnpmversion/test/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ t.test('test out bumping the version in all the ways', async t => {
'package.json': JSON.stringify({
...pkg,
workspaces: [
"packages/a"
'packages/a',
],
}, null, 2),
'package-lock.json': JSON.stringify(lock, null, 2),
Expand All @@ -65,8 +65,8 @@ t.test('test out bumping the version in all the ways', async t => {
'package.json': JSON.stringify({
...pkg,
workspaces: [
"packages/b"
]
'packages/b',
],
}, null, 2),
},
'not-git/packages/b': {
Expand Down

0 comments on commit 101dc1c

Please sign in to comment.