Skip to content

Commit

Permalink
feat: Always in-memory update package.json (#15732)
Browse files Browse the repository at this point in the history
Update package.json contents in memory
when checking for bundle build as platform
version might have changed for instance.

Fixes #15727
  • Loading branch information
caalador committed Jan 24, 2023
1 parent 7cb2241 commit 3951cce
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ private static <T extends JsonValue> T computeIfAbsent(
return result;
}

Map<String, String> getDefaultDependencies() {
static Map<String, String> getDefaultDependencies() {
return readDependencies("default", "dependencies");
}

private Map<String, String> readDependencies(String id,
private static Map<String, String> readDependencies(String id,
String packageJsonKey) {
try {
Map<String, String> map = new HashMap<>();
Expand All @@ -319,16 +319,16 @@ private Map<String, String> readDependencies(String id,

return map;
} catch (IOException e) {
log().error(
LoggerFactory.getLogger(NodeUpdater.class).error(
"Unable to read " + packageJsonKey + " from '" + id + "'",
e);
return new HashMap<>();
}

}

private JsonObject readPackageJson(String id) throws IOException {
try (InputStream packageJson = getClass()
private static JsonObject readPackageJson(String id) throws IOException {
try (InputStream packageJson = NodeUpdater.class
.getResourceAsStream("dependencies/" + id + "/package.json")) {
JsonObject content = Json.parse(
IOUtils.toString(packageJson, StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ private static boolean hashAndBundleModulesEqual(JsonObject statsJson,
String bundlePackageJsonHash = getStatsHash(statsJson);

if (packageJsonHash == null || packageJsonHash.isEmpty()) {
getLogger().warn("No hash found for 'package.json'.");
getLogger().error(
"No hash found for 'package.json' even though one should always be generated!");
return false;
}

Expand Down Expand Up @@ -309,6 +310,20 @@ private static boolean versionAccepted(String expected, String actual) {
return expectedVersion.isEqualTo(actualVersion);
}

/**
* Get the package.json file from disk if available else generate in memory.
* <p>
* For the loaded file update versions as per in memory to get correct
* application versions.
*
* @param options
* the task options
* @param frontendDependencies
* frontend dependency scanner
* @param finder
* classfinder
* @return package.json content as JsonObject
*/
private static JsonObject getPackageJson(Options options,
FrontendDependenciesScanner frontendDependencies,
ClassFinder finder) {
Expand All @@ -319,65 +334,47 @@ private static JsonObject getPackageJson(Options options,
final JsonObject packageJson = Json
.parse(FileUtils.readFileToString(packageJsonFile,
StandardCharsets.UTF_8));
if (!packageJson.hasKey(NodeUpdater.VAADIN_DEP_KEY)
|| !packageJson.getObject(NodeUpdater.VAADIN_DEP_KEY)
.hasKey(NodeUpdater.HASH_KEY)) {
updatePackageJsonWithDefaultDependencies(options,
frontendDependencies, finder, packageJson);
}
return packageJson;
return getDefaultPackageJson(options, frontendDependencies,
finder, packageJson);
} catch (IOException e) {
getLogger().warn("Failed to read package.json", e);
}
} else {
JsonObject packageJson = getDefaultPackageJson(options,
frontendDependencies, finder);
frontendDependencies, finder, null);
if (packageJson != null) {
return packageJson;
}
}
return null;
}

private static void updatePackageJsonWithDefaultDependencies(
Options options, FrontendDependenciesScanner frontendDependencies,
ClassFinder finder, JsonObject packageJson) {
// If we have executed flow:clean-frontend we will have an empty
// package.json with no hash field.
JsonObject defaultPackageJson = getDefaultPackageJson(options,
frontendDependencies, finder);

// Add dependencies as it should be enough to have the expected
// dependencies in the package.json
final JsonObject defaultDependencies = defaultPackageJson
.getObject(NodeUpdater.DEPENDENCIES);
for (String key : defaultDependencies.keys()) {
packageJson.getObject(NodeUpdater.DEPENDENCIES).put(key,
defaultDependencies.getString(key));
}

// Add hash to package.json so we don't fail just because it is missing
final String hash = TaskUpdatePackages
.generatePackageJsonHash(packageJson);
if (!packageJson.hasKey(NodeUpdater.VAADIN_DEP_KEY)) {
packageJson.put(NodeUpdater.VAADIN_DEP_KEY, Json.createObject());
}
packageJson.getObject(NodeUpdater.VAADIN_DEP_KEY)
.put(NodeUpdater.HASH_KEY, hash);
}

protected static JsonObject getDefaultPackageJson(Options options,
FrontendDependenciesScanner frontendDependencies,
ClassFinder finder) {
ClassFinder finder, JsonObject packageJson) {
NodeUpdater nodeUpdater = new NodeUpdater(finder, frontendDependencies,
options) {
@Override
public void execute() {
}
};
try {
JsonObject packageJson = nodeUpdater.getPackageJson();
if (packageJson == null) {
packageJson = nodeUpdater.getPackageJson();
}
nodeUpdater.addVaadinDefaultsToJson(packageJson);
nodeUpdater.updateDefaultDependencies(packageJson);

final Map<String, String> applicationDependencies = frontendDependencies
.getPackages();

// Add application dependencies
for (Map.Entry<String, String> dep : applicationDependencies
.entrySet()) {
nodeUpdater.addDependency(packageJson, NodeUpdater.DEPENDENCIES,
dep.getKey(), dep.getValue());
}

final String hash = TaskUpdatePackages
.generatePackageJsonHash(packageJson);
packageJson.getObject(NodeUpdater.VAADIN_DEP_KEY)
Expand Down

0 comments on commit 3951cce

Please sign in to comment.