This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
87 lines (73 loc) · 2.45 KB
/
index.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
const { promisify } = require('util')
const fs = require('fs')
const rimraf = require('rimraf')
const consola = require('consola')
const ncp = require('ncp')
const tmp = require('tmp-promise')
const asyncRimRaf = promisify(rimraf)
const asyncNcp = promisify(ncp)
const generateIcons = require('./lib/generate-icons')
const generateBuildFile = require('./lib/generate-build-file')
const { generateAssetLinksFile } = require('./lib/generate-asset-links-file')
const moduleRoot = __dirname
module.exports = function nuxtTwa(options) {
const { rootDir } = this.nuxt.options
const pckg = require(rootDir + '/package.json')
const defaultOptions = {
applicationId: `com.${pckg.name}.${pckg.name}`,
launcherName: pckg.name,
versionCode: Number(String(pckg.version).replace(/\./g, '')),
versionName: pckg.version,
iconPath: '/static/icon.png',
distFolder: rootDir + '/.nuxt/dist/client',
androidFolder: rootDir + '/android',
statusBarColor: options.statusBarColor || '#fff'
}
this.nuxt.hook('build:before', async () => {
options = {
...defaultOptions,
...options,
}
let tempDir
let tmpRes
try {
tmpRes = await tmp.dir()
tempDir = tmpRes.path + '/android'
} catch (err) {
throw ('Temperary directory generation failed:', err)
}
try {
await asyncNcp(moduleRoot + '/android', tempDir)
} catch (err) {
throw ('Temporary directory copy failed', err)
}
try {
await generateBuildFile(options, tempDir + '/app/build.gradle')
consola.success('TWA build.gradle generated')
} catch (err) {
throw ('Generating build file failed', err)
}
try {
const iconPath = rootDir + options.iconPath
const androidIconsPath = tempDir + '/app/src/main/res'
await generateIcons(iconPath, androidIconsPath)
} catch (err) {
return consola.error('Generating icons failed', err)
}
try {
await asyncNcp(tempDir, options.androidFolder)
asyncRimRaf(tempDir)
} catch (err) {
return consola.log('Copy temporary directory to root failed', err)
}
})
this.nuxt.hook('build:done', () => {
generateAssetLinksFile(options, rootDir + options.distFolder)
consola.success(`TWA generated successfully`)
})
this.nuxt.hook('generate:done', () => {
generateAssetLinksFile(options, rootDir + options.distFolder)
consola.success(`TWA generated successfully`)
})
}
module.exports.meta = require('./package.json')