This repository has been archived by the owner on Jul 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
android_custom_resources.js
94 lines (74 loc) · 2.18 KB
/
android_custom_resources.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
var fs = require('fs');
var path = require('path');
var sourceDir = 'resources/android/custom';
var platformDir = 'platforms/android';
var resourceDirs = [
'res/drawable-ldpi',
'res/drawable-mdpi',
'res/drawable-hdpi',
'res/drawable-xhdpi',
'res/drawable-xxhdpi',
'res/drawable-xxxhdpi'
];
module.exports = function(ctx) {
if (ctx.opts.platforms.indexOf('android') < 0) {
return;
}
var Q = ctx.requireCordovaModule('q');
var deferred = Q.defer();
var androidPlatformDir = path.join(ctx.opts.projectRoot, platformDir);
var customResourcesDir = path.join(ctx.opts.projectRoot, sourceDir);
function copy(src, dest) {
var deferred = Q.defer();
fs.stat(src, function(err, stats) {
if (err || !stats.isFile()) {
return deferred.reject(err);
}
fs.stat(path.dirname(dest), function(err, stats) {
if (err || !stats.isDirectory()) {
return deferred.reject(err);
}
var rs = fs.createReadStream(src);
rs.on('error', function(err) {
console.error(err.stack);
deferred.reject(err);
});
var ws = fs.createWriteStream(dest);
ws.on('error', function(err) {
console.error(err.stack);
deferred.reject(err);
});
ws.on('close', function() {
deferred.resolve();
});
rs.pipe(ws);
});
});
return deferred.promise;
}
fs.stat(customResourcesDir, function(err, stats) {
if (err || !stats.isDirectory()) {
return deferred.resolve();
}
fs.readdir(customResourcesDir, function(err, files) {
var copies = [];
for (var i in files) {
for (var j in resourceDirs) {
var filePath = path.join(ctx.opts.projectRoot, sourceDir, files[i]);
var destPath = path.join(androidPlatformDir, resourceDirs[j], files[i]);
copies.push([filePath, destPath]);
}
}
copies.map(function(args) {
return copy.apply(copy, args);
});
Q.all(copies).then(function(r) {
deferred.resolve();
}, function(err) {
console.error(err.stack);
deferred.reject(err);
});
});
});
return deferred.promise;
}