Skip to content
Merged
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "update-ts-references",
"version": "5.0.0",
"version": "5.0.1",
"description": "Updates TypeScript references automatically while using workspaces",
"bin": "src/index.js",
"scripts": {
"lint": "eslint src tests",
"test": "scripts/prepareTests.sh && vitest run",
"vitest": "vitest run"
"test": "scripts/prepareTests.sh && vitest run --no-file-parallelism",
"vitest": "vitest run --no-file-parallelism"
},
"contributors": [
{
Expand Down
21 changes: 17 additions & 4 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ const getAllPackageJsons = async (workspaces, cwd) => {
});

return Promise.all(
workspaceGlobs.map((workspace) => fastGlob(`${workspace}/${PACKAGE_JSON}`, { cwd, onlyFiles: true }))
workspaceGlobs.map((workspace) =>
fastGlob(`${workspace}/${PACKAGE_JSON}`, { cwd, onlyFiles: true }).then((files) => files.sort())
)
)
.then((allPackages) =>
allPackages.reduce(
Expand Down Expand Up @@ -109,6 +111,16 @@ const getPackageNamesAndPackageDir = (packageFilePaths, cwd) =>
return map;
}, new Map());

const getDependencyNamesFromPackageJson = ({
dependencies = {},
peerDependencies = {},
devDependencies = {},
} = {}) => Object.keys({
...dependencies,
...peerDependencies,
...devDependencies,
});

const getReferencesFromDependencies = (
configName,
{ packageDir },
Expand Down Expand Up @@ -325,8 +337,8 @@ const execute = async ({

let tsconfigMap = {}
let jsconfigMap = {}
const referencesMap = {}
const referencedPackageNames = new Set()
const explicitRootReferenceNames = new Set(getDependencyNamesFromPackageJson(packageJson))
const rootReferenceCandidates = []
packagesMap.forEach((packageEntry, packageName) => {
const detectedConfig = detectTSConfig(packageEntry.packageDir, configName, packageEntry.hasTsEntry && createTsConfig, cwd)
Expand Down Expand Up @@ -379,7 +391,6 @@ const execute = async ({
packagesMap,
verbose
) || []).map(ensurePosixPathStyle);
referencesMap[packageName] = references
references.forEach(({ name }) => referencedPackageNames.add(name))

const paths = getPathsFromReferences(references, tsconfigMap, jsconfigMap, ignorePathMappings)
Expand All @@ -406,7 +417,9 @@ const execute = async ({
}
});

const rootReferences = rootReferenceCandidates.filter(({ name }) => !referencedPackageNames.has(name));
const rootReferences = rootReferenceCandidates.filter(({ name }) =>
explicitRootReferenceNames.has(name) || !referencedPackageNames.has(name)
);
const rootPaths = getPathsFromReferences(rootReferences, tsconfigMap, {}, ignorePathMappings)

if (verbose) {
Expand Down
12 changes: 12 additions & 0 deletions test-scenarios/prune-minimal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "prune-minimal-workspace",
"version": "0.0.1",
"private": true,
"workspaces": [
"workspace-a",
"workspace-b"
],
"devDependencies": {
"typescript": "latest"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/prune-minimal/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files": [],
"compilerOptions": {
"composite": true
}
}
7 changes: 7 additions & 0 deletions test-scenarios/prune-minimal/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-a",
"version": "1.0.0",
"dependencies": {
"workspace-b": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/prune-minimal/workspace-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
4 changes: 4 additions & 0 deletions test-scenarios/prune-minimal/workspace-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "workspace-b",
"version": "1.0.0"
}
6 changes: 6 additions & 0 deletions test-scenarios/prune-minimal/workspace-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
7 changes: 7 additions & 0 deletions test-scenarios/ts-paths/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
"shared/*",
"utils/**/"
],
"dependencies": {
"workspace-b": "1.0.0"
},
"devDependencies": {
"foo-a": "1.0.0",
"typescript": "latest"
},
"peerDependencies": {
"foo-b": "1.0.0"
}
}
7 changes: 7 additions & 0 deletions test-scenarios/yarn-ws-check-no-changes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
"shared/*",
"utils/**"
],
"dependencies": {
"workspace-b": "1.0.0"
},
"devDependencies": {
"foo-a": "1.0.0",
"typescript": "latest"
},
"peerDependencies": {
"workspace-c": "1.0.0"
}
}
9 changes: 9 additions & 0 deletions test-scenarios/yarn-ws-check-no-changes/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@
{
"path": "workspace-a"
},
{
"path": "workspace-b"
},
{
"path": "shared/workspace-c"
},
{
"path": "shared/workspace-d"
},
{
"path": "utils/foos/foo-a"
}
]
}
Expand Down
7 changes: 7 additions & 0 deletions test-scenarios/yarn-ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
"shared/*",
"utils/**"
],
"dependencies": {
"workspace-b": "1.0.0"
},
"devDependencies": {
"foo-a": "1.0.0",
"typescript": "latest"
},
"peerDependencies": {
"workspace-c": "1.0.0"
}
}
39 changes: 38 additions & 1 deletion tests/update-ts-references.check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ const rootTsConfig = [
},
];

