Skip to content

Commit

Permalink
Switched to spawn, added analytics error logs, debugged analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
zateutsch committed Sep 18, 2023
1 parent 9dae989 commit 93f724a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pwabuilder/cli",
"version": "0.0.14",
"version": "0.0.15",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
1 change: 1 addition & 0 deletions apps/cli/src/analytics/track-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function initAnalyticsAndTrack(name: string, properties: any) {
}

function parseTrackEventParameters(): TrackEventParameters {
console.log(process.argv);
return {
name: process.argv[2],
properties: process.argv[3] ? JSON.parse(process.argv[3]) : {}
Expand Down
12 changes: 6 additions & 6 deletions apps/cli/src/analytics/usage-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ export function getAnalyticsClient() {
}

// function to trackEvent
export function trackEvent(name: string, properties: any) {
export function trackEvent(name: string, properties?: any) {
try {
if (getFlag("analytics") === true) {

defaultClient.trackEvent({
name,
properties
Expand Down Expand Up @@ -102,21 +101,22 @@ function addUserIDtoTelemetry(id: string): void {
}

function spawnAnalyticsProcess(event: string, properties?: any) {
const logPath: string = path.resolve(__dirname, 'out.log');
const out = fs.openSync(logPath, 'a');
const err = fs.openSync(logPath, 'a');
const child = spawn('node', resolveNodeSpawnArgs(event, properties), {
detached: true,
stdio: 'ignore'
stdio: ['ignore', out, err]
});

child.on('error', (err: Error) => {
trackException(err);
})

child.unref();
}

function resolveNodeSpawnArgs(event: string, properties?: any): string [] {
const scriptPath: string = path.resolve(__dirname, 'track-events.js')
return properties ? [scriptPath, event] : [scriptPath, event, JSON.stringify(properties)];
return properties ? [scriptPath, event, JSON.stringify(properties)] : [scriptPath, event];
}

export function trackErrorWrapper(_error: Error): void {
Expand Down
10 changes: 5 additions & 5 deletions apps/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Arguments, CommandBuilder } from "yargs";
import { isDirectoryTemplate, outputError, execSyncWrapper } from "../util/util";
import { isDirectoryTemplate, outputError, spawnWrapper } from "../util/util";
import { trackBuildEventWrapper, trackErrorWrapper } from "../analytics/usage-analytics";

const COMMAND_DESCRIPTION_STRING: string = 'Build the PWA Starter using Vite.';
Expand Down Expand Up @@ -38,15 +38,15 @@ export const handler = async (argv: Arguments<BuildOptions>): Promise<void> => {
async function handleBuildCommand(argv: Arguments<BuildOptions>): Promise<void> {
const { viteArgs } = argv;
if(isDirectoryTemplate()) {
execBuildCommand(viteArgs);
spawnBuildCommand(viteArgs);
} else {
outputError(INVALID_DIRECTORY_ERROR_STRING);
}
}
async function execBuildCommand(viteArgs: string | undefined) {
async function spawnBuildCommand(viteArgs: string | undefined) {
if(viteArgs) {
execSyncWrapper(EXEC_BUILD_ARGS_STRING(viteArgs), false);
spawnWrapper(EXEC_BUILD_ARGS_STRING(viteArgs));
} else {
execSyncWrapper(EXEC_BUILD_NO_ARGS_STRING, false);
spawnWrapper(EXEC_BUILD_NO_ARGS_STRING);
}
}
10 changes: 5 additions & 5 deletions apps/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Arguments, CommandBuilder } from "yargs";
import { outputError, isDirectoryTemplate, execSyncWrapper } from "../util/util";
import { outputError, isDirectoryTemplate, spawnWrapper } from "../util/util";
import { trackErrorWrapper, trackStartEventWrapper } from "../analytics/usage-analytics";

const COMMAND_DESCRIPTION_STRING: string = 'Run the PWA Starter on a Vite dev server.';
Expand Down Expand Up @@ -39,16 +39,16 @@ export const handler = (argv: Arguments<StartOptions>): void => {
function handleStartCommand(argv: Arguments<StartOptions>) {
const { viteArgs } = argv;
if(isDirectoryTemplate()) {
execStartCommand(viteArgs);
spawnStartCommand(viteArgs);
} else {
outputError(INVALID_DIRECTORY_ERROR_STRING);
}
}

function execStartCommand(viteArgs: string | undefined) {
function spawnStartCommand(viteArgs: string | undefined) {
if(viteArgs) {
execSyncWrapper(EXEC_START_ARGS_STRING(viteArgs), false);
spawnWrapper(EXEC_START_ARGS_STRING(viteArgs));
} else {
execSyncWrapper(EXEC_START_NO_ARGS_STRING, false);
spawnWrapper(EXEC_START_NO_ARGS_STRING);
}
}
48 changes: 36 additions & 12 deletions apps/cli/src/util/util.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,57 @@
import { spawn } from "node:child_process";
import { doesStringExistInFile, doesFileExist } from "./fileUtil";
import { promisify } from 'node:util';

export type HandlerSignature = (...args: any[]) => void;

const exec = promisify(require('node:child_process').exec);
const execSync = require('node:child_process').execSync;

const promisifiedExec = promisify(require('node:child_process').exec);
const path = require('path');

const defaultErrorMessage: string = "Command failed due to unknown error.";

type SpawnCommand = {
command: string,
args: string[]
};

export async function promisifiedExecWrapper(command: string, suppressOutput: boolean, directory?: string | undefined) {
try{
await exec(command, {
await promisifiedExec(command, {
stdio: suppressOutput ? 'pipe' : [0, 1, 2],
cwd: path.resolve(process.cwd(), directory ? directory : '')
});
} catch (err) {
console.log("Process exited.")
console.error("Process exited with error.");
}
}

export async function execSyncWrapper(command: string, suppressOutput: boolean, directory?: string | undefined) {
try{
execSync(command, {
stdio: suppressOutput ? 'pipe' : [0, 1, 2],
cwd: path.resolve(process.cwd(), directory ? directory : '')
export async function spawnWrapper(spawnString: string, suppressOutput: boolean = false): Promise<void> {
try {
spawnHandler(spawnString, suppressOutput);
} catch ( err ) {
console.error("Process exited with error.");
}
}

async function spawnHandler(spawnString: string, suppressOutput: boolean): Promise<void> {
const { command, args } = parseSpawnString(spawnString);
const spawnedChild = spawn(command, args, {
cwd: process.cwd(),
shell: true
});

if(!suppressOutput) {
spawnedChild.stdout.on('data', (data) => {
process.stdout.write(data.toString());
});
} catch (err) {
console.log("Process exited.")
}
}

function parseSpawnString(spawnString: string): SpawnCommand {
const splitCommand: string[] = spawnString.split(' ');
return {
command: splitCommand[0],
args: splitCommand.slice(1)
}
}

Expand Down

0 comments on commit 93f724a

Please sign in to comment.