Skip to content

Commit

Permalink
feat(deploy): apply publishConfig to all packages during deploy
Browse files Browse the repository at this point in the history
When deploying packages, the package.json of the deployed package
(as well as any other locally defined dependencies)
should be treated as if it published, and mutate the package.json
according to `publishConfig` and local `workspace:` dependencies.

Issue: #6693
  • Loading branch information
JacobLey committed Aug 16, 2023
1 parent 9365e68 commit 6e980d0
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/stupid-falcons-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-deploy": minor
---

Apply publishConfig for workspace packages on deployment.
Issue: [#6693](https://github.com/pnpm/pnpm/issues/6693)
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion releasing/plugin-commands-deploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,21 @@
"@pnpm/lockfile-types": "workspace:*",
"@pnpm/plugin-commands-deploy": "workspace:*",
"@pnpm/prepare": "workspace:*",
"@pnpm/registry-mock": "3.11.0"
"@pnpm/registry-mock": "3.11.0",
"@types/cross-spawn": "^6.0.2",
"cross-spawn": "^7.0.3",
"write-yaml-file": "^5.0.0"
},
"dependencies": {
"@pnpm/cli-utils": "workspace:*",
"@pnpm/common-cli-options-help": "workspace:*",
"@pnpm/directory-fetcher": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/exportable-manifest": "workspace:*",
"@pnpm/fs.indexed-pkg-importer": "workspace:*",
"@pnpm/plugin-commands-installation": "workspace:*",
"@pnpm/read-project-manifest": "workspace:*",
"@pnpm/store-controller-types": "workspace:*",
"@pnpm/types": "workspace:*",
"@zkochan/rimraf": "^2.1.2",
"render-help": "^1.0.3"
Expand Down
21 changes: 19 additions & 2 deletions releasing/plugin-commands-deploy/src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import fs from 'fs'
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import { fetchFromDir } from '@pnpm/directory-fetcher'
import { createExportableManifest } from '@pnpm/exportable-manifest'
import { createIndexedPkgImporter } from '@pnpm/fs.indexed-pkg-importer'
import { install } from '@pnpm/plugin-commands-installation'
import { FILTERING } from '@pnpm/common-cli-options-help'
import { PnpmError } from '@pnpm/error'
import { readProjectManifest } from '@pnpm/read-project-manifest'
import type { ImportOptions } from '@pnpm/store-controller-types'
import rimraf from '@zkochan/rimraf'
import renderHelp from 'render-help'
import { deployHook } from './deployHook'
Expand Down Expand Up @@ -91,6 +94,7 @@ export async function handler (
...(opts.hooks?.readPackage ?? []),
deployHook,
],
importPackage: importPkgWithExportableManifest,
},
frozenLockfile: false,
preferFrozenLockfile: false,
Expand All @@ -108,6 +112,19 @@ export async function handler (

async function copyProject (src: string, dest: string, opts: { includeOnlyPackageFiles: boolean }) {
const { filesIndex } = await fetchFromDir(src, opts)
const importPkg = createIndexedPkgImporter('clone-or-copy')
await importPkg(dest, { filesMap: filesIndex, force: true, fromStore: true })
return importPkgWithExportableManifest(dest, { filesMap: filesIndex, force: true, fromStore: true })
}

const importPkg = createIndexedPkgImporter('clone-or-copy')
async function importPkgWithExportableManifest (dest: string, opts: ImportOptions): Promise<string | undefined> {
const res = await importPkg(dest, opts)

const manifest = await readProjectManifest(dest)
const exportableManifest = await createExportableManifest(
path.dirname(opts.filesMap[manifest.fileName]),
manifest.manifest
)
await manifest.writeProjectManifest(exportableManifest)

return res
}
43 changes: 43 additions & 0 deletions releasing/plugin-commands-deploy/test/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import path from 'path'
import { deploy } from '@pnpm/plugin-commands-deploy'
import { assertProject } from '@pnpm/assert-project'
import { preparePackages } from '@pnpm/prepare'
import { REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import { readProjects } from '@pnpm/filter-workspace-packages'
import crossSpawn from 'cross-spawn'
import writeYamlFile from 'write-yaml-file'
import { DEFAULT_OPTS } from './utils'

const pnpmBin = path.join(__dirname, '../../../pnpm/bin/pnpm.cjs')

test('deploy', async () => {
preparePackages([
{
Expand All @@ -20,6 +25,10 @@ test('deploy', async () => {
'project-3': 'workspace:*',
'is-negative': '1.0.0',
},
main: 'local-file-1.js',
publishConfig: {
main: 'publish-file-1.js',
},
},
{
name: 'project-2',
Expand All @@ -29,6 +38,10 @@ test('deploy', async () => {
'project-3': 'workspace:*',
'is-odd': '1.0.0',
},
main: 'local-file-2.js',
publishConfig: {
main: 'publish-file-2.js',
},
},
{
name: 'project-3',
Expand All @@ -46,6 +59,10 @@ test('deploy', async () => {
fs.writeFileSync(`${name}/index.js`, '', 'utf8')
})

await writeYamlFile('pnpm-workspace.yaml', { packages: ['*'] })
crossSpawn.sync(pnpmBin, ['install', '--ignore-scripts', '--store-dir=../store', `--registry=http://localhost:${REGISTRY_MOCK_PORT}`])
fs.rmSync('pnpm-lock.yaml')

const { allProjects, selectedProjectsGraph } = await readProjects(process.cwd(), [{ namePattern: 'project-1' }])

await deploy.handler({
Expand Down Expand Up @@ -73,6 +90,32 @@ test('deploy', async () => {
expect(fs.existsSync('deploy/node_modules/.pnpm/file+project-3/node_modules/project-3/index.js')).toBeTruthy()
expect(fs.existsSync('deploy/node_modules/.pnpm/file+project-3/node_modules/project-3/test.js')).toBeFalsy()
expect(fs.existsSync('pnpm-lock.yaml')).toBeFalsy() // no changes to the lockfile are written
const [
project1Package,
project2Package,
] = await Promise.all([
fs.promises.readFile('deploy/package.json', 'utf8').then(file => JSON.parse(file)),
fs.promises.readFile('deploy/node_modules/.pnpm/file+project-2/node_modules/project-2/package.json', 'utf8').then(file => JSON.parse(file)),
])
console.log({ project1Package, project2Package })
expect(project1Package).toMatchObject({
name: 'project-1',
main: 'publish-file-1.js',
dependencies: {
'is-positive': '1.0.0',
'project-2': '2.0.0',
},
})
expect(project1Package).not.toHaveProperty('publishConfig')
expect(project2Package).toMatchObject({
name: 'project-2',
main: 'publish-file-2.js',
dependencies: {
'project-3': '2.0.0',
'is-odd': '1.0.0',
},
})
expect(project2Package).not.toHaveProperty('publishConfig')
})

test('deploy with dedupePeerDependents=true ignores the value of dedupePeerDependents', async () => {
Expand Down
9 changes: 9 additions & 0 deletions releasing/plugin-commands-deploy/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
{
"path": "../../pkg-manager/plugin-commands-installation"
},
{
"path": "../../pkg-manifest/exportable-manifest"
},
{
"path": "../../pkg-manifest/read-project-manifest"
},
{
"path": "../../store/store-controller-types"
},
{
"path": "../../workspace/filter-workspace-packages"
}
Expand Down

0 comments on commit 6e980d0

Please sign in to comment.