-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathembedVersion.js
More file actions
57 lines (43 loc) · 1.67 KB
/
embedVersion.js
File metadata and controls
57 lines (43 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFile, writeFile } from 'node:fs/promises';
import { glob } from 'glob';
import packageJson from './package.json' with { type: 'json' };
const __dirname = dirname(fileURLToPath(import.meta.url));
async function replaceVersionInFile(filePath, version) {
try {
let content = await readFile(filePath, 'utf8');
if (content.includes('__TELEMETRY_VERSION__')) {
console.log(`Updating version in ${filePath}`);
content = content.replace(/__TELEMETRY_VERSION__/g, `${version}`);
await writeFile(filePath, content, 'utf8');
return true;
}
return false;
} catch (error) {
console.error(`Error processing ${filePath}:`, error);
return false;
}
}
const { version } = packageJson;
console.log(`Embedding version ${version} into files...`);
const libDir = join(__dirname, 'lib');
const esmDir = join(__dirname, 'esm');
try {
const libFiles = glob.sync('**/*.js', { cwd: libDir, absolute: true });
console.log(`Found ${libFiles.length} JS files in lib directory`);
const esmFiles = glob.sync('**/*.js', { cwd: esmDir, absolute: true });
console.log(`Found ${esmFiles.length} JS files in esm directory`);
const allFiles = [...libFiles, ...esmFiles];
let updatedCount = 0;
for (const file of allFiles) {
const updated = await replaceVersionInFile(file, version);
if (updated) {
updatedCount++;
}
}
console.log(`Updated version in ${updatedCount} files`);
} catch (error) {
console.error('Error:', error);
process.exit(1);
}