Skip to content

Commit 3137481

Browse files
authored
feat!: required changes for TypeScript 6 (#57)
BREAKING CHANGE: `baseUrl` is deprecated and we assume it isn't set anymore in you ts config
1 parent 0dd308d commit 3137481

6 files changed

Lines changed: 56 additions & 12 deletions

File tree

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
lts/hydrogen
1+
lts/jod

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "update-ts-references",
3-
"version": "5.0.2",
3+
"version": "6.0.0",
44
"description": "Updates TypeScript references automatically while using workspaces",
55
"bin": "src/index.js",
66
"scripts": {

src/update-ts-references.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ const getReferencesFromDependencies = (
153153
.reduce((referenceArray, dependency) => {
154154
if (packagesMap.has(dependency)) {
155155
const { packageDir: dependencyDir } = packagesMap.get(dependency);
156-
const relativePath = path.relative(packageDir, dependencyDir);
156+
const relativePath = ensureLeadingDotSlash(path.relative(packageDir, dependencyDir));
157157
const detectedConfig = detectTSConfig(dependencyDir, configName)
158158
if (detectedConfig !== null) {
159159
return [
@@ -183,6 +183,13 @@ const getReferencesFromDependencies = (
183183
.sort((refA, refB) => (refA.path > refB.path ? 1 : -1));
184184
};
185185

186+
const ensureLeadingDotSlash = (p) => {
187+
if (p == null || path.isAbsolute(p) || p.startsWith('.' + path.sep) || p.startsWith('..' + path.sep)) {
188+
return p;
189+
}
190+
return p.length === 0 ? '.' : '.' + path.sep + p;
191+
}
192+
186193
const ensurePosixPathStyle = (reference) => ({
187194
...reference,
188195
path: reference.path.split(path.sep).join(path.posix.sep),
@@ -381,8 +388,8 @@ const execute = async ({
381388
if (detectedConfig) {
382389
rootReferenceCandidates.push(ensurePosixPathStyle({
383390
name: packageName,
384-
path: path.join(path.relative(cwd, packageEntry.packageDir), detectedConfig !== TSCONFIG_JSON ? detectedConfig : ''),
385-
folder: path.relative(cwd, packageEntry.packageDir),
391+
path: path.join(ensureLeadingDotSlash(path.relative(cwd, packageEntry.packageDir)), detectedConfig !== TSCONFIG_JSON ? detectedConfig : ''),
392+
folder: ensureLeadingDotSlash(path.relative(cwd, packageEntry.packageDir)),
386393
}));
387394
const references = (getReferencesFromDependencies(
388395
configName,
@@ -442,4 +449,4 @@ const execute = async ({
442449
return changesCount;
443450
};
444451

445-
module.exports = { execute, defaultOptions };
452+
module.exports = { execute, defaultOptions, ensureLeadingDotSlash };
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const path = require('path');
2+
const { ensureLeadingDotSlash } = require('../src/update-ts-references');
3+
4+
test('returns null as-is', () => {
5+
expect(ensureLeadingDotSlash(null)).toBe(null);
6+
});
7+
8+
test('returns undefined as-is', () => {
9+
expect(ensureLeadingDotSlash(undefined)).toBe(undefined);
10+
});
11+
12+
test('returns . for empty string', () => {
13+
expect(ensureLeadingDotSlash('')).toBe('.');
14+
});
15+
16+
test('returns absolute path as-is', () => {
17+
const p = path.resolve('/some/absolute/path');
18+
expect(ensureLeadingDotSlash(p)).toBe(p);
19+
});
20+
21+
test('returns path already starting with .' + path.sep + ' as-is', () => {
22+
const p = '.' + path.sep + 'foo';
23+
expect(ensureLeadingDotSlash(p)).toBe(p);
24+
});
25+
26+
test('returns path already starting with ..' + path.sep + ' as-is', () => {
27+
const p = '..' + path.sep + 'foo';
28+
expect(ensureLeadingDotSlash(p)).toBe(p);
29+
});
30+
31+
test('prepends .' + path.sep + ' to a plain relative path', () => {
32+
expect(ensureLeadingDotSlash('foo')).toBe('.' + path.sep + 'foo');
33+
});
34+
35+
test('prepends .' + path.sep + ' to a nested relative path', () => {
36+
expect(ensureLeadingDotSlash('foo' + path.sep + 'bar')).toBe('.' + path.sep + 'foo' + path.sep + 'bar');
37+
});

tests/update-ts-references.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ test('create paths mappings ', async () => {
334334
compilerOptions: {
335335
composite: true,
336336
paths: {
337-
"workspace-a": ["workspace-a/src"],
338-
"workspace-b": ["workspace-b"],
339-
"foo-a": ["utils/foos/foo-a/src"],
340-
"foo-b": ["utils/foos/foo-b/src"]
337+
"workspace-a": ["./workspace-a/src"],
338+
"workspace-b": ["./workspace-b"],
339+
"foo-a": ["./utils/foos/foo-a/src"],
340+
"foo-b": ["./utils/foos/foo-b/src"]
341341
}
342342
},
343343
files: [],

tests/update-ts-references.yaml.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ test('receive options via the config', async () => {
172172
{
173173
compilerOptions: {
174174
composite: true,
175-
paths:{"workspace-a": ["workspace-a/src"]}
175+
paths:{"workspace-a": ["./workspace-a/src"]}
176176
},
177177
files: [],
178178
references: [
@@ -226,7 +226,7 @@ test('receive options for a different usecase', async () => {
226226
{
227227
compilerOptions: {
228228
composite: true,
229-
paths:{"workspace-a": ["workspace-a/src"]}
229+
paths:{"workspace-a": ["./workspace-a/src"]}
230230
},
231231
files: [],
232232
references: [

0 commit comments

Comments
 (0)