Skip to content

Commit

Permalink
feat: support package.yaml and package.json5
Browse files Browse the repository at this point in the history
PR #1799
close #1100
  • Loading branch information
zkochan authored May 1, 2019
1 parent 82f9176 commit a955f71
Show file tree
Hide file tree
Showing 71 changed files with 1,745 additions and 147 deletions.
21 changes: 21 additions & 0 deletions packages/find-packages/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017-2019 Zoltan Kochan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 51 additions & 0 deletions packages/find-packages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# find-packages

> Find all packages inside a directory
<!--@shields('npm')-->
[![npm version](https://img.shields.io/npm/v/find-packages.svg)](https://www.npmjs.com/package/find-packages)
<!--/@-->

## Installation

```sh
<npm|yarn|pnpm> add find-packages
```

## Usage

```js
const path = require('path')
const findPkgs = require('find-packages')

findPkgs(path.join(__dirname, 'test', 'fixture'))
.then(pkgs => console.log(pkgs))
.catch(err => console.error(err))
//> [ { path: '/home/zkochan/src/find-packages/test/fixture/pkg',
// manifest: { name: 'foo', version: '1.0.0' },
// writeImporterManifest: [AsyncFunction] } ]
```

## API

### `findPackages(dir, [opts])`

#### `dir`

The directory in which to search for packages.

#### `opts`

Parameters normally passed to [glob](https://www.npmjs.com/package/glob)

#### `opts.patterns`

Array of globs to use as package locations. For example: `['packages/**', 'utils/**']`.

#### `opts.ignore`

Patterns to ignore when searching for packages. By default: `**/node_modules/**`, `**/bower_components/**`, `**/test/**`, `**/tests/**`.

## License

[MIT](./LICENSE) © [Zoltan Kochan](https://www.kochan.io)
6 changes: 6 additions & 0 deletions packages/find-packages/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const path = require('path')
const findPkgs = require('find-packages')

findPkgs(path.join(__dirname, 'test/fixtures/one-pkg'))
.then(pkgs => console.log(pkgs))
.catch(err => console.error(err))
60 changes: 60 additions & 0 deletions packages/find-packages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "find-packages",
"version": "4.0.1",
"description": "Find all packages inside a directory",
"main": "lib/index.js",
"files": [
"lib"
],
"typings": "lib/index.d.ts",
"scripts": {
"lint": "tslint -c tslint.json src/**/*.ts test/**/*.ts",
"test": "npm run tsc && npm run lint && ts-node test --type-check",
"tsc": "tsc",
"prepublishOnly": "npm run tsc",
"md": "mos"
},
"repository": "https://github.com/pnpm/pnpm/blob/master/packages/find-packages",
"keywords": [
"find",
"package"
],
"mos": {
"plugins": [
"readme"
],
"installation": {
"useShortAlias": true
}
},
"author": {
"name": "Zoltan Kochan",
"email": "z@kochan.io",
"url": "https://www.kochan.io"
},
"engines": {
"node": ">=8.15"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/pnpm/pnpm/issues"
},
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/find-packages#readme",
"devDependencies": {
"@pnpm/tslint-config": "0.0.0",
"@types/tape": "^4.2.29",
"find-packages": "link:",
"mos": "^2.0.0-alpha.3",
"mos-plugin-readme": "^1.0.4",
"tape": "^4.6.3",
"ts-node": "^8.0.1",
"tslint": "^5.0.0",
"typescript": "^3.0.0"
},
"dependencies": {
"@pnpm/read-importer-manifest": "0.0.0",
"@types/node": "^10.12.18",
"fast-glob": "^2.0.4",
"p-filter": "^2.0.0"
}
}
57 changes: 57 additions & 0 deletions packages/find-packages/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { readExactImporterManifest } from '@pnpm/read-importer-manifest'
import fastGlob = require('fast-glob')
import pFilter = require('p-filter')
import path = require('path')

const DEFAULT_IGNORE = [
'**/node_modules/**',
'**/bower_components/**',
'**/test/**',
'**/tests/**',
]

async function findPkgs (
root: string,
opts?: {
ignore?: string[],
patterns?: string[],
},
) {
opts = opts || {}
const globOpts = { ...opts, cwd: root }
globOpts.ignore = opts.ignore || DEFAULT_IGNORE
globOpts.patterns = opts.patterns
? normalizePatterns(opts.patterns)
: ['**/package.{json,yaml,json5}']

const paths: string[] = await fastGlob(globOpts.patterns, globOpts)

return pFilter(
paths
.sort()
.map((manifestPath) => path.join(root, manifestPath))
.map(async (manifestPath) => {
try {
return {
path: path.dirname(manifestPath),
...await readExactImporterManifest(manifestPath),
}
} catch (err) {
if (err.code === 'ENOENT') {
return null
}
throw err
}
}),
Boolean,
)
}

function normalizePatterns (patterns: string[]) {
return patterns.map((pattern) => pattern.replace(/\/?$/, '/package.{json,yaml,json5}'))
}

// for backward compatibility
findPkgs['default'] = findPkgs // tslint:disable-line

export = findPkgs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: component-1
version: 1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
name: 'component-2',
version: '1.0.0',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "component-1",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "component-2",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo",
"version": "1.0.0"
}
4 changes: 4 additions & 0 deletions packages/find-packages/test/fixtures/one-pkg/pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo",
"version": "1.0.0"
}
55 changes: 55 additions & 0 deletions packages/find-packages/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import findPackages from 'find-packages'
import path = require('path')
import test = require('tape')

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

test('finds package', async t => {
const root = path.join(fixtures, 'one-pkg')
const pkgs = await findPackages(root)

t.equal(pkgs.length, 1)
t.ok(pkgs[0].path)
t.ok(pkgs[0].manifest)
t.end()
})

test('finds packages by patterns', async t => {
const root = path.join(fixtures, 'many-pkgs')
const pkgs = await findPackages(root, { patterns: ['components/**'] })

t.equal(pkgs.length, 2)
t.ok(pkgs[0].path)
t.ok(pkgs[0].manifest)
t.ok(pkgs[1].path)
t.ok(pkgs[1].manifest)
t.deepEqual([pkgs[0].manifest.name, pkgs[1].manifest.name].sort(), ['component-1', 'component-2'])
t.end()
})

test('ignore packages by patterns', async t => {
const root = path.join(fixtures, 'many-pkgs')
const pkgs = await findPackages(root, { patterns: ['**', '!libs/**'] })

t.equal(pkgs.length, 2)
t.ok(pkgs[0].path)
t.ok(pkgs[0].manifest)
t.ok(pkgs[1].path)
t.ok(pkgs[1].manifest)
t.deepEqual([pkgs[0].manifest.name, pkgs[1].manifest.name].sort(), ['component-1', 'component-2'])
t.end()
})

test('json and yaml manifests are also found', async t => {
const root = path.join(fixtures, 'many-pkgs-with-different-manifest-types')
const pkgs = await findPackages(root)

t.equal(pkgs.length, 3)
t.ok(pkgs[0].path)
t.equal(pkgs[0].manifest.name, 'component-1')
t.ok(pkgs[1].path)
t.equal(pkgs[1].manifest.name, 'component-2')
t.ok(pkgs[2].path)
t.equal(pkgs[2].manifest.name, 'foo')
t.end()
})
10 changes: 10 additions & 0 deletions packages/find-packages/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../utils/tsconfig.json",
"compilerOptions": {
"outDir": "lib"
},
"include": [
"src/**/*.ts",
"typings/**/*.d.ts"
]
}
3 changes: 3 additions & 0 deletions packages/find-packages/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@pnpm/tslint-config"
}
14 changes: 14 additions & 0 deletions packages/find-packages/typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare module 'fast-glob' {
const anything: any;
export = anything;
}

