Skip to content

Commit 291bcec

Browse files
committed
Fix error when tsconfig has file as reference path.
Ref: jqassistant-plugin/jqassistant-typescript-plugin#126
1 parent b09c44a commit 291bcec

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

scripts/findPathsToScan.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
# Finds all files and directories to scan and analyze and provides them as comma-separated list.
44
# This includes the scan of Typescript projects that leads to the intermediate json data file for jQAssistant.
55

6+
# Uses: patchJQAssistantTypescriptPlugin.sh
7+
68
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
79
set -o errexit -o pipefail
810

911
ARTIFACTS_DIRECTORY=${ARTIFACTS_DIRECTORY:-"artifacts"}
1012
SOURCE_DIRECTORY=${SOURCE_DIRECTORY:-"source"}
1113

14+
## Get this "scripts" directory if not already set
15+
# Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
16+
# CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
17+
# This way non-standard tools like readlink aren't needed.
18+
SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts
19+
echo "findPathsToScan SCRIPTS_DIR=${SCRIPTS_DIR}" >&2
20+
1221
# This function returns the argument followed by a comma (separator) if it is not empty
1322
# and just an empty string otherwise.
1423
appendNonEmpty() {
@@ -31,6 +40,8 @@ fi
3140

3241
if [ -d "./${SOURCE_DIRECTORY}" ] ; then
3342
if command -v "npx" &> /dev/null ; then
43+
# TODO: Remove patchJQAssistantTypescriptPlugin when issue is resolved: https://github.com/jqassistant-plugin/jqassistant-typescript-plugin/issues/125
44+
source "${SCRIPTS_DIR}/patchJQAssistantTypescriptPlugin.sh" >&2
3445
echo "findPathsToScan: Scanning Typescript source using @jqassistant/ts-lce..." >&2
3546
( cd "./${SOURCE_DIRECTORY}" && npx --yes @jqassistant/[email protected] --extension React >"./../runtime/logs/jqassistant-typescript-scan.log" 2>&1 || exit )
3647
else
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
# Patches jQAssistant Typescript Plugin as a workaround for https://github.com/jqassistant-plugin/jqassistant-typescript-plugin/issues/125
4+
5+
# Note: As soon as the issue is resolved, this file and its call can be deleted.
6+
7+
# Requires "apply"
8+
9+
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
10+
set -o errexit -o pipefail
11+
12+
## Get this "scripts" directory if not already set
13+
# Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
14+
# CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
15+
# This way non-standard tools like readlink aren't needed.
16+
SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts
17+
echo "setupJQAssistant: SCRIPTS_DIR=$SCRIPTS_DIR"
18+
19+
if ! command -v "npx" &> /dev/null ; then
20+
echo "patchJQAssistantTypescriptPlugin Error: Command npx not found. It's needed to install @jqassistant/ts-lce temporarily."
21+
fi
22+
23+
echo "patchJQAssistantTypescriptPlugin: Installing jQAssistant Typescript Plugin @jqassistant/ts-lce using npx"
24+
jqassistant_typescript_plugin_installation_path=$(npx --yes --package @jqassistant/[email protected] which jqa-ts-lce)
25+
echo "patchJQAssistantTypescriptPlugin: jqassistant_typescript_plugin_installation_path=${jqassistant_typescript_plugin_installation_path}"
26+
27+
jqassistant_typescript_plugin_bin_path=$(dirname -- "${jqassistant_typescript_plugin_installation_path}")
28+
jqassistant_typescript_plugin_utils_file_to_patch_relative="${jqassistant_typescript_plugin_bin_path}/../@jqassistant/ts-lce/dist/src/core/utils/project.utils.js"
29+
echo "patchJQAssistantTypescriptPlugin: jqassistant_typescript_plugin_utils_file_to_patch_relative=${jqassistant_typescript_plugin_utils_file_to_patch_relative}"
30+
31+
jqassistant_typescript_plugin_utils_path_resolved=$( CDPATH=. cd -- "$(dirname -- "${jqassistant_typescript_plugin_utils_file_to_patch_relative}")" && pwd -P )
32+
jqassistant_typescript_plugin_utils_file_to_patch="${jqassistant_typescript_plugin_utils_path_resolved}/project.utils.js"
33+
echo "patchJQAssistantTypescriptPlugin: jqassistant_typescript_plugin_utils_file_to_patch=${jqassistant_typescript_plugin_utils_file_to_patch}"
34+
35+
echo "patchJQAssistantTypescriptPlugin: Patching jQAssistant Typescript Plugin @jqassistant/ts-lce"
36+
patch --forward -p1 "${jqassistant_typescript_plugin_utils_file_to_patch_relative}" -i "${SCRIPTS_DIR}/patches/@jqassistant+ts-lce+1.2.0.patch" || true
37+
echo "patchJQAssistantTypescriptPlugin: Successfully patched jQAssistant Typescript Plugin @jqassistant/ts-lce"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
diff --git a/node_modules/@jqassistant/ts-lce/dist/src/core/utils/project.utils.js b/node_modules/@jqassistant/ts-lce/dist/src/core/utils/project.utils.js
2+
index d1c33c4..672e6a1 100644
3+
--- a/node_modules/@jqassistant/ts-lce/dist/src/core/utils/project.utils.js
4+
+++ b/node_modules/@jqassistant/ts-lce/dist/src/core/utils/project.utils.js
5+
@@ -35,7 +35,7 @@ class ProjectUtils {
6+
const projectRoot = dirsToScan[0];
7+
const tsConfigPath = path_1.default.join(projectRoot, "tsconfig.json");
8+
if (fs_1.default.existsSync(tsConfigPath)) {
9+
- result.push(...this.getProjectInfo(projectRoot));
10+
+ result.push(...this.getProjectInfo(projectRoot, 'tsconfig.json'));
11+
}
12+
else {
13+
// add all subdirectories as potential project candidates
14+
@@ -61,13 +61,19 @@ class ProjectUtils {
15+
}
16+
});
17+
}
18+
- static getProjectInfo(projectPath) {
19+
+ static getConfigFileName(rawConfigPath) {
20+
+ const pathIsADirectory = fs_1.default.statSync(rawConfigPath).isDirectory();
21+
+ const defaultConfigFileName = 'tsconfig.json';
22+
+ return pathIsADirectory? path_1.default.join(rawConfigPath, defaultConfigFileName) : rawConfigPath;
23+
+ }
24+
+ static getProjectInfo(projectPath, configFileName) {
25+
const result = [];
26+
- const tsConfig = this.parseTsConfig(projectPath);
27+
+ const tsConfig = this.parseTsConfig(projectPath, configFileName);
28+
const subProjectPaths = [];
29+
if (tsConfig.projectReferences) {
30+
for (const ref of tsConfig.projectReferences) {
31+
- const subProjectInfos = this.getProjectInfo(ref.path);
32+
+ const referencedConfigFileName = this.getConfigFileName(ref.path);
33+
+ const subProjectInfos = this.getProjectInfo(path_1.default.dirname(referencedConfigFileName), path_1.default.basename(referencedConfigFileName));
34+
subProjectPaths.push(...subProjectInfos.map(spi => spi.projectPath.replace(/\\/g, "/")));
35+
result.push(...subProjectInfos);
36+
}
37+
@@ -85,10 +91,10 @@ class ProjectUtils {
38+
});
39+
return result;
40+
}
41+
- static parseTsConfig(projectRoot) {
42+
- const tsConfigPath = path_1.default.join(projectRoot, "tsconfig.json");
43+
+ static parseTsConfig(projectRoot, configFile) {
44+
+ const tsConfigPath = path_1.default.join(projectRoot, configFile);
45+
const configFileText = fs_1.default.readFileSync(tsConfigPath, 'utf8');
46+
- const configFileSourceFile = (0, typescript_1.createSourceFile)('tsconfig.json', configFileText, typescript_1.ScriptTarget.JSON);
47+
+ const configFileSourceFile = (0, typescript_1.createSourceFile)(configFile, configFileText, typescript_1.ScriptTarget.JSON);
48+
// Parse the tsconfig.json
49+
const parsedCommandLine = (0, typescript_1.parseJsonSourceFileConfigFileContent)(configFileSourceFile, typescript_1.sys, path_1.default.dirname(tsConfigPath));
50+
// explicitly set rootDir option to default value, if not set manually

0 commit comments

Comments
 (0)