-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
transforms.js
104 lines (95 loc) · 2.63 KB
/
transforms.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
const {Transform} = require('stream');
const fs = require('fs');
const path = require('path');
function extTransform(properties, features) {
return new Transform({
objectMode: true,
transform: function(file, env, cb) {
if (file.isBuffer()) {
if (file.extname === '.ext') {
// change .ext to .ts or .js file
file.extname = features.includes('typescript') ? '.ts' : '.js';
}
}
cb(null, file);
}
});
};
// Special treatment of 'project.csproj' file
function skipDotnetCsprojIfExists(properties, features, targetDir) {
let hasCsproj = false;
try {
hasCsproj = fs.readdirSync(targetDir).some(
f => f.endsWith('.csproj')
);
} catch (e) {
//
}
return new Transform({
objectMode: true,
transform: function(file, env, cb) {
if (file.isBuffer() && hasCsproj && file.relative === 'project.csproj') {
// skip 'project.csproj' file
cb();
return;
}
cb(null, file);
}
});
}
function isFile(path) {
try {
return fs.statSync(path).isFile();
} catch (err) {
// ignore
return false;
}
};
function instructionsForSkippedFiles(properties, features, targetDir) {
const skipped = [];
const instructions = {};
return new Transform({
objectMode: true,
transform: function(file, env, cb) {
if (file.isBuffer()) {
if (file.writePolicy === 'skip' && isFile(path.join(targetDir, file.relative))) {
// This file is to be skipped
skipped.push(file.relative);
}
if (file.basename.endsWith('__instructions')) {
instructions[file.relative.slice(0, -14)] = file.contents.toString('utf8');
// remove instruction files
cb();
return;
}
}
cb(null, file);
},
// flush
flush: function(cb) {
let all = '';
skipped.forEach(filename => {
const instruction = instructions[filename];
if (instruction) {
all += instruction + '\n';
}
});
if (!all) {
cb();
return;
}
const instFileName = 'instructions.txt';
console.warn('Manual changes are necessary:\n');
console.log(all + '\n');
console.info(`If you would like to do this at a later time, we've written these instructions to a file called '${instFileName}' in the project directory for you.\n`);
// Avoid using Vinyl file, in order to have zero dependencies.
fs.writeFileSync(path.join(process.cwd(), targetDir, instFileName), all);
cb();
}
});
}
exports.append = [
extTransform,
skipDotnetCsprojIfExists,
instructionsForSkippedFiles
];