Skip to content

Commit

Permalink
Merge pull request #59 from yowainwright/add-glob-codependencies
Browse files Browse the repository at this point in the history
feat: adds script to glob match updates
  • Loading branch information
yowainwright committed Oct 26, 2022
2 parents 047a3a4 + 55edeb3 commit 6a0248b
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 213 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ A **required** option or \*config array! **Codependencies** are required via bei
#### \*Config Array Detail
The Codependence `codependencies` array supports `latest` like so, `["fs-extra", "lodash"]`. It will also match a specified version, like so `[{ "foo": "1.0.0" }]` and `[{ "foo": "^1.0.0" }]` or `[{ "foo": "~1.0.0" }]`.
The Codependence `codependencies` array supports `latest` out-of-the-box. So having this `["fs-extra", "lodash"]` will return the `latest` versions of the packages within the array.
It will also match a specified version, like so `[{ "foo": "1.0.0" }]` and `[{ "foo": "^1.0.0" }]` or `[{ "foo": "~1.0.0" }]`.
You can also include a `*` **at the end** of a name you would like to match. For example, `@foo/*` will match all packages with `@foo/` in the name and return their latest versions. This will also work with `foo-*`, etc.
**Codependence** is built in to give you more capability to control your dependencies!
---
Expand Down Expand Up @@ -248,7 +253,12 @@ Starting out, you may not want a config object. Have no fear, **Codependence** c
```sh
codependence --codependencies 'lodash' '{ \"fs-extra\": \"10.0.1\" }'
```
### Want to grab all dependencies which match a `<name>*` (name star) pattern to return the latest version of them? Sure!
```sh
codependence --codependencies '@foo/*' --update
```
## Demos
Expand Down
32 changes: 30 additions & 2 deletions src/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { sync as glob } from 'fast-glob'
import { readFileSync, writeFileSync } from 'fs-extra'
import {
CheckFiles,
CodeDependencies,
CodeDependenciesItem,
ConstructVersionMapOptions,
CheckMatches,
CheckDependenciesForVersionOptions,
Expand Down Expand Up @@ -180,6 +182,30 @@ export const constructDepsToUpdateList = (
}))
}

/**
* constructDepsToUpdateList
* @description returns an array of codependencies including globs
* @param {codependencies} array
* @param {opts} object
* @returns {array} codependencies
*/
export const constructCodependenciesList = (codependencies: CodeDependencies, files: Array<string>, rootDir: string) =>
codependencies.reduce((acc: CodeDependencies, dep: CodeDependenciesItem): CodeDependencies => {
// returns all packages which match a <name>* pattern
if (typeof dep === 'string' && dep.includes('*')) {
const codependentsGroupName = dep.split('*')[0]
const codependentsGroup = files.reduce((acc = [], file: string) => {
const path = `${rootDir}${file}`
const packageJson = readFileSync(path, 'utf8')
const { dependencies = {}, devDependencies = {} } = JSON.parse(packageJson) as unknown as PackageJSON
const deps = { ...dependencies, ...devDependencies }
return [...acc, ...Object.keys(deps).filter((dep) => dep.includes(codependentsGroupName))]
}, [] as Array<string>)
return [...acc, ...codependentsGroup]
}
return [...acc, dep]
}, [])

/**
* writeConsoleMsgs
* @param {packageName} string
Expand Down Expand Up @@ -403,10 +429,12 @@ export const checkFiles = async ({
isTesting = false,
}: CheckFiles): Promise<void> => {
try {
const files = glob(matchers, { cwd: rootDir, ignore })
const globOpts = { cwd: rootDir, ignore }
const files = glob(matchers, globOpts)
if (!codependencies) throw '"codependencies" are required'
const codependenciesList = constructCodependenciesList(codependencies, files, rootDir)
const versionMap = await constructVersionMap({
codependencies,
codependencies: codependenciesList,
exec: execPromise,
debug,
yarnConfig,
Expand Down

0 comments on commit 6a0248b

Please sign in to comment.