UNPKG

@bubblewrap/cli

Version:

CLI tool to Generate TWA projects from a Web Manifest

77 lines (76 loc) 3.32 kB
"use strict"; /* * 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.loadOrCreateConfig = exports.DEFAULT_CONFIG_FILE_PATH = void 0; const path_1 = require("path"); const os_1 = require("os"); const core_1 = require("@bubblewrap/core"); const inquirer = require("inquirer"); const fs_1 = require("fs"); const fs_2 = require("fs"); const DEFAULT_CONFIG_FOLDER = path_1.join(os_1.homedir(), '.bubblewrap'); const DEFAULT_CONFIG_NAME = 'config.json'; exports.DEFAULT_CONFIG_FILE_PATH = path_1.join(DEFAULT_CONFIG_FOLDER, DEFAULT_CONFIG_NAME); const LEGACY_CONFIG_FOLDER = path_1.join(os_1.homedir(), '.llama-pack'); const LEGACY_CONFIG_NAME = 'llama-pack-config.json'; const LEGACY_CONFIG_FILE_PATH = path_1.join(LEGACY_CONFIG_FOLDER, LEGACY_CONFIG_NAME); async function createConfig() { const result = await inquirer.prompt([ { name: 'jdkPath', message: 'Path to the JDK:', validate: fs_1.existsSync, }, { name: 'androidSdkPath', message: 'Path to the Android SDK:', validate: fs_1.existsSync, }, ]); return new core_1.Config(result.jdkPath, result.androidSdkPath); } async function renameConfigIfNeeded(log) { if (fs_1.existsSync(exports.DEFAULT_CONFIG_FILE_PATH)) return; // No new named config file found. if (!fs_1.existsSync(LEGACY_CONFIG_FILE_PATH)) return; // Old named config file found - rename it and its folder. log.info('An old named config file was found, changing it now'); const files = await fs_2.promises.readdir(LEGACY_CONFIG_FOLDER); const numOfFiles = files.length; if (numOfFiles != 1) { // At this point, we know that's at least one file in the folder, `LEGACY_CONFIG_NAME, so // `numOfFiles' will be at least `1`. We avoid destroying / moving other files in this folder. await fs_2.promises.mkdir(DEFAULT_CONFIG_FOLDER); await fs_2.promises.rename(LEGACY_CONFIG_FILE_PATH, exports.DEFAULT_CONFIG_FILE_PATH); } else { await fs_2.promises.rename(LEGACY_CONFIG_FOLDER, DEFAULT_CONFIG_FOLDER); await fs_2.promises .rename(path_1.join(DEFAULT_CONFIG_FOLDER, LEGACY_CONFIG_NAME), exports.DEFAULT_CONFIG_FILE_PATH); } } async function loadOrCreateConfig(log = new core_1.ConsoleLog('config'), path = exports.DEFAULT_CONFIG_FILE_PATH) { await renameConfigIfNeeded(log); const existingConfig = await core_1.Config.loadConfig(path); if (existingConfig) return existingConfig; const config = await createConfig(); await config.saveConfig(path); return config; } exports.loadOrCreateConfig = loadOrCreateConfig;