From 74bc91c1b5225f0bfa6eb39389058dc7b6ff3e95 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sat, 4 May 2019 02:26:26 +0300 Subject: [PATCH] feat: "pnpm publish" supports package.{json5,yaml} ref #1803 --- packages/pnpm/package.json | 4 +- packages/pnpm/src/bin/pnpm.ts | 1 - packages/pnpm/src/cmd/help.ts | 8 ++++ packages/pnpm/src/cmd/index.ts | 2 + packages/pnpm/src/cmd/publish.ts | 27 +++++++++++ packages/pnpm/src/main.ts | 2 + packages/pnpm/src/readImporterManifest.ts | 24 ---------- packages/pnpm/test/index.ts | 1 + packages/pnpm/test/publish.ts | 47 ++++++++++++++++++++ packages/read-importer-manifest/src/index.ts | 7 +++ pnpm-lock.yaml | 2 +- 11 files changed, 97 insertions(+), 28 deletions(-) create mode 100644 packages/pnpm/src/cmd/publish.ts delete mode 100644 packages/pnpm/src/readImporterManifest.ts create mode 100644 packages/pnpm/test/publish.ts diff --git a/packages/pnpm/package.json b/packages/pnpm/package.json index d2f0f3793cb..89b08f526cd 100644 --- a/packages/pnpm/package.json +++ b/packages/pnpm/package.json @@ -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", @@ -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" }, diff --git a/packages/pnpm/src/bin/pnpm.ts b/packages/pnpm/src/bin/pnpm.ts index 7117938f20f..adaa97e1794 100755 --- a/packages/pnpm/src/bin/pnpm.ts +++ b/packages/pnpm/src/bin/pnpm.ts @@ -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': diff --git a/packages/pnpm/src/cmd/help.ts b/packages/pnpm/src/cmd/help.ts index 37d0a447dae..697f4b4ac9d 100644 --- a/packages/pnpm/src/cmd/help.ts +++ b/packages/pnpm/src/cmd/help.ts @@ -215,6 +215,13 @@ function getHelpText (command: string) { Removes extraneous packages ` + case 'publish': + return stripIndent` + pnpm publish [|] [--tag ] [--access ] + + Publishes a package to the npm registry. + ` + case 'install-test': return stripIndent` pnpm install-test @@ -474,6 +481,7 @@ function getHelpText (command: string) { - list - outdated - prune + - publish - rebuild - restart - root diff --git a/packages/pnpm/src/cmd/index.ts b/packages/pnpm/src/cmd/index.ts index dca31e5742b..bc39a373dbc 100644 --- a/packages/pnpm/src/cmd/index.ts +++ b/packages/pnpm/src/cmd/index.ts @@ -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' @@ -25,6 +26,7 @@ export default { list, outdated, prune, + publish, rebuild, recursive, restart, diff --git a/packages/pnpm/src/cmd/publish.ts b/packages/pnpm/src/cmd/publish.ts new file mode 100644 index 00000000000..58829d6e863 --- /dev/null +++ b/packages/pnpm/src/cmd/publish.ts @@ -0,0 +1,27 @@ +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) + if (fileName !== 'package.json') { + await writeJsonFile(path.join(prefix, 'package.json'), manifest) + } + await runNpm(['publish', ...args]) + if (fileName !== 'package.json') { + await rimraf(path.join(prefix, 'package.json')) + await writeImporterManifest(manifest) + } +} diff --git a/packages/pnpm/src/main.ts b/packages/pnpm/src/main.ts index 095e0cddaf9..d8562de6e11 100644 --- a/packages/pnpm/src/main.ts +++ b/packages/pnpm/src/main.ts @@ -38,6 +38,7 @@ type CANONICAL_COMMAND_NAMES = 'help' | 'list' | 'outdated' | 'prune' + | 'publish' | 'rebuild' | 'recursive' | 'restart' @@ -60,6 +61,7 @@ const supportedCmds = new Set([ 'update', 'link', 'prune', + 'publish', 'install-test', 'restart', 'server', diff --git a/packages/pnpm/src/readImporterManifest.ts b/packages/pnpm/src/readImporterManifest.ts deleted file mode 100644 index 8bf95456588..00000000000 --- a/packages/pnpm/src/readImporterManifest.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { ImporterManifest } from '@pnpm/types' -import loadJsonFile = require('load-json-file') -import path = require('path') - -export async function readImporterManifest (filename: string) { - return loadJsonFile(filename) -} - -export async function readImporterManifestFromDir (dir: string) { - return readImporterManifest(path.join(dir, 'package.json')) -} - -export async function safeReadImporterManifest (filename: string): Promise { - try { - return await readImporterManifest(filename) - } catch (err) { - if (err['code'] !== 'ENOENT') throw err - return null - } -} - -export function safeReadImporterManifestFromDir (dir: string) { - return safeReadImporterManifest(path.join(dir, 'package.json')) -} diff --git a/packages/pnpm/test/index.ts b/packages/pnpm/test/index.ts index 828ec2bad46..52e35b69c15 100644 --- a/packages/pnpm/test/index.ts +++ b/packages/pnpm/test/index.ts @@ -7,6 +7,7 @@ import './list' import './monorepo' import './outdated' import './prune' +import './publish' import './rebuild' import './recursive' import './root' diff --git a/packages/pnpm/test/publish.ts b/packages/pnpm/test/publish.ts new file mode 100644 index 00000000000..d015c8d2de9 --- /dev/null +++ b/packages/pnpm/test/publish.ts @@ -0,0 +1,47 @@ +import { + prepareWithYamlManifest, +} from '@pnpm/prepare' +import tape = require('tape') +import promisifyTape from 'tape-promise' +import { execPnpm } from './utils' + +const test = promisifyTape(tape) +const testOnly = promisifyTape(tape.only) + +test('publish: package with package.json', async (t: tape.Test) => { + prepareWithYamlManifest(t, { + name: 'test-publish-package.json', + version: '0.0.0', + }) + + await execPnpm('publish') +}) + +test('publish: package with package.yaml', async (t: tape.Test) => { + prepareWithYamlManifest(t, { + name: 'test-publish-package.yaml', + version: '0.0.0', + }) + + await execPnpm('publish') +}) + +test('publish: package with package.json5', async (t: tape.Test) => { + prepareWithYamlManifest(t, { + name: 'test-publish-package.json5', + version: '0.0.0', + }) + + await execPnpm('publish') +}) + +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') +}) diff --git a/packages/read-importer-manifest/src/index.ts b/packages/read-importer-manifest/src/index.ts index bd03637e0bd..64d830ca9a5 100644 --- a/packages/read-importer-manifest/src/index.ts +++ b/packages/read-importer-manifest/src/index.ts @@ -15,12 +15,14 @@ import { const stat = promisify(fs.stat) export default async function readImporterManifest (importerDir: string): Promise<{ + fileName: string, manifest: ImporterManifest writeImporterManifest: (manifest: ImporterManifest) => Promise }> { const result = await tryReadImporterManifest(importerDir) if (result.manifest !== null) { return result as { + fileName: string, manifest: ImporterManifest writeImporterManifest: (manifest: ImporterManifest) => Promise } @@ -36,6 +38,7 @@ export async function readImporterManifestOnly (importerDir: string): Promise Promise }> { @@ -44,6 +47,7 @@ export async function tryReadImporterManifest (importerDir: string): Promise<{ const { data, text } = await readJsonFile(manifestPath) const { indent } = detectIndent(text) return { + fileName: 'package.json', manifest: data, writeImporterManifest: createManifestWriter({ indent, @@ -59,6 +63,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, @@ -73,6 +78,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 }), } @@ -95,6 +101,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), } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 630aa9d63de..12602d9ecd8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1448,6 +1448,7 @@ importers: tree-kill: 1.2.1 update-notifier: 2.5.0 version-selector-type: 2.0.1 + write-json-file: 3.2.0 devDependencies: '@pnpm/assert-project': 'link:../../privatePackages/assert-project' '@pnpm/lockfile-types': 'link:../lockfile-types' @@ -1489,7 +1490,6 @@ importers: ts-node: 8.1.0_typescript@3.4.5 tslint: 5.16.0_typescript@3.4.5 typescript: 3.4.5 - write-json-file: 3.2.0 write-pkg: 4.0.0 write-yaml-file: 2.0.0 specifiers: