@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.js CHANGED
@@ -752,41 +752,26 @@ export async function register() {
752
752
  }
753
753
  `;
754
754
  var NEXT_INSTRUMENTATION_NODE_TS = `${NEXT_INSTRUMENTATION_HEADER}
755
- // Loads .env.neat (OTEL_SERVICE_NAME, OTEL_EXPORTER_OTLP_ENDPOINT) before
756
- // the OTel SDK starts so the exporter target and service name are in scope.
757
- import { fileURLToPath } from 'node:url'
758
- import path from 'node:path'
759
- import dotenv from 'dotenv'
755
+ process.env.OTEL_SERVICE_NAME ||= '__PROJECT_NAME__'
756
+ process.env.OTEL_EXPORTER_OTLP_ENDPOINT ||= 'http://localhost:4318'
757
+
760
758
  import { NodeSDK } from '@opentelemetry/sdk-node'
761
759
  import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'
762
760
 
763
- const here = path.dirname(fileURLToPath(import.meta.url))
764
- dotenv.config({ path: path.join(here, '.env.neat') })
765
-
766
- const sdk = new NodeSDK({
767
- serviceName: process.env.OTEL_SERVICE_NAME,
768
- instrumentations: [getNodeAutoInstrumentations()],
769
- })
770
- sdk.start()
761
+ new NodeSDK({ instrumentations: [getNodeAutoInstrumentations()] }).start()
771
762
  `;
772
763
  var NEXT_INSTRUMENTATION_NODE_JS = `${NEXT_INSTRUMENTATION_HEADER}
773
- // Loads .env.neat (OTEL_SERVICE_NAME, OTEL_EXPORTER_OTLP_ENDPOINT) before
774
- // the OTel SDK starts so the exporter target and service name are in scope.
775
- import { fileURLToPath } from 'node:url'
776
- import path from 'node:path'
777
- import dotenv from 'dotenv'
778
- import { NodeSDK } from '@opentelemetry/sdk-node'
779
- import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'
764
+ process.env.OTEL_SERVICE_NAME ||= '__PROJECT_NAME__'
765
+ process.env.OTEL_EXPORTER_OTLP_ENDPOINT ||= 'http://localhost:4318'
780
766
 
781
- const here = path.dirname(fileURLToPath(import.meta.url))
782
- dotenv.config({ path: path.join(here, '.env.neat') })
767
+ const { NodeSDK } = require('@opentelemetry/sdk-node')
768
+ const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node')
783
769
 
784
- const sdk = new NodeSDK({
785
- serviceName: process.env.OTEL_SERVICE_NAME,
786
- instrumentations: [getNodeAutoInstrumentations()],
787
- })
788
- sdk.start()
770
+ new NodeSDK({ instrumentations: [getNodeAutoInstrumentations()] }).start()
789
771
  `;
772
+ function renderNextInstrumentationNode(template, projectName) {
773
+ return template.replace(/__PROJECT_NAME__/g, projectName);
774
+ }
790
775
  var FRAMEWORK_OTEL_INIT_HEADER = "// Generated by `neat init --apply` (ADR-074). OpenTelemetry SDK hook.";
791
776
  var FRAMEWORK_OTEL_INIT_TS_BODY = `${FRAMEWORK_OTEL_INIT_HEADER}
792
777
  // Loads .env.neat (OTEL_SERVICE_NAME, OTEL_EXPORTER_OTLP_ENDPOINT) before
