@bubblewrap/core 1.13.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +203 -0
- package/README.md +63 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +56 -0
- package/dist/lib/BufferedLog.d.ts +20 -0
- package/dist/lib/BufferedLog.js +83 -0
- package/dist/lib/Config.d.ts +9 -0
- package/dist/lib/Config.js +50 -0
- package/dist/lib/DigitalAssetLinks.d.ts +3 -0
- package/dist/lib/DigitalAssetLinks.js +37 -0
- package/dist/lib/FetchUtils.d.ts +17 -0
- package/dist/lib/FetchUtils.js +79 -0
- package/dist/lib/GooglePlay.d.ts +41 -0
- package/dist/lib/GooglePlay.js +92 -0
- package/dist/lib/GradleWrapper.d.ts +31 -0
- package/dist/lib/GradleWrapper.js +62 -0
- package/dist/lib/ImageHelper.d.ts +37 -0
- package/dist/lib/ImageHelper.js +112 -0
- package/dist/lib/Log.d.ts +88 -0
- package/dist/lib/Log.js +114 -0
- package/dist/lib/Result.d.ts +46 -0
- package/dist/lib/Result.js +97 -0
- package/dist/lib/ShortcutInfo.d.ts +32 -0
- package/dist/lib/ShortcutInfo.js +84 -0
- package/dist/lib/TwaGenerator.d.ts +39 -0
- package/dist/lib/TwaGenerator.js +362 -0
- package/dist/lib/TwaManifest.d.ts +234 -0
- package/dist/lib/TwaManifest.js +384 -0
- package/dist/lib/androidSdk/AndroidSdkTools.d.ts +87 -0
- package/dist/lib/androidSdk/AndroidSdkTools.js +226 -0
- package/dist/lib/errors/ValidatePathError.d.ts +9 -0
- package/dist/lib/errors/ValidatePathError.js +16 -0
- package/dist/lib/features/AppsFlyerFeature.d.ts +8 -0
- package/dist/lib/features/AppsFlyerFeature.js +70 -0
- package/dist/lib/features/EmptyFeature.d.ts +28 -0
- package/dist/lib/features/EmptyFeature.js +44 -0
- package/dist/lib/features/Feature.d.ts +120 -0
- package/dist/lib/features/Feature.js +17 -0
- package/dist/lib/features/FeatureManager.d.ts +36 -0
- package/dist/lib/features/FeatureManager.js +137 -0
- package/dist/lib/features/FirstRunFlagFeature.d.ts +8 -0
- package/dist/lib/features/FirstRunFlagFeature.js +43 -0
- package/dist/lib/features/LocationDelegationFeature.d.ts +7 -0
- package/dist/lib/features/LocationDelegationFeature.js +32 -0
- package/dist/lib/features/PlayBillingFeature.d.ts +7 -0
- package/dist/lib/features/PlayBillingFeature.js +53 -0
- package/dist/lib/jdk/JarSigner.d.ts +13 -0
- package/dist/lib/jdk/JarSigner.js +55 -0
- package/dist/lib/jdk/JdkHelper.d.ts +55 -0
- package/dist/lib/jdk/JdkHelper.js +127 -0
- package/dist/lib/jdk/KeyTool.d.ts +57 -0
- package/dist/lib/jdk/KeyTool.js +136 -0
- package/dist/lib/mock/MockLog.d.ts +37 -0
- package/dist/lib/mock/MockLog.js +70 -0
- package/dist/lib/types/WebManifest.d.ts +44 -0
- package/dist/lib/types/WebManifest.js +17 -0
- package/dist/lib/util.d.ts +82 -0
- package/dist/lib/util.js +279 -0
- package/dist/lib/wrappers/svg2img.d.ts +14 -0
- package/dist/lib/wrappers/svg2img.js +39 -0
- package/package.json +58 -0
- package/template_project/app/.gitignore +1 -0
- package/template_project/app/build.gradle +211 -0
- package/template_project/app/src/main/AndroidManifest.xml +204 -0
- package/template_project/app/src/main/java/Application.java +35 -0
- package/template_project/app/src/main/java/DelegationService.java +18 -0
- package/template_project/app/src/main/java/LauncherActivity.java +62 -0
- package/template_project/app/src/main/res/drawable-anydpi/shortcut_legacy_background.xml +25 -0
- package/template_project/app/src/main/res/drawable-anydpi/shortcut_monochrome.xml +27 -0
- package/template_project/app/src/main/res/drawable-anydpi-v26/shortcut_maskable.xml +32 -0
- package/template_project/app/src/main/res/drawable-anydpi-v26/shortcut_monochrome.xml +26 -0
- package/template_project/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml +33 -0
- package/template_project/app/src/main/res/values/colors.xml +18 -0
- package/template_project/app/src/main/res/values/strings.xml +50 -0
- package/template_project/app/src/main/res/xml/filepaths.xml +18 -0
- package/template_project/app/src/main/res/xml/shortcuts.xml +16 -0
- package/template_project/build.gradle +42 -0
- package/template_project/enable-debug.sh +27 -0
- package/template_project/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/template_project/gradle/wrapper/gradle-wrapper.properties +5 -0
- package/template_project/gradle.properties +14 -0
- package/template_project/gradlew +188 -0
- package/template_project/gradlew.bat +100 -0
- package/template_project/settings.gradle +1 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { WebManifestShortcutJson } from './types/WebManifest';
|
|
2
|
+
/**
|
|
3
|
+
* A wrapper around the WebManifest's ShortcutInfo.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ShortcutInfo {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly shortName: string;
|
|
8
|
+
readonly url: string;
|
|
9
|
+
readonly chosenIconUrl?: string | undefined;
|
|
10
|
+
readonly chosenMaskableIconUrl?: string | undefined;
|
|
11
|
+
readonly chosenMonochromeIconUrl?: string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} name
|
|
14
|
+
* @param {string} shortName
|
|
15
|
+
* @param {string} url target Url for when the shortcut is clicked
|
|
16
|
+
* @param {string} chosenIconUrl Url for the icon with an "any" purpose
|
|
17
|
+
* @param {string} chosenMaskableIconUrl Url for the icon with a maskable purpose
|
|
18
|
+
* @param {string} chosenMonochromeIconUrl Url for the icon with a monochrome purpose
|
|
19
|
+
*/
|
|
20
|
+
constructor(name: string, shortName: string, url: string, chosenIconUrl?: string | undefined, chosenMaskableIconUrl?: string | undefined, chosenMonochromeIconUrl?: string | undefined);
|
|
21
|
+
toString(index: number): string;
|
|
22
|
+
assetName(index: number): string;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new TwaManifest, using the URL for the Manifest as a base URL and uses the content
|
|
25
|
+
* of the Web Manifest to generate the fields for the TWA Manifest.
|
|
26
|
+
*
|
|
27
|
+
* @param {URL} webManifestUrl the URL where the webmanifest is available.
|
|
28
|
+
* @param {WebManifest} webManifest the Web Manifest, used as a base for the TWA Manifest.
|
|
29
|
+
* @returns {TwaManifest}
|
|
30
|
+
*/
|
|
31
|
+
static fromShortcutJson(webManifestUrl: URL, shortcut: WebManifestShortcutJson): ShortcutInfo;
|
|
32
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Google Inc. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
'use strict';
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.ShortcutInfo = void 0;
|
|
19
|
+
const util_1 = require("./util");
|
|
20
|
+
// As described on https://developer.chrome.com/apps/manifest/name#short_name
|
|
21
|
+
const SHORT_NAME_MAX_SIZE = 12;
|
|
22
|
+
// The minimum size needed for the shortcut icon
|
|
23
|
+
const MIN_SHORTCUT_ICON_SIZE = 96;
|
|
24
|
+
/**
|
|
25
|
+
* A wrapper around the WebManifest's ShortcutInfo.
|
|
26
|
+
*/
|
|
27
|
+
class ShortcutInfo {
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} name
|
|
30
|
+
* @param {string} shortName
|
|
31
|
+
* @param {string} url target Url for when the shortcut is clicked
|
|
32
|
+
* @param {string} chosenIconUrl Url for the icon with an "any" purpose
|
|
33
|
+
* @param {string} chosenMaskableIconUrl Url for the icon with a maskable purpose
|
|
34
|
+
* @param {string} chosenMonochromeIconUrl Url for the icon with a monochrome purpose
|
|
35
|
+
*/
|
|
36
|
+
constructor(name, shortName, url, chosenIconUrl, chosenMaskableIconUrl, chosenMonochromeIconUrl) {
|
|
37
|
+
this.name = name;
|
|
38
|
+
this.shortName = shortName;
|
|
39
|
+
this.url = url;
|
|
40
|
+
this.chosenIconUrl = chosenIconUrl;
|
|
41
|
+
this.chosenMaskableIconUrl = chosenMaskableIconUrl;
|
|
42
|
+
this.chosenMonochromeIconUrl = chosenMonochromeIconUrl;
|
|
43
|
+
if (!chosenIconUrl && !chosenMonochromeIconUrl) {
|
|
44
|
+
throw new Error(`ShortcutInfo ${name} must have either chosenIconUrl or chosenMonochromeIconUrl`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
toString(index) {
|
|
48
|
+
return `[name:'${this.name}', short_name:'${this.shortName}', ` +
|
|
49
|
+
`url:'${this.url}', icon:'${this.assetName(index)}']`;
|
|
50
|
+
}
|
|
51
|
+
assetName(index) {
|
|
52
|
+
return `shortcut_${index}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Creates a new TwaManifest, using the URL for the Manifest as a base URL and uses the content
|
|
56
|
+
* of the Web Manifest to generate the fields for the TWA Manifest.
|
|
57
|
+
*
|
|
58
|
+
* @param {URL} webManifestUrl the URL where the webmanifest is available.
|
|
59
|
+
* @param {WebManifest} webManifest the Web Manifest, used as a base for the TWA Manifest.
|
|
60
|
+
* @returns {TwaManifest}
|
|
61
|
+
*/
|
|
62
|
+
static fromShortcutJson(webManifestUrl, shortcut) {
|
|
63
|
+
const name = shortcut.name || shortcut.short_name;
|
|
64
|
+
if (!shortcut.icons || !shortcut.url || !name) {
|
|
65
|
+
throw new Error('missing metadata');
|
|
66
|
+
}
|
|
67
|
+
const suitableIcon = util_1.findSuitableIcon(shortcut.icons, 'any', MIN_SHORTCUT_ICON_SIZE);
|
|
68
|
+
const suitableMaskableIcon = util_1.findSuitableIcon(shortcut.icons, 'maskable', MIN_SHORTCUT_ICON_SIZE);
|
|
69
|
+
const suitableMonochromeIcon = util_1.findSuitableIcon(shortcut.icons, 'monochrome', MIN_SHORTCUT_ICON_SIZE);
|
|
70
|
+
if (!suitableIcon && !suitableMonochromeIcon) {
|
|
71
|
+
// maskable icons also need an equivalent any icon for lower API versions.
|
|
72
|
+
// any and monochrome icons work on all API versions.
|
|
73
|
+
throw new Error('not finding a suitable icon');
|
|
74
|
+
}
|
|
75
|
+
function resolveIconUrl(icon) {
|
|
76
|
+
return icon ? new URL(icon.src, webManifestUrl).toString() : undefined;
|
|
77
|
+
}
|
|
78
|
+
const shortName = shortcut.short_name || shortcut.name.substring(0, SHORT_NAME_MAX_SIZE);
|
|
79
|
+
const url = new URL(shortcut.url, webManifestUrl).toString();
|
|
80
|
+
const shortcutInfo = new ShortcutInfo(name, shortName, url, resolveIconUrl(suitableIcon), resolveIconUrl(suitableMaskableIcon), resolveIconUrl(suitableMonochromeIcon));
|
|
81
|
+
return shortcutInfo;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.ShortcutInfo = ShortcutInfo;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { TwaManifest } from './TwaManifest';
|
|
2
|
+
import { Log } from './Log';
|
|
3
|
+
export declare type twaGeneratorProgress = (progress: number, total: number) => void;
|
|
4
|
+
/**
|
|
5
|
+
* Generates TWA Projects from a TWA Manifest
|
|
6
|
+
*/
|
|
7
|
+
export declare class TwaGenerator {
|
|
8
|
+
private imageHelper;
|
|
9
|
+
private copyStaticFile;
|
|
10
|
+
private copyStaticFiles;
|
|
11
|
+
private applyTemplate;
|
|
12
|
+
private applyTemplateList;
|
|
13
|
+
private applyJavaTemplate;
|
|
14
|
+
private applyJavaTemplates;
|
|
15
|
+
private applyTemplateMap;
|
|
16
|
+
private generateIcons;
|
|
17
|
+
private writeWebManifest;
|
|
18
|
+
/**
|
|
19
|
+
* Generates shortcut data for a new TWA Project.
|
|
20
|
+
*
|
|
21
|
+
* @param {String} targetDirectory the directory where the project will be created
|
|
22
|
+
* @param {String} templateDirectory the directory where templates are located.
|
|
23
|
+
* @param {Object} twaManifest configurations values for the project.
|
|
24
|
+
*/
|
|
25
|
+
private generateShortcuts;
|
|
26
|
+
private static generateShareTargetIntentFilter;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new TWA Project.
|
|
29
|
+
*
|
|
30
|
+
* @param {String} targetDirectory the directory where the project will be created
|
|
31
|
+
* @param {Object} twaManifest configurations values for the project.
|
|
32
|
+
*/
|
|
33
|
+
createTwaProject(targetDirectory: string, twaManifest: TwaManifest, log: Log, reportProgress?: twaGeneratorProgress): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Removes all files generated by crateTwaProject.
|
|
36
|
+
* @param targetDirectory the directory where the project was created.
|
|
37
|
+
*/
|
|
38
|
+
removeTwaProject(targetDirectory: string): Promise<void>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2019 Google Inc. All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.TwaGenerator = void 0;
|
|
19
|
+
const path = require("path");
|
|
20
|
+
const fs = require("fs");
|
|
21
|
+
const lodash_1 = require("lodash");
|
|
22
|
+
const util_1 = require("util");
|
|
23
|
+
const ImageHelper_1 = require("./ImageHelper");
|
|
24
|
+
const FeatureManager_1 = require("./features/FeatureManager");
|
|
25
|
+
const util_2 = require("./util");
|
|
26
|
+
const FetchUtils_1 = require("./FetchUtils");
|
|
27
|
+
const COPY_FILE_LIST = [
|
|
28
|
+
'settings.gradle',
|
|
29
|
+
'gradle.properties',
|
|
30
|
+
'build.gradle',
|
|
31
|
+
'gradlew',
|
|
32
|
+
'gradlew.bat',
|
|
33
|
+
'gradle/wrapper/gradle-wrapper.jar',
|
|
34
|
+
'gradle/wrapper/gradle-wrapper.properties',
|
|
35
|
+
'app/src/main/res/values/colors.xml',
|
|
36
|
+
'app/src/main/res/xml/filepaths.xml',
|
|
37
|
+
'app/src/main/res/xml/shortcuts.xml',
|
|
38
|
+
'app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml',
|
|
39
|
+
'app/src/main/res/drawable-anydpi/shortcut_legacy_background.xml',
|
|
40
|
+
];
|
|
41
|
+
const TEMPLATE_FILE_LIST = [
|
|
42
|
+
'app/build.gradle',
|
|
43
|
+
'app/src/main/AndroidManifest.xml',
|
|
44
|
+
'app/src/main/res/values/strings.xml',
|
|
45
|
+
];
|
|
46
|
+
const JAVA_DIR = 'app/src/main/java/';
|
|
47
|
+
const JAVA_FILE_LIST = [
|
|
48
|
+
'LauncherActivity.java',
|
|
49
|
+
'Application.java',
|
|
50
|
+
'DelegationService.java',
|
|
51
|
+
];
|
|
52
|
+
const DELETE_PROJECT_FILE_LIST = [
|
|
53
|
+
'settings.gradle',
|
|
54
|
+
'gradle.properties',
|
|
55
|
+
'build.gradle',
|
|
56
|
+
'gradlew',
|
|
57
|
+
'gradlew.bat',
|
|
58
|
+
'store_icon.png',
|
|
59
|
+
'gradle/',
|
|
60
|
+
'app/',
|
|
61
|
+
];
|
|
62
|
+
const DELETE_FILE_LIST = [
|
|
63
|
+
'app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml',
|
|
64
|
+
];
|
|
65
|
+
const SPLASH_IMAGES = [
|
|
66
|
+
{ dest: 'app/src/main/res/drawable-mdpi/splash.png', size: 300 },
|
|
67
|
+
{ dest: 'app/src/main/res/drawable-hdpi/splash.png', size: 450 },
|
|
68
|
+
{ dest: 'app/src/main/res/drawable-xhdpi/splash.png', size: 600 },
|
|
69
|
+
{ dest: 'app/src/main/res/drawable-xxhdpi/splash.png', size: 900 },
|
|
70
|
+
{ dest: 'app/src/main/res/drawable-xxxhdpi/splash.png', size: 1200 },
|
|
71
|
+
];
|
|
72
|
+
const IMAGES = [
|
|
73
|
+
{ dest: 'app/src/main/res/mipmap-mdpi/ic_launcher.png', size: 48 },
|
|
74
|
+
{ dest: 'app/src/main/res/mipmap-hdpi/ic_launcher.png', size: 72 },
|
|
75
|
+
{ dest: 'app/src/main/res/mipmap-xhdpi/ic_launcher.png', size: 96 },
|
|
76
|
+
{ dest: 'app/src/main/res/mipmap-xxhdpi/ic_launcher.png', size: 144 },
|
|
77
|
+
{ dest: 'app/src/main/res/mipmap-xxxhdpi/ic_launcher.png', size: 192 },
|
|
78
|
+
{ dest: 'store_icon.png', size: 512 },
|
|
79
|
+
];
|
|
80
|
+
const ADAPTIVE_IMAGES = [
|
|
81
|
+
{ dest: 'app/src/main/res/mipmap-mdpi/ic_maskable.png', size: 82 },
|
|
82
|
+
{ dest: 'app/src/main/res/mipmap-hdpi/ic_maskable.png', size: 123 },
|
|
83
|
+
{ dest: 'app/src/main/res/mipmap-xhdpi/ic_maskable.png', size: 164 },
|
|
84
|
+
{ dest: 'app/src/main/res/mipmap-xxhdpi/ic_maskable.png', size: 246 },
|
|
85
|
+
{ dest: 'app/src/main/res/mipmap-xxxhdpi/ic_maskable.png', size: 328 },
|
|
86
|
+
];
|
|
87
|
+
const NOTIFICATION_IMAGES = [
|
|
88
|
+
{ dest: 'app/src/main/res/drawable-mdpi/ic_notification_icon.png', size: 24 },
|
|
89
|
+
{ dest: 'app/src/main/res/drawable-hdpi/ic_notification_icon.png', size: 36 },
|
|
90
|
+
{ dest: 'app/src/main/res/drawable-xhdpi/ic_notification_icon.png', size: 48 },
|
|
91
|
+
{ dest: 'app/src/main/res/drawable-xxhdpi/ic_notification_icon.png', size: 72 },
|
|
92
|
+
{ dest: 'app/src/main/res/drawable-xxxhdpi/ic_notification_icon.png', size: 96 },
|
|
93
|
+
];
|
|
94
|
+
const WEB_MANIFEST_LOCATION = '/app/src/main/res/raw/';
|
|
95
|
+
const WEB_MANIFEST_FILE_NAME = 'web_app_manifest.json';
|
|
96
|
+
function shortcutMaskableTemplateFileMap(assetName) {
|
|
97
|
+
return {
|
|
98
|
+
'app/src/main/res/drawable-anydpi-v26/shortcut_maskable.xml': `app/src/main/res/drawable-anydpi-v26/${assetName}.xml`,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function shortcutMonochromeTemplateFileMap(assetName) {
|
|
102
|
+
return {
|
|
103
|
+
'app/src/main/res/drawable-anydpi/shortcut_monochrome.xml': `app/src/main/res/drawable-anydpi/${assetName}.xml`,
|
|
104
|
+
'app/src/main/res/drawable-anydpi-v26/shortcut_monochrome.xml': `app/src/main/res/drawable-anydpi-v26/${assetName}.xml`,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function shortcutImages(assetName) {
|
|
108
|
+
return [
|
|
109
|
+
{ dest: `app/src/main/res/drawable-mdpi/${assetName}.png`, size: 48 },
|
|
110
|
+
{ dest: `app/src/main/res/drawable-hdpi/${assetName}.png`, size: 72 },
|
|
111
|
+
{ dest: `app/src/main/res/drawable-xhdpi/${assetName}.png`, size: 96 },
|
|
112
|
+
{ dest: `app/src/main/res/drawable-xxhdpi/${assetName}.png`, size: 144 },
|
|
113
|
+
{ dest: `app/src/main/res/drawable-xxxhdpi/${assetName}.png`, size: 192 },
|
|
114
|
+
];
|
|
115
|
+
}
|
|
116
|
+
// fs.promises is marked as experimental. This should be replaced when stable.
|
|
117
|
+
const fsMkDir = util_1.promisify(fs.mkdir);
|
|
118
|
+
const fsCopyFile = util_1.promisify(fs.copyFile);
|
|
119
|
+
const fsWriteFile = util_1.promisify(fs.writeFile);
|
|
120
|
+
const fsReadFile = util_1.promisify(fs.readFile);
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
122
|
+
const noOpProgress = () => { };
|
|
123
|
+
/**
|
|
124
|
+
* An utility class to help ensure progress tracking is consistent.
|
|
125
|
+
*/
|
|
126
|
+
class Progress {
|
|
127
|
+
constructor(total, progress) {
|
|
128
|
+
this.total = total;
|
|
129
|
+
this.progress = progress;
|
|
130
|
+
this.current = 0;
|
|
131
|
+
this.progress(this.current, this.total);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Updates the progress. Increments current by 1.
|
|
135
|
+
*/
|
|
136
|
+
update() {
|
|
137
|
+
if (this.current === this.total) {
|
|
138
|
+
throw new Error('Progress already reached total.' +
|
|
139
|
+
` current: ${this.current}, total: ${this.total}`);
|
|
140
|
+
}
|
|
141
|
+
this.current++;
|
|
142
|
+
this.progress(this.current, this.total);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Should be called for the last update. Throws an error if total !== current after incrementing
|
|
146
|
+
* current.
|
|
147
|
+
*/
|
|
148
|
+
done() {
|
|
149
|
+
this.update();
|
|
150
|
+
if (this.current !== this.total) {
|
|
151
|
+
throw new Error('Invoked done before current equals total.' +
|
|
152
|
+
` current: ${this.current}, total: ${this.total}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Generates TWA Projects from a TWA Manifest
|
|
158
|
+
*/
|
|
159
|
+
class TwaGenerator {
|
|
160
|
+
constructor() {
|
|
161
|
+
this.imageHelper = new ImageHelper_1.ImageHelper();
|
|
162
|
+
}
|
|
163
|
+
// Ensures targetDir exists and copies a file from sourceDir to target dir.
|
|
164
|
+
async copyStaticFile(sourceDir, targetDir, filename) {
|
|
165
|
+
const sourceFile = path.join(sourceDir, filename);
|
|
166
|
+
const destFile = path.join(targetDir, filename);
|
|
167
|
+
await fsMkDir(path.dirname(destFile), { recursive: true });
|
|
168
|
+
await fsCopyFile(sourceFile, destFile);
|
|
169
|
+
}
|
|
170
|
+
// Copies a list of file from sourceDir to targetDir.
|
|
171
|
+
copyStaticFiles(sourceDir, targetDir, fileList) {
|
|
172
|
+
return Promise.all(fileList.map((file) => {
|
|
173
|
+
return this.copyStaticFile(sourceDir, targetDir, file);
|
|
174
|
+
}));
|
|
175
|
+
}
|
|
176
|
+
async applyTemplate(sourceFile, destFile, args) {
|
|
177
|
+
await fsMkDir(path.dirname(destFile), { recursive: true });
|
|
178
|
+
const templateFile = await fsReadFile(sourceFile, 'utf-8');
|
|
179
|
+
const output = lodash_1.template(templateFile)(args);
|
|
180
|
+
await fsWriteFile(destFile, output);
|
|
181
|
+
}
|
|
182
|
+
async applyTemplateList(sourceDir, targetDir, fileList, args) {
|
|
183
|
+
await Promise.all(fileList.map((filename) => {
|
|
184
|
+
const sourceFile = path.join(sourceDir, filename);
|
|
185
|
+
const destFile = path.join(targetDir, filename);
|
|
186
|
+
this.applyTemplate(sourceFile, destFile, args);
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
async applyJavaTemplate(sourceDir, targetDir, packageName, filename, args) {
|
|
190
|
+
const sourceFile = path.join(sourceDir, JAVA_DIR, filename);
|
|
191
|
+
const destFile = path.join(targetDir, JAVA_DIR, packageName.split('.').join('/'), filename);
|
|
192
|
+
await fsMkDir(path.dirname(destFile), { recursive: true });
|
|
193
|
+
const templateFile = await fsReadFile(sourceFile, 'utf-8');
|
|
194
|
+
const output = lodash_1.template(templateFile)(args);
|
|
195
|
+
await fsWriteFile(destFile, output);
|
|
196
|
+
}
|
|
197
|
+
applyJavaTemplates(sourceDir, targetDir, packageName, fileList, args) {
|
|
198
|
+
return Promise.all(fileList.map((file) => {
|
|
199
|
+
this.applyJavaTemplate(sourceDir, targetDir, packageName, file, args);
|
|
200
|
+
}));
|
|
201
|
+
}
|
|
202
|
+
async applyTemplateMap(sourceDir, targetDir, fileMap, args) {
|
|
203
|
+
await Promise.all(Object.keys(fileMap).map((filename) => {
|
|
204
|
+
const sourceFile = path.join(sourceDir, filename);
|
|
205
|
+
const destFile = path.join(targetDir, fileMap[filename]);
|
|
206
|
+
this.applyTemplate(sourceFile, destFile, args);
|
|
207
|
+
}));
|
|
208
|
+
}
|
|
209
|
+
async generateIcons(iconUrl, targetDir, iconList, backgroundColor) {
|
|
210
|
+
const icon = await this.imageHelper.fetchIcon(iconUrl);
|
|
211
|
+
await Promise.all(iconList.map((iconDef) => {
|
|
212
|
+
return this.imageHelper.generateIcon(icon, targetDir, iconDef, backgroundColor);
|
|
213
|
+
}));
|
|
214
|
+
}
|
|
215
|
+
async writeWebManifest(twaManifest, targetDirectory) {
|
|
216
|
+
if (!twaManifest.webManifestUrl) {
|
|
217
|
+
throw new Error('Unable to write the Web Manifest. The TWA Manifest does not have a webManifestUrl');
|
|
218
|
+
}
|
|
219
|
+
const response = await FetchUtils_1.fetchUtils.fetch(twaManifest.webManifestUrl.toString());
|
|
220
|
+
if (response.status !== 200) {
|
|
221
|
+
throw new Error(`Failed to download Web Manifest ${twaManifest.webManifestUrl}.` +
|
|
222
|
+
`Responded with status ${response.status}`);
|
|
223
|
+
}
|
|
224
|
+
// We're writing as a string, but attempt to convert to check if it's a well-formed JSON.
|
|
225
|
+
const webManifestJson = await response.json();
|
|
226
|
+
// We want to ensure that "start_url" is the same used to launch the Trusted Web Activity.
|
|
227
|
+
webManifestJson['start_url'] = twaManifest.startUrl;
|
|
228
|
+
const webManifestLocation = path.join(targetDirectory, WEB_MANIFEST_LOCATION);
|
|
229
|
+
// Ensures the target directory exists.
|
|
230
|
+
await fs.promises.mkdir(webManifestLocation, { recursive: true });
|
|
231
|
+
const webManifestFileName = path.join(webManifestLocation, WEB_MANIFEST_FILE_NAME);
|
|
232
|
+
await fs.promises.writeFile(webManifestFileName, JSON.stringify(webManifestJson));
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Generates shortcut data for a new TWA Project.
|
|
236
|
+
*
|
|
237
|
+
* @param {String} targetDirectory the directory where the project will be created
|
|
238
|
+
* @param {String} templateDirectory the directory where templates are located.
|
|
239
|
+
* @param {Object} twaManifest configurations values for the project.
|
|
240
|
+
*/
|
|
241
|
+
async generateShortcuts(targetDirectory, templateDirectory, twaManifest) {
|
|
242
|
+
await Promise.all(twaManifest.shortcuts.map(async (shortcut, i) => {
|
|
243
|
+
const assetName = shortcut.assetName(i);
|
|
244
|
+
const monochromeAssetName = `${assetName}_monochrome`;
|
|
245
|
+
const maskableAssetName = `${assetName}_maskable`;
|
|
246
|
+
const templateArgs = { assetName, monochromeAssetName, maskableAssetName };
|
|
247
|
+
if (shortcut.chosenMonochromeIconUrl) {
|
|
248
|
+
await this.applyTemplateMap(templateDirectory, targetDirectory, shortcutMonochromeTemplateFileMap(assetName), templateArgs);
|
|
249
|
+
const monochromeImages = shortcutImages(monochromeAssetName);
|
|
250
|
+
const baseMonochromeIcon = await this.imageHelper.fetchIcon(shortcut.chosenMonochromeIconUrl);
|
|
251
|
+
const monochromeIcon = await this.imageHelper.monochromeFilter(baseMonochromeIcon, twaManifest.themeColor);
|
|
252
|
+
return await Promise.all(monochromeImages.map((iconDef) => {
|
|
253
|
+
return this.imageHelper.generateIcon(monochromeIcon, targetDirectory, iconDef);
|
|
254
|
+
}));
|
|
255
|
+
}
|
|
256
|
+
if (!shortcut.chosenIconUrl) {
|
|
257
|
+
throw new Error(`ShortcutInfo ${shortcut.name} is missing chosenIconUrl and chosenMonochromeIconUrl`);
|
|
258
|
+
}
|
|
259
|
+
if (shortcut.chosenMaskableIconUrl) {
|
|
260
|
+
await this.applyTemplateMap(templateDirectory, targetDirectory, shortcutMaskableTemplateFileMap(assetName), templateArgs);
|
|
261
|
+
const maskableImages = shortcutImages(maskableAssetName);
|
|
262
|
+
await this.generateIcons(shortcut.chosenMaskableIconUrl, targetDirectory, maskableImages);
|
|
263
|
+
}
|
|
264
|
+
const images = shortcutImages(assetName);
|
|
265
|
+
return this.generateIcons(shortcut.chosenIconUrl, targetDirectory, images);
|
|
266
|
+
}));
|
|
267
|
+
}
|
|
268
|
+
static generateShareTargetIntentFilter(twaManifest) {
|
|
269
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
270
|
+
if (!twaManifest.shareTarget) {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
const shareTargetIntentFilter = {
|
|
274
|
+
actions: ['android.intent.action.SEND'],
|
|
275
|
+
mimeTypes: [],
|
|
276
|
+
};
|
|
277
|
+
if (((_b = (_a = twaManifest.shareTarget) === null || _a === void 0 ? void 0 : _a.params) === null || _b === void 0 ? void 0 : _b.url) || ((_d = (_c = twaManifest.shareTarget) === null || _c === void 0 ? void 0 : _c.params) === null || _d === void 0 ? void 0 : _d.title) || ((_f = (_e = twaManifest.shareTarget) === null || _e === void 0 ? void 0 : _e.params) === null || _f === void 0 ? void 0 : _f.text)) {
|
|
278
|
+
shareTargetIntentFilter.mimeTypes.push('text/plain');
|
|
279
|
+
}
|
|
280
|
+
if ((_h = (_g = twaManifest.shareTarget) === null || _g === void 0 ? void 0 : _g.params) === null || _h === void 0 ? void 0 : _h.files) {
|
|
281
|
+
shareTargetIntentFilter.actions.push('android.intent.action.SEND_MULTIPLE');
|
|
282
|
+
for (const file of twaManifest.shareTarget.params.files) {
|
|
283
|
+
file.accept.forEach((accept) => shareTargetIntentFilter.mimeTypes.push(accept));
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return shareTargetIntentFilter;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Creates a new TWA Project.
|
|
290
|
+
*
|
|
291
|
+
* @param {String} targetDirectory the directory where the project will be created
|
|
292
|
+
* @param {Object} twaManifest configurations values for the project.
|
|
293
|
+
*/
|
|
294
|
+
async createTwaProject(targetDirectory, twaManifest, log, reportProgress = noOpProgress) {
|
|
295
|
+
const features = new FeatureManager_1.FeatureManager(twaManifest, log);
|
|
296
|
+
const progress = new Progress(9, reportProgress);
|
|
297
|
+
const error = twaManifest.validate();
|
|
298
|
+
if (error !== null) {
|
|
299
|
+
throw new Error(`Invalid TWA Manifest: ${error}`);
|
|
300
|
+
}
|
|
301
|
+
const templateDirectory = path.join(__dirname, '../../template_project');
|
|
302
|
+
const copyFileList = new Set(COPY_FILE_LIST);
|
|
303
|
+
if (!twaManifest.maskableIconUrl) {
|
|
304
|
+
DELETE_FILE_LIST.forEach((file) => copyFileList.delete(file));
|
|
305
|
+
}
|
|
306
|
+
progress.update();
|
|
307
|
+
// Copy Project Files
|
|
308
|
+
await this.copyStaticFiles(templateDirectory, targetDirectory, Array.from(copyFileList));
|
|
309
|
+
// Apply proper permissions to gradlew. See https://nodejs.org/api/fs.html#fs_file_modes
|
|
310
|
+
await fs.promises.chmod(path.join(targetDirectory, 'gradlew'), '755');
|
|
311
|
+
progress.update();
|
|
312
|
+
// Those are the arguments passed when applying templates. Functions are not automatically
|
|
313
|
+
// copied from objects, so we explicitly copy generateShortcuts.
|
|
314
|
+
const args = {
|
|
315
|
+
...twaManifest,
|
|
316
|
+
...features,
|
|
317
|
+
shareTargetIntentFilter: TwaGenerator.generateShareTargetIntentFilter(twaManifest),
|
|
318
|
+
generateShortcuts: twaManifest.generateShortcuts,
|
|
319
|
+
escapeJsonString: util_2.escapeJsonString,
|
|
320
|
+
escapeGradleString: util_2.escapeGradleString,
|
|
321
|
+
toAndroidScreenOrientation: util_2.toAndroidScreenOrientation,
|
|
322
|
+
};
|
|
323
|
+
// Generate templated files
|
|
324
|
+
await this.applyTemplateList(templateDirectory, targetDirectory, TEMPLATE_FILE_LIST, args);
|
|
325
|
+
progress.update();
|
|
326
|
+
// Generate java files
|
|
327
|
+
await this.applyJavaTemplates(templateDirectory, targetDirectory, twaManifest.packageId, JAVA_FILE_LIST, args);
|
|
328
|
+
progress.update();
|
|
329
|
+
// Generate images
|
|
330
|
+
if (twaManifest.iconUrl) {
|
|
331
|
+
await this.generateIcons(twaManifest.iconUrl, targetDirectory, IMAGES);
|
|
332
|
+
await this.generateIcons(twaManifest.iconUrl, targetDirectory, SPLASH_IMAGES, twaManifest.backgroundColor);
|
|
333
|
+
}
|
|
334
|
+
progress.update();
|
|
335
|
+
await this.generateShortcuts(targetDirectory, templateDirectory, twaManifest);
|
|
336
|
+
progress.update();
|
|
337
|
+
// Generate adaptive images
|
|
338
|
+
if (twaManifest.maskableIconUrl) {
|
|
339
|
+
await this.generateIcons(twaManifest.maskableIconUrl, targetDirectory, ADAPTIVE_IMAGES);
|
|
340
|
+
}
|
|
341
|
+
progress.update();
|
|
342
|
+
// Generate notification images
|
|
343
|
+
const iconOrMonochromeIconUrl = twaManifest.monochromeIconUrl || twaManifest.iconUrl;
|
|
344
|
+
if (twaManifest.enableNotifications && iconOrMonochromeIconUrl) {
|
|
345
|
+
await this.generateIcons(iconOrMonochromeIconUrl, targetDirectory, NOTIFICATION_IMAGES);
|
|
346
|
+
}
|
|
347
|
+
progress.update();
|
|
348
|
+
if (twaManifest.webManifestUrl) {
|
|
349
|
+
// Save the Web Manifest into the project
|
|
350
|
+
await this.writeWebManifest(twaManifest, targetDirectory);
|
|
351
|
+
}
|
|
352
|
+
progress.done();
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Removes all files generated by crateTwaProject.
|
|
356
|
+
* @param targetDirectory the directory where the project was created.
|
|
357
|
+
*/
|
|
358
|
+
async removeTwaProject(targetDirectory) {
|
|
359
|
+
await Promise.all(DELETE_PROJECT_FILE_LIST.map((entry) => util_2.rmdir(path.join(targetDirectory, entry))));
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
exports.TwaGenerator = TwaGenerator;
|