Skip to content

Commit

Permalink
close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
binjospookie committed Dec 30, 2023
1 parent 3ea2d8f commit 876154d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.0.40",
"description": "Utility for monorepos. It helps to find unused exports from packages or get a list of all unique uses of any package",
"main": "./src/index.js",
"main": "./src/api/index.js",
"bin": "./bin/index.js",
"author": "Viktor Pasynok",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'node:path'
import { expect, test } from 'vitest'

import { collectUsages } from '../../index.js'
import { collectUsages } from '../collectUsages.js'
import { Result } from '../../utils/index.js'

test.each([
Expand Down
37 changes: 37 additions & 0 deletions src/api/collectUsages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { collectUsages as _collectUsages } from '../collectUsages.js'
import { BASE_CONFIG } from '../getConfig.js'
import { Result } from '../utils/index.js'

// name and list[0].dir are required
const collectUsages = async (name, list) => {
const tasks = list.map(x =>
_collectUsages({
config: {
babelPlugins: x.babelPlugins || BASE_CONFIG.babelPlugins,
batch: x.batch || BASE_CONFIG.batch,
exclude: x.exclude
? new Set([...BASE_CONFIG.exclude, ...x.exclude])
: BASE_CONFIG.exclude,
extensions: x.extensions || BASE_CONFIG.extensions,
dir: x.dir || BASE_CONFIG.dir,
collectUsages: name
}
})
)

const result = await Promise.all(tasks)

const mergedUsages = result.reduce((acc, x) => {
if (x.success) {
acc = acc.concat([...x.value.usages])
}

return acc
}, [])

return mergedUsages.length === 0
? Result.Err({ usages: new Set() })
: Result.Ok({ usages: new Set(mergedUsages) })
}

export { collectUsages }
43 changes: 5 additions & 38 deletions src/index.js → src/api/findUnusedExports.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { baseFlow } from './baseFlow.js'
import { collectUsages as _collectUsages } from './collectUsages.js'
import { BASE_CONFIG } from './getConfig.js'
import { readJSON, Result } from './utils/index.js'
import { baseFlow } from '../baseFlow.js'
import { BASE_CONFIG } from '../getConfig.js'
import { readJSON, Result } from '../utils/index.js'

const check = async (entry, list) => {
const findUnusedExports = async (entry, list) => {
const { name } = await readJSON('package.json')
const pkg = { name, path: entry }

Expand Down Expand Up @@ -62,36 +61,4 @@ const check = async (entry, list) => {
return Result.Err({ reason: 'unused_exports', exports: head })
}

// name and list[0].dir are required
const collectUsages = async (name, list) => {
const tasks = list.map(x =>
_collectUsages({
config: {
babelPlugins: x.babelPlugins || BASE_CONFIG.babelPlugins,
batch: x.batch || BASE_CONFIG.batch,
exclude: x.exclude
? new Set([...BASE_CONFIG.exclude, ...x.exclude])
: BASE_CONFIG.exclude,
extensions: x.extensions || BASE_CONFIG.extensions,
dir: x.dir || BASE_CONFIG.dir,
collectUsages: name
}
})
)

const result = await Promise.all(tasks)

const mergedUsages = result.reduce((acc, x) => {
if (x.success) {
acc = acc.concat([...x.value.usages])
}

return acc
}, [])

return mergedUsages.length === 0
? Result.Err({ usages: new Set() })
: Result.Ok({ usages: new Set(mergedUsages) })
}

export { check, collectUsages }
export { findUnusedExports }
2 changes: 2 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { collectUsages } from './collectUsages.js'
export { findUnusedExports } from './findUnusedExports.js'

0 comments on commit 876154d

Please sign in to comment.