feat!: deploy with local dependencies (#6016)

Co-authored-by: Zoltan Kochan <z@kochan.io>

Close #5911
Close #5961
This commit is contained in:
await-ovo
2023-02-19 10:23:31 +08:00
committed by GitHub
parent 417c8ac595
commit 7a0ce1df08
20 changed files with 47 additions and 8 deletions

View File

@@ -0,0 +1,10 @@
---
"@pnpm/plugin-commands-installation": major
"@pnpm/plugin-commands-deploy": major
"@pnpm/store-connection-manager": major
"@pnpm/client": major
"pnpm": major
---
When there's a `files` field in the `package.json`, only deploy those files that are listed in it.
Use the same logic also when injecting packages. This behavior can be changed by setting the `deploy-all-files` setting to `true` [#5911](https://github.com/pnpm/pnpm/issues/5911).

View File

@@ -89,6 +89,7 @@ export interface Config {
failedToLoadBuiltInConfig: boolean
resolvePeersFromWorkspaceRoot?: boolean
useLockfileV6?: boolean
deployAllFiles?: boolean
// proxy
httpProxy?: string

View File

@@ -39,6 +39,7 @@ export const types = Object.assign({
'merge-git-branch-lockfiles-branch-pattern': Array,
color: ['always', 'auto', 'never'],
'config-dir': String,
'deploy-all-files': Boolean,
dev: [null, true],
dir: String,
'enable-modules-dir': Boolean,
@@ -183,6 +184,7 @@ export async function getConfig (
'auto-install-peers': true,
bail: true,
color: 'auto',
'deploy-all-files': false,
'enable-modules-dir': true,
'extend-node-path': true,
'fetch-retries': 2,

View File

@@ -39,6 +39,7 @@ export interface StrictRebuildOptions {
unsafePerm: boolean
pending: boolean
shamefullyHoist: boolean
deployAllFiles: boolean
}
export type RebuildOptions = Partial<StrictRebuildOptions> &

View File

@@ -11,6 +11,7 @@ export const DEFAULT_OPTS = {
cacheDir: '../cache',
cert: undefined,
cliOptions: {},
deployAllFiles: false,
fetchRetries: 2,
fetchRetryFactor: 90,
fetchRetryMaxtimeout: 90,

View File

@@ -26,6 +26,7 @@ export type ClientOptions = {
userConfig?: Record<string, string>
gitShallowHosts?: string[]
resolveSymlinksInInjectedDirs?: boolean
includeOnlyPackageFiles?: boolean
} & ResolverFactoryOptions & AgentOptions
export interface Client {
@@ -56,13 +57,13 @@ type Fetchers = {
function createFetchers (
fetchFromRegistry: FetchFromRegistry,
getAuthHeader: GetAuthHeader,
opts: Pick<ClientOptions, 'rawConfig' | 'retry' | 'gitShallowHosts' | 'resolveSymlinksInInjectedDirs' | 'unsafePerm'>,
opts: Pick<ClientOptions, 'rawConfig' | 'retry' | 'gitShallowHosts' | 'resolveSymlinksInInjectedDirs' | 'unsafePerm' | 'includeOnlyPackageFiles'>,
customFetchers?: CustomFetchers
): Fetchers {
const defaultFetchers = {
...createTarballFetcher(fetchFromRegistry, getAuthHeader, opts),
...createGitFetcher(opts),
...createDirectoryFetcher({ resolveSymlinks: opts.resolveSymlinksInInjectedDirs }),
...createDirectoryFetcher({ resolveSymlinks: opts.resolveSymlinksInInjectedDirs, includeOnlyPackageFiles: opts.includeOnlyPackageFiles }),
}
const overwrites = mapValues(

View File

@@ -245,6 +245,7 @@ export type InstallCommandOptions = Pick<Config,
| 'bail'
| 'bin'
| 'cliOptions'
| 'deployAllFiles'
| 'depth'
| 'dev'
| 'engineStrict'
@@ -297,6 +298,7 @@ export type InstallCommandOptions = Pick<Config,
dedupe?: boolean
saveLockfile?: boolean
workspace?: boolean
includeOnlyPackageFiles?: boolean
} & Partial<Pick<Config, 'modulesCacheMaxAge' | 'pnpmHomeDir' | 'preferWorkspacePackages'>>
export async function handler (

View File

@@ -92,6 +92,7 @@ export type InstallDepsOptions = Pick<Config,
recursive?: boolean
dedupe?: boolean
workspace?: boolean
includeOnlyPackageFiles?: boolean
} & Partial<Pick<Config, 'pnpmHomeDir'>>
export async function installDeps (

View File

@@ -18,6 +18,7 @@ const DEFAULT_OPTIONS = {
cacheDir: path.join(tmp, 'cache'),
extraEnv: {},
cliOptions: {},
deployAllFiles: false,
include: {
dependencies: true,
devDependencies: true,

View File

@@ -13,6 +13,7 @@ const DEFAULT_OPTIONS = {
bail: false,
bin: 'node_modules/.bin',
cliOptions: {},
deployAllFiles: false,
extraEnv: {},
include: {
dependencies: true,

View File

@@ -18,6 +18,7 @@ const DEFAULT_OPTIONS = {
cacheDir: path.join(tmp, 'cache'),
extraEnv: {},
cliOptions: {},
deployAllFiles: false,
include: {
dependencies: true,
devDependencies: true,

View File

@@ -16,6 +16,7 @@ const DEFAULT_OPTIONS = {
cacheDir: path.join(TMP, 'cache'),
extraEnv: {},
cliOptions: {},
deployAllFiles: false,
include: {
dependencies: true,
devDependencies: true,

View File

@@ -15,6 +15,7 @@ const DEFAULT_OPTIONS = {
bin: 'node_modules/.bin',
extraEnv: {},
cliOptions: {},
deployAllFiles: false,
include: {
dependencies: true,
devDependencies: true,

View File

@@ -27,6 +27,7 @@ const DEFAULT_OPTIONS = {
bin: 'node_modules/.bin',
extraEnv: {},
cliOptions: {},
deployAllFiles: false,
include: {
dependencies: true,
devDependencies: true,

View File

@@ -13,6 +13,7 @@ export const DEFAULT_OPTS = {
cert: undefined,
extraEnv: {},
cliOptions: {},
deployAllFiles: false,
fetchRetries: 2,
fetchRetryFactor: 90,
fetchRetryMaxtimeout: 90,

View File

@@ -74,7 +74,8 @@ export async function handler (
const deployDir = path.isAbsolute(deployDirParam) ? deployDirParam : path.join(opts.dir, deployDirParam)
await rimraf(deployDir)
await fs.promises.mkdir(deployDir, { recursive: true })
await copyProject(deployedDir, deployDir)
const includeOnlyPackageFiles = !opts.deployAllFiles
await copyProject(deployedDir, deployDir, { includeOnlyPackageFiles })
await install.handler({
...opts,
depth: Infinity,
@@ -95,11 +96,12 @@ export async function handler (
// This is a workaround to prevent frozen install in CI envs.
'frozen-lockfile': false,
},
includeOnlyPackageFiles,
})
}
async function copyProject (src: string, dest: string) {
const { filesIndex } = await fetchFromDir(src, { includeOnlyPackageFiles: true })
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 })
}

View File

@@ -24,6 +24,7 @@ test('deploy', async () => {
{
name: 'project-2',
version: '2.0.0',
files: ['index.js'],
dependencies: {
'project-3': 'workspace:*',
'is-odd': '1.0.0',
@@ -32,6 +33,7 @@ test('deploy', async () => {
{
name: 'project-3',
version: '2.0.0',
files: ['index.js'],
dependencies: {
'project-3': 'workspace:*',
'is-odd': '1.0.0',
@@ -39,8 +41,10 @@ test('deploy', async () => {
},
])
fs.writeFileSync('project-1/test.js', '', 'utf8')
fs.writeFileSync('project-1/index.js', '', 'utf8')
; ['project-1', 'project-2', 'project-3'].forEach(name => {
fs.writeFileSync(`${name}/test.js`, '', 'utf8')
fs.writeFileSync(`${name}/index.js`, '', 'utf8')
})
const { allProjects, selectedProjectsGraph } = await readProjects(process.cwd(), [{ namePattern: 'project-1' }])
@@ -64,5 +68,9 @@ test('deploy', async () => {
await project.hasNot('is-negative')
expect(fs.existsSync('deploy/index.js')).toBeTruthy()
expect(fs.existsSync('deploy/test.js')).toBeFalsy()
expect(fs.existsSync('deploy/node_modules/.pnpm/file+project-2/node_modules/project-2/index.js')).toBeTruthy()
expect(fs.existsSync('deploy/node_modules/.pnpm/file+project-2/node_modules/project-2/test.js')).toBeFalsy()
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
})

View File

@@ -13,6 +13,7 @@ export const DEFAULT_OPTS = {
cert: undefined,
extraEnv: {},
cliOptions: {},
deployAllFiles: false,
fetchRetries: 2,
fetchRetryFactor: 90,
fetchRetryMaxtimeout: 90,

View File

@@ -13,6 +13,7 @@ export const DEFAULT_OPTS = {
cert: undefined,
extraEnv: {},
cliOptions: {},
deployAllFiles: false,
fetchRetries: 2,
fetchRetryFactor: 90,
fetchRetryMaxtimeout: 90,

View File

@@ -43,7 +43,7 @@ export type CreateNewStoreControllerOptions = CreateResolverOptions & Pick<Confi
| 'verifyStoreIntegrity'
> & {
ignoreFile?: (filename: string) => boolean
} & Partial<Pick<Config, 'userConfig'>> & Pick<ClientOptions, 'resolveSymlinksInInjectedDirs'>
} & Partial<Pick<Config, 'userConfig' | 'deployAllFiles'>> & Pick<ClientOptions, 'resolveSymlinksInInjectedDirs'>
export async function createNewStoreController (
opts: CreateNewStoreControllerOptions
@@ -84,6 +84,7 @@ export async function createNewStoreController (
),
gitShallowHosts: opts.gitShallowHosts,
resolveSymlinksInInjectedDirs: opts.resolveSymlinksInInjectedDirs,
includeOnlyPackageFiles: !opts.deployAllFiles,
})
await fs.mkdir(opts.storeDir, { recursive: true })
return {