feat: 新增包文件
This commit is contained in:
3
packages/electron-chrome-extensions/script/native-messaging-host/.gitignore
vendored
Normal file
3
packages/electron-chrome-extensions/script/native-messaging-host/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
crxtesthost
|
||||
crxtesthost.blob
|
||||
crxtesthost.exe
|
||||
106
packages/electron-chrome-extensions/script/native-messaging-host/build.js
Executable file
106
packages/electron-chrome-extensions/script/native-messaging-host/build.js
Executable file
@@ -0,0 +1,106 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { promises: fs } = require('node:fs')
|
||||
const path = require('node:path')
|
||||
const os = require('node:os')
|
||||
const util = require('node:util')
|
||||
const cp = require('node:child_process')
|
||||
const exec = util.promisify(cp.exec)
|
||||
|
||||
const basePath = 'script/native-messaging-host/'
|
||||
const outDir = path.join(__dirname, '.')
|
||||
const exeName = `crxtesthost${process.platform === 'win32' ? '.exe' : ''}`
|
||||
const seaBlobName = 'crxtesthost.blob'
|
||||
|
||||
async function createSEA() {
|
||||
await fs.rm(path.join(outDir, seaBlobName), { force: true })
|
||||
await fs.rm(path.join(outDir, exeName), { force: true })
|
||||
|
||||
await exec('node --experimental-sea-config sea-config.json', { cwd: outDir })
|
||||
await fs.cp(process.execPath, path.join(outDir, exeName))
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
await exec(`codesign --remove-signature ${exeName}`, { cwd: outDir })
|
||||
}
|
||||
|
||||
console.info(`Building ${exeName}…`)
|
||||
const buildCmd = [
|
||||
'npx postject',
|
||||
`${basePath}${exeName}`,
|
||||
'NODE_SEA_BLOB',
|
||||
`${basePath}${seaBlobName}`,
|
||||
'--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
|
||||
...(process.platform === 'darwin' ? ['--macho-segment-name NODE_SEA'] : []),
|
||||
]
|
||||
await exec(buildCmd.join(' '), { cwd: outDir })
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
await exec(`codesign --sign - ${exeName}`, { cwd: outDir })
|
||||
}
|
||||
}
|
||||
|
||||
async function installConfig(extensionIds) {
|
||||
console.info(`Installing config…`)
|
||||
|
||||
const hostName = 'com.crx.test'
|
||||
const manifest = {
|
||||
name: hostName,
|
||||
description: 'electron-chrome-extensions test',
|
||||
path: path.join(outDir, exeName),
|
||||
type: 'stdio',
|
||||
allowed_origins: extensionIds.map((id) => `chrome-extension://${id}/`),
|
||||
}
|
||||
|
||||
const writeManifest = async (manifestPath) => {
|
||||
await fs.mkdir(manifestPath, { recursive: true })
|
||||
const filePath = path.join(manifestPath, `${hostName}.json`)
|
||||
const data = Buffer.from(JSON.stringify(manifest, null, 2))
|
||||
await fs.writeFile(filePath, data)
|
||||
return filePath
|
||||
}
|
||||
|
||||
switch (process.platform) {
|
||||
case 'darwin': {
|
||||
const manifestDir = path.join(
|
||||
os.homedir(),
|
||||
'Library',
|
||||
'Application Support',
|
||||
'Electron',
|
||||
'NativeMessagingHosts',
|
||||
)
|
||||
await writeManifest(manifestDir)
|
||||
break
|
||||
}
|
||||
case 'win32': {
|
||||
const manifestDir = path.join(
|
||||
os.homedir(),
|
||||
'AppData',
|
||||
'Roaming',
|
||||
'Electron',
|
||||
'NativeMessagingHosts',
|
||||
)
|
||||
const manifestPath = await writeManifest(manifestDir)
|
||||
const registryKey = `HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\${hostName}`
|
||||
await exec(`reg add "${registryKey}" /ve /t REG_SZ /d "${manifestPath}" /f`, {
|
||||
stdio: 'inherit',
|
||||
})
|
||||
break
|
||||
}
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const extensionIdsArg = process.argv[2]
|
||||
if (!extensionIdsArg) {
|
||||
console.error('Must pass in csv of allowed extension IDs')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const extensionIds = extensionIdsArg.split(',')
|
||||
await createSEA()
|
||||
await installConfig(extensionIds)
|
||||
}
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,26 @@
|
||||
const fs = require('node:fs')
|
||||
|
||||
function readMessage() {
|
||||
let buffer = Buffer.alloc(4)
|
||||
if (fs.readSync(0, buffer, 0, 4, null) !== 4) {
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
let messageLength = buffer.readUInt32LE(0)
|
||||
let messageBuffer = Buffer.alloc(messageLength)
|
||||
fs.readSync(0, messageBuffer, 0, messageLength, null)
|
||||
|
||||
return JSON.parse(messageBuffer.toString())
|
||||
}
|
||||
|
||||
function sendMessage(message) {
|
||||
let json = JSON.stringify(message)
|
||||
let buffer = Buffer.alloc(4 + json.length)
|
||||
buffer.writeUInt32LE(json.length, 0)
|
||||
buffer.write(json, 4)
|
||||
|
||||
fs.writeSync(1, buffer)
|
||||
}
|
||||
|
||||
const message = readMessage()
|
||||
sendMessage(message)
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"main": "main.js",
|
||||
"output": "crxtesthost.blob"
|
||||
}
|
||||
85
packages/electron-chrome-extensions/script/spec-runner.js
Normal file
85
packages/electron-chrome-extensions/script/spec-runner.js
Normal file
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const childProcess = require('child_process')
|
||||
const path = require('path')
|
||||
const unknownFlags = []
|
||||
|
||||
require('colors')
|
||||
const pass = '✓'.green
|
||||
const fail = '✗'.red
|
||||
|
||||
const args = require('minimist')(process.argv, {
|
||||
string: ['target'],
|
||||
unknown: (arg) => unknownFlags.push(arg),
|
||||
})
|
||||
|
||||
const unknownArgs = []
|
||||
for (const flag of unknownFlags) {
|
||||
unknownArgs.push(flag)
|
||||
const onlyFlag = flag.replace(/^-+/, '')
|
||||
if (args[onlyFlag]) {
|
||||
unknownArgs.push(args[onlyFlag])
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await runElectronTests()
|
||||
}
|
||||
|
||||
async function runElectronTests() {
|
||||
const errors = []
|
||||
|
||||
const testResultsDir = process.env.ELECTRON_TEST_RESULTS_DIR
|
||||
|
||||
try {
|
||||
console.info('\nRunning:')
|
||||
if (testResultsDir) {
|
||||
process.env.MOCHA_FILE = path.join(testResultsDir, `test-results.xml`)
|
||||
}
|
||||
await runMainProcessElectronTests()
|
||||
} catch (err) {
|
||||
errors.push([err])
|
||||
}
|
||||
|
||||
if (errors.length !== 0) {
|
||||
for (const err of errors) {
|
||||
console.error('\n\nRunner Failed:', err[0])
|
||||
console.error(err[1])
|
||||
}
|
||||
console.log(`${fail} Electron test runners have failed`)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
async function runMainProcessElectronTests() {
|
||||
let exe = require('electron')
|
||||
const runnerArgs = ['spec', ...unknownArgs.slice(2)]
|
||||
|
||||
// Fix issue in CI
|
||||
// "The SUID sandbox helper binary was found, but is not configured correctly."
|
||||
if (process.platform === 'linux') {
|
||||
runnerArgs.push('--no-sandbox')
|
||||
}
|
||||
|
||||
const { status, signal } = childProcess.spawnSync(exe, runnerArgs, {
|
||||
cwd: path.resolve(__dirname, '..'),
|
||||
env: process.env,
|
||||
stdio: 'inherit',
|
||||
})
|
||||
if (status !== 0) {
|
||||
if (status) {
|
||||
const textStatus =
|
||||
process.platform === 'win32' ? `0x${status.toString(16)}` : status.toString()
|
||||
console.log(`${fail} Electron tests failed with code ${textStatus}.`)
|
||||
} else {
|
||||
console.log(`${fail} Electron tests failed with kill signal ${signal}.`)
|
||||
}
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(`${pass} Electron main process tests passed.`)
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error('An error occurred inside the spec runner:', error)
|
||||
process.exit(1)
|
||||
})
|
||||
Reference in New Issue
Block a user