Skip to content

Commit 4f6e377

Browse files
committed
feat: support package.yaml and package.json5
close #1100
1 parent 54e7be5 commit 4f6e377

File tree

20 files changed

+486
-4
lines changed

20 files changed

+486
-4
lines changed

packages/pnpm/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@pnpm/logger": "2.1.0",
3333
"@pnpm/outdated": "2.0.8",
3434
"@pnpm/package-store": "4.0.6",
35+
"@pnpm/read-importer-manifest": "0.0.0",
3536
"@pnpm/server": "3.0.3",
3637
"@pnpm/store-controller-types": "3.0.3",
3738
"@pnpm/store-path": "1.0.4",

packages/pnpm/src/cmd/install.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import readImporterManifest from '@pnpm/read-importer-manifest'
12
import { getSaveType } from '@pnpm/utils'
3+
import writeImporterManifest from '@pnpm/write-importer-manifest'
4+
import path = require('path')
25
import {
36
install,
47
mutateModules,
58
rebuild,
69
} from 'supi'
7-
import writePkg = require('write-pkg')
810
import createStoreController from '../createStoreController'
911
import findWorkspacePackages, { arrayOfLocalPackagesToMap } from '../findWorkspacePackages'
1012
import getPinnedVersion from '../getPinnedVersion'
@@ -54,7 +56,7 @@ export default async function installCmd (
5456
storeController: store.ctrl,
5557
}
5658

57-
let manifest = await safeReadImporterManifestFromDir(opts.prefix)
59+
let { manifest, fileName } = await readImporterManifest(opts.prefix)
5860
if (manifest === null) {
5961
if (opts.update) {
6062
const err = new Error('No package.json found')
@@ -87,7 +89,7 @@ export default async function installCmd (
8789
targetDependenciesField: getSaveType(installOpts),
8890
},
8991
], installOpts)
90-
await writePkg(opts.prefix, updatedImporter.manifest)
92+
await writeImporterManifest(path.join(opts.prefix, fileName), updatedImporter.manifest)
9193
}
9294

