-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcreate-cordova.js
112 lines (106 loc) · 3.14 KB
/
create-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
const exec = require('exec-sh');
const path = require('path');
const rm = require('rimraf');
const cpy = require('cpy');
const fse = require('../../utils/fs-extra');
const generateConfigXml = require('./generate-config-xml');
module.exports = (options) => {
const cwd = options.cwd || process.cwd();
const isRunningInCwd = cwd === process.cwd();
const { pkg, name, cordova } = options;
// eslint-disable-next-line
return new Promise(async (resolve, reject) => {
try {
if (!isRunningInCwd) {
await exec.promise(
`cd ${cwd.replace(/ /g, '\\ ')} && cordova create ${cordova.folder} ${pkg} XXXXXX`,
true,
);
} else {
await exec.promise(`cordova create ${cordova.folder} ${pkg} XXXXXX`, true);
}
} catch (err) {
reject(err);
return;
}
// Modify Name
['package.json', 'config.xml'].forEach((f) => {
const contents = fse.readFileSync(path.resolve(cwd, cordova.folder, f));
fse.writeFileSync(path.resolve(cwd, cordova.folder, f), contents.replace(/XXXXXX/g, name));
});
// Install plugins
const plugins = cordova.plugins; // eslint-disable-line
// Install cordova plugins
if (plugins.length) {
try {
if (!isRunningInCwd) {
await exec.promise(
`cd ${cwd.replace(/ /g, '\\ ')} && cd ${
cordova.folder
} && cordova plugin add ${plugins.join(' ')}`,
true,
);
} else {
await exec.promise(
`cd ${cordova.folder} && cordova plugin add ${plugins.join(' ')}`,
true,
);
}
} catch (err) {
reject(err);
return;
}
}
// Modify config.xml
let configXmlContent = fse.readFileSync(path.resolve(cwd, cordova.folder, 'config.xml'));
configXmlContent = `${configXmlContent.split('</widget>')[0]}${generateConfigXml(
options,
)}</widget>`;
fse.writeFileSync(path.resolve(cwd, cordova.folder, 'config.xml'), configXmlContent);
// Upload res files
try {
await new Promise((subResolve, subReject) => {
rm(path.resolve(cwd, cordova.folder, 'res'), (err) => {
if (err) subReject(err);
else subResolve();
});
});
} catch (err) {
reject(err);
return;
}
try {
await cpy('**/*.*', path.resolve(cwd, cordova.folder, 'res'), {
parents: true,
cwd: path.resolve(__dirname, 'common', 'cordova-res'),
});
} catch (err) {
reject(err);
return;
}
// Add cordova platforms
const platforms = cordova.platforms.map((platform) => {
if (platform === 'ios') return 'ios@latest';
return platform;
});
try {
if (!isRunningInCwd) {
await exec.promise(
`cd ${cwd.replace(/ /g, '\\ ')} && cd ${
cordova.folder
} && cordova platform add ${platforms.join(' ')}`,
true,
);
} else {
await exec.promise(
`cd ${cordova.folder} && cordova platform add ${platforms.join(' ')}`,
true,
);
}
} catch (err) {
reject(err);
return;
}
resolve();
});
};