Skip to content

Commit

Permalink
feat(js): change dependsOn, DependsOnOptions, rootDir handling
Browse files Browse the repository at this point in the history
- **BREAKING CHANGE**: `dependsOn` and `DependsOnOptions` is changed.
- **BREAKING CHANGE**: For every functions, when the argument or an option `rootDir` is given as relative path, `getConfigDirname()` of `@haetae/core` is joined with the `rootDir`.
  • Loading branch information
jjangga0214 committed Aug 23, 2022
1 parent 433e05d commit 8d2a79d
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .changeset/javascript-0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
'@haetae/utils': patch
'@haetae/javascript': patch
---

**BREAKING CHANGE**: `RootDirOption` is removed in favor of `VersionOptions`.
5 changes: 5 additions & 0 deletions .changeset/javascript-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@haetae/javascript': patch
---

**BREAKING CHANGE**: `dependsOn` and `DependsOnOptions` is changed.
5 changes: 5 additions & 0 deletions .changeset/javascript-rootdir-joined-with-configdirname.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@haetae/javascript': patch
---

**BREAKING CHANGE**: For every functions, when the argument or an option `rootDir` is given as relative path, `getConfigDirname()` of `@haetae/core` is joined with the `rootDir`.
3 changes: 3 additions & 0 deletions packages/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
"@haetae/utils": "workspace:~"
},
"dependencies": {
"@haetae/common": "workspace:~",
"dependency-tree": "^8.1.1",
"find-up": "^5",
"memoizee": "^0.4.15",
"read-pkg": "^5.2.0",
"read-pkg-up": "^7.0.1",
"serialize-javascript": "^6.0.0",
"semver": "^7.3.7",
"upath": "^2.0.1",
"yaml": "^2.1.1"
Expand Down
89 changes: 52 additions & 37 deletions packages/javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,68 @@ import fs from 'fs'
import upath from 'upath'
import dependencyTree from 'dependency-tree'
import { getConfigDirname } from '@haetae/core'
import { DepsGraph, graph } from '@haetae/utils'
import { DepsGraph, graph, dependsOn as graphDependsOn } from '@haetae/utils'
import { parsePkg, toAbsolutePath } from '@haetae/common'

export const pkg = parsePkg({ name: '@haetae/javascript', rootDir: __dirname })

export { default as pkg } from './pkg'
export { VersionOptions, version } from './version'

export interface DependsOnOptions {
rootDir?: string
dependent: string
dependencies: readonly string[] | Set<string>
tsConfig?: string
additionalGraph?: DepsGraph // you can manually specify additional dependency graph
rootDir?: string
additionalGraph?: DepsGraph
}

export function dependsOn(
dependencyCandidates: readonly string[],
{
tsConfig,
rootDir = getConfigDirname(),
additionalGraph = graph({ edges: [], rootDir }),
}: DependsOnOptions = {},
): (target: string) => boolean {
// default option.tsConfig if exists
if (fs.existsSync(upath.join(rootDir, 'tsconfig.json'))) {
export function dependsOn({
dependent,
dependencies,
tsConfig,
rootDir = getConfigDirname(),
additionalGraph = graph({ edges: [], rootDir }),
}: DependsOnOptions): boolean {
// eslint-disable-next-line no-param-reassign
rootDir = toAbsolutePath({ path: rootDir, rootDir: getConfigDirname() })
if (tsConfig) {
// eslint-disable-next-line no-param-reassign
tsConfig = toAbsolutePath({ path: tsConfig, rootDir })
} else if (fs.existsSync(upath.join(rootDir, 'tsconfig.json'))) {
// eslint-disable-next-line no-param-reassign
tsConfig = tsConfig || upath.join(rootDir, 'tsconfig.json')
tsConfig = upath.join(rootDir, 'tsconfig.json')
}
// eslint-disable-next-line @typescript-eslint/naming-convention
const _dependencyCandidates = dependencyCandidates
.map((dc) => (upath.isAbsolute(dc) ? dc : upath.join(rootDir, dc)))
.map((dc) => upath.normalize(dc))

return (dependentCandidate: string): boolean => {
// This includes target file itself as well.
const deepDepsList = dependencyTree.toList({
directory: rootDir,
filename: dependentCandidate,
tsConfig,

// eslint-disable-next-line no-param-reassign
dependencies = [...dependencies].map((d) =>
toAbsolutePath({ path: d, rootDir }),
)

// eslint-disable-next-line no-param-reassign
dependent = toAbsolutePath({ path: dependent, rootDir })

if (
graphDependsOn({
dependent,
dependencies,
graph: additionalGraph,
rootDir,
})
console.log(deepDepsList)
for (const dependencyCandidate of _dependencyCandidates) {
if (
deepDepsList.includes(dependencyCandidate) ||
// TODO: deep graph search
additionalGraph[dependentCandidate]?.has(dependencyCandidate) ||
dependentCandidate === dependencyCandidate
) {
return true
}
) {
return true
}

// This includes target file (dependent) itself as well.
const deepDepsList = dependencyTree.toList({
directory: rootDir,
filename: dependent,
tsConfig,
})

for (const dependency of dependencies) {
if (dependent === dependency || deepDepsList.includes(dependency)) {
return true
}
return false
}
return false
}
23 changes: 0 additions & 23 deletions packages/javascript/src/pkg.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/javascript/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"include": ["src"],
"references": [
{ "path": "../core/tsconfig.build.json" },
{ "path": "../utils/tsconfig.build.json" }
{ "path": "../utils/tsconfig.build.json" },
{ "path": "../common/tsconfig.build.json" }
]
}
3 changes: 2 additions & 1 deletion packages/javascript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../tsconfig.json",
"references": [
{ "path": "../core/tsconfig.build.json" },
{ "path": "../utils/tsconfig.build.json" }
{ "path": "../utils/tsconfig.build.json" },
{ "path": "../common/tsconfig.build.json" }
]
}

0 comments on commit 8d2a79d

Please sign in to comment.