Skip to content

Commit

Permalink
chore: add tracking for CLI relatd to creator hub (#276)
Browse files Browse the repository at this point in the history
* chore: add tracking for CLI relatd to creator hub

* chore: update sdk
  • Loading branch information
cazala authored Oct 29, 2024
1 parent 90d821e commit 6a2486d
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 56 deletions.
60 changes: 30 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"dependencies": {
"@dcl/mini-rpc": "^1.0.7",
"@dcl/schemas": "^11.10.4",
"@dcl/sdk": "^7.6.2",
"@dcl/sdk": "^7.6.4",
"@segment/analytics-node": "^2.1.2",
"cmd-shim": "^6.0.3",
"decentraland-connect": "^7.1.0",
Expand Down
17 changes: 16 additions & 1 deletion packages/main/src/modules/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Analytics, type TrackParams } from '@segment/analytics-node';
import path from 'node:path';
import log from 'electron-log';
import { randomUUID } from 'node:crypto';
import { randomUUID, type UUID } from 'node:crypto';
import { FileSystemStorage } from '/shared/types/storage';
import { config } from './config';
import { getWorkspaceConfigPath } from './electron';

let analytics: Analytics | null = null;
const sessionId = randomUUID();
Expand Down Expand Up @@ -76,3 +79,15 @@ export async function identify(userId: string, traits: Record<string, any> = {})
// do nothing
}
}

export async function getProjectId(_path: string): Promise<UUID> {
const projectInfoPath = path.join(await getWorkspaceConfigPath(_path), 'project.json');
const projectInfo = new FileSystemStorage(projectInfoPath);
const hasId = await projectInfo.has('id');
if (!hasId) {
const projectId = randomUUID();
await projectInfo.set('id', projectId);
return projectId;
}
return projectInfo.get<UUID>('id');
}
12 changes: 12 additions & 0 deletions packages/main/src/modules/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ import type { DeployOptions } from '/shared/types/ipc';
import { run, type Child } from './bin';
import { getAvailablePort } from './port';
import { install } from './npm';
import { getProjectId } from './analytics';

async function getEnv(path: string) {
const projectId = await getProjectId(path);
return {
ANALYTICS_PROJECT_ID: projectId,
ANALYTICS_APP_ID: 'creator-hub',
};
}

export async function init(path: string, repo?: string) {
const initCommand = run('@dcl/sdk-commands', 'sdk-commands', {
args: ['init', '--yes', '--skip-install', ...(repo ? ['--github-repo', repo] : [])],
cwd: path,
env: await getEnv(path),
});
await initCommand.wait();
}
Expand All @@ -20,6 +30,7 @@ export async function start(path: string) {
previewServer = run('@dcl/sdk-commands', 'sdk-commands', {
args: ['start', '--explorer-alpha', '--hub'],
cwd: path,
env: await getEnv(path),
});
await previewServer.waitFor(/decentraland:\/\//i);
}
Expand All @@ -39,6 +50,7 @@ export async function deploy({ path, target, targetContent }: DeployOptions) {
...(targetContent ? ['--target-content', targetContent] : []),
],
cwd: path,
env: await getEnv(path),
});

// App ready at
Expand Down
11 changes: 11 additions & 0 deletions packages/main/src/modules/electron.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import fs from 'fs/promises';
import { app, BrowserWindow, dialog, type OpenDialogOptions, shell } from 'electron';

