Skip to content

Commit

Permalink
feat: publish and pack support package.{json5,yaml}
Browse files Browse the repository at this point in the history
PR #1809
ref #1803
  • Loading branch information
zkochan committed May 4, 2019
1 parent bb3fd53 commit c043e5c
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 38 deletions.
4 changes: 2 additions & 2 deletions packages/pnpm/package.json
Expand Up @@ -84,7 +84,8 @@
"text-table": "0.2.0",
"tree-kill": "1.2.1",
"update-notifier": "2.5.0",
"version-selector-type": "2.0.1"
"version-selector-type": "2.0.1",
"write-json-file": "3.2.0"
},
"devDependencies": {
"@pnpm/assert-project": "link:../../privatePackages/assert-project",
Expand Down Expand Up @@ -127,7 +128,6 @@
"ts-node": "8.1.0",
"tslint": "5.16.0",
"typescript": "3.4.5",
"write-json-file": "3.2.0",
"write-pkg": "4.0.0",
"write-yaml-file": "2.0.0"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/pnpm/src/bin/pnpm.ts
Expand Up @@ -37,11 +37,9 @@ if (argv.includes('--help') || argv.includes('-h') || argv.includes('--h')) {
case 'login':
case 'logout':
case 'owner':
case 'pack':
case 'ping':
case 'prefix':
case 'profile':
case 'publish':
case 'repo':
case 's':
case 'se':
Expand Down Expand Up @@ -78,5 +76,6 @@ async function runPnpm () {

async function passThruToNpm () {
const runNpm = (await import('../cmd/runNpm')).default
runNpm(argv)
const { status } = runNpm(argv)
process.exit(status)
}
16 changes: 16 additions & 0 deletions packages/pnpm/src/cmd/help.ts
Expand Up @@ -215,6 +215,20 @@ function getHelpText (command: string) {
Removes extraneous packages
`

case 'pack':
return stripIndent`
pnpm pack
Creates a compressed gzip archive of package dependencies.
`

case 'publish':
return stripIndent`
pnpm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>]
Publishes a package to the npm registry.
`

case 'install-test':
return stripIndent`
pnpm install-test
Expand Down Expand Up @@ -473,7 +487,9 @@ function getHelpText (command: string) {
- link
- list
- outdated
- pack
- prune
- publish
- rebuild
- restart
- root
Expand Down
3 changes: 3 additions & 0 deletions packages/pnpm/src/cmd/index.ts
Expand Up @@ -6,6 +6,7 @@ import link from './link'
import list from './list'
import outdated from './outdated'
import prune from './prune'
import publish, { pack } from './publish'
import rebuild from './rebuild'
import recursive from './recursive'
import root from './root'
Expand All @@ -24,7 +25,9 @@ export default {
link,
list,
outdated,
pack,
prune,
publish,
rebuild,
recursive,
restart,
Expand Down
56 changes: 56 additions & 0 deletions packages/pnpm/src/cmd/publish.ts
@@ -0,0 +1,56 @@
import readImporterManifest from '@pnpm/read-importer-manifest'
import path = require('path')
import rimraf = require('rimraf-then')
import writeJsonFile = require('write-json-file')
import { PnpmOptions } from '../types'
import runNpm from './runNpm'

export default async function (
args: string[],
opts: PnpmOptions,
command: string,
) {
if (args.length && args[0].endsWith('.tgz')) {
await runNpm(['publish', ...args])
return
}
const prefix = args.length && args[0] || process.cwd()

let _status!: number
await fakeRegularManifest(prefix, async () => {
const { status } = await runNpm(['publish', ...opts.argv.original.slice(1)])
_status = status
})
if (_status !== 0) {
process.exit(_status)
}
}

export async function pack (
args: string[],
opts: PnpmOptions,
command: string,
) {
let _status!: number
await fakeRegularManifest(opts.prefix, async () => {
const { status } = await runNpm(['pack', ...opts.argv.original.slice(1)])
_status = status
})
if (_status !== 0) {
process.exit(_status)
}
}

async function fakeRegularManifest (prefix: string, fn: () => Promise<void>) {
const { fileName, manifest, writeImporterManifest } = await readImporterManifest(prefix)
const exoticManifestFormat = fileName !== 'package.json'
if (exoticManifestFormat) {
await rimraf(path.join(prefix, fileName))
await writeJsonFile(path.join(prefix, 'package.json'), manifest)
}
await fn()
if (exoticManifestFormat) {
await rimraf(path.join(prefix, 'package.json'))
await writeImporterManifest(manifest, true)
}
}
3 changes: 1 addition & 2 deletions packages/pnpm/src/cmd/runNpm.ts
@@ -1,10 +1,9 @@
import { sync as runScriptSync } from '../runScript'

export default function runNpm (args: string[]) {
const result = runScriptSync('npm', args, {
return runScriptSync('npm', args, {
cwd: process.cwd(),
stdio: 'inherit',
userAgent: undefined,
})
process.exit(result.status)
}
4 changes: 4 additions & 0 deletions packages/pnpm/src/main.ts
Expand Up @@ -37,7 +37,9 @@ type CANONICAL_COMMAND_NAMES = 'help'
| 'link'
| 'list'
| 'outdated'
| 'pack'
| 'prune'
| 'publish'
| 'rebuild'
| 'recursive'
| 'restart'
Expand All @@ -59,7 +61,9 @@ const supportedCmds = new Set<CANONICAL_COMMAND_NAMES>([
'uninstall',
'update',
'link',
'pack',
'prune',
'publish',
'install-test',
'restart',
'server',
Expand Down
24 changes: 0 additions & 24 deletions packages/pnpm/src/readImporterManifest.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/pnpm/test/index.ts
Expand Up @@ -6,7 +6,9 @@ import './link'
import './list'
import './monorepo'
import './outdated'
import './pack'
import './prune'
import './publish'
import './rebuild'
import './recursive'
import './root'
Expand Down
49 changes: 49 additions & 0 deletions packages/pnpm/test/pack.ts
@@ -0,0 +1,49 @@
import prepare, {
prepareWithYamlManifest,
prepareWithJson5Manifest,
} from '@pnpm/prepare'
import exists = require('path-exists')
import tape = require('tape')
import promisifyTape from 'tape-promise'
import { execPnpm } from './utils'

const test = promisifyTape(tape)
const testOnly = promisifyTape(tape.only)

test('pack: package with package.json', async (t: tape.Test) => {
prepare(t, {
name: 'test-publish-package.json',
version: '0.0.0',
})

await execPnpm('pack')

t.ok(await exists('test-publish-package.json-0.0.0.tgz'))
t.ok(await exists('package.json'))
})

test('pack: package with package.yaml', async (t: tape.Test) => {
prepareWithYamlManifest(t, {
name: 'test-publish-package.yaml',
version: '0.0.0',
})

await execPnpm('pack')

t.ok(await exists('test-publish-package.yaml-0.0.0.tgz'))
t.ok(await exists('package.yaml'))
t.notOk(await exists('package.json'))
})

test('pack: package with package.json5', async (t: tape.Test) => {
prepareWithJson5Manifest(t, {
name: 'test-publish-package.json5',
version: '0.0.0',
})

await execPnpm('pack')

t.ok(await exists('test-publish-package.json5-0.0.0.tgz'))
t.ok(await exists('package.json5'))
t.notOk(await exists('package.json'))
})
64 changes: 64 additions & 0 deletions packages/pnpm/test/publish.ts
@@ -0,0 +1,64 @@
import prepare, {
prepareWithYamlManifest,
prepareWithJson5Manifest,
} from '@pnpm/prepare'
import exists = require('path-exists')
import tape = require('tape')
import promisifyTape from 'tape-promise'
import { execPnpm } from './utils'

const test = promisifyTape(tape)
const testOnly = promisifyTape(tape.only)

const CREDENTIALS = [
'--//localhost:4873/:username=username',
`--//localhost:4873/:_password=${Buffer.from('password').toString('base64')}`,
'--//localhost:4873/:email=foo@bar.net',
]

test('publish: package with package.json', async (t: tape.Test) => {
prepare(t, {
name: 'test-publish-package.json',
version: '0.0.0',
})

await execPnpm('publish', ...CREDENTIALS)
})

test('publish: package with package.yaml', async (t: tape.Test) => {
prepareWithYamlManifest(t, {
name: 'test-publish-package.yaml',
version: '0.0.0',
})

await execPnpm('publish', ...CREDENTIALS)

t.ok(await exists('package.yaml'))
t.notOk(await exists('package.json'))
})

test('publish: package with package.json5', async (t: tape.Test) => {
prepareWithJson5Manifest(t, {
name: 'test-publish-package.json5',
version: '0.0.0',
})

await execPnpm('publish', ...CREDENTIALS)

t.ok(await exists('package.json5'))
t.notOk(await exists('package.json'))
})

test('publish: package with package.json5 running publish from different folder', async (t: tape.Test) => {
prepareWithJson5Manifest(t, {
name: 'test-publish-package.json5',
version: '0.0.1',
})

process.chdir('..')

await execPnpm('publish', 'project', ...CREDENTIALS)

t.ok(await exists('project/package.json5'))
t.notOk(await exists('project/package.json'))
})

0 comments on commit c043e5c

Please sign in to comment.