/
write.js
125 lines (109 loc) · 3.12 KB
/
write.js
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
var fs = require("fs")
, Path = require("path")
var orig_writeFile = fs.writeFile
, orig_writeFileSync = fs.writeFileSync
, orig_exists = fs.exists
, orig_existsSync = fs.existsSync
, orig_readFile = fs.readFile
, orig_readFileSync = fs.readFileSync
, orig_renameFile = fs.rename
// stringified json -> stringified json
function json2json(data) {
var JSON5 = require("jju")
return JSON5.stringify(JSON.parse(data.toString("utf8")))
}
function update(file, old_data, new_data) {
if (file === "package.json5") {
if (!old_data) {
return json2json(new_data)
} else {
return require("jju").update(old_data, JSON.parse(new_data))
}
} else {
if (!old_data) {
return new_data
} else {
return require("jju").update(old_data, JSON.parse(new_data), {mode: "json"})
}
}
}
fs.rename = function(oldPath, newPath, cb) {
if (
Path.basename(newPath).indexOf("package.json") === 0 &&
newPath.indexOf("/package/package.json") === -1 &&
newPath.indexOf("/node_modules/") === -1
) {
return cb();
} else {
orig_renameFile.apply(fs, arguments)
}
}
fs.writeFile = function(path, data) {
if (
Path.basename(path).indexOf("package.json") === 0 &&
path.indexOf("/package/package.json") === -1 &&
path.indexOf("/node_modules/") === -1
) {
var orig_args = arguments
var args = Array.prototype.slice.apply(orig_args)
check_override(module.exports.formats.slice(0))
} else {
orig_writeFile.apply(fs, arguments)
}
function check_override(list) {
var alt_file = list.shift()
args[0] = Path.join(Path.dirname(path), alt_file)
orig_readFile(args[0], "utf8", function(err, olddata) {
if (err) {
// alt_file doesn't exist, looking for next one
if (list.length) {
return check_override(list)
} else {
alt_file = module.exports.formats[0]
args[0] = Path.join(Path.dirname(path), alt_file)
olddata = undefined
/* fallthrough */
}
}
// alt_file exists, so assuming that user works with this format
data = update(alt_file, olddata, data)
args[1] = data
orig_writeFile.apply(fs, args)
})
}
}
fs.writeFileSync = function(path, data) {
if (
Path.basename(path).indexOf("package.json") === 0 &&
path.indexOf("/package/package.json") === -1 &&
path.indexOf("/node_modules/") === -1
) {
var orig_args = arguments
var args = Array.prototype.slice.apply(orig_args)
return check_override(module.exports.formats.slice(0))
function check_override(list) {
var err
var alt_file = list.shift()
args[0] = Path.join(Path.dirname(path), alt_file)
try {
var olddata = orig_readFileSync(args[0], "utf8")
} catch(err) {
// alt_file doesn't exist, looking for next one
if (list.length) {
return check_override(list)
} else {
alt_file = module.exports.formats[0]
args[0] = Path.join(Path.dirname(path), alt_file)
olddata = undefined
/* fallthrough */
}
}
// alt_file exists, so assuming that user works with this format
data = update(alt_file, olddata, data)
args[1] = data
return orig_writeFileSync.apply(fs, args)
}
} else {
return orig_writeFileSync.apply(fs, arguments)
}
}