Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"execa": "^4.0.2",
"fs-extra": "^9.1.0",
"json-stable-stringify": "^1.0.1",
"jsonc-parser": "3.0.0",
"lodash.mapvalues": "^4.6.0",
"pkg-dir": "^4.2.0",
"prettier": "^2.2.1",
Expand Down
69 changes: 41 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ import pkgDir from 'pkg-dir'
import path from 'path'
import prettier from 'prettier'
import stringify from 'json-stable-stringify'
import {parse as jsonParse} from 'jsonc-parser';

const argv = yargs
.option('dirPrefix', {
alias: 'd',
description: 'If specified, this tool will only look at packages under this directory prefix path.',
type: 'string'
})
.command(
'check',
'Check that the tsconfig file project references are synced with dependencies.',
(v: any) => v,
async () => {
await run({ mode: 'check' })
},
)
.command(
'write',
'Write the dependencies to tsconfig file project references.',
(v: any) => v,
async () => {
await run({ mode: 'write' })
},
)
.parse()


interface WorkSpaceInfo {
[key: string]: {
Expand All @@ -29,7 +55,7 @@ const stringifyTSConfig = async (
})
}

const run = async ({ mode }: { mode: 'check' | 'write' }) => {
async function run({ mode }: { mode: 'check' | 'write' }) {
const root = await pkgDir(process.cwd())
if (!root) {
throw new Error('Could not find workspace root.')
Expand All @@ -42,7 +68,9 @@ const run = async ({ mode }: { mode: 'check' | 'write' }) => {
'--json',
])
const workspaceInfo: WorkSpaceInfo = JSON.parse(raw)
const packageNames = Object.keys(workspaceInfo)
const packageNames = Object.entries(workspaceInfo)
.filter(([_, packageInfo]) => !argv.dirPrefix || packageInfo.location.startsWith(argv.dirPrefix))
.map(([packageName]) => packageName);

const getPackageInfo = async (name: string) => {
const info = workspaceInfo[name]
Expand Down Expand Up @@ -78,7 +106,13 @@ const run = async ({ mode }: { mode: 'check' | 'write' }) => {
const tsConfigString = await fs.readFile(tsConfigPath, {
encoding: 'utf8',
})
const tsConfig = JSON.parse(tsConfigString)
let tsConfig;
try {
tsConfig = jsonParse(tsConfigString)
} catch (e) {
console.log('Could not parse tsconfig file:', tsConfigPath);
throw e;
}
const tsConfigTarget = {
...tsConfig,
references: info.workspaceDependencies
Expand Down Expand Up @@ -120,7 +154,7 @@ const run = async ({ mode }: { mode: 'check' | 'write' }) => {
await Promise.all(
packageNames.map(async (name) => {
const i = await processPackage(name)
infoAboutPackages.push(i)
infoAboutPackages.push({name, ...i})
}),
)

Expand All @@ -144,12 +178,10 @@ const run = async ({ mode }: { mode: 'check' | 'write' }) => {
rootTSConfigString === rootTSConfigTargetString

if (mode === 'check') {
if (
infoAboutPackages.some((v) => v.wasOutOfSync) ||
!rootTSConfigMatchesTarget
) {
const outOfSyncPackages = infoAboutPackages.filter((v) => v.wasOutOfSync);
if (outOfSyncPackages.length || !rootTSConfigMatchesTarget) {
console.error(
'Project references are not in sync with dependencies.\nYou can run "yarn yarn-workspaces-to-typescript-project-references write" to fix them.',
`Project references are not in sync with dependencies:\n${outOfSyncPackages.map(pkg => `\t* ${pkg.name}`).join('\n')}\nYou can run "yarn yarn-workspaces-to-typescript-project-references write" to fix them.`,
)
process.exit(1)
}
Expand All @@ -167,22 +199,3 @@ const run = async ({ mode }: { mode: 'check' | 'write' }) => {
}
}
}

yargs
.command(
'check',
'Check that the tsconfig file project references are synced with dependencies.',
(v: any) => v,
async () => {
await run({ mode: 'check' })
},
)
.command(
'write',
'Write the dependencies to tsconfig file project references.',
(v: any) => v,
async () => {
await run({ mode: 'write' })
},
)
.parse()
Loading