MCPcopy Index your code
hub / github.com/jhen0409/react-native-debugger / injectCode

Function injectCode

npm-package/src/injectDevToolsMiddleware.js:148–192  ·  view source on GitHub ↗
(
  modulePath,
  {
    keyFunc,
    func: funcFlag,
    replaceFunc: replaceFuncFlag,
    funcCall,
    args,
    target,
    dir,
    file,
  },
)

Source from the content-addressed store, hash-verified

146}
147
148const injectCode = (
149 modulePath,
150 {
151 keyFunc,
152 func: funcFlag,
153 replaceFunc: replaceFuncFlag,
154 funcCall,
155 args,
156 target,
157 dir,
158 file,
159 },
160) => {
161 const filePath = join(modulePath, target, dir, file)
162 if (!fs.existsSync(filePath)) return false
163 const code = es6Template(template, {
164 startFlag,
165 replaceFuncFlag,
166 keyFunc,
167 funcCall,
168 endFlag,
169 args,
170 })
171
172 const middlewareCode = fs.readFileSync(filePath, 'utf-8')
173 let start = middlewareCode.indexOf(startFlag)
174 let end = middlewareCode.indexOf(endFlag) + endFlag.length
175 // already injected
176 if (start > -1 && middlewareCode.indexOf(replaceFuncFlag) === -1) {
177 start = -1
178 end = -1
179 }
180 if (start === -1) {
181 start = middlewareCode.indexOf(funcFlag)
182 end = start + funcFlag.length
183 }
184 if (start === -1) return false
185 fs.writeFileSync(
186 filePath,
187 middlewareCode.substr(0, start)
188 + code
189 + middlewareCode.substr(end, middlewareCode.length),
190 )
191 return true
192}
193
194export const inject = (modulePath, moduleName) => {
195 const info = getModuleInfo(modulePath, moduleName)

Callers 1

injectFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected