Skip to content

Commit

Permalink
test: package.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Apr 28, 2019
1 parent b2e6bb7 commit 658c23e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
7 changes: 3 additions & 4 deletions packages/pnpm/src/cmd/install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import readImporterManifest from '@pnpm/read-importer-manifest'
import readImporterManifest, { tryReadImporterManifest } from '@pnpm/read-importer-manifest'
import { getSaveType } from '@pnpm/utils'
import writeImporterManifest from '@pnpm/write-importer-manifest'
import path = require('path')
Expand All @@ -10,7 +10,6 @@ import {
import createStoreController from '../createStoreController'
import findWorkspacePackages, { arrayOfLocalPackagesToMap } from '../findWorkspacePackages'
import getPinnedVersion from '../getPinnedVersion'
import { readImporterManifestFromDir, safeReadImporterManifestFromDir } from '../readImporterManifest'
import requireHooks from '../requireHooks'
import { PnpmOptions } from '../types'
import updateToLatestSpecsFromManifest, { createLatestSpecs } from '../updateToLatestSpecsFromManifest'
Expand Down Expand Up @@ -56,7 +55,7 @@ export default async function installCmd (
storeController: store.ctrl,
}

let { manifest, fileName } = await readImporterManifest(opts.prefix)
let { manifest, fileName } = await tryReadImporterManifest(opts.prefix)
if (manifest === null) {
if (opts.update) {
const err = new Error('No package.json found')
Expand Down Expand Up @@ -115,7 +114,7 @@ export default async function installCmd (
[
{
buildIndex: 0,
manifest: await readImporterManifestFromDir(opts.prefix),
manifest: (await readImporterManifest(opts.prefix)).manifest,
prefix: opts.prefix,
},
], {
Expand Down
16 changes: 16 additions & 0 deletions packages/pnpm/test/install/misc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { WANTED_LOCKFILE } from '@pnpm/constants'
import { Lockfile } from '@pnpm/lockfile-types'
import prepare, { prepareEmpty } from '@pnpm/prepare'
import readImporterManifest from '@pnpm/read-importer-manifest'
import { fromDir as readPackageJsonFromDir } from '@pnpm/read-package-json'
import writeImporterManifest from '@pnpm/write-importer-manifest'
import caw = require('caw')
import crossSpawn = require('cross-spawn')
import delay = require('delay')
Expand Down Expand Up @@ -129,6 +131,20 @@ test('install --save-exact', async (t: tape.Test) => {
t.deepEqual(pkg.devDependencies, { 'is-positive': '3.1.0' })
})

test('install to a project that uses package.yaml', async (t: tape.Test) => {
const project = prepareEmpty(t)

await writeImporterManifest(path.resolve('package.yaml'), { name: 'foo', version: '1.0.0' })

await execPnpm('install', 'is-positive@3.1.0', '--save-exact', '--save-dev')

await project.has('is-positive')

const { manifest } = await readImporterManifest(process.cwd())

t.deepEqual(manifest && manifest.devDependencies, { 'is-positive': '3.1.0' })
})

test('install save new dep with the specified spec', async (t: tape.Test) => {
const project = prepare(t)

Expand Down
20 changes: 17 additions & 3 deletions packages/read-importer-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ import path = require('path')
import readJson5File = require('read-json5-file')
import readYamlFile from 'read-yaml-file'

export interface ReadImporterManifestResult {
export default async function readImporterManifest (importerDir: string): Promise<{
fileName: string
manifest: ImporterManifest | null
manifest: ImporterManifest
}> {
const result = await tryReadImporterManifest(importerDir)
if (result.manifest !== null) {
return result as {
fileName: string
manifest: ImporterManifest
}
}
const err = new Error(`No package.json (or package.yaml, or package.json5) was found in "${importerDir}".`)
err['code'] = 'ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND'
throw err
}

export default async function readImporterManifest (importerDir: string): Promise<ReadImporterManifestResult> {
export async function tryReadImporterManifest (importerDir: string): Promise<{
fileName: string
manifest: ImporterManifest | null
}> {
try {
return {
fileName: 'package.json',
Expand Down
10 changes: 5 additions & 5 deletions packages/read-importer-manifest/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import readImporterManifest from '@pnpm/read-importer-manifest'
import { tryReadImporterManifest } from '@pnpm/read-importer-manifest'
import path = require('path')
import test = require('tape')

const fixtures = path.join(__dirname, 'fixtures')

test('readImporterManifest()', async (t) => {
t.deepEqual(
await readImporterManifest(path.join(fixtures, 'package-json')),
await tryReadImporterManifest(path.join(fixtures, 'package-json')),
{
fileName: 'package.json',
manifest: { name: 'foo', version: '1.0.0' },
},
)

t.deepEqual(
await readImporterManifest(path.join(fixtures, 'package-json5')),
await tryReadImporterManifest(path.join(fixtures, 'package-json5')),
{
fileName: 'package.json5',
manifest: { name: 'foo', version: '1.0.0' },
},
)

t.deepEqual(
await readImporterManifest(path.join(fixtures, 'package-yaml')),
await tryReadImporterManifest(path.join(fixtures, 'package-yaml')),
{
fileName: 'package.yaml',
manifest: { name: 'foo', version: '1.0.0' },
},
)

t.deepEqual(
await readImporterManifest(fixtures),
await tryReadImporterManifest(fixtures),
{
fileName: 'package.json',
manifest: null,
Expand Down
1 change: 1 addition & 0 deletions packages/write-importer-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import writeJsonFile = require('write-json-file')
import writeJson5File = require('write-json5-file')
import writeYamlFile = require('write-yaml-file')

// TODO: normalize before save + preserve indent
export default function writeImporterManifest (filePath: string, manifest: ImporterManifest): Promise<void> {
switch (filePath.substr(filePath.lastIndexOf('.') + 1).toLowerCase()) {
case 'json5':
Expand Down

0 comments on commit 658c23e

Please sign in to comment.