declare module 'read-pkg' {
const anything: any;
export = anything;
}

declare module 'p-filter' {
const anything: any;
export = anything;
}
1 change: 1 addition & 0 deletions packages/list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/list#readme",
"dependencies": {
"@pnpm/read-importer-manifest": "0.0.0",
"@pnpm/read-package-json": "2.0.1",
"@pnpm/types": "3.2.0",
"@types/archy": "0.0.31",
Expand Down
7 changes: 3 additions & 4 deletions packages/list/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { readImporterManifestOnly } from '@pnpm/read-importer-manifest'
import { Registries } from '@pnpm/types'
import npa = require('@zkochan/npm-package-arg')
import dh, {
forPackages as dhForPackages,
PackageSelector,
} from 'dependencies-hierarchy'
import path = require('path')
import readPkg from './readPkg'
import renderParseable from './renderParseable'
import renderTree from './renderTree'

Expand Down Expand Up @@ -55,7 +54,7 @@ export async function forPackages (
})

const print = getPrinter(opts.parseable)
const entryPkg = await readPkg(path.resolve(projectPath, 'package.json'))
const entryPkg = await readImporterManifestOnly(projectPath)
return print({
name: entryPkg.name,
path: projectPath,
Expand Down Expand Up @@ -90,7 +89,7 @@ export default async function (
})

const print = getPrinter(opts.parseable)
const entryPkg = await readPkg(path.resolve(projectPath, 'package.json'))
const entryPkg = await readImporterManifestOnly(projectPath)
return print({
name: entryPkg.name,
path: projectPath,
Expand Down
4 changes: 2 additions & 2 deletions packages/list/src/renderParseable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const sortPackages = R.sortBy(R.prop('name'))

export default async function (
project: {
name: string,
version: string,
name?: string,
version?: string,
path: string,
},
tree: PackageNode[],
Expand Down
4 changes: 2 additions & 2 deletions packages/list/src/renderTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const sortPackages = R.sortBy(R.path(['pkg', 'name']) as (pkg: object) => R.Ord)

export default async function (
project: {
name: string,
version: string,
name?: string,
version?: string,
path: string,
},
tree: PackageNode[],
Expand Down
2 changes: 1 addition & 1 deletion packages/local-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/local-resolver#readme",
"dependencies": {
"@pnpm/read-package-json": "2.0.1",
"@pnpm/read-importer-manifest": "0.0.0",
"@pnpm/resolver-base": "3.1.2",
"@pnpm/types": "3.2.0",
"@types/graceful-fs": "4.1.3",
Expand Down
Loading

0 comments on commit a955f71

Please sign in to comment.