forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile-info.js
173 lines (155 loc) · 4.46 KB
/
file-info.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
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
'use strict';
var fs = require('fs');
var Promise = require('../ext/promise');
var readFile = Promise.denodeify(fs.readFile);
var lstat = Promise.denodeify(fs.stat);
var chalk = require('chalk');
var EditFileDiff = require('./edit-file-diff');
var EOL = require('os').EOL;
var isBinaryFile = require('isbinaryfile');
var template = require('lodash/template');
var canEdit = require('../utilities/open-editor').canEdit;
function processTemplate(content, context) {
var options = {
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
escape: /<%-([\s\S]+?)%>/g
};
return template(content, options)(context);
}
function diffHighlight(line) {
if (line[0] === '+') {
return chalk.green(line);
} else if (line[0] === '-') {
return chalk.red(line);
} else if (line.match(/^@@/)) {
return chalk.cyan(line);
} else {
return line;
}
}
FileInfo.prototype.confirmOverwrite = function(path) {
var promptOptions = {
type: 'expand',
name: 'answer',
default: false,
message: chalk.red('Overwrite') + ' ' + path + '?',
choices: [
{ key: 'y', name: 'Yes, overwrite', value: 'overwrite' },
{ key: 'n', name: 'No, skip', value: 'skip' },
{ key: 'd', name: 'Diff', value: 'diff' }
]
};
if (canEdit()) {
promptOptions.choices.push({ key: 'e', name: 'Edit', value: 'edit' });
}
return this.ui.prompt(promptOptions)
.then(function(response) {
return response.answer;
});
};
FileInfo.prototype.displayDiff = function() {
var info = this,
jsdiff = require('diff');
return Promise.hash({
input: this.render(),
output: readFile(info.outputPath)
}).then(function(result) {
var diff = jsdiff.createPatch(
info.outputPath, result.output.toString(), result.input
);
var lines = diff.split('\n');
for (var i = 0; i < lines.length; i++) {
info.ui.write(
diffHighlight(lines[i] + EOL)
);
}
});
};
function FileInfo(options) {
this.action = options.action;
this.outputPath = options.outputPath;
this.displayPath = options.displayPath;
this.inputPath = options.inputPath;
this.templateVariables = options.templateVariables;
this.ui = options.ui;
}
FileInfo.prototype.render = function() {
var path = this.inputPath,
context = this.templateVariables;
if (!this.rendered) {
this.rendered = readFile(path).then(function(content) {
return lstat(path).then(function(fileStat) {
if (isBinaryFile(content, fileStat.size)) {
return content;
} else {
try {
return processTemplate(content.toString(), context);
} catch (err) {
err.message += ' (Error in blueprint template: ' + path + ')';
throw err;
}
}
});
});
}
return this.rendered;
};
FileInfo.prototype.checkForConflict = function() {
return new Promise(function (resolve, reject) {
fs.exists(this.outputPath, function (doesExist, error) {
if (error) {
reject(error);
return;
}
var result;
if (doesExist) {
result = Promise.hash({
input: this.render(),
output: readFile(this.outputPath)
}).then(function(result) {
var type;
if (result.input === result.output.toString()) {
type = 'identical';
} else {
type = 'confirm';
}
return type;
}.bind(this));
} else {
result = 'none';
}
resolve(result);
}.bind(this));
}.bind(this));
};
FileInfo.prototype.confirmOverwriteTask = function() {
var info = this;
return function() {
return new Promise(function(resolve, reject) {
function doConfirm() {
info.confirmOverwrite(info.displayPath).then(function(action) {
if (action === 'diff') {
info.displayDiff().then(doConfirm, reject);
} else if (action === 'edit') {
var editFileDiff = new EditFileDiff({info: info});
editFileDiff.edit().then(function() {
info.action = action;
resolve(info);
}).catch(function() {
doConfirm()
.finally(function() {
resolve(info);
});
});
} else {
info.action = action;
resolve(info);
}
}, reject);
}
doConfirm();
});
}.bind(this);
};
module.exports = FileInfo;