Skip to content

Commit 15ca96e

Browse files
committed
Fix hanging when starting daemon / Use __dirname for cwd / Debug logs / Properly exit process
1 parent 4f17f70 commit 15ca96e

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

buildFiles.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
const {
22
existsSync
33
} = require('fs');
4-
const {
5-
subDirectories
6-
} = require('./io')
74
const {
85
join
96
} = require('path')
107
const fs = require('fs')
118

12-
function getBuildFiles(externalFiles) {
9+
function getBuildFiles(externalFiles, debugLog) {
1310
let buildFiles = [];
1411
exports.buildFiles = buildFiles;
1512
if (externalFiles && externalFiles.length) {
@@ -23,31 +20,36 @@ function getBuildFiles(externalFiles) {
2320
});
2421
}
2522

26-
const allRecursiveFiles = getAllBuildFiles('.')
23+
const directoryToSearchIn = __dirname
24+
25+
debugLog(`Recursively looking for build files in directory ${directoryToSearchIn}`)
2726

28-
const recursiveBuildFiles = allRecursiveFiles.filter(it => it.endsWith('build.gradle') ||it.endsWith('build.gradle.kts'))
27+
const allRecursiveFiles = getAllBuildFiles(directoryToSearchIn)
28+
29+
const recursiveBuildFiles = allRecursiveFiles.filter(it => it.endsWith('build.gradle') || it.endsWith('build.gradle.kts'))
2930

3031
buildFiles.push(...recursiveBuildFiles)
3132

3233
return buildFiles
3334
}
3435

35-
const getAllBuildFiles = function(dirPath, arrayOfFiles) {
36+
const getAllBuildFiles = function (dirPath, arrayOfFiles) {
3637
const files = fs.readdirSync(dirPath)
37-
38+
3839
arrayOfFiles = arrayOfFiles || []
39-
40-
files.forEach(function(file) {
41-
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
42-
arrayOfFiles = getAllBuildFiles(dirPath + "/" + file, arrayOfFiles)
43-
} else {
44-
arrayOfFiles.push(join(__dirname, dirPath, "/", file))
45-
46-
}
40+
41+
files.forEach((file) => {
42+
const fsStat = fs.statSync(join(dirPath, "/", file))
43+
44+
if (fsStat.isDirectory()) {
45+
arrayOfFiles = getAllBuildFiles(join(dirPath, "/", file), arrayOfFiles)
46+
} else {
47+
arrayOfFiles.push(join(__dirname, dirPath, "/", file))
48+
}
4749
})
48-
50+
4951
return arrayOfFiles
50-
}
52+
}
5153

5254
module.exports = {
5355
getBuildFiles

gradleCommand.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const {
88
function determineGradleCommand(debugLog) {
99
let gradleCommand = null
1010
let gradleWrapper = false
11+
1112
debugLog('Determining gradle command')
13+
1214
try {
1315
const isWindows = process.platform === 'win32'
1416
debugLog('isWindows: ' + isWindows)

index.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ const {
4545

4646
if (!gradleCommand) {
4747
console.log('Unable to find Gradle Wrapper or Gradle CLI.'.bgRed)
48-
return
48+
process.exit()
4949
}
5050

5151
const externalFiles = argv['external-file'];
52-
const buildFiles = getBuildFiles(externalFiles)
53-
debugLog('Build Files:\n' + buildFiles.join('\n'))
52+
const buildFiles = getBuildFiles(externalFiles, debugLog)
53+
debugLog(`Build Files:\n ${buildFiles.join('\n')}`)
5454
if (!buildFiles.length) {
5555
console.log('Unable to find build.gradle, build.gradle.kts or external build file.'.bgRed)
56-
return
56+
process.exit()
5757
}
5858

5959
exports.debugLog = debugLog;
@@ -76,6 +76,10 @@ async function executeCommandAndWaitForExitCode(command, args) {
7676
commandExitCode = code
7777
})
7878

79+
child.on('exit', (code) => {
80+
commandExitCode = code
81+
})
82+
7983
while (commandExitCode === undefined) {
8084
debugLog('Waiting for command to finish')
8185
await new Promise(resolve => setTimeout(resolve, 500));
@@ -99,16 +103,14 @@ async function executeCommandAndWaitForExitCode(command, args) {
99103

100104
if (gradleDependencyUpdateProcessExitCode !== 0) {
101105
informUserAboutInstallingUpdatePlugin(gradleDependencyUpdateProcessExitCode);
102-
return
106+
process.exit()
103107
}
104108

105109
if (!buildFiles.length) {
106110
console.log('Unable to find build.gradle, build.gradle.kts or external build file.'.bgRed);
107-
return;
111+
process.exit()
108112
}
109113

110-
debugLog('Build files ' + buildFiles);
111-
112114
debugLog(`Reading JSON report file\n`)
113115

114116
const dependencyUpdates = findOutdatedDependencies();
@@ -123,7 +125,7 @@ async function executeCommandAndWaitForExitCode(command, args) {
123125

124126
if (!choices.length) {
125127
console.log('Everything up to date.')
126-
return
128+
process.exit()
127129
}
128130

129131
const response = await prompts({
@@ -134,11 +136,11 @@ async function executeCommandAndWaitForExitCode(command, args) {
134136
});
135137

136138
if (!response.upgrades || !response.upgrades.length) {
137-
console.log('No upgrades select')
138-
return
139+
console.log('No upgrades selected')
140+
process.exit()
139141
}
140142

141-
if (response.upgrades.some(it => it === 'gradle')) {
143+
if (latestGradleRelease && response.upgrades.some(it => it === 'gradle')) {
142144
console.log('Upgrading gradle wrapper')
143145
const upgradeArgs = ['wrapper', '--gradle-version=' + latestGradleRelease]
144146

@@ -148,7 +150,7 @@ async function executeCommandAndWaitForExitCode(command, args) {
148150
if (upgradeGradleWrapperExitCode !== 0) {
149151
console.log(`Error upgrading gradle wrapper (StatusCode=${upgradeGradleWrapper.status}).`.bgRed)
150152
console.log(upgradeGradleWrapper.stderr.toString().red)
151-
return
153+
process.exit()
152154
}
153155
}
154156

@@ -192,7 +194,7 @@ async function executeCommandAndWaitForExitCode(command, args) {
192194
writeFileSync(buildFile, modifiedContent, 'utf8');
193195
} catch (err) {
194196
console.log(`Unable to write gradle build file.\n${err}`.bgRed);
195-
return
197+
process.exit()
196198
}
197199

198200
}, buildFileContentMap)
@@ -238,9 +240,10 @@ function buildUpgradeChoicesForUser(outdatedDependencies, dependencyUpdates) {
238240
choices.sort((a, b) => a.title.localeCompare(b.title));
239241
debugLog(`Choices\n${JSON.stringify(choices)}\n\n`);
240242

243+
let latestGradleRelease
241244
if (dependencyUpdates.gradle) {
242245
let currentGradleRelease = dependencyUpdates.gradle.running.version;
243-
let latestGradleRelease = dependencyUpdates.gradle.current.version;
246+
latestGradleRelease = dependencyUpdates.gradle.current.version;
244247
if (gradleWrapper && currentGradleRelease !== latestGradleRelease) {
245248
choices.unshift({
246249
title: `Gradle - ${currentGradleRelease} => ${latestGradleRelease}`,

0 commit comments

Comments
 (0)