123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const tslib_1 = require("tslib");
- const path = require("path");
- const os = require("os");
- const chalk_1 = require("chalk");
- const backends_1 = require("./backends");
- const errors_1 = require("./errors");
- const format_1 = require("./utils/format");
- const fs_1 = require("@ionic/cli-framework/utils/fs");
- class BaseConfig {
- constructor(directory, fileName) {
- this.fileName = fileName;
- // TODO: better way to check if in project
- if (directory) {
- this.directory = path.resolve(directory);
- }
- else {
- this.directory = '';
- }
- this.filePath = path.resolve(this.directory, fileName);
- }
- prepare() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- try {
- const stats = yield fs_1.fsStat(this.directory);
- if (!stats.isDirectory()) {
- throw new errors_1.FatalException(`${chalk_1.default.bold(this.directory)} appears to be a file, but it must be a directory.`);
- }
- }
- catch (e) {
- if (e.code !== 'ENOENT') {
- throw e;
- }
- yield fs_1.fsMkdirp(this.directory);
- }
- });
- }
- load(options = {}) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- if (options.disk || !this.configFile) {
- let o;
- try {
- const stats = yield fs_1.fsStat(this.filePath);
- if (stats.size < 5) {
- o = {};
- }
- }
- catch (e) {
- if (e.code !== 'ENOENT') {
- throw e;
- }
- o = {};
- }
- if (typeof o === 'undefined') {
- try {
- o = yield fs_1.fsReadJsonFile(this.filePath);
- }
- catch (e) {
- if (e === fs_1.ERROR_FILE_INVALID_JSON) {
- throw new errors_1.FatalException(`The config file (${chalk_1.default.bold(format_1.prettyPath(this.filePath))}) is not valid JSON format.\n\n` +
- `Please fix any JSON errors in the file.`);
- }
- else {
- throw e;
- }
- }
- }
- const cloneDeep = yield Promise.resolve().then(() => require('lodash/cloneDeep'));
- this.originalConfigFile = cloneDeep(o);
- o = yield this.provideDefaults(o);
- if (this.is(o)) {
- this.configFile = o;
- }
- else {
- throw new errors_1.FatalException(`The config file (${chalk_1.default.bold(format_1.prettyPath(this.filePath))}) has an unrecognized JSON format.\n\n` +
- `This usually means a key had an unexpected value. Please look through it and fix any issues.\n` +
- `If all else fails--the CLI will recreate the file if you delete it.`);
- }
- }
- return this.configFile;
- });
- }
- save(configFile) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- if (!configFile) {
- configFile = this.configFile;
- }
- if (configFile) {
- const [isEqual, cloneDeep] = yield Promise.all([Promise.resolve().then(() => require('lodash/isEqual')), Promise.resolve().then(() => require('lodash/cloneDeep'))]);
- if (!isEqual(configFile, this.originalConfigFile)) {
- yield fs_1.fsWriteJsonFile(this.filePath, configFile, { encoding: 'utf8' });
- this.configFile = configFile;
- this.originalConfigFile = cloneDeep(configFile);
- }
- }
- });
- }
- }
- exports.BaseConfig = BaseConfig;
- exports.CONFIG_FILE = 'config.json';
- exports.DEFAULT_CONFIG_DIRECTORY = path.resolve(os.homedir(), '.ionic');
- class Config extends BaseConfig {
- provideDefaults(o) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const cloneDeep = yield Promise.resolve().then(() => require('lodash/cloneDeep'));
- const results = cloneDeep(o);
- if (!results.state) {
- results.state = {};
- }
- if (!results.state.doctor) {
- results.state.doctor = {};
- }
- if (typeof results.state.doctor.ignored === 'undefined') {
- results.state.doctor.ignored = [];
- }
- if (!results.state.lastCommand) {
- if (results.lastCommand) {
- results.state.lastCommand = results.lastCommand;
- }
- else {
- results.state.lastCommand = new Date().toISOString();
- }
- }
- if (!results.created) {
- results.created = new Date().toISOString();
- }
- if (!results.daemon) {
- results.daemon = {};
- }
- if (typeof results.daemon.updates === 'undefined') {
- results.daemon.updates = true;
- }
- if (!results.devapp) {
- results.devapp = {};
- }
- if (!results.devapp.knownInterfaces) {
- results.devapp.knownInterfaces = [];
- }
- if (!results.addresses) {
- results.addresses = {};
- }
- if (!results.git) {
- results.git = {};
- }
- if (!results.user) {
- results.user = {};
- }
- if (typeof results.user.id === 'string') {
- results.user.id = Number(results.user.id);
- }
- if (!results.tokens) {
- results.tokens = {};
- }
- if (!results.tokens.appUser) {
- results.tokens.appUser = {};
- }
- if (typeof results.backend !== 'string') {
- results.backend = backends_1.BACKEND_PRO;
- }
- if (typeof results.telemetry === 'undefined') {
- if (results.cliFlags && typeof results.cliFlags.enableTelemetry !== 'undefined') {
- results.telemetry = results.cliFlags.enableTelemetry;
- }
- else if (results.cliFlags && typeof results.cliFlags.telemetry !== 'undefined') {
- results.telemetry = results.cliFlags.telemetry;
- }
- else {
- results.telemetry = true;
- }
- }
- if (typeof results.yarn === 'undefined') {
- if (results.cliFlags && typeof results.cliFlags.yarn !== 'undefined') {
- results.yarn = results.cliFlags.yarn;
- }
- else {
- results.yarn = false;
- }
- }
- delete results.lastCommand;
- delete results.lastUpdated;
- delete results.cliFlags;
- delete results.urls;
- delete results.git.host;
- delete results.git.port;
- return results;
- });
- }
- is(j) {
- return j
- && typeof j.addresses === 'object'
- && typeof j.state === 'object'
- && typeof j.state.lastCommand === 'string'
- && typeof j.state.doctor === 'object'
- && typeof j.daemon === 'object'
- && typeof j.devapp === 'object'
- && typeof j.user === 'object'
- && typeof j.tokens === 'object'
- && typeof j.tokens.appUser === 'object'
- && typeof j.backend === 'string'
- && typeof j.telemetry === 'boolean'
- && typeof j.yarn === 'boolean';
- }
- isUpdatingEnabled() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const config = yield this.load();
- if (!config.daemon.updates) {
- return false;
- }
- return !config.state.lastNoResponseToUpdate || (new Date().getTime() - new Date(config.state.lastNoResponseToUpdate).getTime() > 86400000);
- });
- }
- getAPIUrl() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const config = yield this.load();
- if (config.addresses.apiUrl) {
- return config.addresses.apiUrl;
- }
- if (config.backend === 'legacy') {
- return 'https://api.ionic.io';
- }
- return 'https://api.ionicjs.com';
- });
- }
- getDashUrl() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const config = yield this.load();
- if (config.addresses.dashUrl) {
- return config.addresses.dashUrl;
- }
- if (config.backend === 'legacy') {
- return 'https://apps.ionic.io';
- }
- return 'https://dashboard.ionicjs.com';
- });
- }
- getGitHost() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const config = yield this.load();
- if (config.addresses.gitHost) {
- return config.addresses.gitHost;
- }
- return 'git.ionicjs.com';
- });
- }
- getGitPort() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const config = yield this.load();
- if (config.addresses.gitPort) {
- return config.addresses.gitPort;
- }
- return 22;
- });
- }
- }
- exports.Config = Config;
- function gatherFlags(argv) {
- return {
- interactive: typeof argv['interactive'] === 'undefined' ? true : argv['interactive'],
- confirm: typeof argv['confirm'] === 'undefined' ? false : argv['confirm'],
- };
- }
- exports.gatherFlags = gatherFlags;
|