Skip to content

Commit

Permalink
feat: add configName fallback resolution including ref path generatio…
Browse files Browse the repository at this point in the history
…n fix
  • Loading branch information
MKruschke committed Jun 23, 2021
1 parent 036535e commit d32eeb4
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-ts-references",
"version": "2.3.0",
"version": "2.4.0",
"bin": "src/index.js",
"scripts": {
"lint": "eslint src tests",
Expand Down
33 changes: 22 additions & 11 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const readlineSync = require('readline-sync');
const assert = require('assert').strict;

const PACKAGE_JSON = 'package.json';
const TSCONFIG_JSON = 'tsconfig.json'

const defaultOptions = {
configName: 'tsconfig.json',
Expand Down Expand Up @@ -62,6 +63,15 @@ const getAllPackageJsons = async (workspaces) => {
);
};

const detectTSConfig = (directory, configName) => {
let detectedConfig = fs.existsSync(path.join(directory, configName)) ? configName : null
if (configName !== TSCONFIG_JSON && detectedConfig === null) {
detectedConfig = fs.existsSync(path.join(directory, TSCONFIG_JSON)) ? TSCONFIG_JSON : null
}
return detectedConfig
}


const getPackageNamesAndPackageDir = (packageFilePaths) =>
packageFilePaths.reduce((map, packageFilePath) => {
const fullPackageFilePath = path.join(process.cwd(), packageFilePath);
Expand Down Expand Up @@ -103,13 +113,13 @@ const getReferencesFromDependencies = (
.reduce((referenceArray, dependency) => {
if (packagesMap.has(dependency)) {
const { packageDir: dependencyDir } = packagesMap.get(dependency);
if (fs.existsSync(path.join(dependencyDir, configName))) {
const relativePath = path.relative(packageDir, dependencyDir);

const relativePath = path.relative(packageDir, dependencyDir);
const detectedConfig = detectTSConfig(dependencyDir, configName)
if (detectedConfig !== null) {
return [
...referenceArray,
{
path: relativePath,
path: detectedConfig !== TSCONFIG_JSON ? path.join(relativePath, detectedConfig) : relativePath,
},
];
}
Expand Down Expand Up @@ -149,8 +159,8 @@ const updateTsConfig = (
if (check === false && pureJson === false && discardComments === false) {
if (
!readlineSync.keyInYN(
`Found comments in the tsconfig. ${tsconfigFilePath}
Do you want to discard them and proceed?`
`Found comments in the tsconfig.${tsconfigFilePath}
Do you want to discard them and proceed ? `
)
) {
process.exit(0);
Expand Down Expand Up @@ -239,10 +249,11 @@ const execute = async ({
const rootReferences = [];

packagesMap.forEach((packageEntry, packageName) => {
const tsconfigFilePath = path.join(packageEntry.packageDir, configName);
if (fs.existsSync(tsconfigFilePath)) {
const detectedConfig = detectTSConfig(packageEntry.packageDir, configName)

if (detectedConfig) {
rootReferences.push({
path: path.relative(process.cwd(), packageEntry.packageDir),
path: path.join(path.relative(process.cwd(), packageEntry.packageDir), detectedConfig !== TSCONFIG_JSON ? detectedConfig : ''),
});
const references = getReferencesFromDependencies(
configName,
Expand All @@ -255,15 +266,15 @@ const execute = async ({
console.log(`references of ${packageName}`, references);
}
changesCount += updateTsConfig(
configName,
detectedConfig,
references,
discardComments,
check,
packageEntry
);
} else {
// eslint-disable-next-line no-console
console.log(`NO ${configName} for ${packageName}`);
console.log(`NO ${configName === TSCONFIG_JSON ? configName : `${configName} nor ${TSCONFIG_JSON}`} for ${packageName}`);
}
});

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
},"references": [

{
"path": "../some/old/ref"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"references": [
{
"path": "../utils/foos/foo-a",
"path": "../utils/foos/foo-a/tsconfig.dev.json",
"prepend": false
},
{
Expand Down
81 changes: 70 additions & 11 deletions tests/update-ts-references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ const setup = async (rootFolder, configName) => {
try {
await execSh(
`npx update-ts-references --discardComments${
configName ? ` --configName ${configName}` : ''
configName ? ` --configName ${configName}` : ''
}`,
{
stdio: null,
cwd: rootFolder,
}
);
Expand Down Expand Up @@ -292,32 +291,92 @@ test('Support custom tsconfig names', async () => {
await setup(rootFolder, configName);

const tsconfigs = [
rootTsConfig,
[
'.',
{
compilerOptions: {
composite: true,
},
files: [],
references: [
{
path: 'workspace-a/tsconfig.dev.json',
},
{
path: 'workspace-b/tsconfig.dev.json',
},
{
path: 'shared/workspace-c/tsconfig.dev.json',
},
{
path: 'shared/workspace-d/tsconfig.dev.json',
},
{
path: 'utils/foos/foo-a/tsconfig.dev.json',
},
{
path: 'utils/foos/foo-b',
},
],
},
],
[
'./workspace-a',
{
compilerOptions,
references: [
{
path: '../utils/foos/foo-a',
path: '../utils/foos/foo-a/tsconfig.dev.json',
prepend: false,
},
{
path: '../workspace-b',
path: '../workspace-b/tsconfig.dev.json',
},
],
},
],
wsBTsConfig,
wsCTsConfig,
wsDTsConfig,
fooATsConfig,
fooBTsConfig,
[
'./shared/workspace-c',
{
compilerOptions,

references: [
{
path: '../../utils/foos/foo-a/tsconfig.dev.json',
},
],
},
],
[
'./shared/workspace-d',
{
compilerOptions,

references: [
{
path: '../workspace-c/tsconfig.dev.json',
},
],
},
],
[
'./utils/foos/foo-a',
{
compilerOptions,
references: [
{
path: '../foo-b',
},
],
},
],
[...fooBTsConfig, 'tsconfig.json'],
];

tsconfigs.forEach((tsconfig) => {
const [configPath, config] = tsconfig;
expect(require(path.join(rootFolder, configPath, configName))).toEqual(
const [configPath, config, configNameOverride] = tsconfig;
expect(require(path.join(rootFolder, configPath, configNameOverride || configName))).toEqual(
config
);
});
Expand Down

0 comments on commit d32eeb4

Please sign in to comment.