const rootTsConfigWithRootDependencies = [
'.',
{
compilerOptions: {
composite: true,
},
files: [],
references: [
{
path: 'workspace-a',
},
{
path: 'workspace-b',
},
{
path: 'shared/workspace-c',
},
{
path: 'shared/workspace-d',
},
{
path: 'utils/foos/foo-a',
},
],
},
];

const wsATsConfig = [
'./workspace-a',
{
Expand Down Expand Up @@ -133,6 +160,16 @@ const tsconfigs = [
fooBTsConfig,
];

const tsconfigsWithRootDependencies = [
rootTsConfigWithRootDependencies,
wsATsConfig,
wsBTsConfig,
wsCTsConfig,
wsDTsConfig,
fooATsConfig,
fooBTsConfig,
];


test('Detect changes in references with the --check option', async () => {
let errorCode = 0;
Expand Down Expand Up @@ -169,7 +206,7 @@ test('No changes in references detected with the --check option', async () => {

expect(errorCode).toBe(0);

tsconfigs.forEach((tsconfig) => {
tsconfigsWithRootDependencies.forEach((tsconfig) => {
const [configPath, config] = tsconfig;

expect(
Expand Down
88 changes: 84 additions & 4 deletions tests/update-ts-references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const rootFolderYarnCreate = path.join(
const rootFolderPnpm = path.join(process.cwd(), 'test-run', 'pnpm');
const rootFolderTsPaths = path.join(process.cwd(), 'test-run', 'ts-paths');
const rootFolderTsPathsIgnore = path.join(process.cwd(), 'test-run', 'ts-paths-ignore');
const rootFolderPruneMinimal = path.join(process.cwd(), 'test-run', 'prune-minimal');

const rootFolderLerna = path.join(process.cwd(), 'test-run', 'lerna');
const rootFolderConfigName = path.join(
Expand Down Expand Up @@ -49,6 +50,33 @@ const rootTsConfig = [
},
];

const rootTsConfigWithRootDependencies = [
'.',
{
compilerOptions: {
composite: true,
},
files: [],
references: [
{
path: 'workspace-a',
},
{
path: 'workspace-b',
},
{
path: 'shared/workspace-c',
},
{
path: 'shared/workspace-d',
},
{
path: 'utils/foos/foo-a',
},
],
},
];

const wsATsConfig = [
'./workspace-a',
{
Expand Down Expand Up @@ -133,8 +161,8 @@ const tsconfigs = [
fooBTsConfig,
];

const tsconfigsIncludingPrepend = [
rootTsConfig,
const tsconfigsIncludingPrependWithRootDependencies = [
rootTsConfigWithRootDependencies,
[
'./workspace-a',
{
Expand Down Expand Up @@ -203,7 +231,7 @@ test('avoid adding an empty compilerOptions', async () => {
test('Support yarn and npm workspaces', async () => {
await setup(rootFolderYarn);

tsconfigsIncludingPrepend.forEach((tsconfig) => {
tsconfigsIncludingPrependWithRootDependencies.forEach((tsconfig) => {
const [configPath, config] = tsconfig;

expect(
Expand All @@ -216,12 +244,50 @@ test('Support yarn and npm workspaces', async () => {
parse(fs.readFileSync(path.join(rootFolderYarn, 'tsconfig.json')).toString()).references
).toEqual([
{ path: 'workspace-a' },
{ path: 'workspace-b' },
{ path: 'shared/workspace-c' },
{ path: 'shared/workspace-d' },
{ path: 'utils/foos/foo-a' },
]);
// still has the comment
expect(fs.readFileSync(path.join(rootFolderYarn, 'tsconfig.json')).toString()).toMatch(/\/\* Basic Options \*\//)
});

test('Prune root references when the root package does not explicitly keep them', async () => {
await setup(rootFolderPruneMinimal);

expect(
parse(fs.readFileSync(path.join(rootFolderPruneMinimal, 'tsconfig.json')).toString())
).toEqual({
files: [],
compilerOptions: {
composite: true,
},
references: [
{
path: 'workspace-a',
},
],
});

expect(
parse(fs.readFileSync(path.join(rootFolderPruneMinimal, 'workspace-a', 'tsconfig.json')).toString())
).toEqual({
compilerOptions,
references: [
{
path: '../workspace-b',
},
],
});

expect(
parse(fs.readFileSync(path.join(rootFolderPruneMinimal, 'workspace-b', 'tsconfig.json')).toString())
).toEqual({
compilerOptions,
});
});

test('Support lerna', async () => {
await setup(rootFolderLerna);

Expand Down Expand Up @@ -267,13 +333,27 @@ test('create paths mappings ', async () => {
{
compilerOptions: {
composite: true,
paths: { "workspace-a": ["workspace-a/src"] }
paths: {
"workspace-a": ["workspace-a/src"],
"workspace-b": ["workspace-b"],
"foo-a": ["utils/foos/foo-a/src"],
"foo-b": ["utils/foos/foo-b/src"]
}
},
files: [],
references: [
{
path: 'workspace-a',
},
{
path: 'workspace-b',
},
{
path: 'utils/foos/foo-a',
},
{
path: 'utils/foos/foo-b',
},
],
},
];
Expand Down
Loading