Skip to content

Commit

Permalink
feat: add manifestRelPath to IPackageEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 25, 2023
1 parent cb2f9c2 commit 6949ae0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/main/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface IPackageEntry {
manifest: IPackageJson
manifestRaw: string
manifestPath: string
manifestRelPath: string
manifestAbsPath: string
path: string
absPath: string
relPath: string
Expand Down
37 changes: 13 additions & 24 deletions src/main/ts/topo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,7 @@ export const getPackages = async (
const { pkgFilter } = options
const manifestsPaths = await getManifestsPaths(options)
const entries = await Promise.all(
manifestsPaths.map(async manifestPath => {
const absPath = dirname(manifestPath)
const relPath = relative(options.cwd, absPath)
const manifestRaw = await fs.readFile(manifestPath, 'utf8')
const manifest = JSON.parse(manifestRaw)
return {
name: manifest.name,
manifestRaw,
manifestPath,
manifest,
path: relPath, // legacy
relPath,
absPath
}
})
manifestsPaths.map(manifestPath => getPackage(options.cwd, manifestPath))
)

checkDuplicates(entries)
Expand All @@ -66,19 +52,22 @@ const checkDuplicates = (named: { name: string }[]): void => {
}
}

export const getRootPackage = async (cwd: string): Promise<IPackageEntry> => {
const manifestPath = resolve(cwd, 'package.json')
export const getPackage = async (cwd: string, manifestPath: string): Promise<IPackageEntry> => {
const absPath = dirname(manifestPath)
const relPath = relative(cwd, absPath) || '.'
const manifestRelPath = relative(cwd, manifestPath)
const manifestRaw = await fs.readFile(manifestPath, 'utf8')
const manifest = JSON.parse(manifestRaw)

return {
name: manifest.name,
manifest,
manifestPath,
manifestRaw,
path: '/',
relPath: '/',
absPath: dirname(manifestPath)
manifestPath, // legacy
manifestRelPath,
manifestAbsPath: manifestPath,
manifest,
path: relPath, // legacy
relPath,
absPath
}
}

Expand All @@ -93,7 +82,7 @@ export const topo = async (
workspaces,
workspacesExtra = []
} = options
const root = await getRootPackage(cwd)
const root = await getPackage(cwd, resolve(cwd, 'package.json'))
const _options: ITopoOptionsNormalized = {
cwd,
filter,
Expand Down
34 changes: 28 additions & 6 deletions src/test/ts/topo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ test('`topo` returns monorepo digest: release queue, deps graph, package manifes
},
manifestRaw: '{\n "name": "a",\n "private": true\n}\n',
manifestPath: join(cwd, 'packages/a/package.json'),
manifestAbsPath: join(cwd, 'packages/a/package.json'),
manifestRelPath: 'packages/a/package.json',
path: 'packages/a',
relPath: 'packages/a',
absPath: resolve(cwd, 'packages/a')
Expand All @@ -70,6 +72,8 @@ test('`topo` returns monorepo digest: release queue, deps graph, package manifes
}
},
manifestPath: join(cwd, 'packages/c/package.json'),
manifestAbsPath: join(cwd, 'packages/c/package.json'),
manifestRelPath: 'packages/c/package.json',
manifestRaw: fs.readFileSync(join(cwd, 'packages/c/package.json'), {
encoding: 'utf8'
}),
Expand All @@ -83,6 +87,8 @@ test('`topo` returns monorepo digest: release queue, deps graph, package manifes
name: 'e'
},
manifestPath: join(cwd, 'packages/e/package.json'),
manifestAbsPath: join(cwd, 'packages/e/package.json'),
manifestRelPath: 'packages/e/package.json',
manifestRaw: fs.readFileSync(join(cwd, 'packages/e/package.json'), {
encoding: 'utf8'
}),
Expand All @@ -97,11 +103,13 @@ test('`topo` returns monorepo digest: release queue, deps graph, package manifes
name: 'root'
},
manifestPath: join(cwd, 'package.json'),
manifestAbsPath: join(cwd, 'package.json'),
manifestRelPath: 'package.json',
manifestRaw: fs.readFileSync(join(cwd, 'package.json'), {
encoding: 'utf8'
}),
path: '/',
relPath: '/',
path: '.',
relPath: '.',
absPath: resolve(cwd)
},
graphs: [
Expand Down Expand Up @@ -143,6 +151,8 @@ test('`topo` applies filter/pkgFilter', async () => {
}
},
manifestPath: join(cwd, 'packages/c/package.json'),
manifestAbsPath: join(cwd, 'packages/c/package.json'),
manifestRelPath: 'packages/c/package.json',
manifestRaw: fs.readFileSync(join(cwd, 'packages/c/package.json'), {
encoding: 'utf8'
}),
Expand All @@ -156,6 +166,8 @@ test('`topo` applies filter/pkgFilter', async () => {
name: 'e'
},
manifestPath: join(cwd, 'packages/e/package.json'),
manifestAbsPath: join(cwd, 'packages/e/package.json'),
manifestRelPath: 'packages/e/package.json',
manifestRaw: fs.readFileSync(join(cwd, 'packages/e/package.json'), {
encoding: 'utf8'
}),
Expand All @@ -170,11 +182,13 @@ test('`topo` applies filter/pkgFilter', async () => {
name: 'root'
},
manifestPath: join(cwd, 'package.json'),
manifestAbsPath: join(cwd, 'package.json'),
manifestRelPath: 'package.json',
manifestRaw: fs.readFileSync(join(cwd, 'package.json'), {
encoding: 'utf8'
}),
path: '/',
relPath: '/',
path: '.',
relPath: '.',
absPath: resolve(cwd)
},
graphs: [
Expand Down Expand Up @@ -209,6 +223,8 @@ test('`topo` applies depFilter', async () => {
private: true
},
manifestPath: join(cwd, 'packages/a/package.json'),
manifestAbsPath: join(cwd, 'packages/a/package.json'),
manifestRelPath: 'packages/a/package.json',
manifestRaw: fs.readFileSync(join(cwd, 'packages/a/package.json'), {
encoding: 'utf8'
}),
Expand All @@ -225,6 +241,8 @@ test('`topo` applies depFilter', async () => {
}
},
manifestPath: join(cwd, 'packages/c/package.json'),
manifestAbsPath: join(cwd, 'packages/c/package.json'),
manifestRelPath: 'packages/c/package.json',
manifestRaw: fs.readFileSync(join(cwd, 'packages/c/package.json'), {
encoding: 'utf8'
}),
Expand All @@ -238,6 +256,8 @@ test('`topo` applies depFilter', async () => {
name: 'e'
},
manifestPath: join(cwd, 'packages/e/package.json'),
manifestAbsPath: join(cwd, 'packages/e/package.json'),
manifestRelPath: 'packages/e/package.json',
manifestRaw: fs.readFileSync(join(cwd, 'packages/e/package.json'), {
encoding: 'utf8'
}),
Expand All @@ -252,11 +272,13 @@ test('`topo` applies depFilter', async () => {
name: 'root'
},
manifestPath: join(cwd, 'package.json'),
manifestAbsPath: join(cwd, 'package.json'),
manifestRelPath: 'package.json',
manifestRaw: fs.readFileSync(join(cwd, 'package.json'), {
encoding: 'utf8'
}),
path: '/',
relPath: '/',
path: '.',
relPath: '.',
absPath: resolve(cwd)
},
graphs: [
Expand Down

0 comments on commit 6949ae0

Please sign in to comment.