Skip to content

Commit

Permalink
refactor: remove posthog-node dependency
Browse files Browse the repository at this point in the history
Posthog node has been replaced for a fetch call. Removing 3 (posthog + 2 transitive dependencies).
  • Loading branch information
samuelstroschein committed Dec 25, 2024
1 parent 1d62451 commit 5d906bd
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 127 deletions.
7 changes: 7 additions & 0 deletions .changeset/flat-parrots-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@inlang/paraglide-js": minor
---

refactor: remove posthog-node dependency

Posthog node has been replaced for a fetch call. Removing 3 (posthog + 2 transitive dependencies).
4 changes: 2 additions & 2 deletions packages/inlang-paraglide-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
],
"scripts": {
"dev": "vite build --mode development --watch",
"build": "vite build --mode production",
"build": "npm run env-variables && vite build --mode production",
"test": "tsc --noEmit && vitest run --coverage ./src/**/*",
"test:watch": "vitest --watch ./src/**/*",
"env-variables": "node ./src/services/env-variables/createIndexFile.js",
"lint": "eslint ./src --fix",
"format": "prettier ./src --write",
"clean": "rm -rf ./dist ./node_modules",
Expand All @@ -58,7 +59,6 @@
"consola": "3.2.3",
"fast-glob": "^3.2.12",
"json5": "2.2.3",
"posthog-node": "^4.0.1",
"prettier": "^3.4.2",
"prettier-plugin-jsdoc": "^1.3.0"
},
Expand Down
17 changes: 0 additions & 17 deletions packages/inlang-paraglide-js/src/ambient.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Logger } from "~/services/logger/index.js";
import { runCompiler } from "~/cli/steps/run-compiler.js";
import { DEFAULT_OUTDIR } from "~/cli/defaults.js";
import { loadProjectFromDirectory } from "@inlang/sdk";
import { ENV_VARIABLES } from "~/services/env-variables/index.js";

export const compileCommand = new Command()
.name("compile")
Expand All @@ -31,7 +32,11 @@ export const compileCommand = new Command()

logger.info(`Compiling inlang project at "${options.project}".`);

const project = await loadProjectFromDirectory({ path, fs });
const project = await loadProjectFromDirectory({
path,
fs,
appId: ENV_VARIABLES.PARJS_APP_ID,
});

await runCompiler({
project,
Expand Down
29 changes: 5 additions & 24 deletions packages/inlang-paraglide-js/src/cli/commands/init/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Command } from "commander";
import consola from "consola";
import * as nodePath from "node:path";
import { telemetry } from "~/services/telemetry/implementation.js";
import { Logger } from "~/services/logger/index.js";
import { findPackageJson } from "~/services/environment/package.js";
import { checkForUncommittedChanges } from "~/cli/steps/check-for-uncomitted-changes.js";
Expand All @@ -14,6 +13,7 @@ import { runCompiler } from "~/cli/steps/run-compiler.js";
import type { CliStep } from "../../utils.js";
import nodeFs from "node:fs";
import type { NodeishFilesystem } from "~/services/file-handling/types.js";
import { ENV_VARIABLES } from "~/services/env-variables/index.js";

export const initCommand = new Command()
.name("init")
Expand All @@ -22,37 +22,23 @@ export const initCommand = new Command()
const logger = new Logger({ silent: false, prefix: false });

logger.box("Welcome to inlang Paraglide-JS 🪂");
telemetry.capture({
event: "PARAGLIDE-JS init started",
properties: { version: PARJS_PACKAGE_VERSION },
});

const ctx = {
logger,
fs: nodeFs.promises,
syncFs: nodeFs,
root: process.cwd(),
appId: PARJS_MARKTEPLACE_ID,
} as const;

const ctx1 = await checkForUncommittedChanges(ctx);
const ctx2 = await enforcePackageJsonExists(ctx1);
const ctx3 = await initializeInlangProject(ctx2);
const ctx4 = await promptForOutdir(ctx3);
telemetry.capture({
event: "PARAGLIDE-JS init project initialized",
properties: { version: PARJS_PACKAGE_VERSION },
});

const ctx5 = await addParaglideJsToDevDependencies(ctx4);
telemetry.capture({
event: "PARAGLIDE-JS init added to devDependencies",
properties: { version: PARJS_PACKAGE_VERSION },
});

const ctx6 = await addCompileStepToPackageJSON(ctx5);
telemetry.capture({
event: "PARAGLIDE-JS init added compile commands",
properties: { version: PARJS_PACKAGE_VERSION },
});

const ctx7 = await maybeChangeTsConfig(ctx6);
const ctx8 = await maybeAddSherlock(ctx7);

Expand All @@ -65,11 +51,6 @@ export const initCommand = new Command()
);
}

telemetry.capture({
event: "PARAGLIDE-JS init finished",
properties: { version: PARJS_PACKAGE_VERSION },
});

