-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpmTasks.gradle
182 lines (156 loc) · 4.91 KB
/
npmTasks.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
task setVersion {
def packageJson = file("package.json")
inputs.file(packageJson)
inputs.property("VERSION", VERSION)
outputs.file(packageJson)
doLast {
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parse(file("package.json"))
object.version = VERSION
if (object.dependencies != null) {
object.dependencies.keySet().each {
object.dependencies[it] = it.startsWith("@mcma/") ? VERSION : object.dependencies[it]
}
}
if (object.peerDependencies != null) {
object.peerDependencies.keySet().each {
object.peerDependencies[it] = it.startsWith("@mcma/") ? VERSION : object.peerDependencies[it]
}
}
if (object.devDependencies != null) {
object.devDependencies.keySet().each {
object.devDependencies[it] = it.startsWith("@mcma/") ? VERSION : object.devDependencies[it]
}
}
def json = JsonOutput.toJson(object)
def jsonPretty = JsonOutput.prettyPrint(json) + "\n"
packageJson.write(jsonPretty)
}
}
task npmInstall(type: Exec) {
dependsOn ":verifyNodeJS"
dependsOn setVersion
inputs.file("package.json")
outputs.file("package-lock.json")
outputs.dir("node_modules")
commandLine npmExecutable
args "install"
}
task npmUpdate(type: Exec) {
dependsOn ":verifyNodeJS"
inputs.file "package.json"
inputs.property("todaysDate", new Date().clearTime())
outputs.upToDateWhen { true }
commandLine npmExecutable
args "update"
}
task tsc(type: Exec) {
dependsOn ":verifyTsc"
dependsOn npmInstall
inputs.file("tsconfig.json")
inputs.file("package.json")
inputs.dir("lib")
inputs.dir("node_modules")
outputs.dir("dist")
commandLine tscExecutable
doFirst {
delete "dist"
}
}
task npmPublish(type: Exec) {
dependsOn ":verifyNodeJS"
dependsOn tsc
inputs.file("package.json")
commandLine npmExecutable
args "publish", "--access", "public", "--registry", "https://registry.npmjs.org"
def jsonSlurper = new JsonSlurper()
def packageJson = jsonSlurper.parse(file("package.json"))
String packageName = packageJson.name + "@" + VERSION
onlyIf {
// check if package is already published
def stdout = new ByteArrayOutputStream()
def stderr = new ByteArrayOutputStream()
try {
providers.exec {
commandLine npmExecutable
args "view", packageName, "--registry", "https://registry.npmjs.org"
standardOutput = stdout
errorOutput = stderr
}
} catch (ignored) {
}
return stdout.toString().trim().isEmpty()
}
doLast {
def tempDir = File.createTempDir()
boolean available = false
while (!available) {
try {
sleep(15000)
providers.exec {
commandLine npmExecutable
args "install", packageName, "--no-save"
workingDir = tempDir
}
available = true
println "Package " + packageName + " available"
} catch (ignored) {
println "Package " + packageName + " not yet available"
}
}
delete tempDir
}
}
task npmPublishLocal(type: Exec) {
dependsOn ":verifyNodeJS"
dependsOn setVersion
dependsOn tsc
inputs.file("package.json")
commandLine npmExecutable
args "publish", "--access", "public", "--registry", "http://localhost:4873"
def jsonSlurper = new JsonSlurper()
def packageJson = jsonSlurper.parse(file("package.json"))
String packageName = packageJson.name + "@" + VERSION
onlyIf {
// check if package is already published
def stdout = new ByteArrayOutputStream()
def stderr = new ByteArrayOutputStream()
try {
providers.exec {
commandLine npmExecutable
args "view", packageName, "--registry", "http://localhost:4873"
standardOutput = stdout
errorOutput = stderr
}
} catch (ignored) {
}
return stdout.toString().trim().isEmpty()
}
}
task clean(type: Delete) {
delete "dist"
delete "node_modules"
}
task cleanPackageLocks(type: Delete) {
delete "package-lock.json"
}
task npmAudit(type: Exec) {
dependsOn ":verifyNodeJS"
inputs.file("package.json")
commandLine npmExecutable
args "audit"
}
task npmAuditFix(type: Exec) {
dependsOn ":verifyNodeJS"
inputs.file("package.json")
commandLine npmExecutable
args "audit", "fix"
}
task npmForceAuditFix(type: Exec) {
dependsOn ":verifyNodeJS"
inputs.file("package.json")
commandLine npmExecutable
args "audit", "fix", "--force"
}