Skip to content

Commit

Permalink
feat: "pnpm publish" supports package.{json5,yaml}
Browse files Browse the repository at this point in the history
ref #1803
  • Loading branch information
zkochan committed May 4, 2019
1 parent bb3fd53 commit f200c16
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 37 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
4 changes: 2 additions & 2 deletions packages/pnpm/src/bin/pnpm.ts
Expand Up @@ -41,7 +41,6 @@ if (argv.includes('--help') || argv.includes('-h') || argv.includes('--h')) {
case 'ping':
case 'prefix':
case 'profile':
case 'publish':
case 'repo':
case 's':
case 'se':
Expand Down Expand Up @@ -78,5 +77,6 @@ async function runPnpm () {

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

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 @@ -474,6 +481,7 @@ function getHelpText (command: string) {
- list
- outdated
- prune
- publish
- rebuild
- restart
- root
Expand Down
2 changes: 2 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 from './publish'
import rebuild from './rebuild'
import recursive from './recursive'
import root from './root'
Expand All @@ -25,6 +26,7 @@ export default {
list,
outdated,
prune,
publish,
rebuild,
recursive,
restart,
Expand Down
32 changes: 32 additions & 0 deletions packages/pnpm/src/cmd/publish.ts
@@ -0,0 +1,32 @@
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()
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)
}
const { status } = await runNpm(['publish', ...opts.argv.original.slice(1)])
if (exoticManifestFormat) {
await rimraf(path.join(prefix, 'package.json'))
await writeImporterManifest(manifest, true)
}
if (status !== 0) {
process.exit(status)
}
}
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)
}
2 changes: 2 additions & 0 deletions packages/pnpm/src/main.ts
Expand Up @@ -38,6 +38,7 @@ type CANONICAL_COMMAND_NAMES = 'help'
| 'list'
| 'outdated'
| 'prune'
| 'publish'
| 'rebuild'
| 'recursive'
| 'restart'
Expand All @@ -60,6 +61,7 @@ const supportedCmds = new Set<CANONICAL_COMMAND_NAMES>([
'update',
'link',
'prune',
'publish',
'install-test',
'restart',
'server',
Expand Down
24 changes: 0 additions & 24 deletions packages/pnpm/src/readImporterManifest.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/pnpm/test/index.ts
Expand Up @@ -7,6 +7,7 @@ import './list'
import './monorepo'
import './outdated'
import './prune'
import './publish'
import './rebuild'
import './recursive'
import './root'
Expand Down
57 changes: 57 additions & 0 deletions packages/pnpm/test/publish.ts
@@ -0,0 +1,57 @@
import prepare, {
prepareWithYamlManifest,
} 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) => {
prepareWithYamlManifest(t, {
name: 'test-publish-package.json5',
version: '0.0.0',
})

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

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

process.chdir('..')

await execPnpm('publish', 'project', ...CREDENTIALS)
})
21 changes: 15 additions & 6 deletions packages/read-importer-manifest/src/index.ts
Expand Up @@ -14,15 +14,19 @@ import {

const stat = promisify(fs.stat)

type WriteImporterManifest = (manifest: ImporterManifest, force?: boolean) => Promise<void>

export default async function readImporterManifest (importerDir: string): Promise<{
fileName: string,
manifest: ImporterManifest
writeImporterManifest: (manifest: ImporterManifest) => Promise<void>
writeImporterManifest: WriteImporterManifest
}> {
const result = await tryReadImporterManifest(importerDir)
if (result.manifest !== null) {
return result as {
fileName: string,
manifest: ImporterManifest
writeImporterManifest: (manifest: ImporterManifest) => Promise<void>
writeImporterManifest: WriteImporterManifest
}
}
const err = new Error(`No package.json (or package.yaml, or package.json5) was found in "${importerDir}".`)
Expand All @@ -36,14 +40,16 @@ export async function readImporterManifestOnly (importerDir: string): Promise<Im
}

export async function tryReadImporterManifest (importerDir: string): Promise<{
fileName: string,
manifest: ImporterManifest | null
writeImporterManifest: (manifest: ImporterManifest) => Promise<void>
writeImporterManifest: WriteImporterManifest
}> {
try {
const manifestPath = path.join(importerDir, 'package.json')
const { data, text } = await readJsonFile(manifestPath)
const { indent } = detectIndent(text)
return {
fileName: 'package.json',
manifest: data,
writeImporterManifest: createManifestWriter({
indent,
Expand All @@ -59,6 +65,7 @@ export async function tryReadImporterManifest (importerDir: string): Promise<{
const { data, text } = await readJson5File(manifestPath)
const { indent } = detectIndent(text)
return {
fileName: 'package.json5',
manifest: data,
writeImporterManifest: createManifestWriter({
indent,
Expand All @@ -73,6 +80,7 @@ export async function tryReadImporterManifest (importerDir: string): Promise<{
const manifestPath = path.join(importerDir, 'package.yaml')
const manifest = await readPackageYaml(manifestPath)
return {
fileName: 'package.yaml',
manifest,
writeImporterManifest: createManifestWriter({ initialManifest: manifest, manifestPath }),
}
Expand All @@ -95,6 +103,7 @@ export async function tryReadImporterManifest (importerDir: string): Promise<{
}
const filePath = path.join(importerDir, 'package.json')
return {
fileName: 'package.json',
manifest: null,
writeImporterManifest: writeImporterManifest.bind(null, filePath),
}
Expand Down Expand Up @@ -148,10 +157,10 @@ function createManifestWriter (
indent?: string | number | null | undefined,
manifestPath: string,
},
): ((manifest: ImporterManifest) => Promise<void>) {
): (WriteImporterManifest) {
const stringifiedInitialManifest = JSON.stringify(opts.initialManifest)
return async (updatedManifest: ImporterManifest) => {
if (stringifiedInitialManifest !== JSON.stringify(updatedManifest)) {
return async (updatedManifest: ImporterManifest, force?: boolean) => {
if (force === true || stringifiedInitialManifest !== JSON.stringify(updatedManifest)) {
return writeImporterManifest(opts.manifestPath, updatedManifest, { indent: opts.indent })
}
}
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

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

0 comments on commit f200c16

Please sign in to comment.