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 15, 2023
1 parent 9365e68 commit 4049b5c
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .changeset/stupid-falcons-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@pnpm/plugin-commands-deploy": minor
"@pnpm/store-controller-types": patch
"@pnpm/fs.indexed-pkg-importer": patch
---

Apply publishConfig for workspace packages on deployment.
Issue: [#6693](https://github.com/pnpm/pnpm/issues/6693)
2 changes: 2 additions & 0 deletions fs/indexed-pkg-importer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
},
"dependencies": {
"@pnpm/core-loggers": "workspace:*",
"@pnpm/exportable-manifest": "workspace:*",
"@pnpm/graceful-fs": "workspace:*",
"@pnpm/read-project-manifest": "workspace:*",
"@pnpm/store-controller-types": "workspace:*",
"@zkochan/rimraf": "^2.1.2",
"fs-extra": "^11.1.1",
Expand Down
15 changes: 12 additions & 3 deletions fs/indexed-pkg-importer/src/importIndexedDir.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { promises as fs } from 'fs'
import { copy } from 'fs-extra'
import path from 'path'
import { createExportableManifest } from '@pnpm/exportable-manifest'
import { globalWarn, logger } from '@pnpm/logger'
import { readProjectManifest } from '@pnpm/read-project-manifest'
import type { ImportOptions } from '@pnpm/store-controller-types'
import rimraf from '@zkochan/rimraf'
import sanitizeFilename from 'sanitize-filename'
import makeEmptyDir from 'make-empty-dir'
Expand All @@ -16,9 +19,7 @@ export async function importIndexedDir (
importFile: ImportFile,
newDir: string,
filenames: Record<string, string>,
opts: {
keepModulesDir?: boolean
}
opts: ImportOptions
) {
const stage = pathTemp(newDir)
try {
Expand All @@ -28,6 +29,14 @@ export async function importIndexedDir (
await moveOrMergeModulesDirs(path.join(newDir, 'node_modules'), path.join(stage, 'node_modules'))
}
await renameOverwrite(stage, newDir)
if (opts.makeExportableManifest) {
const manifest = await readProjectManifest(newDir)
const exportableManifest = await createExportableManifest(
path.dirname(filenames[manifest.fileName]),
manifest.manifest
)
await manifest.writeProjectManifest(exportableManifest)
}
} catch (err: any) { // eslint-disable-line
try {
await rimraf(stage)
Expand Down
6 changes: 6 additions & 0 deletions fs/indexed-pkg-importer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
{
"path": "../../packages/core-loggers"
},
{
"path": "../../pkg-manifest/exportable-manifest"
},
{
"path": "../../pkg-manifest/read-project-manifest"
},
{
"path": "../../store/store-controller-types"
},
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

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

5 changes: 4 additions & 1 deletion releasing/plugin-commands-deploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
"@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:*",
Expand Down
8 changes: 6 additions & 2 deletions releasing/plugin-commands-deploy/src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export function help () {
})
}

const importPkg = createIndexedPkgImporter('clone-or-copy')

export async function handler (
opts: install.InstallCommandOptions,
params: string[]
Expand Down Expand Up @@ -91,6 +93,9 @@ export async function handler (
...(opts.hooks?.readPackage ?? []),
deployHook,
],
importPackage: async (to, opts) => {
return importPkg(to, { ...opts, makeExportableManifest: true })
},
},
frozenLockfile: false,
preferFrozenLockfile: false,
Expand All @@ -108,6 +113,5 @@ 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 })
await importPkg(dest, { filesMap: filesIndex, force: true, fromStore: true, makeExportableManifest: true })
}
42 changes: 42 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,31 @@ 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)),
])
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
1 change: 1 addition & 0 deletions store/store-controller-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export interface ImportOptions {
force: boolean
fromStore: boolean
keepModulesDir?: boolean
makeExportableManifest?: boolean
}

export type ImportIndexedPackage = (to: string, opts: ImportOptions) => Promise<string | undefined>

0 comments on commit 4049b5c

Please sign in to comment.