0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(add): Fixes astro add modifying baseUrl by accident (#10774)

* fix(add): Fixes `astro add` modifying `baseUrl` by accident

* chore: changeset

* test: add test

* fix: tsconfig not being a json maybe is a mistake, I don't know!

* test: fix

* Update packages/astro/test/fixtures/tsconfig-handling/baseUrl/tsconfig.json

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Erika 2024-04-12 13:56:54 -04:00 committed by GitHub
parent 3ff6b403db
commit 308b5d8c12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 39 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes `astro add` sometimes modifying `baseUrl` unintentionally

View file

@ -968,16 +968,16 @@ async function updateTSConfig(
inputConfig = {
tsconfig: defaultTSConfig,
tsconfigFile: path.join(cwd, 'tsconfig.json'),
rawConfig: { tsconfig: defaultTSConfig, tsconfigFile: path.join(cwd, 'tsconfig.json') },
rawConfig: defaultTSConfig,
};
} else {
inputConfigText = JSON.stringify(inputConfig.rawConfig.tsconfig, null, 2);
inputConfigText = JSON.stringify(inputConfig.rawConfig, null, 2);
}
const configFileName = path.basename(inputConfig.tsconfigFile);
const outputConfig = updateTSConfigForFramework(
inputConfig.rawConfig.tsconfig,
inputConfig.rawConfig,
firstIntegrationWithTSSettings
);

View file

@ -1,10 +1,12 @@
import { join } from 'node:path';
import { readFile } from "node:fs/promises"
import {
TSConfckParseError,
type TSConfckParseOptions,
type TSConfckParseResult,
find,
parse,
toJson
} from 'tsconfck';
import type { CompilerOptions, TypeAcquisition } from 'typescript';
@ -64,7 +66,7 @@ type TSConfigResult<T = {}> = Promise<
export async function loadTSConfig(
root: string | undefined,
findUp = false
): Promise<TSConfigResult<{ rawConfig: TSConfckParseResult }>> {
): Promise<TSConfigResult<{ rawConfig: TSConfig }>> {
const safeCwd = root ?? process.cwd();
const [jsconfig, tsconfig] = await Promise.all(
@ -85,7 +87,13 @@ export async function loadTSConfig(
return parsedConfig;
}
return { ...parsedConfig, rawConfig: parsedConfig.extended?.[0] ?? parsedConfig.tsconfig };
// tsconfck does not return the original config, so we need to parse it ourselves
// https://github.com/dominikg/tsconfck/issues/138
const rawConfig = await readFile(tsconfig, 'utf-8')
.then(toJson)
.then((content) => JSON.parse(content) as TSConfig);
return { ...parsedConfig, rawConfig };
}
if (jsconfig) {
@ -95,7 +103,11 @@ export async function loadTSConfig(
return parsedConfig;
}
return { ...parsedConfig, rawConfig: parsedConfig.extended?.[0] ?? parsedConfig.tsconfig };
const rawConfig = await readFile(jsconfig, 'utf-8')
.then(toJson)
.then((content) => JSON.parse(content) as TSConfig);
return { ...parsedConfig, rawConfig: rawConfig };
}
return 'missing-config';

View file

@ -0,0 +1,6 @@
{
"files": ["i-have-base-url"],
"compilerOptions": {
"baseUrl": ".",
}
}

View file

@ -3,6 +3,8 @@ import * as path from 'node:path';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import { loadTSConfig, updateTSConfigForFramework } from '../../../dist/core/config/index.js';
import { readFile } from 'node:fs/promises';
import { toJson } from 'tsconfck';
const cwd = fileURLToPath(new URL('../../fixtures/tsconfig-handling/', import.meta.url));
@ -37,6 +39,14 @@ describe('TSConfig handling', () => {
assert.equal(invalidConfig, 'invalid-config');
assert.equal(missingConfig, 'missing-config');
});
it('does not change baseUrl in raw config', async () => {
const loadedConfig = await loadTSConfig(path.join(cwd, 'baseUrl'));
const rawConfig = await readFile(path.join(cwd, 'baseUrl', 'tsconfig.json'), 'utf-8').then(toJson)
.then((content) => JSON.parse(content));
assert.deepEqual(loadedConfig.rawConfig, rawConfig);
});
});
describe('tsconfig / jsconfig updates', () => {