-
Notifications
You must be signed in to change notification settings - Fork 153
/
beforedep.js
112 lines (91 loc) · 4.08 KB
/
beforedep.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
module.exports = function (ctx) {
const Q = require('q'),
path = require('path'),
fs = require('fs'),
pRoot = ctx.opts.projectRoot,
wwwFolder = path.resolve(pRoot, "www/"),
moveAllTo = path.resolve(wwwFolder, "platform_cordova_files/")
const sys = {
getPlatformDir(platform, cordovaFile) {
if( platform === 'android')
return path.resolve(__dirname, `../platforms/${platform}/platform_www/${cordovaFile ? "cordova.js" : ""}`)
else
return path.resolve(__dirname, `../platforms/${platform}/www/${cordovaFile ? "cordova.js" : ""}`)
},
copyRecursiveSync(src, dest) {
let exists = fs.existsSync(src),
stats = exists && fs.statSync(src),
isDirectory = exists && stats.isDirectory()
if (exists && isDirectory) {
if(!fs.existsSync(dest))
fs.mkdirSync(dest)
fs.readdirSync(src).forEach((childItemName) => {
sys.copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName))
})
} else
fs.linkSync(src, dest)
},
checkAndCopy(platforms) {
platforms.forEach((platform) => {
if (fs.existsSync(sys.getPlatformDir(platform)) && fs.existsSync(sys.getPlatformDir(platform, true))) {
let movePath = path.resolve(moveAllTo, platform + '/')
if (!fs.existsSync(movePath))
fs.mkdirSync(movePath)
sys.copyRecursiveSync(sys.getPlatformDir(platform), movePath)
}
})
},
copyMainXml() {
fs.createReadStream(path.resolve(__dirname, "../config.xml")).pipe(fs.createWriteStream(path.resolve(wwwFolder, "config.xml")))
},
checkOption(name) {
return (
typeof ctx.opts !== "undefined" &&
typeof ctx.opts.options !== "undefined" &&
typeof ctx.opts.options[name] !== "undefined" &&
ctx.opts.options[name] === true
)
},
checkArgv(name) {
return (
typeof ctx.opts !== "undefined" &&
typeof ctx.opts.options !== "undefined" &&
typeof ctx.opts.options.argv !== "undefined" &&
(
Array.isArray(ctx.opts.options.argv) &&
ctx.opts.options.argv.indexOf(name) > -1 ||
ctx.opts.options.argv[name] === true
)
)
},
isFoundInCmdline( cmdCommand ) {
return (
ctx.cmdLine.indexOf(`cordova ${cmdCommand}`) > -1 ||
ctx.cmdLine.indexOf(`phonegap ${cmdCommand}`) > -1
)
}
}
let deferral = new Q.defer(),
isRun = sys.isFoundInCmdline('run'),
isEmulate = sys.isFoundInCmdline('emulate'),
isPrepare = sys.isFoundInCmdline('prepare'),
isServe = sys.isFoundInCmdline('serve'),
isLiveReload = sys.checkArgv('--live-reload') || sys.checkArgv('--lr') || sys.checkArgv('lr') || sys.checkArgv('live-reload')
if (ctx.opts.platforms.length === 0 && !isPrepare) {
console.log("Update happened. Skipping...")
deferral.resolve()
}
else {
if (isServe || (isRun || isEmulate) && isLiveReload) {
console.log("Copying platform cordova files...")
if (!fs.existsSync(moveAllTo))
fs.mkdirSync(moveAllTo)
sys.checkAndCopy(['android', 'ios', 'browser'])
sys.copyMainXml()
console.log("All platform files copied to www/platform_cordova_files/ directory!")
} else
console.log("Dev server not running. Skipping...")
deferral.resolve()
}
return deferral.promise
}