@@ -1129,17 +1114,29 @@ function lineIsOtelInjection(line) {
1129
1114
  if (trimmed.length === 0) return false;
1130
1115
  return /(?:require\(|import\s+)['"]\.\/otel-init[^'"]*['"]/.test(trimmed);
1131
1116
  }
1117
+ async function detectsSrcLayout(serviceDir) {
1118
+ const [hasSrcApp, hasSrcPages, hasRootApp, hasRootPages] = await Promise.all([
1119
+ exists(path4.join(serviceDir, "src", "app")),
1120
+ exists(path4.join(serviceDir, "src", "pages")),
1121
+ exists(path4.join(serviceDir, "app")),
1122
+ exists(path4.join(serviceDir, "pages"))
1123
+ ]);
1124
+ return (hasSrcApp || hasSrcPages) && !hasRootApp && !hasRootPages;
1125
+ }
1132
1126
  async function planNext(serviceDir, pkg, manifestPath, nextConfigPath, project) {
1133
1127
  const useTs = await isTypeScriptProject(serviceDir);
1134
- const instrumentationFile = path4.join(serviceDir, useTs ? "instrumentation.ts" : "instrumentation.js");
1128
+ const srcLayout = await detectsSrcLayout(serviceDir);
1129
+ const baseDir = srcLayout ? path4.join(serviceDir, "src") : serviceDir;
1130
+ const instrumentationFile = path4.join(baseDir, useTs ? "instrumentation.ts" : "instrumentation.js");
1135
1131
  const instrumentationNodeFile = path4.join(
1136
- serviceDir,
1132
+ baseDir,
1137
1133
  useTs ? "instrumentation.node.ts" : "instrumentation.node.js"
1138
1134
  );
1139
- const envNeatFile = path4.join(serviceDir, ".env.neat");
1135
+ const envNeatFile = path4.join(baseDir, ".env.neat");
1140
1136
  const existingDeps = { ...pkg.dependencies ?? {}, ...pkg.devDependencies ?? {} };
1141
1137
  const dependencyEdits = [];
1142
1138
  for (const sdk of SDK_PACKAGES) {
1139
+ if (sdk.name === "dotenv") continue;
1143
1140
  if (sdk.name in existingDeps) continue;
1144
1141
  dependencyEdits.push({
1145
1142
  file: manifestPath,
@@ -1148,6 +1145,7 @@ async function planNext(serviceDir, pkg, manifestPath, nextConfigPath, project)
1148
1145
  version: sdk.version
1149
1146
  });
1150
1147
  }
1148
+ const projectName = envServiceName(pkg, serviceDir, project);
1151
1149
  const generatedFiles = [];
1152
1150
  if (!await exists(instrumentationFile)) {
1153
1151
  generatedFiles.push({
@@ -1159,14 +1157,17 @@ async function planNext(serviceDir, pkg, manifestPath, nextConfigPath, project)
1159
1157
  if (!await exists(instrumentationNodeFile)) {
1160
1158
  generatedFiles.push({
1161
1159
  file: instrumentationNodeFile,
1162
- contents: useTs ? NEXT_INSTRUMENTATION_NODE_TS : NEXT_INSTRUMENTATION_NODE_JS,
1160
+ contents: renderNextInstrumentationNode(
1161
+ useTs ? NEXT_INSTRUMENTATION_NODE_TS : NEXT_INSTRUMENTATION_NODE_JS,
1162
+ projectName
1163
+ ),
1163
1164
  skipIfExists: true
1164
1165
  });
1165
1166
  }
1166
1167
  if (!await exists(envNeatFile)) {
1167
1168
  generatedFiles.push({
1168
1169
  file: envNeatFile,
1169
- contents: renderEnvNeat(envServiceName(pkg, serviceDir, project)),
1170
+ contents: renderEnvNeat(projectName),
1170
1171
  skipIfExists: true
1171
1172
  });
1172
1173
  }
@@ -1592,9 +1593,13 @@ function isAllowedWritePath(serviceDir, target) {
1592
1593
  if (base === "package.json") return true;
1593
1594
  if (base === ".env.neat") return true;
1594
1595
  if (/^otel-init\.(?:js|cjs|mjs|ts)$/.test(base)) return true;
1595
- if (/^instrumentation(?:\.node)?\.(?:js|cjs|mjs|ts)$/.test(base)) return true;
1596
- if (/^next\.config\.(?:js|mjs|ts)$/.test(base)) return true;
1597
1596
  const relPosix = rel.split(path4.sep).join("/");
1597
+ if (/^instrumentation(?:\.node)?\.(?:js|cjs|mjs|ts)$/.test(base)) {
1598
+ if (relPosix === base) return true;
1599
+ if (relPosix === `src/${base}`) return true;
1600
+ return false;
1601
+ }
1602
+ if (/^next\.config\.(?:js|mjs|ts)$/.test(base)) return true;
1598
1603
  if (relPosix === "app/otel.server.ts" || relPosix === "app/otel.server.js") return true;
1599
1604
  if (/^app\/entry\.server\.(?:tsx?|jsx?)$/.test(relPosix)) return true;
1600
1605
  if (relPosix === "src/otel-init.ts" || relPosix === "src/otel-init.js") return true;
@@ -3923,7 +3928,7 @@ async function runQueryVerb(cmd, parsed) {
3923
3928
  }
3924
3929
  }
3925
3930
  var entry = process.argv[1] ?? "";
3926
- if (/[\\/]cli\.(?:cjs|js)$/.test(entry) || entry.endsWith("/cli") || entry.endsWith("/neat")) {
3931
+ if (/[\\/]cli\.(?:cjs|js)$/.test(entry) || entry.endsWith("/cli") || entry.endsWith("/neat") || entry.endsWith("/neat.is")) {
3927
3932
  main().catch((err) => {
3928
3933
  console.error(err);
3929
3934
  process.exit(1);