wdepres 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -0
- package/cli.mjs +31 -0
- package/index.mjs +73 -0
- package/package.json +45 -0
- package/types.d.ts +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# `wdepres` - Workspace Dependency Resolver
|
|
2
|
+
|
|
3
|
+
This small package has just one purpose: resolve inter-project dependencies between workspace projects
|
|
4
|
+
|
|
5
|
+
I have tried really hard to find some documentation as to how to do it directly with NPM but there is nothing. While writing the [mrdr](https://npmjs.com/package/mrdr) I was in dire need to get the tree of dependent projects so that `mrdr` knows which projects to run first.
|
|
6
|
+
|
|
7
|
+
Since that capability was not present I created one and now it is available to you too, if you need it.
|
|
8
|
+
|
|
9
|
+
## Command-line interface
|
|
10
|
+
|
|
11
|
+
The application can be started directly using npx:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
Usage: npx wdepres@latest [options] [path]
|
|
15
|
+
|
|
16
|
+
Arguments:
|
|
17
|
+
path Path to workspace root (default: ".")
|
|
18
|
+
|
|
19
|
+
Options:
|
|
20
|
+
-p, --project <name> Optional project name to resolve dependencies for
|
|
21
|
+
-w, --workspace <name> Optional workspace path to resolve dependencies for
|
|
22
|
+
-h, --help display help for command
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## API
|
|
26
|
+
|
|
27
|
+
There is one function exported from this library, called `deps`. It takes as an argument path to the root of a workspace and produces an array of resolved workspaces.
|
|
28
|
+
|
|
29
|
+
The returned list of objects contain:
|
|
30
|
+
|
|
31
|
+
- `path: string` - path to the workspace package
|
|
32
|
+
- `filename: string` - filename (including `path`) to `package.json` of the workspace package
|
|
33
|
+
- `package: Workspace` - a workspace package object containing all of the contents of package's `package.json` plus `workspaceDependencies` - a concatenated list of all `*Dependencies` keys resolved to a Workspace object
|
package/cli.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { join, resolve } from 'node:path'
|
|
4
|
+
import { program } from 'commander'
|
|
5
|
+
import printTree from 'print-tree'
|
|
6
|
+
import { deps } from './index.mjs'
|
|
7
|
+
|
|
8
|
+
async function main(path, { project, workspace }) {
|
|
9
|
+
const result = await deps(path)
|
|
10
|
+
|
|
11
|
+
const root = (
|
|
12
|
+
project
|
|
13
|
+
? [result.find(prj => prj.package.name === project)]
|
|
14
|
+
: workspace
|
|
15
|
+
? [result.find(prj => resolve(prj.path) === resolve(join(path, workspace)))]
|
|
16
|
+
: result
|
|
17
|
+
).filter(x => x)
|
|
18
|
+
|
|
19
|
+
printTree(
|
|
20
|
+
{ package: { name: path, workspaceDependencies: root } },
|
|
21
|
+
node => node?.package?.name + (node?.package?.version ? `@${node?.package?.version}` : '') || '',
|
|
22
|
+
node => node?.package?.workspaceDependencies || [],
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
program
|
|
27
|
+
.argument('[path]', 'Path to workspace root', '.')
|
|
28
|
+
.option('-p, --project <name>', 'Optional project name to resolve dependencies for')
|
|
29
|
+
.option('-w, --workspace <name>', 'Optional workspace path to resolve dependencies for')
|
|
30
|
+
.action(main)
|
|
31
|
+
.parse(process.argv)
|
package/index.mjs
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { readFile } from 'node:fs/promises'
|
|
4
|
+
import { existsSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { glob } from 'glob'
|
|
7
|
+
|
|
8
|
+
async function parse(source) {
|
|
9
|
+
if (typeof source === 'string') {
|
|
10
|
+
return JSON.parse(source)
|
|
11
|
+
} else if (source instanceof Buffer) {
|
|
12
|
+
return JSON.parse(source.toString())
|
|
13
|
+
} else {
|
|
14
|
+
throw new Error('Unknown type of source')
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function read(filename) {
|
|
19
|
+
return parse(await readFile(filename))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function resolve(dependencies, packages) {
|
|
23
|
+
return dependencies.map(dependency => packages.find(pkg => pkg.package.name === dependency) || dependency)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function deps(workspace) {
|
|
27
|
+
const root = await read(join(workspace, 'package.json'))
|
|
28
|
+
|
|
29
|
+
const workspaces = (await Promise.all((root.workspaces || [])
|
|
30
|
+
.flatMap(pattern => glob(pattern, { cwd: workspace }))))
|
|
31
|
+
.flat()
|
|
32
|
+
.map(path => join(workspace, path)
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
const packages = await Promise.all(workspaces
|
|
36
|
+
.map(workspace => ({
|
|
37
|
+
path: workspace,
|
|
38
|
+
filename: join(workspace, 'package.json'),
|
|
39
|
+
}))
|
|
40
|
+
.filter(({ path, filename }) => existsSync(filename))
|
|
41
|
+
.map(async ({ path, filename }) => ({
|
|
42
|
+
path,
|
|
43
|
+
filename,
|
|
44
|
+
package: await read(filename),
|
|
45
|
+
}))
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
const isResolved = entry => typeof entry !== 'string'
|
|
49
|
+
|
|
50
|
+
return [...packages]
|
|
51
|
+
.map((workspace, idx, list) => {
|
|
52
|
+
workspace.package.dependencies = resolve(Object.keys(workspace.package.dependencies || {}), list)
|
|
53
|
+
workspace.package.devDependencies = resolve(Object.keys(workspace.package.devDependencies || {}), list)
|
|
54
|
+
workspace.package.peerDependencies = resolve(Object.keys(workspace.package.peerDependencies || {}), list)
|
|
55
|
+
workspace.package.bundledDependencies = resolve(Object.keys(workspace.package.bundledDependencies || {}), list)
|
|
56
|
+
workspace.package.optionalDependencies = resolve(Object.keys(workspace.package.optionalDependencies || {}), list)
|
|
57
|
+
workspace.package.extensionDependencies = resolve(Object.keys(workspace.package.extensionDependencies || {}), list)
|
|
58
|
+
|
|
59
|
+
return workspace
|
|
60
|
+
})
|
|
61
|
+
.map(workspace => {
|
|
62
|
+
workspace.package.workspaceDependencies = [
|
|
63
|
+
...workspace.package.dependencies,
|
|
64
|
+
...workspace.package.devDependencies,
|
|
65
|
+
...workspace.package.peerDependencies,
|
|
66
|
+
...workspace.package.bundledDependencies,
|
|
67
|
+
...workspace.package.optionalDependencies,
|
|
68
|
+
...workspace.package.extensionDependencies,
|
|
69
|
+
].filter(isResolved)
|
|
70
|
+
|
|
71
|
+
return workspace
|
|
72
|
+
})
|
|
73
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wdepres",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"author": "Matthias Hryniszak <padcom@gmail.com>",
|
|
5
|
+
"description": "NPM Workspace dependency resolver",
|
|
6
|
+
"keywords": ["npm", "workspaces", "monorepo"],
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"files": [
|
|
10
|
+
"index.mjs",
|
|
11
|
+
"cli.mjs",
|
|
12
|
+
"types.d.ts",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"main": "index.mjs",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./index.mjs",
|
|
19
|
+
"types": "./types.d.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/padcom/wdepres"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/padcom/mrdr/wdepres"
|
|
28
|
+
},
|
|
29
|
+
"funding": {
|
|
30
|
+
"type": "patreon",
|
|
31
|
+
"url": "https://www.patreon.com/padcom"
|
|
32
|
+
},
|
|
33
|
+
"bin": {
|
|
34
|
+
"wdepres": "cli.mjs"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "",
|
|
38
|
+
"prepublishOnly": "npm install && npm run build"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"commander": "^11.1.0",
|
|
42
|
+
"glob": "^10.3.10",
|
|
43
|
+
"print-tree": "^0.1.5"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Workspace definition */
|
|
2
|
+
export interface Workspace {
|
|
3
|
+
/** Path to workspace */
|
|
4
|
+
path: string
|
|
5
|
+
/** package.json location */
|
|
6
|
+
filename: string
|
|
7
|
+
/** package.json content */
|
|
8
|
+
package: {
|
|
9
|
+
/** Package name */
|
|
10
|
+
name: string
|
|
11
|
+
/** Package version */
|
|
12
|
+
version?: string
|
|
13
|
+
/** transitive dependencies */
|
|
14
|
+
dependencies: (Workspace | string)[]
|
|
15
|
+
/** dev dependencies */
|
|
16
|
+
devDependencies: (Workspace | string)[]
|
|
17
|
+
/** peer dependencies */
|
|
18
|
+
peerDependencies: (Workspace | string)[]
|
|
19
|
+
/** bundled dependencies */
|
|
20
|
+
bundledDependencies: (Workspace | string)[]
|
|
21
|
+
/** optional dependencies */
|
|
22
|
+
optionalDependencies: (Workspace | string)[]
|
|
23
|
+
/** extension dependencies */
|
|
24
|
+
extensionDependencies: (Workspace | string)[]
|
|
25
|
+
/** workspace dependencies */
|
|
26
|
+
workspaceDependencies: Workspace[]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Resolve dependencies in workspace
|
|
32
|
+
*
|
|
33
|
+
* @param {String} workspace path to workspace
|
|
34
|
+
* @returns {Promise<Workspace[]>} list of trees or tree (if pkg was specified) of resolved packages
|
|
35
|
+
*/
|
|
36
|
+
export function deps(workspace: string): Promise<Workspace[]>
|