@neat.is/core 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.cjs CHANGED
@@ -6542,41 +6542,26 @@ export async function register() {
6542
6542
  }
6543
6543
  `;
6544
6544
  var NEXT_INSTRUMENTATION_NODE_TS = `${NEXT_INSTRUMENTATION_HEADER}
6545
- // Loads .env.neat (OTEL_SERVICE_NAME, OTEL_EXPORTER_OTLP_ENDPOINT) before
6546
- // the OTel SDK starts so the exporter target and service name are in scope.
6547
- import { fileURLToPath } from 'node:url'
6548
- import path from 'node:path'
6549
- import dotenv from 'dotenv'
6545
+ process.env.OTEL_SERVICE_NAME ||= '__PROJECT_NAME__'
6546
+ process.env.OTEL_EXPORTER_OTLP_ENDPOINT ||= 'http://localhost:4318'
6547
+
6550
6548
  import { NodeSDK } from '@opentelemetry/sdk-node'
6551
6549
  import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'
6552
6550
 
6553
- const here = path.dirname(fileURLToPath(import.meta.url))
6554
- dotenv.config({ path: path.join(here, '.env.neat') })
6555
-
6556
- const sdk = new NodeSDK({
6557
- serviceName: process.env.OTEL_SERVICE_NAME,
6558
- instrumentations: [getNodeAutoInstrumentations()],
6559
- })
6560
- sdk.start()
6551
+ new NodeSDK({ instrumentations: [getNodeAutoInstrumentations()] }).start()
6561
6552
  `;
6562
6553
  var NEXT_INSTRUMENTATION_NODE_JS = `${NEXT_INSTRUMENTATION_HEADER}
6563
- // Loads .env.neat (OTEL_SERVICE_NAME, OTEL_EXPORTER_OTLP_ENDPOINT) before
6564
- // the OTel SDK starts so the exporter target and service name are in scope.
6565
- import { fileURLToPath } from 'node:url'
6566
- import path from 'node:path'
6567
- import dotenv from 'dotenv'
6568
- import { NodeSDK } from '@opentelemetry/sdk-node'
6569
- import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'
6554
+ process.env.OTEL_SERVICE_NAME ||= '__PROJECT_NAME__'
6555
+ process.env.OTEL_EXPORTER_OTLP_ENDPOINT ||= 'http://localhost:4318'
6570
6556
 
6571
- const here = path.dirname(fileURLToPath(import.meta.url))
6572
- dotenv.config({ path: path.join(here, '.env.neat') })
6557
+ const { NodeSDK } = require('@opentelemetry/sdk-node')
6558
+ const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node')
6573
6559
 
6574
- const sdk = new NodeSDK({
6575
- serviceName: process.env.OTEL_SERVICE_NAME,
6576
- instrumentations: [getNodeAutoInstrumentations()],
6577
- })
6578
- sdk.start()
6560
+ new NodeSDK({ instrumentations: [getNodeAutoInstrumentations()] }).start()
6579
6561
  `;
6562
+ function renderNextInstrumentationNode(template, projectName) {
6563
+ return template.replace(/__PROJECT_NAME__/g, projectName);
6564
+ }
6580
6565
  var FRAMEWORK_OTEL_INIT_HEADER = "// Generated by `neat init --apply` (ADR-074). OpenTelemetry SDK hook.";
6581
6566
  var FRAMEWORK_OTEL_INIT_TS_BODY = `${FRAMEWORK_OTEL_INIT_HEADER}
6582
6567
  // Loads .env.neat (OTEL_SERVICE_NAME, OTEL_EXPORTER_OTLP_ENDPOINT) before
