| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import { spawnSync } from 'node:child_process'
- import { readFileSync } from 'node:fs'
- function git(args,input) {
- const out=spawnSync('git',args,{input,encoding:'utf8'})
- if(out.status!==0) throw new Error(out.stderr)
- return out.stdout
- }
- function replaceOnce(text,oldValue,newValue) {
- if(text.split(oldValue).length!==2) throw new Error('Expected exactly one stage anchor: '+oldValue.slice(0,70))
- return text.replace(oldValue,newValue)
- }
- const head=git(['rev-parse','HEAD']).trim()
- const paths=['src/modules/cockpit/services/cesiumPipeLayer.js','src/modules/cockpit/services/mapService.js','src/modules/cockpit/components/MapInfoPanel.vue']
- for(const path of paths) {
- let staged=git(['show',`${head}:${path}`])
- const current=readFileSync(path,'utf8').replace(/\r\n/g,'\n')
- if(path.endsWith('cesiumPipeLayer.js')) {
- const commentStart=' * 渲染口径'
- const a=staged.indexOf(commentStart),b=staged.indexOf('\n */',a)
- const c=current.indexOf(commentStart),d=current.indexOf('\n */',c)
- staged=staged.slice(0,a)+current.slice(c,d)+staged.slice(b)
- const constants=current.slice(current.indexOf('// Viewer-scoped'),current.indexOf('/** 与 `cesiumEngine'))
- staged=replaceOnce(staged,'/** 与 `cesiumEngine',constants+'/** 与 `cesiumEngine')
- staged=replaceOnce(staged,'export async function syncPipeLayer(viewer, network) {','export async function syncPipeLayer(viewer, network, { onModelChange = () => {} } = {}) {')
- const versions=' const version = (layerVersions.get(viewer) || 0) + 1\n layerVersions.set(viewer, version)\n'
- staged=replaceOnce(staged,' clearIndex(viewer)\n',versions+' clearIndex(viewer)\n')
- staged=replaceOnce(staged,' const source = await ensureSource(viewer)\n',' const source = await ensureSource(viewer)\n if (layerVersions.get(viewer) !== version) return { pipes: 0, components: 0, rendered: false }\n')
- const models=current.slice(current.indexOf(' // Keep fallback entities'),current.indexOf(' return { pipes: pipes.length',current.indexOf(' // Keep fallback entities')))
- staged=replaceOnce(staged,' return { pipes: pipes.length, components: components.length, rendered: true }',models+' return { pipes: pipes.length, components: components.length, rendered: true }')
- const cleanup=current.slice(current.indexOf(' if (viewer) layerVersions'),current.indexOf(' const source = viewer &&'))
- staged=replaceOnce(staged,' const source = viewer &&',cleanup+' const source = viewer &&')
- } else if(path.endsWith('mapService.js')) {
- staged=replaceOnce(staged,' if (record.viewer) {\n record.engine.destroyViewer(record.viewer)',' if (record.viewer) {\n if (record.pipeLayer) record.pipeLayer.clearPipeLayer(record.viewer)\n record.engine.destroyViewer(record.viewer)')
- const old=" const engine = await import('./cesiumPipeLayer.js')\n await engine.syncPipeLayer(instance.viewer, layer)\n const host = instance.host"
- const start=current.indexOf(' const record = instance\n const engine = await import(\'./cesiumPipeLayer.js\')')
- const end=current.indexOf(' const host = record.host\n if (host',start)+' const host = record.host'.length
- if(start<0||end<start) throw new Error('Missing service stage block')
- staged=replaceOnce(staged,old,current.slice(start,end))
- } else {
- const noteStart=current.indexOf(' <p class="map-info__note" data-pipe-model-note>')
- const noteEnd=current.indexOf(' <!-- 坐标准入',noteStart)
- staged=replaceOnce(staged,' <!-- 坐标准入',current.slice(noteStart,noteEnd)+' <!-- 坐标准入')
- const script=current.slice(current.indexOf("import { PIPE_MODEL_NOTE }"),current.indexOf('defineProps({'))
- staged=replaceOnce(staged,'defineProps({',script+'defineProps({')
- staged=replaceOnce(staged,' if (host) {\n observer',' if (host) {\n modelHost = host\n host.addEventListener(\'cockpit-pipe-model-state\', updatePipeModelState)\n observer')
- staged=replaceOnce(staged,'onBeforeUnmount(() => {\n if (observer)','onBeforeUnmount(() => {\n if (modelHost) modelHost.removeEventListener(\'cockpit-pipe-model-state\', updatePipeModelState)\n modelHost = null\n if (observer)')
- }
- if(git(['rev-parse','HEAD']).trim()!==head) throw new Error('HEAD changed during staging')
- const hash=git(['hash-object','-w','--stdin'],staged).trim()
- git(['update-index','--cacheinfo',`100644,${hash},${path}`])
- }
- console.log('Staged only ticket32 text replacements against fixed HEAD; working tree preserved')
|