"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Integration = void 0;
const cli_framework_1 = require("@ionic/cli-framework");
const utils_fs_1 = require("@ionic/utils-fs");
const utils_terminal_1 = require("@ionic/utils-terminal");
const chalk = require("chalk");
const path = require("path");
const __1 = require("../");
const color_1 = require("../../color");
const npm_1 = require("../../utils/npm");
const config_1 = require("./config");
class Integration extends __1.BaseIntegration {
constructor() {
super(...arguments);
this.name = 'capacitor';
this.summary = `Target native iOS and Android with Capacitor, Ionic's new native layer`;
this.archiveUrl = undefined;
}
get config() {
return new __1.IntegrationConfig(this.e.project.filePath, { pathPrefix: [...this.e.project.pathPrefix, 'integrations', this.name] });
}
async add(details) {
const confPath = this.getCapacitorConfigPath();
if (await utils_fs_1.pathExists(confPath)) {
this.e.log.nl();
this.e.log.warn(`Capacitor already exists in project.\n` +
`Since the Capacitor config already exists (${color_1.strong(utils_terminal_1.prettyPath(confPath))}), the Capacitor integration has been ${chalk.green('enabled')}.\n\n` +
`You can re-integrate this project by doing the following:\n\n` +
`- Run ${color_1.input(`ionic integrations disable ${this.name}`)}\n` +
`- Remove the ${color_1.strong(utils_terminal_1.prettyPath(confPath))} file\n` +
`- Run ${color_1.input(`ionic integrations enable ${this.name} --add`)}\n`);
}
else {
let name = this.e.project.config.get('name');
let packageId = 'io.ionic.starter';
let webDir = await this.e.project.getDefaultDistDir();
const options = [];
if (details.enableArgs && details.enableArgs.length > 0) {
const parsedArgs = cli_framework_1.parseArgs(details.enableArgs);
name = parsedArgs._[0] || name;
packageId = parsedArgs._[1] || packageId;
if (parsedArgs['web-dir']) {
webDir = parsedArgs['web-dir'];
}
}
options.push('--web-dir', webDir);
options.push('--npm-client', this.e.config.get('npmClient'));
await this.installCapacitorCore();
await this.installCapacitorCLI();
await utils_fs_1.mkdirp(details.root);
await this.e.shell.run('capacitor', ['init', name, packageId, ...options], { cwd: details.root });
}
await super.add(details);
}
async getCapacitorConfig() {
const confPath = this.getCapacitorConfigPath();
const conf = new config_1.CapacitorConfig(confPath);
return conf;
}
getCapacitorConfigPath() {
return path.resolve(this.e.project.directory, config_1.CAPACITOR_CONFIG_FILE);
}
async installCapacitorCore() {
const [manager, ...managerArgs] = await npm_1.pkgManagerArgs(this.e.config.get('npmClient'), { command: 'install', pkg: '@capacitor/core' });
await this.e.shell.run(manager, managerArgs, { cwd: this.e.project.directory });
}
async installCapacitorCLI() {
const [manager, ...managerArgs] = await npm_1.pkgManagerArgs(this.e.config.get('npmClient'), { command: 'install', pkg: '@capacitor/cli', saveDev: true });
await this.e.shell.run(manager, managerArgs, { cwd: this.e.project.directory });
}
async personalize({ name, packageId }) {
const conf = await this.getCapacitorConfig();
conf.set('appName', name);
if (packageId) {
conf.set('appId', packageId);
}
}
async getInfo() {
const conf = await this.getCapacitorConfig();
const bundleId = conf.get('appId');
const [[capacitorCorePkg, capacitorCorePkgPath], capacitorCLIVersion,] = await (Promise.all([
this.e.project.getPackageJson('@capacitor/core'),
this.getCapacitorCLIVersion(),
]));
const info = [
{
group: 'capacitor',
name: 'Capacitor CLI',
key: 'capacitor_cli_version',
value: capacitorCLIVersion || 'not installed',
},
{
group: 'capacitor',
name: '@capacitor/core',
key: 'capacitor_core_version',
value: capacitorCorePkg ? capacitorCorePkg.version : 'not installed',
path: capacitorCorePkgPath,
},
{
group: 'capacitor',
name: 'Bundle ID',
key: 'bundle_id',
value: bundleId || 'unknown',
hidden: true,
},
];
return info;
}
async getCapacitorCLIVersion() {
return this.e.shell.cmdinfo('capacitor', ['--version'], { cwd: this.e.project.directory });
}
}
exports.Integration = Integration;