-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Spike: Pre-bundle certain plugins for Insomnia and Inso #8813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
cwangsmv
wants to merge
6
commits into
spike/support-vault-plugin
Choose a base branch
from
spike/bundle-plugin-in-app
base: spike/support-vault-plugin
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+344
−38
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7190366
add hook for production build to bundle plugin
cwangsmv 0a7680d
allow inso running locally
cwangsmv a036b9a
support pre-bundle plugin for inso build
cwangsmv 873fbe5
fix type-check issue
cwangsmv 2a7385a
fix ut failure
cwangsmv eea6b59
refine bundle-plugin script
cwangsmv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ coverage | |
build/Release | ||
node_modules/ | ||
**/node_modules/ | ||
plugins/ | ||
.npm | ||
.eslintcache | ||
.node_repl_history | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,21 @@ | |
"bin": { | ||
"inso": "bin/inso" | ||
}, | ||
"pkg": { | ||
"assets": "plugins/**/*", | ||
"outputPath": "plugins" | ||
}, | ||
"scripts": { | ||
"lint": "eslint . --ext .js,.ts,.tsx --cache", | ||
"bundle-plugins": "esr src/scripts/bundle-plugins.ts", | ||
"test:unit": "cross-env NO_COLOR=1 vitest run --exclude '**/cli.test.ts'", | ||
"test:bundle": "vitest cli.test.ts -t \"inso dev bundle\"", | ||
"test:binary": "vitest cli.test.ts -t \"inso packaged binary\"", | ||
"type-check": "tsc --noEmit --project tsconfig.json", | ||
"build": "esr esbuild.ts", | ||
"build:production": "cross-env NODE_ENV=production esr esbuild.ts", | ||
"start": "ESBUILD_WATCH=true esr esbuild.ts", | ||
"prepackage": "npm run build:production && rm -rf binaries/inso", | ||
"prepackage": "npm run build:production && rm -rf binaries/inso && npm run bundle-plugins", | ||
"package": "npx -y @yao-pkg/[email protected] . --output binaries/inso --targets host", | ||
"postpackage": "node src/scripts/verify-pkg.js", | ||
"artifacts": "esr src/scripts/artifacts.ts" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// scripts/bundle-plugins.ts | ||
import { execSync } from 'node:child_process'; | ||
import { existsSync } from 'node:fs'; | ||
import { cp, mkdir, rm } from 'node:fs/promises'; | ||
import { resolve } from 'node:path'; | ||
|
||
const appPluginDir = resolve(__dirname, '../../../insomnia/plugins'); | ||
const insoPluginDir = resolve(__dirname, '../../plugins'); | ||
|
||
if (require.main === module) { | ||
process.nextTick(async () => { | ||
try { | ||
// remove existing bundle plugins directory | ||
await rm(insoPluginDir, { recursive: true, force: true }); | ||
// Recreate empty | ||
await mkdir(insoPluginDir, { recursive: true }); | ||
// Check if app plugin directory exists | ||
if (!existsSync(appPluginDir)) { | ||
console.log(`Source plugins directory not found at ${appPluginDir}. Creating and bundle plugins`); | ||
|
||
execSync('npm run bundle-plugins', { | ||
stdio: 'inherit', | ||
cwd: resolve(appPluginDir, '../'), | ||
}); | ||
} | ||
// Copy plugins from app to inso directory | ||
console.log(`Copying plugins from ${appPluginDir} to ${insoPluginDir}`); | ||
await cp(appPluginDir, insoPluginDir, { | ||
recursive: true, | ||
}); | ||
} catch (err) { | ||
console.log('[bundle-plugin] ERROR:', err); | ||
process.exit(1); | ||
} | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,13 @@ | |
"author": "Kong <[email protected]>", | ||
"main": "src/main.min.js", | ||
"scripts": { | ||
"build": "esr --cache ./scripts/build.ts --noErrorTruncation", | ||
"build": "NODE_OPTIONS='--max-old-space-size=8192' esr --cache ./scripts/build.ts --noErrorTruncation", | ||
"bundle-plugins": "esr scripts/bundle-plugins.ts", | ||
"generate:schema": "esr ./src/schema.ts", | ||
"build:main.min.js": "cross-env NODE_ENV=development esr esbuild.main.ts", | ||
"lint": "eslint . --ext .js,.ts,.tsx --cache", | ||
"package": "npm run build && cross-env USE_HARD_LINKS=false electron-builder build --config electron-builder.config.js", | ||
"package:windows:unpacked": "npm run build && cross-env USE_HARD_LINKS=false electron-builder build --config electron-builder.config.js --dir", | ||
"package": "npm run build && npm run bundle-plugins && cross-env USE_HARD_LINKS=false electron-builder build --config electron-builder.config.js", | ||
"package:windows:unpacked": "npm run build && npm run bundle-plugins && cross-env USE_HARD_LINKS=false electron-builder build --config electron-builder.config.js --dir", | ||
"package:windows:dist": "cross-env USE_HARD_LINKS=false electron-builder build --config electron-builder.config.js --win squirrel --prepackaged ./dist/win-unpacked", | ||
"start": "npx -y concurrently -n browser,main --kill-others \"npm run start:dev-server\" \"npm run start:electron\"", | ||
"start:dev-server": "vite dev", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import { execFile } from 'node:child_process'; | ||
import { cp, mkdir, mkdtemp, readdir, rm, stat, writeFile } from 'node:fs/promises'; | ||
import { tmpdir } from 'node:os'; | ||
import path from 'node:path'; | ||
import { promisify } from 'node:util'; | ||
|
||
const bundlePlugins = ['@kenttest/plugin-external-vault']; | ||
const bundlePluginsDir = path.resolve(__dirname, '..', 'plugins'); | ||
const yarnPath = path.resolve(__dirname, '..', 'bin', 'yarn-standalone.js'); | ||
const execFilePromise = promisify(execFile); | ||
|
||
if (require.main === module) { | ||
process.nextTick(async () => { | ||
try { | ||
// remove existing bundle plugins directory | ||
await rm(bundlePluginsDir, { recursive: true, force: true }); | ||
// Recreate empty | ||
await mkdir(bundlePluginsDir, { recursive: true }); | ||
for (const bundlePlugin of bundlePlugins) { | ||
await installPlugin(bundlePlugin); | ||
} | ||
} catch (err) { | ||
console.log('[bundle-plugin] ERROR:', err); | ||
process.exit(1); | ||
} | ||
}); | ||
} | ||
|
||
export async function runYarnCommand(args: string[], cwd?: string) { | ||
const { stdout, stderr } = await execFilePromise(process.execPath, ['--no-deprecation', yarnPath, ...args], { | ||
cwd, | ||
env: { | ||
NODE_ENV: 'production', | ||
}, | ||
timeout: 5 * 60 * 1000, // 5 minutes | ||
maxBuffer: 1024 * 1024, // 1MB buffer | ||
}); | ||
|
||
if (stderr) { | ||
throw new Error(`Yarn error: ${stderr}`); | ||
} | ||
|
||
return stdout.toString(); | ||
} | ||
|
||
export default async function installPlugin(name: string) { | ||
const { scope, pkg: pluginName } = parsePackageName(name); | ||
const pluginDir = path.resolve(bundlePluginsDir, pluginName); | ||
// Ensure the plugin directory exists | ||
await mkdir(pluginDir, { recursive: true }); | ||
let tmpDir = ''; | ||
|
||
try { | ||
// Install the plugin into a temporary directory | ||
tmpDir = await installPluginToTmpDir(name); | ||
console.log(`[plugins] Moving plugin from temp directory ${tmpDir} to final plugin directory ${pluginDir}`); | ||
|
||
// Handle the plugin's dependencies | ||
// Create a node_modules directory inside the plugin directory | ||
const pluginModulesDir = path.resolve(pluginDir, 'node_modules'); | ||
await mkdir(pluginModulesDir, { recursive: true }); | ||
|
||
// Read all folders/files in the temp directory | ||
const tmpFiles = await readdir(tmpDir); | ||
console.log(`[plugins] Moving plugin from temp directory ${tmpDir} to final plugin directory ${pluginDir}`); | ||
|
||
// Move the main plugin folder into the plugin directory | ||
await cp(path.resolve(tmpDir, scope || '', pluginName), pluginDir, { | ||
recursive: true, | ||
verbatimSymlinks: true, | ||
}); | ||
|
||
// Filter out the main plugin directory and non-directories | ||
// and copy each directory to the plugin's node_modules directory | ||
// Use Promise.all to copy all directories in parallel | ||
const filtered = await Promise.all( | ||
tmpFiles.map(async filename => { | ||
const fullPath = path.resolve(tmpDir, filename); | ||
const fileStat = await stat(fullPath); | ||
return { | ||
filename, | ||
include: filename !== (scope || pluginName) && filename !== 'node_modules' && fileStat.isDirectory(), | ||
}; | ||
}), | ||
); | ||
|
||
await Promise.all( | ||
filtered | ||
.filter(f => f.include) | ||
.map(async ({ filename }) => { | ||
const src = path.resolve(tmpDir, filename); | ||
const dest = path.resolve(pluginModulesDir, filename); | ||
await cp(src, dest, { recursive: true, verbatimSymlinks: true }); | ||
}), | ||
); | ||
} catch (error) { | ||
console.error(`Failed to install plugin ${pluginName}: ${error.toString()}`); | ||
throw error; | ||
} finally { | ||
// Ensure the temporary directory is cleaned up | ||
if (tmpDir) { | ||
try { | ||
console.log(`[plugins] Cleaning up temporary directory: ${tmpDir}`); | ||
await rm(tmpDir, { recursive: true, force: true }); | ||
} catch (error) { | ||
console.warn(`[plugins] Failed to clean tmp dir ${tmpDir}: ${error.toString()}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Installs a plugin into a temporary directory using Yarn. | ||
* Creates a minimal package.json and downloads the dependency. | ||
*/ | ||
export async function installPluginToTmpDir(name: string) { | ||
const { scope, pkg: pluginName } = parsePackageName(name); | ||
try { | ||
const tmpDir = await mkdtemp(path.resolve(tmpdir(), `${pluginName}-${Date.now()}`)); | ||
|
||
await writeFile( | ||
path.resolve(tmpDir, 'package.json'), | ||
JSON.stringify({ license: 'ISC', workspaces: [] }, null, 2), | ||
'utf-8', | ||
); | ||
|
||
console.log(`[plugins] Installing plugin into temp dir: ${tmpDir}`); | ||
|
||
await runYarnCommand( | ||
[ | ||
'add', | ||
name, | ||
'--modules-folder', | ||
tmpDir, | ||
'--cwd', | ||
tmpDir, | ||
'--no-lockfile', | ||
'--production', | ||
'--no-progress', | ||
'--ignore-workspace-root-check', | ||
'--registry', | ||
'https://registry.npmjs.org/', | ||
], | ||
tmpDir, | ||
); | ||
|
||
// Check if the plugin was installed successfully | ||
const pluginDir = path.resolve(tmpDir, scope || pluginName); | ||
const pluginExists = await stat(pluginDir) | ||
.then(() => true) | ||
.catch(() => false); | ||
if (!pluginExists) { | ||
throw new Error(`Plugin "${name}" not found in temporary directory`); | ||
} | ||
|
||
console.log(`[plugins] Plugin installed successfully in temp dir: ${tmpDir}`); | ||
|
||
// Check if the plugin has a package.json file | ||
const packageJsonPath = path.resolve(tmpDir, scope || '', pluginName, 'package.json'); | ||
const packageJsonExists = await stat(packageJsonPath) | ||
.then(() => true) | ||
.catch(() => false); | ||
|
||
if (!packageJsonExists) { | ||
throw new Error(`Plugin "${name}" does not have a package.json file`); | ||
} | ||
|
||
return tmpDir; | ||
} catch (err) { | ||
throw new Error(`Failed to install plugin: ${(err as Error).message}`); | ||
} | ||
} | ||
|
||
function parsePackageName(name: string) { | ||
if (name.startsWith('@')) { | ||
const [scope, pkg] = name.split('/'); | ||
return { scope, pkg }; | ||
} | ||
return { scope: null, pkg: name }; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now this isn't in the beforePack function you shouldn't need this code.