Skip to content

Commit b306eee

Browse files
committed
TS: Option to install dependencies
1 parent 8f39989 commit b306eee

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ public class AutoBuild {
195195
private final String defaultEncoding;
196196
private ExecutorService threadPool;
197197
private volatile boolean seenCode = false;
198+
private boolean installDependencies = false;
199+
private int installDependenciesTimeout;
200+
201+
/** The default timeout when running <code>yarn</code>, in milliseconds. */
202+
public static final int INSTALL_DEPENDENCIES_DEFAULT_TIMEOUT = 10 * 60 * 1000; // 10 minutes
198203

199204
public AutoBuild() {
200205
this.LGTM_SRC = toRealPath(getPathFromEnvVar("LGTM_SRC"));
@@ -204,6 +209,10 @@ public AutoBuild() {
204209
this.typeScriptMode =
205210
getEnumFromEnvVar("LGTM_INDEX_TYPESCRIPT", TypeScriptMode.class, TypeScriptMode.FULL);
206211
this.defaultEncoding = getEnvVar("LGTM_INDEX_DEFAULT_ENCODING");
212+
this.installDependencies = Boolean.valueOf(getEnvVar("LGTM_TYPESCRIPT_INSTALL_DEPS"));
213+
this.installDependenciesTimeout =
214+
Env.systemEnv()
215+
.getInt("LGTM_TYPESCRIPT_INSTALL_DEPS_TIMEOUT", INSTALL_DEPENDENCIES_DEFAULT_TIMEOUT);
207216
setupFileTypes();
208217
setupXmlMode();
209218
setupMatchers();
@@ -533,6 +542,10 @@ private void extractSource() throws IOException {
533542
List<Path> tsconfigFiles = new ArrayList<>();
534543
findFilesToExtract(defaultExtractor, filesToExtract, tsconfigFiles);
535544

545+
if (!tsconfigFiles.isEmpty() && this.installDependencies) {
546+
this.installDependencies(filesToExtract);
547+
}
548+
536549
// extract TypeScript projects and files
537550
Set<Path> extractedFiles = extractTypeScript(defaultExtractor, filesToExtract, tsconfigFiles);
538551

@@ -549,6 +562,34 @@ private void extractSource() throws IOException {
549562
}
550563
}
551564

565+
protected void installDependencies(Set<Path> filesToExtract) {
566+
for (Path file : filesToExtract) {
567+
if (file.getFileName().toString().equals("package.json")) {
568+
System.out.println("Installing dependencies from " + file);
569+
ProcessBuilder pb =
570+
new ProcessBuilder(
571+
Arrays.asList(
572+
"yarn",
573+
"install",
574+
"--verbose",
575+
"--ignore-scripts",
576+
"--ignore-platform",
577+
"--ignore-engines",
578+
"--ignore-optional",
579+
"--no-bin-links",
580+
"--pure-lockfile"));
581+
pb.directory(file.getParent().toFile());
582+
pb.redirectOutput(Redirect.INHERIT);
583+
pb.redirectError(Redirect.INHERIT);
584+
try {
585+
pb.start().waitFor(this.installDependenciesTimeout, TimeUnit.MILLISECONDS);
586+
} catch (IOException | InterruptedException ex) {
587+
throw new ResourceError("Could not install dependencies from " + file, ex);
588+
}
589+
}
590+
}
591+
}
592+
552593
private ExtractorConfig mkExtractorConfig() {
553594
ExtractorConfig config = new ExtractorConfig(true);
554595
config = config.withSourceType(getSourceType());

javascript/extractor/src/com/semmle/js/extractor/test/AutoBuildTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ public void extractTypeScriptFiles(
128128
}
129129
}
130130

131+
@Override
132+
protected void installDependencies(Set<Path> filesToExtract) {
133+
// never install dependencies during testing
134+
}
135+
131136
@Override
132137
protected void extractXml() throws IOException {
133138
Files.walkFileTree(

0 commit comments

Comments
 (0)