|
16 | 16 | */ |
17 | 17 |
|
18 | 18 | /** |
19 | | - * Generate a "src/semconv.ts" in the current workspace dir, which includes |
| 19 | + * Generate a "src/semconv.ts" in the given workspace dir, which includes |
20 | 20 | * copies of the unstable semconv definitions used in this package. |
| 21 | + * (It finds usages by looking for imports from |
| 22 | + * `@opentelemetry/semantic-conventions/incubating`.) |
21 | 23 | * |
22 | 24 | * This is to support the recommendation from |
23 | 25 | * https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv |
|
26 | 28 | * |
27 | 29 | * Usage: |
28 | 30 | * node scripts/gen-semconv-ts.js [WORKSPACE-DIR] |
| 31 | + * |
| 32 | + * If WORKSPACE-DIR is not given, it defaults to the current dir. |
29 | 33 | */ |
30 | 34 |
|
31 | 35 | const fs = require('fs'); |
32 | 36 | const { execSync } = require('child_process'); |
33 | 37 | const path = require('path'); |
34 | 38 | const { globSync } = require('glob'); |
| 39 | +const rimraf = require('rimraf'); |
| 40 | + |
| 41 | +const TOP = path.resolve(__dirname, '..'); |
| 42 | +const BUILD_DIR = path.resolve(TOP, 'build', 'gen-semconv-ts'); |
35 | 43 |
|
36 | 44 | const USE_COLOR = process.stdout.isTTY && !process.env.NO_COLOR?.length > 0; |
37 | 45 |
|
@@ -62,6 +70,7 @@ function genSemconvTs(wsDir) { |
62 | 70 | const semconvPath = require.resolve('@opentelemetry/semantic-conventions', |
63 | 71 | {paths: [path.join(wsDir, 'node_modules')]}); |
64 | 72 | const semconvStable = require(semconvPath); |
| 73 | + const semconvVer = require(path.resolve(semconvPath, '../../../package.json')).version; |
65 | 74 |
|
66 | 75 | // Gather unstable semconv imports. Consider any imports from |
67 | 76 | // '@opentelemetry/semantic-conventions/incubating' or from an existing local |
@@ -98,15 +107,34 @@ function genSemconvTs(wsDir) { |
98 | 107 | console.log(`Found import of ${names.size} unstable semconv definitions.`) |
99 | 108 | } |
100 | 109 |
|
101 | | - // Load the source from the semconv package from which we'll copy. |
102 | | - // We are cheating a bit in knowing the semconv package structure. We want |
103 | | - // "build/esnext/experimental_*.js" files. Use the "esnext" build because it |
104 | | - // is closest to the TypeScript we want. |
105 | | - const semconvEsnextDir = path.resolve( |
106 | | - semconvPath, |
107 | | - '../../esnext', // .../build/src/index.js -> .../build/esnext |
108 | | - ); |
109 | | - const srcPaths = globSync(path.join(semconvEsnextDir, 'experimental_*.js')); |
| 110 | + // Find or get a |
| 111 | + let srcIsLocal = false; |
| 112 | + try { |
| 113 | + const gitRemoteUrl = execSync(`git -C "${wsDir}" remote get-url origin`, {encoding: 'utf8'}).trim(); |
| 114 | + if (gitRemoteUrl.endsWith('/opentelemetry-js.git')) { |
| 115 | + srcIsLocal = true; |
| 116 | + } |
| 117 | + } catch {} |
| 118 | + |
| 119 | + // Find or get semconv sources from a opentelemetry-js.git clone. |
| 120 | + let semconvSrcDir; |
| 121 | + if (srcIsLocal) { |
| 122 | + const gitRootDir = execSync(`git -C "${wsDir}" rev-parse --show-toplevel`, {encoding: 'utf8'}).trim(); |
| 123 | + semconvSrcDir = path.join(gitRootDir, 'semantic-conventions'); |
| 124 | + console.log(`Using local sources at "${semconvSrcDir}"`); |
| 125 | + } else { |
| 126 | + const tag = `semconv/v${semconvVer}`; |
| 127 | + console.log(`Cloning opentelemetry-js.git#${tag} to working dir "${BUILD_DIR}"`); |
| 128 | + rimraf.sync(BUILD_DIR); |
| 129 | + fs.mkdirSync(BUILD_DIR, { recursive: true }); |
| 130 | + execSync(`git clone --depth 1 --branch ${tag} https://github.com/open-telemetry/opentelemetry-js.git`, { |
| 131 | + cwd: BUILD_DIR, |
| 132 | + stdio: 'ignore' |
| 133 | + }); |
| 134 | + semconvSrcDir = path.join(BUILD_DIR, 'opentelemetry-js', 'semantic-conventions'); |
| 135 | + console.log(`Using sources at "${semconvSrcDir}"`); |
| 136 | + } |
| 137 | + const srcPaths = globSync(path.join(semconvSrcDir, 'src', 'experimental_*.ts')); |
110 | 138 | const src = srcPaths |
111 | 139 | .map(f => fs.readFileSync(f)) |
112 | 140 | .join('\n\n'); |
|
0 commit comments