-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·118 lines (97 loc) · 3.35 KB
/
Copy pathcli.js
File metadata and controls
executable file
·118 lines (97 loc) · 3.35 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
'use strict'
const { readFileSync } = require('fs')
const { resolve, basename } = require('path')
const { Transformer } = require('./lib/transformer.js')
function printUsage () {
console.error(`Usage: npx @apm-js-collab/code-transformer <transformer.js> <source-file>
Arguments:
transformer.js Path to transformer configuration file
source-file Path to source file to transform (can be a node module path)
The transformer.js file should export one of:
- An array of instrumentation configs
- An object with { configs, dcModule?, customTransforms? }
Example transformer.js:
module.exports = [{
channelName: 'my-channel',
module: { name: 'my-module', versionRange: '>=1.0.0', filePath: 'index.js' },
functionQuery: { functionName: 'myFunction', kind: 'Async' }
}]
The transformed code will be written to stdout.
`)
process.exit(1)
}
function main () {
const args = process.argv.slice(2)
if (args.length !== 2) {
printUsage()
}
const [transformerPath, sourceFilePath] = args
// Resolve paths
const absoluteTransformerPath = resolve(process.cwd(), transformerPath)
const absoluteSourcePath = resolve(process.cwd(), sourceFilePath)
// Load transformer configuration
let transformerConfig
try {
transformerConfig = require(absoluteTransformerPath)
} catch (error) {
console.error(`Error loading transformer file: ${error.message}`)
process.exit(1)
}
// Parse transformer config
let configs
let dcModule
let customTransforms = {}
if (Array.isArray(transformerConfig)) {
configs = transformerConfig
} else if (typeof transformerConfig === 'object' && transformerConfig.configs) {
configs = transformerConfig.configs
dcModule = transformerConfig.dcModule
customTransforms = transformerConfig.customTransforms || {}
} else {
console.error('Transformer file must export an array of configs or an object with { configs, dcModule?, customTransforms? }')
process.exit(1)
}
if (!Array.isArray(configs) || configs.length === 0) {
console.error('Transformer must provide at least one config')
process.exit(1)
}
// Read source file
let sourceCode
try {
sourceCode = readFileSync(absoluteSourcePath, 'utf-8')
} catch (error) {
console.error(`Error reading source file: ${error.message}`)
process.exit(1)
}
// Determine module type from file extension
const moduleType = absoluteSourcePath.endsWith('.mjs') || absoluteSourcePath.endsWith('.mts')
? 'esm'
: absoluteSourcePath.endsWith('.cjs') || absoluteSourcePath.endsWith('.cts')
? 'cjs'
: 'unknown'
// Extract module information from first config or use defaults
const firstConfig = configs[0]
const moduleName = firstConfig.module?.name || 'cli-module'
const version = '1.0.0'
const filePath = basename(absoluteSourcePath)
// Create transformer and apply transformations
try {
// Create transformer directly with the configs
const transformer = new Transformer(
moduleName,
version,
filePath,
configs,
dcModule || 'diagnostics_channel',
customTransforms
)
const result = transformer.transform(sourceCode, moduleType)
// Output transformed code to stdout
process.stdout.write(result.code)
} catch (error) {
console.error(`Error transforming code: ${error.message}`)
process.exit(1)
}
}
main()