Skip to content

Commit c058095

Browse files
committed
update the script to use *typescript* sources to copy from, rather than build/esnext/... sources in a local npm install.
This does a shallow git clone if necessary.
1 parent 629f255 commit c058095

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

scripts/gen-semconv-ts.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
*/
1717

1818
/**
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
2020
* copies of the unstable semconv definitions used in this package.
21+
* (It finds usages by looking for imports from
22+
* `@opentelemetry/semantic-conventions/incubating`.)
2123
*
2224
* This is to support the recommendation from
2325
* https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv
@@ -26,12 +28,18 @@
2628
*
2729
* Usage:
2830
* node scripts/gen-semconv-ts.js [WORKSPACE-DIR]
31+
*
32+
* If WORKSPACE-DIR is not given, it defaults to the current dir.
2933
*/
3034

3135
const fs = require('fs');
3236
const { execSync } = require('child_process');
3337
const path = require('path');
3438
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');
3543

3644
const USE_COLOR = process.stdout.isTTY && !process.env.NO_COLOR?.length > 0;
3745

@@ -62,6 +70,7 @@ function genSemconvTs(wsDir) {
6270
const semconvPath = require.resolve('@opentelemetry/semantic-conventions',
6371
{paths: [path.join(wsDir, 'node_modules')]});
6472
const semconvStable = require(semconvPath);
73+
const semconvVer = require(path.resolve(semconvPath, '../../../package.json')).version;
6574

6675
// Gather unstable semconv imports. Consider any imports from
6776
// '@opentelemetry/semantic-conventions/incubating' or from an existing local
@@ -98,15 +107,34 @@ function genSemconvTs(wsDir) {
98107
console.log(`Found import of ${names.size} unstable semconv definitions.`)
99108
}
100109

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'));
110138
const src = srcPaths
111139
.map(f => fs.readFileSync(f))
112140
.join('\n\n');

0 commit comments

Comments
 (0)