const absoluteSettingsPath = nodePath.resolve(
ctx8.projectPath,
"settings.json"
Expand Down Expand Up @@ -105,7 +86,7 @@ export const addParaglideJsToDevDependencies: CliStep<
const ctx1 = await updatePackageJson({
devDependencies: async (devDeps) => ({
...devDeps,
"@inlang/paraglide-js": PARJS_PACKAGE_VERSION,
"@inlang/paraglide-js": ENV_VARIABLES.PARJS_PACKAGE_VERSION,
}),
})(ctx);
ctx.logger.success(
Expand Down
3 changes: 2 additions & 1 deletion packages/inlang-paraglide-js/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from "commander";
import { compileCommand } from "./commands/compile/command.js";
import { ENV_VARIABLES } from "~/services/env-variables/index.js";

export { checkForUncommittedChanges } from "./steps/check-for-uncomitted-changes.js";
export { initializeInlangProject } from "./steps/initialize-inlang-project.js";
Expand All @@ -13,7 +14,7 @@ export const cli = new Command()
.name("paraglide-js")
.addCommand(compileCommand)
.showHelpAfterError()
.version(PARJS_PACKAGE_VERSION);
.version(ENV_VARIABLES.PARJS_PACKAGE_VERSION);

export * as Utils from "./utils.js";
export * as Defaults from "./defaults.js";
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import nodePath from "node:path";
import consola from "consola";
import fg from "fast-glob";
import fs from "node:fs";
import { ENV_VARIABLES } from "~/services/env-variables/index.js";

export const initializeInlangProject: CliStep<
{
fs: typeof fs.promises;
syncFs: typeof fs;
logger: Logger;
root: string;
appId: string;
},
{
project: InlangProject;
Expand All @@ -30,7 +30,6 @@ export const initializeInlangProject: CliStep<
fs: ctx.fs,
syncFs: fs,
logger: ctx.logger,
appId: ctx.appId,
});

return {
Expand All @@ -54,7 +53,6 @@ export const existingProjectFlow = async (ctx: {
fs: typeof fs.promises;
syncFs: typeof fs;
logger: Logger;
appId: string;
}): Promise<{ project: InlangProject; projectPath: string }> => {
const NEW_PROJECT_VALUE = "newProject";

Expand Down Expand Up @@ -84,7 +82,7 @@ export const existingProjectFlow = async (ctx: {
const project = await loadProjectFromDirectory({
path: projectPath,
fs: ctx.syncFs,
// appId: ctx.appId,
appId: ENV_VARIABLES.PARJS_APP_ID,
});

if ((await project.errors.get()).length > 0) {
Expand Down Expand Up @@ -165,7 +163,6 @@ export const createNewProjectFlow = async (ctx: {
fs: typeof fs.promises;
syncFs: typeof fs;
logger: Logger;
appId: string;
}): Promise<{
project: InlangProject;
/** An absolute path to the created project */
Expand Down Expand Up @@ -223,7 +220,7 @@ export const createNewProjectFlow = async (ctx: {
const project = await loadProjectFromDirectory({
path: projectPath,
fs: ctx.syncFs,
appId: ctx.appId,
appId: ENV_VARIABLES.PARJS_APP_ID,
});

if ((await project.errors.get()).length > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
index.ts
index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-undef */
/**
* This script writes public environment variables
* to an importable env file.
*
* - The SDK must bundle this file with the rest of the SDK
* - This scripts avoids the need for a bundler
* - Must be ran before building the SDK
*/

import fs from "node:fs/promises";
import url from "node:url";
import path from "node:path";

const dirname = path.dirname(url.fileURLToPath(import.meta.url));

const packageJson = JSON.parse(
await fs.readFile(path.resolve(dirname, "../../../package.json"), "utf-8")
);

await fs.writeFile(
dirname + "/index.ts",
`
export const ENV_VARIABLES = {
PARJS_APP_ID: "library.inlang.paraglideJs",
PARJS_POSTHOG_TOKEN: ${ifDefined(process.env.PUBLIC_POSTHOG_TOKEN)},
PARJS_PACKAGE_VERSION: ${ifDefined(packageJson.version)},
}
`
);

// console.log("✅ Created env variable index file.");

function ifDefined(value) {
return value ? `"${value}"` : undefined;
}
22 changes: 22 additions & 0 deletions packages/inlang-paraglide-js/src/services/env-variables/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Avoiding TypeScript errors before the `createIndexFile` script
* is invoked by defining the type ahead of time.
*/

/**
* Env variables that are available at runtime.
*/
export declare const ENV_VARIABLES: {
/**
* The inlang app id.
*/
PARJS_APP_ID: string;
PARJS_POSTHOG_TOKEN?: string;
/**
* The Package version from package.json
*
* This value is inlined by vite during build to avoid having to read the package.json
* at runtime
*/
PARJS_PACKAGE_VERSION: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect, test, vi } from "vitest";
import { capture } from "./capture.js";
import type { ENV_VARIABLES } from "../env-variables/index.js";

test("it should not capture if telemetry is off", async () => {
// @ts-expect-error - global.fetch is not defined
global.fetch = vi.fn(() => Promise.resolve());

vi.mock("../env-variables/index.js", async () => {
return {
ENV_VARIABLES: {
PARJS_POSTHOG_TOKEN: "mock-defined",
} satisfies Partial<typeof ENV_VARIABLES>,
};
});

await capture("PARAGLIDE-JS compile executed", {
projectId: "test",
settings: {
telemetry: "off",
},
properties: {},
});

expect(global.fetch).not.toHaveBeenCalled();
});

test("it should not capture if telemetry is NOT off", async () => {
// @ts-expect-error - global.fetch is not defined
global.fetch = vi.fn(() => Promise.resolve());

vi.mock("../env-variables/index.js", async () => {
return {
ENV_VARIABLES: {
PARJS_POSTHOG_TOKEN: "mock-defined",
} satisfies Partial<typeof ENV_VARIABLES>,
};
});

await capture("PARAGLIDE-JS compile executed", {
projectId: "test",
settings: {
telemetry: undefined,
},
properties: {},
});

expect(global.fetch).toHaveBeenCalled();
});
Loading

0 comments on commit 5d906bd

Please sign in to comment.