@@ -6919,17 +6904,29 @@ function lineIsOtelInjection(line) {
6919
6904
  if (trimmed.length === 0) return false;
6920
6905
  return /(?:require\(|import\s+)['"]\.\/otel-init[^'"]*['"]/.test(trimmed);
6921
6906
  }
6907
+ async function detectsSrcLayout(serviceDir) {
6908
+ const [hasSrcApp, hasSrcPages, hasRootApp, hasRootPages] = await Promise.all([
6909
+ exists2(import_node_path40.default.join(serviceDir, "src", "app")),
6910
+ exists2(import_node_path40.default.join(serviceDir, "src", "pages")),
6911
+ exists2(import_node_path40.default.join(serviceDir, "app")),
6912
+ exists2(import_node_path40.default.join(serviceDir, "pages"))
6913
+ ]);
6914
+ return (hasSrcApp || hasSrcPages) && !hasRootApp && !hasRootPages;
6915
+ }
6922
6916
  async function planNext(serviceDir, pkg, manifestPath, nextConfigPath, project) {
6923
6917
  const useTs = await isTypeScriptProject(serviceDir);
6924
- const instrumentationFile = import_node_path40.default.join(serviceDir, useTs ? "instrumentation.ts" : "instrumentation.js");
6918
+ const srcLayout = await detectsSrcLayout(serviceDir);
6919
+ const baseDir = srcLayout ? import_node_path40.default.join(serviceDir, "src") : serviceDir;
6920
+ const instrumentationFile = import_node_path40.default.join(baseDir, useTs ? "instrumentation.ts" : "instrumentation.js");
6925
6921
  const instrumentationNodeFile = import_node_path40.default.join(
6926
- serviceDir,
6922
+ baseDir,
6927
6923
  useTs ? "instrumentation.node.ts" : "instrumentation.node.js"
6928
6924
  );
6929
- const envNeatFile = import_node_path40.default.join(serviceDir, ".env.neat");
6925
+ const envNeatFile = import_node_path40.default.join(baseDir, ".env.neat");
6930
6926
  const existingDeps = { ...pkg.dependencies ?? {}, ...pkg.devDependencies ?? {} };
6931
6927
  const dependencyEdits = [];
6932
6928
  for (const sdk of SDK_PACKAGES) {
6929
+ if (sdk.name === "dotenv") continue;
6933
6930
  if (sdk.name in existingDeps) continue;
6934
6931
  dependencyEdits.push({
6935
6932
  file: manifestPath,
@@ -6938,6 +6935,7 @@ async function planNext(serviceDir, pkg, manifestPath, nextConfigPath, project)
6938
6935
  version: sdk.version
6939
6936
  });
6940
6937
  }
6938
+ const projectName = envServiceName(pkg, serviceDir, project);
6941
6939
  const generatedFiles = [];
6942
6940
  if (!await exists2(instrumentationFile)) {
6943
6941
  generatedFiles.push({
@@ -6949,14 +6947,17 @@ async function planNext(serviceDir, pkg, manifestPath, nextConfigPath, project)
6949
6947
  if (!await exists2(instrumentationNodeFile)) {
6950
6948
  generatedFiles.push({
6951
6949
  file: instrumentationNodeFile,
6952
- contents: useTs ? NEXT_INSTRUMENTATION_NODE_TS : NEXT_INSTRUMENTATION_NODE_JS,
6950
+ contents: renderNextInstrumentationNode(
6951
+ useTs ? NEXT_INSTRUMENTATION_NODE_TS : NEXT_INSTRUMENTATION_NODE_JS,
6952
+ projectName
6953
+ ),
6953
6954
  skipIfExists: true
6954
6955
  });
6955
6956
  }
6956
6957
  if (!await exists2(envNeatFile)) {
6957
6958
  generatedFiles.push({
6958
6959
  file: envNeatFile,
6959
- contents: renderEnvNeat(envServiceName(pkg, serviceDir, project)),
6960
+ contents: renderEnvNeat(projectName),
6960
6961
  skipIfExists: true
6961
6962
  });
6962
6963
  }
@@ -7382,9 +7383,13 @@ function isAllowedWritePath(serviceDir, target) {
7382
7383
  if (base === "package.json") return true;
7383
7384
  if (base === ".env.neat") return true;
7384
7385
  if (/^otel-init\.(?:js|cjs|mjs|ts)$/.test(base)) return true;
7385
- if (/^instrumentation(?:\.node)?\.(?:js|cjs|mjs|ts)$/.test(base)) return true;
7386
- if (/^next\.config\.(?:js|mjs|ts)$/.test(base)) return true;
7387
7386
  const relPosix = rel.split(import_node_path40.default.sep).join("/");
7387
+ if (/^instrumentation(?:\.node)?\.(?:js|cjs|mjs|ts)$/.test(base)) {
7388
+ if (relPosix === base) return true;
7389
+ if (relPosix === `src/${base}`) return true;
7390
+ return false;
7391
+ }
7392
+ if (/^next\.config\.(?:js|mjs|ts)$/.test(base)) return true;
7388
7393
  if (relPosix === "app/otel.server.ts" || relPosix === "app/otel.server.js") return true;
7389
7394
  if (/^app\/entry\.server\.(?:tsx?|jsx?)$/.test(relPosix)) return true;
7390
7395
  if (relPosix === "src/otel-init.ts" || relPosix === "src/otel-init.js") return true;
@@ -9718,7 +9723,7 @@ async function runQueryVerb(cmd, parsed) {
9718
9723
  }
9719
9724
  }
9720
9725
  var entry = process.argv[1] ?? "";
9721
- if (/[\\/]cli\.(?:cjs|js)$/.test(entry) || entry.endsWith("/cli") || entry.endsWith("/neat")) {
9726
+ if (/[\\/]cli\.(?:cjs|js)$/.test(entry) || entry.endsWith("/cli") || entry.endsWith("/neat") || entry.endsWith("/neat.is")) {
9722
9727
  main().catch((err) => {
9723
9728
  console.error(err);
9724
9729
  process.exit(1);