1+ #! /usr/bin/env bash
2+
3+ # Downloads a Typescript project from a git repository using git clone.
4+ # The cloned project is then moved into the "source" directory of the current analysis directory.
5+ # After scanning it with jQAssistant's Typescript Plugin, the resulting JSON will be moved into the "artifacts/typescript" directory.
6+
7+ # Note: This script is meant to be started within the temporary analysis directory (e.g. "temp/AnalysisName/")
8+
9+ # Fail on any error (errexit = exit on first error, errtrace = error inherited from sub-shell ,pipefail exist on errors within piped commands)
10+ set -o errexit -o errtrace -o pipefail
11+
12+ # Overrideable Defaults
13+ SOURCE_DIRECTORY=${SOURCE_DIRECTORY:- " source" }
14+ echo " downloadTypescriptProject: SOURCE_DIRECTORY=${SOURCE_DIRECTORY} "
15+
16+ # Display how this command is intended to be used including an example when wrong input parameters were detected
17+ usage () {
18+ echo " "
19+ echo " Usage: $0 \\ "
20+ echo " --url <git-clone-url> \\ "
21+ echo " --version <project version \\ "
22+ echo " [ --tag <git-tag-for-that-version> (default=version) \\ ]"
23+ echo " [ --project <name-of-the-project> (default=url file name) \\ ]"
24+ echo " [ --packageManager <npm/pnpm/yarn> (default=npm) ]"
25+ echo " Example: $0 \\ "
26+ echo " --url https://github.com/ant-design/ant-design.git \\ "
27+ echo " --version 5.19.3"
28+ exit 1
29+ }
30+
31+ # Default command line option values
32+ cloneUrl=" "
33+ projectName=" "
34+ projectVersion=" "
35+ projectTag=" "
36+ packageManager=" npm"
37+
38+ # Parse command line options
39+ while [[ $# -gt 0 ]]; do
40+ key=" ${1} "
41+ value=" ${2} "
42+
43+ case " ${key} " in
44+ --url)
45+ cloneUrl=" ${value} "
46+ shift
47+ ;;
48+ --project)
49+ projectName=" ${value} "
50+ shift
51+ ;;
52+ --version)
53+ projectVersion=" ${value} "
54+ shift
55+ ;;
56+ --tag)
57+ projectTag=" ${value} "
58+ shift
59+ ;;
60+ --packageManager)
61+ packageManager=" ${value} "
62+ shift
63+ ;;
64+ * )
65+ echo " downloadTypescriptProject Error: Unknown option: ${key} "
66+ usage
67+ ;;
68+ esac
69+ shift
70+ done
71+
72+ if [[ -z ${cloneUrl} ]]; then
73+ echo " downloadTypescriptProject Error: Please specify an URL for git clone."
74+ usage
75+ fi
76+
77+ if ! curl --head --fail " ${cloneUrl} " > /dev/null 2>&1 ; then
78+ echo " downloadTypescriptProject Error: Invalid URL: ${cloneUrl} "
79+ exit 1
80+ fi
81+
82+ if [[ -z ${projectName} ]]; then
83+ # When empty, infer the project name from the file name / last part of the clone url excluding the extension .git
84+ projectName=$( basename -s .git " ${cloneUrl} " )
85+ fi
86+
87+ if [[ -z ${projectVersion} ]]; then
88+ echo " downloadTypescriptProject Error: Please specify a project version."
89+ usage
90+ fi
91+
92+ if [[ -z ${projectTag} ]]; then
93+ # When empty, use the value of the option "version" as tag
94+ projectTag=" ${projectVersion} "
95+ fi
96+
97+ case " ${packageManager} " in
98+ npm|pnpm|yarn)
99+ echo " downloadTypescriptProject Using package manager ${packageManager} "
100+ ;;
101+ * )
102+ echo " downloadTypescriptProject Error: Unknown package manager: ${packageManager} "
103+ usage
104+ ;;
105+ esac
106+
107+ if ! command -v " ${packageManager} " & > /dev/null ; then
108+ echo " downloadTypescriptProject Error: Package manager ${packageManager} could not be found"
109+ exit 1
110+ fi
111+
112+ if ! command -v " npx" & > /dev/null ; then
113+ echo " downloadTypescriptProject Error: Command npx not found. It's needed to execute npm packages."
114+ exit 1
115+ fi
116+
117+ echo " downloadTypescriptProject: cloneUrl: ${cloneUrl} "
118+ echo " downloadTypescriptProject: projectName: ${projectName} "
119+ echo " downloadTypescriptProject: projectVersion: ${projectVersion} "
120+ echo " downloadTypescriptProject: projectTag: ${projectTag} "
121+ echo " downloadTypescriptProject: packageManager: ${packageManager} "
122+
123+ usePackageManagerToInstallDependencies () {
124+ echo " downloadTypescriptProject: Installing dependencies using ${packageManager} ..."
125+ case " ${packageManager} " in
126+ npm)
127+ # npm ci is not sufficient for projects like "ant-design" that rely on generating the package-lock
128+ # Even if this is not standard, this is an acceptable solution for standard and edge cases.
129+ npm install --ignore-scripts --verbose || exit
130+ ;;
131+ pnpm)
132+ pnpm install --frozen-lockfile || exit
133+ ;;
134+ yarn)
135+ yarn install --frozen-lockfile --ignore-scripts --non-interactive --verbose || exit
136+ ;;
137+ esac
138+ }
139+
140+ # Create runtime logs directory if it hasn't existed yet
141+ mkdir -p ./runtime/logs
142+
143+ # Download the project to be analyzed
144+ fullProjectName=" ${projectName} -${projectVersion} "
145+ fullSourceDirectory=" ${SOURCE_DIRECTORY} /${fullProjectName} "
146+
147+ if [ ! -d " ${fullSourceDirectory} " ] ; then # only clone if source doesn't exist
148+ echo " downloadTypescriptProject: Cloning ${cloneUrl} with version ${projectVersion} ..."
149+ # A full clone is done since not only the source is scanned, but also the git log history.
150+ git clone --branch " ${projectTag} " " ${cloneUrl} " " ${fullSourceDirectory} "
151+ fi
152+ (
153+ cd " ${fullSourceDirectory} " || exit
154+ usePackageManagerToInstallDependencies
155+ echo " downloadTypescriptProject: Scanning Typescript source using @jqassistant/ts-lce..."
156+ npx --yes @jqassistant/ts-lce > " ./../../runtime/logs/jqassistant-typescript-scan-${projectName} .log" 2>&1 || exit
157+ )
158+ echo " downloadTypescriptProject: Moving scanned results into the artifacts/typescript directory..."
159+ mkdir -p artifacts/typescript
160+ mv -nv " ${fullSourceDirectory} /.reports/jqa/ts-output.json" " artifacts/typescript/${fullProjectName} .json"
0 commit comments