@amplitude/ampli
Version:
Amplitude CLI
228 lines (227 loc) • 9.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const inquirer = require("inquirer");
const chalk_1 = require("chalk");
const fs = require("../util/fs");
const errors_1 = require("../errors");
const base_1 = require("./base");
const login_1 = require("./login");
const UserInfo_1 = require("../components/UserInfo");
const compareByName_1 = require("../util/sort/compareByName");
const types_1 = require("../types");
const icons_1 = require("../ui/icons");
const string_1 = require("../util/string");
const { bold } = chalk_1.default;
class InitAction extends base_1.default {
async run(initDefault) {
const settings = this.getSettings();
let tokenUser;
if (this.flags.token) {
tokenUser = await this.getUser(this.flags.zone ? this.flags.zone : undefined);
}
const isInitialized = settings.getOrgId() && settings.getWorkspaceId();
if (isInitialized) {
return this.checkInitializedProject(settings, tokenUser, initDefault);
}
return this.initializeProject(settings, tokenUser, initDefault);
}
async checkInitializedProject(settings, tokenUser, initDefault) {
let projectUser = settings.projectUser();
if (tokenUser != null) {
if (projectUser == null || projectUser.id !== tokenUser.id) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
return false;
}
return true;
}
if (projectUser == null) {
if (settings.users(this.flags.zone).length === 0) {
await new login_1.default(this.flags, {}, this.config).run();
projectUser = settings.projectUser();
}
if (projectUser == null) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
return false;
}
return true;
}
if (initDefault === undefined) {
this.println('Ampli is already initialized in ampli.json');
}
return true;
}
async initializeProject(settings, tokenUser, initDefault) {
let users;
if (tokenUser != null) {
users = [tokenUser];
}
else {
users = settings.users(this.flags.zone);
if (users.length === 0) {
await new login_1.default(this.flags, {}, this.config).run();
users = settings.users(this.flags.zone);
}
if (users.length === 0) {
this.error(new errors_1.UserFixableError('No logged in users'), 1);
return false;
}
}
const workspaceId = settings.getWorkspaceId();
if (!settings.getOrgId() && workspaceId) {
let orgId;
for (let i = 0; i < users.length; i += 1) {
const org = users[i].orgs.find(o => o.workspaces.some(w => w.id === workspaceId));
if (org) {
orgId = org.id;
break;
}
}
if (orgId) {
settings.setZone(types_1.DEFAULT_ZONE);
settings.setOrgId(orgId);
return this.checkInitializedProject(settings, tokenUser, initDefault);
}
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
return false;
}
if (initDefault === 'never') {
return false;
}
if (initDefault != null) {
this.println(`${icons_1.ICON_WARNING_W_TEXT} Ampli project is not initialized. No ${await fs.exists(settings.project.path) ? 'valid' : 'existing'} 'ampli.json' configuration found.`);
const { init } = await inquirer.prompt([{
name: 'init',
message: `Create a new Ampli project here?`,
type: 'confirm',
default: initDefault,
}]);
if (!init) {
return false;
}
}
let user;
if (tokenUser != null) {
user = tokenUser;
}
else {
if (this.flags.user) {
const foundUser = users.find(u => u.email === this.flags.user);
if (foundUser == null) {
this.error(new errors_1.UserFixableError(`The user ${bold(this.flags.user)} isn't logged in. Run ${bold('ampli login')}`), 1);
return false;
}
user = foundUser;
}
else if (users.length === 1) {
[user] = users;
}
else {
const promptUsers = users.map((u, i) => ({
name: UserInfo_1.UserInfo(u),
value: i,
}));
const { userIndex: selectedUserIndex } = await inquirer.prompt([{
name: 'userIndex',
message: `Select a user`,
type: 'autocomplete',
source: (_, input) => promptUsers.filter(u => string_1.matchPattern(u.name, input)),
}]);
user = users[selectedUserIndex];
}
this.authenticate({ id: user.id, zone: user.zone });
user = await this.getUser(user.zone);
}
let org;
const userOrgs = [...user.orgs].sort(compareByName_1.default);
if (this.flags.org) {
org = userOrgs.find(o => o.name === this.flags.org);
if (!org) {
this.error(new errors_1.UserFixableError(`The user doesn't belong to the organization ${bold(this.flags.org)}`), 1);
return false;
}
}
if (!org) {
if (this.flags.workspace) {
org = userOrgs.find(o => o.workspaces.some(w => w.name === this.flags.workspace));
}
}
if (!org) {
if (settings.getWorkspaceId()) {
org = userOrgs.find(o => o.workspaces.some(w => w.id === settings.getWorkspaceId()));
}
}
if (!org) {
if (userOrgs.length === 0) {
this.error(new errors_1.UserFixableError('The user does not belong to any organization'), 1);
return false;
}
if (userOrgs.length === 1) {
[org] = userOrgs;
}
}
if (!org) {
const promptOrgs = userOrgs.map(o => ({
name: o.name,
value: o.id,
}));
const { orgId: selectedOrgId } = await inquirer.prompt([{
name: 'orgId',
message: `Select an organization`,
type: 'autocomplete',
source: (_, input) => promptOrgs.filter(o => string_1.matchPattern(o.name, input)),
}]);
if (!selectedOrgId) {
this.error(new errors_1.UserFixableError('The user belongs to multiple organizations'), 1);
return false;
}
org = userOrgs.find(o => o.id === selectedOrgId);
}
else {
this.println(`Organization: ${org.name}`);
}
org = org;
let workspace;
const workspaces = [...org.workspaces].sort(compareByName_1.default);
if (this.flags.workspace) {
workspace = workspaces.find(w => w.name === this.flags.workspace);
if (!workspace) {
this.error(new errors_1.UserFixableError(`The user doesn't have the workspace ${bold(this.flags.workspace)}`), 1);
return false;
}
}
if (!workspace) {
if (workspaces.length === 0) {
this.error(new errors_1.UserFixableError('The organization does not have any workspace'), 1);
return false;
}
if (workspaces.length === 1) {
[workspace] = workspaces;
}
}
if (!workspace) {
const promptWorkspaces = workspaces.map(w => ({
name: w.name,
value: w.id,
}));
const { workspaceId: selectedWorkspaceId } = await inquirer.prompt([{
name: 'workspaceId',
message: `Select a workspace`,
type: 'autocomplete',
source: (_, input) => promptWorkspaces.filter(w => string_1.matchPattern(w.name, input)),
}]);
if (!selectedWorkspaceId) {
this.error(new errors_1.UserFixableError('The organization has multiple workspaces'), 1);
return false;
}
workspace = workspaces.find(w => w.id === selectedWorkspaceId);
}
else {
this.println(`Workspace: ${workspace.name}`);
}
settings.setZone(user.zone);
settings.setOrgId(org.id);
settings.setWorkspaceId(workspace.id);
return true;
}
}
exports.default = InitAction;