@bubblewrap/cli
Version:
CLI tool to Generate TWA projects from a Web Manifest
77 lines (76 loc) • 3.53 kB
JavaScript
;
/*
* Copyright 2019 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.update = void 0;
const path = require("path");
const Prompt_1 = require("../Prompt");
const core_1 = require("@bubblewrap/core");
const constants_1 = require("../constants");
const inputHelpers_1 = require("../inputHelpers");
const strings_1 = require("../strings");
const shared_1 = require("./shared");
async function updateVersions(twaManifest, appVersionNameArg, prompt) {
const previousAppVersionCode = twaManifest.appVersionCode;
const appVersionCode = twaManifest.appVersionCode + 1;
// If a version was passed as parameter, use it.
if (appVersionNameArg) {
return {
appVersionCode: appVersionCode,
appVersionName: appVersionNameArg,
};
}
// Otherwise, try to upgrade automatically with the versionCode.
if (twaManifest.appVersionName === previousAppVersionCode.toString()) {
return {
appVersionCode: appVersionCode,
appVersionName: appVersionCode.toString(),
};
}
// If not not possible, ask the user to input a new version.
const appVersionName = await prompt.promptInput(strings_1.enUS.promptNewAppVersionName, null, inputHelpers_1.createValidateString(1));
return {
appVersionCode: appVersionCode,
appVersionName: appVersionName,
};
}
/**
* Updates an existing TWA Project using the `twa-manifest.json`.
* @param {string} [args.manifest] directory where the command should look for the
* `twa-manifest.json`. Defaults to the current folder.
* @param {boolean} [args.skipVersionUpgrade] Skips upgrading appVersionCode and appVersionName
* if set to true.
* @param {string} [args.appVersionName] Value to be used for appVersionName when upgrading
* versions. Ignored if `args.skipVersionUpgrade` is set to true.
*/
async function update(args, prompt = new Prompt_1.InquirerPrompt()) {
const targetDirectory = args.directory || process.cwd();
const manifestFile = args.manifest || path.join(process.cwd(), 'twa-manifest.json');
const twaManifest = await core_1.TwaManifest.fromFile(manifestFile);
twaManifest.generatorApp = constants_1.APP_NAME;
if (!args.skipVersionUpgrade) {
const newVersionInfo = await updateVersions(twaManifest, args.appVersionName, prompt);
twaManifest.appVersionName = newVersionInfo.appVersionName;
twaManifest.appVersionCode = newVersionInfo.appVersionCode;
prompt.printMessage(strings_1.enUS.messageGeneratedNewVersion(newVersionInfo.appVersionName, newVersionInfo.appVersionCode));
twaManifest.saveToFile(manifestFile);
}
const twaGenerator = new core_1.TwaGenerator();
await twaGenerator.removeTwaProject(targetDirectory);
await shared_1.generateTwaProject(prompt, twaGenerator, targetDirectory, twaManifest);
return true;
}
exports.update = update;