export function getHome() {
Expand All @@ -9,6 +10,16 @@ export function getAppHome() {
return path.join(getHome(), '.decentraland');
}

export async function getWorkspaceConfigPath(_path: string) {
const editorHomePath = path.join(_path, '.editor');
try {
await fs.stat(_path);
} catch (error) {
await fs.mkdir(editorHomePath, { recursive: true });
}
return editorHomePath;
}

export async function showOpenDialog(opts: Partial<OpenDialogOptions>): Promise<string[]> {
const window = BrowserWindow.getFocusedWindow();
if (!window) return [];
Expand Down
4 changes: 4 additions & 0 deletions packages/main/src/modules/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export function initIpc() {
// electron
handle('electron.getAppVersion', () => electron.getAppVersion());
handle('electron.getAppHome', () => electron.getAppHome());
handle('electron.getWorkspaceConfigPath', (_event, path) =>
electron.getWorkspaceConfigPath(path),
);
handle('electron.showOpenDialog', (_event, opts) => electron.showOpenDialog(opts));
handle('electron.openExternal', (_event, url) => electron.openExternal(url));

Expand All @@ -29,6 +32,7 @@ export function initIpc() {
handle('analytics.track', (_event, eventName, data) => analytics.track(eventName, data));
handle('analytics.identify', (_event, userId, traits) => analytics.identify(userId, traits));
handle('analytics.getAnonymousId', () => analytics.getAnonymousId());
handle('analytics.getProjectId', (_event, path) => analytics.getProjectId(path));

// npm
handle('npm.install', (_event, path, packages) => npm.install(path, packages));
Expand Down
4 changes: 4 additions & 0 deletions packages/preload/src/modules/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ export async function identify(userId: string, traits?: Record<string, any>) {
export async function getAnonymousId() {
return invoke('analytics.getAnonymousId');
}

export async function getProjectId(path: string) {
return invoke('analytics.getProjectId', path);
}
9 changes: 2 additions & 7 deletions packages/preload/src/modules/editor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import path from 'path';

import type { DeployOptions } from '/shared/types/ipc';

import { invoke } from './invoke';
import * as fs from './fs';

export async function getEditorHome(_path: string) {
const editorHomePath = path.join(_path, '.editor');
if (!(await fs.exists(editorHomePath))) await fs.mkdir(editorHomePath);
return editorHomePath;
export async function getWorkspaceConfigPath(_path: string) {
return invoke('electron.getWorkspaceConfigPath', _path);
}

export async function getVersion() {
Expand Down
21 changes: 4 additions & 17 deletions packages/preload/src/modules/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { randomUUID, type UUID } from 'node:crypto';
import type { Scene } from '@dcl/schemas';
import { shell } from 'electron';
import equal from 'fast-deep-equal';

import { type DependencyState, SortBy, type Project } from '/shared/types/projects';
import { PACKAGES_LIST } from '/shared/types/pkg';
import { DEFAULT_DEPENDENCY_UPDATE_STRATEGY } from '/shared/types/settings';
import { FileSystemStorage } from '/shared/types/storage';
import type { Template, Workspace } from '/shared/types/workspace';

import { getConfig, setConfig } from './config';
import { exists, writeFile as deepWriteFile } from './fs';
import { hasDependency } from './pkg';
import { getRowsAndCols, parseCoords } from './scene';
import { getEditorHome } from './editor';
import { invoke } from './invoke';
import { getOudatedDeps } from './npm';
import { getDefaultScenesPath, getScenesPath } from './settings';

import { DEFAULT_THUMBNAIL, NEW_SCENE_NAME, EMPTY_SCENE_TEMPLATE_REPO } from './constants';
import { getWorkspaceConfigPath } from './editor';
import { getProjectId } from './analytics';

/**
* Get scene json
Expand All @@ -34,18 +33,6 @@ export async function getScene(_path: string): Promise<Scene> {
return JSON.parse(scene);
}

export async function getProjectId(_path: string): Promise<UUID> {
const projectInfoPath = path.join(await getEditorHome(_path), 'project.json');
const projectInfo = new FileSystemStorage(projectInfoPath);
const hasId = await projectInfo.has('id');
if (!hasId) {
const projectId = randomUUID();
await projectInfo.set('id', projectId);
return projectId;
}
return projectInfo.get<UUID>('id');
}

/**
* Returns whether or not the provided directory is a decentraland project or not
*/
Expand Down Expand Up @@ -79,8 +66,8 @@ export async function hasNodeModules(_path: string) {
}

export async function getProjectThumbnailPath(_path: string) {
const editorHomePath = await getEditorHome(_path);
return path.join(editorHomePath, 'images', 'project-thumbnail.png');
const workspaceConfigPath = await getWorkspaceConfigPath(_path);
return path.join(workspaceConfigPath, 'images', 'project-thumbnail.png');
}

export async function getProjectThumbnailAsBase64(
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/types/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type DeployOptions = { path: string; target?: string; targetContent?: str
export interface Ipc {
'electron.getAppHome': () => string;
'electron.getAppVersion': () => Promise<string>;
'electron.getWorkspaceConfigPath': (path: string) => Promise<string>;
'electron.showOpenDialog': (opts: Partial<OpenDialogOptions>) => Promise<string[]>;
'electron.openExternal': (url: string) => Promise<void>;
'inspector.start': () => Promise<number>;
Expand All @@ -18,6 +19,7 @@ export interface Ipc {
'analytics.track': (event: string, data?: Record<string, any>) => void;
'analytics.identify': (userId: string, traits?: Record<string, any>) => void;
'analytics.getAnonymousId': () => Promise<string>;
'analytics.getProjectId': (path: string) => Promise<string>;
'npm.install': (path: string, packages?: string[]) => Promise<void>;
'npm.getOutdatedDeps': (path: string, packages?: string[]) => Promise<Outdated>;
}

0 comments on commit 6a2486d

Please sign in to comment.