-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.v
More file actions
189 lines (167 loc) · 4.17 KB
/
Copy pathnode.v
File metadata and controls
189 lines (167 loc) · 4.17 KB
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
183
184
185
186
187
188
189
import os { create, exists, getenv_opt, home_dir, join_path_single, read_file, read_lines, write_file }
import prantlf.debug { rwd }
import prantlf.jany { Any, any_null }
import prantlf.json { StringifyOpts, parse, stringify_opt }
import prantlf.osutil { execute, find_file }
fn set_package_version(ver string, pkg_dir string, opts &Opts) ! {
pkg_file := join_path_single(pkg_dir, 'package.json')
if !exists(pkg_file) {
return
}
pkg := read_json(pkg_file)!
d.log('setting package version to "%s"', ver)
mut root := pkg.object()!
root['version'] = ver
lck_file := join_path_single(pkg_dir, 'package-lock.json')
lck_is := exists(lck_file)
mut lck := any_null()
if lck_is {
lck = read_json(lck_file)!
d.log('setting package lock version to "%s"', ver)
mut lckroot := lck.object()!
lckroot['version'] = ver
lck.set('packages."".version', Any(ver))!
}
if !opts.dry_run {
d.log_str('stringifying package.json')
text := stringify_opt(pkg, &StringifyOpts{
pretty: true
})
if d.is_enabled() {
len := if text.len > 250 {
250
} else {
text.len
}
d.log_str(text[0..len])
}
dpkg_file := d.rwd(pkg_file)
d.log('writing file "%s"', dpkg_file)
write_file(pkg_file, text)!
if lck_is {
dlck_file := d.rwd(lck_file)
d.log('writing file "%s"', dlck_file)
text2 := stringify_opt(lck, &StringifyOpts{
pretty: true
})
write_file(lck_file, text2)!
}
}
if opts.verbose {
mut mode := if opts.dry_run {
' (dry-run)'
} else {
''
}
if lck_is {
mode = ' and "${rwd(lck_file)}"${mode}'
}
println('updated version in "${rwd(pkg_file)}"${mode}')
}
}
fn publish_package(ver string, opts &Opts) ! {
_, _ := find_package() or { return }
was_authenticated, glob_npmrc, npmrc := authenticate(opts)!
defer {
if was_authenticated {
set_auth_token(glob_npmrc, npmrc, '') or { eprintln(err.msg()) }
}
}
mode := if opts.dry_run {
' (dry-run)'
} else {
''
}
if opts.yes || confirm('publish version ${ver}${mode}')! {
mut extra_args := if opts.verbose { ' --verbose' } else { ' --quiet' }
if opts.dry_run {
extra_args += ' --dry-run'
}
out := execute('npm publish --access public${extra_args}')!
println(out)
}
}
fn authenticate(opts &Opts) !(bool, string, []string) {
mut is_authenticated := false
mut npmrc := []string{}
glob_npmrc := join_path_single(home_dir(), '.npmrc')
if exists(glob_npmrc) {
npmrc = read_npmrc(glob_npmrc)!
is_authenticated = has_auth_token(npmrc)!
}
if !is_authenticated {
pkg_dir, _ := find_package()!
loc_npmrc := join_path_single(pkg_dir, '.npmrc')
if exists(loc_npmrc) {
npmrc2 := read_npmrc(loc_npmrc)!
is_authenticated = has_auth_token(npmrc2)!
}
}
if is_authenticated {
return false, '', []string{}
}
if opts.npm_auth {
token := if opts.npm_token != '' {
opts.npm_token
} else {
get_npm_token()!
}
set_auth_token(glob_npmrc, npmrc, token)!
}
return true, glob_npmrc, npmrc
}
fn read_npmrc(file string) ![]string {
d.log('reading "%s"', file)
return read_lines(file)!
}
fn has_auth_token(npmrc []string) !bool {
for line in npmrc {
if line.contains('//registry.npmjs.org/:_authToken') {
d.log_str('is authenticated')
return true
}
d.log('ignoring line "%s"', line)
}
d.log_str('is not authenticated')
return false
}
fn set_auth_token(file string, lines []string, token string) ! {
d.log('creating "%s"', file)
mut out := create(file)!
defer {
out.close()
}
d.log_str('writing new contents')
for line in lines {
out.writeln(line)!
}
if token != '' {
out.write_string('//registry.npmjs.org/:_authToken=')!
out.writeln(token)!
}
out.close()
d.log_str('file written')
}
fn find_package() !(string, string) {
return find_file('package.json') or { error('package.json not found') }
}
fn read_json(file string) !Any {
dfile := d.rwd(file)
d.log('reading file "%s"', dfile)
text := read_file(file)!
if d.is_enabled() {
len := if text.len > 250 {
250
} else {
text.len
}
d.log_str(text[0..len])
}
d.log_str('parsing package.json')
return parse(text)!
}
fn get_npm_token() !string {
return getenv_opt('NODE_AUTH_TOKEN') or {
getenv_opt('NPM_TOKEN') or { return error('neither NODE_AUTH_TOKEN nor NPM_TOKEN found') }
}
}