9395
if (opts.linkWorkspacePackages && opts.workspacePrefix) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018-2019 Zoltan Kochan <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# @pnpm/read-importer-manifest
2+
3+
> Read an importer manifest (called package.json in most cases)
4+
5+
<!--@shields('npm')-->
6+
[![npm version](https://img.shields.io/npm/v/@pnpm/read-importer-manifest.svg)](https://www.npmjs.com/package/@pnpm/read-importer-manifest)
7+
<!--/@-->
8+
9+
## Installation
10+
11+
```sh
12+
<npm|yarn|pnpm> add @pnpm/read-importer-manifest
13+
```
14+
15+
## Usage
16+
17+
```ts
18+
import readImporterManifest from '@pnpm/read-importer-manifest'
19+
20+
const { manifest, fileName } = await readImporterManifest(process.cwd())
21+
```
22+
23+
## License
24+
25+
[MIT](./LICENSE) © [Zoltan Kochan](https://www.kochan.io/)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"name": "@pnpm/read-importer-manifest",
3+
"version": "0.0.0",
4+
"description": "Read an importer manifest (called package.json in most cases)",
5+
"main": "lib/index.js",
6+
"typings": "lib/index.d.ts",
7+
"engines": {
8+
"node": ">=8.15"
9+
},
10+
"files": [
11+
"lib"
12+
],
13+
"scripts": {
14+
"lint": "tslint -c tslint.json src/**/*.ts test/**/*.ts",
15+
"test": "npm run lint && npm run tsc && ts-node test --type-check",
16+
"prepublishOnly": "tsc",
17+
"tsc": "tsc",
18+
"md": "mos"
19+
},
20+
"repository": "https://github.com/pnpm/pnpm/blob/master/packages/read-importer-manifest",
21+
"keywords": [
22+
"pnpm",
23+
"outdated"
24+
],
25+
"author": {
26+
"name": "Zoltan Kochan",
27+
"email": "[email protected]",
28+
"url": "https://www.kochan.io/",
29+
"twitter": "ZoltanKochan"
30+
},
31+
"license": "MIT",
32+
"bugs": {
33+
"url": "https://github.com/pnpm/pnpm/issues"
34+
},
35+
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/read-importer-manifest#readme",
36+
"dependencies": {
37+
"@pnpm/types": "^3.0.0",
38+
"load-json-file": "6.0.0",
39+
"read-json5-file": "0.0.0",
40+
"read-yaml-file": "1.1.0"
41+
},
42+
"devDependencies": {
43+
"@pnpm/read-importer-manifest": "link:",
44+
"@pnpm/tslint-config": "0.0.0",
45+
"@types/node": "^10.3.2",
46+
"@types/tape": "^4.2.31",
47+
"mos": "^2.0.0-alpha.3",
48+
"mos-plugin-readme": "^1.0.4",
49+
"tape": "^4.8.0",
50+
"ts-node": "8.1.0",
51+
"tslint": "^5.8.0",
52+
"typescript": "3.4.5"
53+
},
54+
"mos": {
55+
"plugins": [
56+
"readme"
57+
],
58+
"installation": {
59+
"useShortAlias": true
60+
}
61+
}
62+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { ImporterManifest } from '@pnpm/types'
2+
import readJsonFile = require('load-json-file')
3+
import path = require('path')
4+
import readJson5File = require('read-json5-file')
5+
import readYamlFile from 'read-yaml-file'
6+
7+
export interface ReadImporterManifestResult {
8+
fileName: string
9+
manifest: ImporterManifest | null
10+
}
11+
12+
export default async function readImporterManifest (importerDir: string): Promise<ReadImporterManifestResult> {
13+
try {
14+
return {
15+
fileName: 'package.json',
16+
manifest: await readJsonFile<ImporterManifest>(path.join(importerDir, 'package.json')),
17+
}
18+
} catch (err) {
19+
if (err.code !== 'ENOENT') throw err
20+
}
21+
try {
22+
return {
23+
fileName: 'package.json5',
24+
manifest: await readJson5File<ImporterManifest>(path.join(importerDir, 'package.json5')),
25+
}
26+
} catch (err) {
27+
if (err.code !== 'ENOENT') throw err
28+
}
29+
try {
30+
return {
31+
fileName: 'package.yaml',
32+
manifest: await readYamlFile<ImporterManifest>(path.join(importerDir, 'package.yaml')),
33+
}
34+
} catch (err) {
35+
if (err.code !== 'ENOENT') throw err
36+
}
37+
return { fileName: 'package.json', manifest: null }
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "foo",
3+
"version": "1.0.0"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
name: 'foo',
3+
version: '1.0.0',
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: foo
2+
version: 1.0.0
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import readImporterManifest from '@pnpm/read-importer-manifest'
2+
import path = require('path')
3+
import test = require('tape')
4+
5+
const fixtures = path.join(__dirname, 'fixtures')
6+
7+
test('readImporterManifest()', async (t) => {
8+
t.deepEqual(
9+
await readImporterManifest(path.join(fixtures, 'package-json')),
10+
{
11+
fileName: 'package.json',
12+
manifest: { name: 'foo', version: '1.0.0' },
13+
},
14+
)
15+
16+
t.deepEqual(
17+
await readImporterManifest(path.join(fixtures, 'package-json5')),
18+
{
19+
fileName: 'package.json5',
20+
manifest: { name: 'foo', version: '1.0.0' },
21+
},
22+
)
23+
24+
t.deepEqual(
25+
await readImporterManifest(path.join(fixtures, 'package-yaml')),
26+
{
27+
fileName: 'package.yaml',
28+
manifest: { name: 'foo', version: '1.0.0' },
29+
},
30+
)
31+
32+
t.deepEqual(
33+
await readImporterManifest(fixtures),
34+
{
35+
fileName: 'package.json',
36+
manifest: null,
37+
},
38+
)
39+
40+
t.end()
41+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../utils/tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "lib"
5+
},
6+
"include": [
7+
"src/**/*.ts",
8+
"typings/**/*.d.ts",
9+
"test/typings/**/*.d.ts"
10+
]
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@pnpm/tslint-config"
3+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018-2019 Zoltan Kochan <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# @pnpm/write-importer-manifest
2+
3+
> Write an importer manifest (called package.json in most cases)
4+
5+
<!--@shields('npm')-->
6+
[![npm version](https://img.shields.io/npm/v/@pnpm/write-importer-manifest.svg)](https://www.npmjs.com/package/@pnpm/write-importer-manifest)
7+
<!--/@-->
8+
9+
## Installation
10+
11+
```sh
12+
<npm|yarn|pnpm> add @pnpm/write-importer-manifest
13+
```
14+
15+
## Usage
16+
17+
```ts
18+
import writeImporterManifest from '@pnpm/write-importer-manifest'
19+
import path = require('path')
20+
21+
(async () => await writeImporterManifest(path.resolve('package.yaml'), { name: 'foo', version: '1.0.0' }))()
22+
```
23+
24+
## License
25+
26+
[MIT](./LICENSE) © [Zoltan Kochan](https://www.kochan.io/)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "@pnpm/write-importer-manifest",
3+
"version": "0.0.0",
4+
"description": "Write an importer manifest (called package.json in most cases)",
5+
"main": "lib/index.js",
6+
"typings": "lib/index.d.ts",
7+
"engines": {
8+
"node": ">=8.15"
9+
},
10+
"files": [
11+
"lib"
12+
],
13+
"scripts": {
14+
"lint": "tslint -c tslint.json src/**/*.ts test/**/*.ts",
15+
"test": "npm run lint && npm run tsc && ts-node test --type-check",
16+
"prepublishOnly": "tsc",
17+
"tsc": "tsc",
18+
"md": "mos"
19+
},
20+
"repository": "https://github.com/pnpm/pnpm/blob/master/packages/write-importer-manifest",
21+
"keywords": [
22+
"pnpm",
23+
"outdated"
24+
],
25+
"author": {
26+
"name": "Zoltan Kochan",
27+
"email": "[email protected]",
28+
"url": "https://www.kochan.io/",
29+
"twitter": "ZoltanKochan"
30+
},
31+
"license": "MIT",
32+
"bugs": {
33+
"url": "https://github.com/pnpm/pnpm/issues"
34+
},
35+
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/write-importer-manifest#readme",
36+
"dependencies": {
37+
"@pnpm/types": "^3.0.0",
38+
"write-json-file": "3.2.0",
39+
"write-json5-file": "2.0.0",
40+
"write-yaml-file": "2.0.0"
41+
},
42+
"devDependencies": {
43+
"@pnpm/tslint-config": "0.0.0",
44+
"@pnpm/write-importer-manifest": "link:",
45+
"@types/node": "^10.3.2",
46+
"@types/tape": "^4.2.31",
47+
"mos": "^2.0.0-alpha.3",
48+
"mos-plugin-readme": "^1.0.4",
49+
"tape": "^4.8.0",
50+
"tempy": "0.3.0",
51+
"ts-node": "8.1.0",
52+
"tslint": "^5.8.0",
53+
"typescript": "3.4.5"
54+
},
55+
"mos": {
56+
"plugins": [
57+
"readme"
58+
],
59+
"installation": {
60+
"useShortAlias": true
61+
}
62+
}
63+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ImporterManifest } from '@pnpm/types'
2+
import writeJsonFile = require('write-json-file')
3+
import writeJson5File = require('write-json5-file')
4+
import writeYamlFile = require('write-yaml-file')
5+
6+
export default function writeImporterManifest (filePath: string, manifest: ImporterManifest): Promise<void> {
7+
switch (filePath.substr(filePath.lastIndexOf('.') + 1).toLowerCase()) {
8+
case 'json5':
9+
return writeJson5File(filePath, manifest)
10+
case 'yaml':
11+
return writeYamlFile(filePath, manifest)
12+
case 'json':
13+
default:
14+
return writeJsonFile(filePath, manifest)
15+
}
16+
}

0 commit comments

Comments
 (0)