22 * Copyright 2017-2026 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33 */
44
5- import org.gradle.api.Project
5+ import org.gradle.api.*
66import org.gradle.kotlin.dsl.the
7- import org.gradle.kotlin.dsl.withType
8- import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnLockMismatchReport
7+ import kotlin.io.path.moveTo
8+ import kotlin.io.path.readText
9+ import kotlin.io.path.writeText
10+ import kotlin.text.replace
911
1012const val DEFAULT_YARN_REGISTRY = " https://registry.yarnpkg.com"
1113const val NPM_REGISTRY_CACHE = " https://cache-redirector.jetbrains.com/registry.npmjs.org"
@@ -14,18 +16,44 @@ const val YARN_DIST_CACHE = "https://cache-redirector.jetbrains.com/github.com/y
1416
1517fun Project.configureJsCacheRedirector () {
1618 rootProject.plugins.withType(org.jetbrains.kotlin.gradle.targets.js.yarn.YarnPlugin ::class .java) {
17- rootProject.the< org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension > ().restoreYarnLockTaskProvider.configure {
18- doLast {
19- // yarn 1.x doesn't and won't support overriding registry used in yarn.lock, so we need to replace it manually
20- // https://github.com/yarnpkg/yarn/issues/6436#issuecomment-426728911
21- val lockFile = outputFile.get()
22- lockFile.writeText(lockFile.readText().replace(DEFAULT_YARN_REGISTRY , NPM_REGISTRY_CACHE ))
19+ rootProject.the< org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension > ().apply {
20+ restoreYarnLockTaskProvider.configure {
21+ doLast {
22+ // yarn 1.x doesn't and won't support overriding registry used in yarn.lock, so we need to replace it manually
23+ // https://github.com/yarnpkg/yarn/issues/6436#issuecomment-426728911
24+ val lockFile = outputFile.get()
25+ lockFile.writeText(lockFile.readText().replace(DEFAULT_YARN_REGISTRY , NPM_REGISTRY_CACHE ))
26+ }
27+ }
28+ // A hacky workaround helping to keep the original URLs in the lock file.
29+ storeYarnLockTaskProvider.configure {
30+ val originalLockFile = inputFile.get().asFile.toPath()
31+ val tempReplacement = originalLockFile.resolveSibling(" ${originalLockFile.fileName} .${System .nanoTime()} " )
32+
33+ doFirst {
34+ // Create a copy of the lock file with preserved URLs,
35+ // replace cache-redirector URLs with the original once in the yarn.lock file.
36+ originalLockFile.moveTo(tempReplacement)
37+ originalLockFile.writeText(
38+ tempReplacement.readText().replace(NPM_REGISTRY_CACHE , DEFAULT_YARN_REGISTRY )
39+ )
40+ }
41+ // In between doFirst and doLast, the task will compare the lock file with the checked-in one.
42+ doLast {
43+ // Rename the lock file with cache-redirector URLs back to yarn.lock
44+ tempReplacement.moveTo(originalLockFile, true )
45+ }
2346 }
2447 }
25- rootProject.the< org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootEnvSpec > ().downloadBaseUrl.set(YARN_DIST_CACHE )
48+
49+ rootProject.the< org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootEnvSpec > ().downloadBaseUrl.set(
50+ YARN_DIST_CACHE
51+ )
2652 }
2753
2854 rootProject.plugins.withType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin ::class .java) {
29- rootProject.the< org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsEnvSpec > ().downloadBaseUrl.set(NODE_DIST_CACHE )
55+ rootProject.the< org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsEnvSpec > ().downloadBaseUrl.set(
56+ NODE_DIST_CACHE
57+ )
3058 }
3159}
0 commit comments