-
Notifications
You must be signed in to change notification settings - Fork 6
/
tag-rename
executable file
·73 lines (57 loc) · 1.46 KB
/
tag-rename
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
#!/usr/bin/env node
const fs = require('fs')
const forEach = require('async-foreach').forEach
const ArgumentParser = require('argparse').ArgumentParser
var parser = new ArgumentParser({
addHelp: true,
description: 'Add a tag to all language files'
})
let all = {}
let allData = {}
parser.addArgument('from', {
help: 'Tag, e.g. amenity=retaurant'
})
parser.addArgument('to', {
help: 'Tag, e.g. amenity=restaurant'
})
parser.addArgument([ '-d', '--description' ], {
help: 'Change description'
})
let args = parser.parseArgs()
fs.readdir('tags/', function (err, files) {
forEach(files, function (file) {
let m = file.match(/^(.*)\.json$/)
if (!m) {
return
}
let done = this.async()
fs.readFile('tags/' + file, (err, contents) => {
let data
try {
data = JSON.parse(contents)
} catch (e) {
return done()
}
if (!data) {
return done()
}
if ('tag:' + args.from in data) {
data['tag:' + args.to] = data['tag:' + args.from]
delete data['tag:' + args.from]
if (args.description) {
data['tag:' + args.to].description = args.description
}
}
var ret = {}
var keys = Object.keys(data)
keys.sort()
for (var i = 0; i < keys.length; i++) {
ret[keys[i]] = data[keys[i]]
}
fs.writeFile('tags/' + file, JSON.stringify(ret, null, ' ') + '\n', done)
})
},
allDone)
})
function allDone () {
}