Skip to content

Commit

Permalink
feat: deploy command
Browse files Browse the repository at this point in the history
close #4378
  • Loading branch information
zkochan committed Jun 25, 2022
1 parent f48d46e commit a3a449d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
66 changes: 66 additions & 0 deletions packages/plugin-commands-installation/src/deploy.ts
@@ -0,0 +1,66 @@
import fs from 'fs'
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import { FILTERING, OPTIONS, UNIVERSAL_OPTIONS } from '@pnpm/common-cli-options-help'
import { types as allTypes } from '@pnpm/config'
import PnpmError from '@pnpm/error'
import pick from 'ramda/src/pick'
import renderHelp from 'render-help'
import * as install from './install'

export function rcOptionsTypes () {
return install.rcOptionsTypes()
}

export function cliOptionsTypes () {
return install.cliOptionsTypes()
}

export const commandNames = ['deploy']

export function help () {
return renderHelp({
description: 'Deploy a package from a workspace',
descriptionLists: [],
url: docsUrl('deploy'),
usages: [],
})
}

export async function handler (
opts: install.InstallCommandOptions,
params: string[]
) {
if (!opts.workspaceDir) {
throw new PnpmError('CANNOT_DEPLOY', 'A deploy is only possible from inside a workspace')
}
const deployDir = path.join(opts.workspaceDir, 'deploy')
await fs.promises.mkdir(deployDir)
const readPackageHook = opts.hooks?.readPackage
// eslint-disable-next-line
const newReadPackageHook = readPackageHook ? (async (pkg: any, context: any) => deployHook(await readPackageHook(pkg, context))) : deployHook
await install.handler({
...opts,
depth: Infinity,
hooks: {
...opts.hooks,
readPackage: newReadPackageHook,
},
frozenLockfile: false,
preferFrozenLockfile: false,
virtualStoreDir: path.join(deployDir, 'node_modules/.pnpm'),
modulesDir: path.relative(Object.keys(opts.selectedProjectsGraph ?? {})[0], path.join(deployDir, 'node_modules')),
})
}

function deployHook (pkg: any) {
pkg.dependenciesMeta = pkg.dependenciesMeta || {}
for (const [depName, depVersion] of Object.entries(pkg.dependencies ?? {})) {
if ((depVersion as string).startsWith('workspace:')) {
pkg.dependenciesMeta[depName] = {
injected: true
}
}
}
return pkg
}
3 changes: 2 additions & 1 deletion packages/plugin-commands-installation/src/index.ts
@@ -1,4 +1,5 @@
import * as add from './add'
import * as deploy from './deploy'
import * as install from './install'
import * as fetch from './fetch'
import * as link from './link'
Expand All @@ -8,4 +9,4 @@ import * as unlink from './unlink'
import * as update from './update'
import * as importCommand from './import'

export { add, fetch, install, link, prune, remove, unlink, update, importCommand }
export { add, deploy, fetch, install, link, prune, remove, unlink, update, importCommand }
4 changes: 4 additions & 0 deletions packages/plugin-commands-installation/src/install.ts
Expand Up @@ -245,6 +245,7 @@ export type InstallCommandOptions = Pick<Config,
| 'depth'
| 'dev'
| 'engineStrict'
| 'frozenLockfile'
| 'global'
| 'globalPnpmfile'
| 'hooks'
Expand All @@ -254,7 +255,9 @@ export type InstallCommandOptions = Pick<Config,
| 'rawLocalConfig'
| 'lockfileDir'
| 'lockfileOnly'
| 'modulesDir'
| 'pnpmfile'
| 'preferFrozenLockfile'
| 'production'
| 'registries'
| 'save'
Expand All @@ -272,6 +275,7 @@ export type InstallCommandOptions = Pick<Config,
| 'sharedWorkspaceLockfile'
| 'tag'
| 'optional'
| 'virtualStoreDir'
| 'workspaceConcurrency'
| 'workspaceDir'
> & CreateStoreControllerOptions & {
Expand Down
3 changes: 2 additions & 1 deletion packages/pnpm/src/cmd/index.ts
Expand Up @@ -2,7 +2,7 @@ import { CompletionFunc } from '@pnpm/command'
import { types as allTypes } from '@pnpm/config'
import { audit } from '@pnpm/plugin-commands-audit'
import { env } from '@pnpm/plugin-commands-env'
import { add, fetch, install, link, prune, remove, unlink, update, importCommand } from '@pnpm/plugin-commands-installation'
import { add, deploy, fetch, install, link, prune, remove, unlink, update, importCommand } from '@pnpm/plugin-commands-installation'
import { list, ll, why } from '@pnpm/plugin-commands-listing'
import { outdated } from '@pnpm/plugin-commands-outdated'
import { pack, publish } from '@pnpm/plugin-commands-publishing'
Expand Down Expand Up @@ -97,6 +97,7 @@ const commands: CommandDefinition[] = [
audit,
bin,
create,
deploy,
dlx,
env,
exec,
Expand Down

0 comments on commit a3a449d

Please sign in to comment.