blob: 9d4152770c89167a5fb4352c532e61586fe974f7 [file] [log] [blame]
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03001/*
Aurimas Liutikas79e555e2021-05-17 17:41:41 +00002 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03003 */
Roman Elizarov94fb2a32018-03-05 17:32:59 +03004
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
Roman Elizarovd2f4b2b2019-09-02 17:22:39 +030017def npmTemplateDir = file("$projectDir/npm")
Roman Elizarov94fb2a32018-03-05 17:32:59 +030018def npmDeployDir = file("$buildDir/npm")
Roman Elizarov94fb2a32018-03-05 17:32:59 +030019
20def authToken = prop("kotlin.npmjs.auth.token", "")
21def dryRun = prop("dryRun", "false")
22
Ilya Goncharov63910892020-05-28 17:58:48 +030023def jsLegacy = kotlin.targets.hasProperty("jsLegacy")
24 ? kotlin.targets.jsLegacy
25 : kotlin.targets.js
26
Roman Elizarovd01f9262018-09-05 13:02:23 +030027// Note: publish transformed files using dependency on sourceSets.main.output
28task preparePublishNpm(type: Copy) {
Roman Elizarov94fb2a32018-03-05 17:32:59 +030029 from(npmTemplateDir) {
Roman Elizarov504c8762019-04-24 12:04:02 +030030 // 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 }
Roman Elizarov94fb2a32018-03-05 17:32:59 +030035 }
Roman Elizarov504c8762019-04-24 12:04:02 +030036 // we must publish output that is transformed by atomicfu
Ilya Goncharov63910892020-05-28 17:58:48 +030037 from(jsLegacy.compilations.main.output.allOutputs)
Roman Elizarov94fb2a32018-03-05 17:32:59 +030038 into npmDeployDir
39}
40
41task publishNpm(type: NpmTask, dependsOn: [preparePublishNpm]) {
42 workingDir = npmDeployDir
Steve Elliottca095be2022-07-25 14:26:10 +000043
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
Roman Elizarov94fb2a32018-03-05 17:32:59 +030053 }
54}