1/* 2 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5def prop(name, defVal) { 6 def value = project.properties[name] 7 if (value == null) return defVal 8 return value 9} 10 11def distTag(version) { 12 def i = version.indexOf('-') 13 if (i > 0) return version.substring(i + 1) 14 return "latest" 15} 16 17def npmTemplateDir = file("$projectDir/npm") 18def npmDeployDir = file("$buildDir/npm") 19 20def authToken = prop("kotlin.npmjs.auth.token", "") 21def dryRun = prop("dryRun", "false") 22 23def jsLegacy = kotlin.targets.hasProperty("jsLegacy") 24 ? kotlin.targets.jsLegacy 25 : kotlin.targets.js 26 27// Note: publish transformed files using dependency on sourceSets.main.output 28task preparePublishNpm(type: Copy) { 29 from(npmTemplateDir) { 30 // Postpone expansion of package.json until we configure version property in build.gradle 31 def copySpec = it 32 afterEvaluate { 33 copySpec.expand(project.properties + [kotlinDependency: "\"kotlin\": \"$kotlin_version\""]) 34 } 35 } 36 // we must publish output that is transformed by atomicfu 37 from(jsLegacy.compilations.main.output.allOutputs) 38 into npmDeployDir 39} 40 41task publishNpm(type: NpmTask, dependsOn: [preparePublishNpm]) { 42 workingDir = npmDeployDir 43 44 def npmDeployTag = distTag(version) 45 def deployArgs = ['publish', 46 "--//registry.npmjs.org/:_authToken=$authToken", 47 "--tag=$npmDeployTag"] 48 if (dryRun == "true") { 49 println("$npmDeployDir \$ npm arguments: $deployArgs") 50 args = ['pack'] 51 } else { 52 args = deployArgs 53 } 54} 55