-
Notifications
You must be signed in to change notification settings - Fork 2
/
steal-cordova.js
149 lines (129 loc) · 3.69 KB
/
steal-cordova.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
var asap = require("pdenodeify");
var assert = require("assert");
var cheerio = require("cheerio");
var cli = require("./cordovacli")();
var extend = require("xtend");
var fse = require("fs-extra");
var path = require("path");
var Platform = require("./platform");
var Promise = require("es6-promise").Promise;
var copy = asap(fse.copy);
var readFile = asap(fse.readFile);
var writeFile = asap(fse.writeFile);
var ensureFile = asap(fse.ensureFile);
var glob = asap(require("multi-glob").glob);
var slice = Array.prototype.slice;
function StealCordova(options) {
this.options = options;
var stealCordova = this;
this.android = new Platform(this, "android");
this.ios = new Platform(this, "ios");
// Bind the public method so people can do
// stealToolsBuild().then(stealCordova.build);
this.build = this.build.bind(this);
}
StealCordova.prototype = {
init: function(opts){
return this.runCli(opts, {
command: ["create", "platform", "plugin"]
});
},
// Only initialize if it hasn't already been.
initIfNeeded: function(opts){
var buildPath = (opts && opts.path) || this.options.path;
assert(!!buildPath, "Path needs to be provided");
var p = path.resolve(buildPath);
var stealCordova = this;
return new Promise(function(resolve, reject){
fse.exists(p, function(doesExist){
if(doesExist) {
resolve();
} else {
stealCordova.init(opts)
.then(function() {
resolve();
})
.catch(reject);
}
});
});
},
copyProductionFiles: function(bundlesPath){
var stealCordova = this;
var cordovaOptions = this.options;
var dest = path.resolve(cordovaOptions.path, "www/");
var files = cordovaOptions.files || cordovaOptions.glob || [];
files.unshift(bundlesPath);
return glob(files).then(function(files){
var copies = files.map(function(file){
return copy(file, destPath(file))
.catch(function(error) {
console.error('First attempt at copying', file,
'failed because of', error, 'Trying again...');
return copy(file, destPath(file));
});
});
// copy the index.html file
copies.push(stealCordova.copyIndexHTML(dest));
return Promise.all(copies);
});
function destPath(p){
return path.join(dest, path.relative(process.cwd(), p));
}
},
copyIndexHTML: function(dest){
var index = path.resolve(this.options.index);
var indexDest = path.join(dest, "index.html");
var readAndEnsure = Promise.all([
readFile(index, "utf8"),
ensureFile(indexDest)
])
.then(function(resolutions){
// Set the steal script tag's "env" variable to cordova-production
var txt = resolutions[0];
var $ = cheerio.load(txt);
var stealScript;
$("script").each(function(idx, el){
var $el = $(el);
var src = $el.attr("src");
if(/steal\.production\.js/.test(src)) {
stealScript = $el;
return false;
}
});
if(stealScript) {
stealScript.attr("env", "cordova-production");
return writeFile(indexDest, $.html(), "utf8")
} else {
return copy(index, indexDest);
}
});
},
platform: function(opts){
return this.runCli(opts, {
command: ["platform"]
});
},
build: function(buildResult){
var stealCordova = this;
return stealCordova.initIfNeeded().then(function(){
var config = buildResult.configuration;
var destPath = config.dest || config.bundlesPath;
return stealCordova.copyProductionFiles(destPath);
}).then(function(){
return stealCordova.runCli({}, {
command: ["build"],
args: ["--release"]
});
});
},
runCli: function(/* opts */){
var args = slice.call(arguments);
args.unshift(this.options);
args.unshift({});
var opts = extend.apply(null, args);
var promise = cli(opts);
return promise;
}
};
module.exports = StealCordova;