Skip to content

Commit a859b93

Browse files
committed
feat: support workbench html in electron-browser dir
close #51
1 parent 1057bc3 commit a859b93

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

src/manager/base.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import fs from 'node:fs'
55
import { readFileSync, writeFileSync } from 'atomically'
66

77
import { log } from '../logger'
8+
import { promptWarn } from '../utils'
89

910
export interface FileManager {
1011
hasBakFile: boolean
1112
reload: () => Promise<void>
1213
rollback: () => Promise<void>
14+
skipAll?: () => Promisable<string | false | undefined>
1315
}
1416

1517
export abstract class BaseFileManager implements FileManager {
@@ -22,24 +24,41 @@ export abstract class BaseFileManager implements FileManager {
2224
return fs.existsSync(this.bakPath)
2325
}
2426

27+
skipAll?: (() => Promisable<string | undefined | false>) | undefined
28+
2529
async reload() {
26-
if (!this.hasBakFile) {
27-
log.warn(`Backup file [${this.bakPath}] does not exist, backuping...`)
28-
fs.cpSync(this.srcPath, this.bakPath)
29-
log.info(`Create backup file [${this.bakPath}]`)
30-
}
31-
const newContent = await this.patch(readFileSync(this.bakPath, 'utf-8'))
32-
writeFileSync(this.srcPath, newContent)
33-
log.info(`Config reload [${this.srcPath}]`)
30+
await this.skipable(async () => {
31+
if (!this.hasBakFile) {
32+
log.warn(`Backup file [${this.bakPath}] does not exist, backuping...`)
33+
fs.cpSync(this.srcPath, this.bakPath)
34+
log.info(`Create backup file [${this.bakPath}]`)
35+
}
36+
const newContent = await this.patch(readFileSync(this.bakPath, 'utf-8'))
37+
writeFileSync(this.srcPath, newContent)
38+
log.info(`Config reload [${this.srcPath}]`)
39+
})
3440
}
3541

3642
async rollback() {
37-
if (!this.hasBakFile) {
38-
log.warn(`Backup file [${this.bakPath}] does not exist, skip rollback`)
39-
} else {
40-
writeFileSync(this.srcPath, readFileSync(this.bakPath, 'utf-8'))
41-
log.info(`Config rollback [${this.srcPath}]`)
43+
await this.skipable(() => {
44+
if (!this.hasBakFile) {
45+
log.warn(`Backup file [${this.bakPath}] does not exist, skip rollback`)
46+
} else {
47+
writeFileSync(this.srcPath, readFileSync(this.bakPath, 'utf-8'))
48+
log.info(`Config rollback [${this.srcPath}]`)
49+
}
50+
})
51+
}
52+
53+
async skipable(fn: () => Promisable<void>) {
54+
let skipMessage
55+
// eslint-disable-next-line no-cond-assign
56+
if (skipMessage = await this.skipAll?.()) {
57+
promptWarn(skipMessage)
58+
return
4259
}
60+
61+
await fn()
4362
}
4463

4564
abstract patch(content: string): Promisable<string>

src/manager/external.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ const entryCSS = '<link rel="stylesheet" href="../../../workbench/workbench.desk
250250
export class ExternalFileManager extends BaseFileManager {
251251
constructor() {
252252
super(htmlPath, htmlBakPath)
253+
if (!htmlPath) {
254+
this.skipAll = () => 'No workbench html found, external resouces are disabled'
255+
}
253256
}
254257

255258
async patch(content: string): Promise<string> {

src/path.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ export const productJSONPath = getProductJSONPath('json')
116116

117117
export const productJSONBakPath = getProductJSONPath('json', `${version}.${bakExt}`)
118118

119-
const sandboxPath = path.join(
120-
baseDir,
121-
'vs',
122-
'code',
123-
'electron-sandbox',
124-
'workbench',
125-
)
119+
const workbenchHtmlDir = (() => {
120+
const base = path.join(baseDir, 'vs', 'code', 'electron-sandbox')
121+
if (fs.existsSync(base)) {
122+
return path.join(base, 'workbench')
123+
}
124+
return path.join(path.dirname(base), 'electron-browser', 'workbench')
125+
})()
126126

127127
export const htmlPath = (() => {
128128
const WORKBENCH_NAMES = [
@@ -132,13 +132,12 @@ export const htmlPath = (() => {
132132
'workbench-dev.esm',
133133
]
134134
for (const name of WORKBENCH_NAMES) {
135-
const result = path.join(sandboxPath, `${name}.html`)
135+
const result = path.join(workbenchHtmlDir, `${name}.html`)
136136
if (fs.existsSync(result)) {
137137
return result
138138
}
139139
}
140-
log.warn('No html found, use default html path')
141-
return path.join(sandboxPath, `${WORKBENCH_NAMES[0]}.html`)
140+
return ''
142141
})()
143142

144143
export const htmlBakPath = htmlPath.replace('.html', `.${bakExt}.html`)
@@ -147,7 +146,7 @@ export const externalCssName = 'external.css'
147146
export const externalJsName = 'external.js'
148147
export const externalJsModuleName = 'external.module.js'
149148

150-
export const externalCssPath = path.join(sandboxPath, externalCssName)
151-
export const externalJsPath = path.join(sandboxPath, externalJsName)
152-
export const externalJsModulePath = path.join(sandboxPath, externalJsModuleName)
153-
export const externalCacheInfoPath = path.join(sandboxPath, 'external.cache.json')
149+
export const externalCssPath = path.join(workbenchHtmlDir, externalCssName)
150+
export const externalJsPath = path.join(workbenchHtmlDir, externalJsName)
151+
export const externalJsModulePath = path.join(workbenchHtmlDir, externalJsModuleName)
152+
export const externalCacheInfoPath = path.join(workbenchHtmlDir, 'external.cache.json')

0 commit comments

Comments
 (0)