From f83a4635b47120097a53b99ab07c009bd44cd75b Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Fri, 10 May 2024 12:59:25 -0700 Subject: [PATCH 1/2] wip --- lib/commands/index.js | 1 + lib/commands/pnpm/index.js | 22 ++++++++++++++++++++++ lib/shadow/pnpm-cli.cjs | 29 +++++++++++++++++++++++++++++ lib/shadow/pnpm-injection.cjs | 20 ++++++++++++++++++++ package-lock.json | 5 ++--- 5 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 lib/commands/pnpm/index.js create mode 100644 lib/shadow/pnpm-cli.cjs create mode 100644 lib/shadow/pnpm-injection.cjs diff --git a/lib/commands/index.js b/lib/commands/index.js index aae0d337..931702ff 100644 --- a/lib/commands/index.js +++ b/lib/commands/index.js @@ -2,6 +2,7 @@ export * from './info/index.js' export * from './report/index.js' export * from './npm/index.js' export * from './npx/index.js' +export * from './pnpm/index.js' export * from './login/index.js' export * from './logout/index.js' export * from './wrapper/index.js' diff --git a/lib/commands/pnpm/index.js b/lib/commands/pnpm/index.js new file mode 100644 index 00000000..0b639060 --- /dev/null +++ b/lib/commands/pnpm/index.js @@ -0,0 +1,22 @@ +import { spawn } from 'child_process' +import { fileURLToPath } from 'url' + +const description = 'pnpm wrapper functionality' + +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ +export const pnpm = { + description, + run: async (argv, _importMeta, _ctx) => { + const wrapperPath = fileURLToPath(new URL('../../shadow/pnpm-cli.cjs', import.meta.url)) + process.exitCode = 1 + spawn(process.execPath, [wrapperPath, ...argv], { + stdio: 'inherit' + }).on('exit', (code, signal) => { + if (signal) { + process.kill(process.pid, signal) + } else if (code !== null) { + process.exit(code) + } + }) + } +} diff --git a/lib/shadow/pnpm-cli.cjs b/lib/shadow/pnpm-cli.cjs new file mode 100644 index 00000000..0ea8b300 --- /dev/null +++ b/lib/shadow/pnpm-cli.cjs @@ -0,0 +1,29 @@ +#!/usr/bin/env node +// THIS FILE USES .cjs to get around the extension-free entrypoint problem with ESM +'use strict' +const { spawn } = require('child_process') +const { realpathSync } = require('fs') +const path = require('path') + +const realFilename = realpathSync(__filename) +const realDirname = path.dirname(realFilename) + +/** + */ +async function main () { + const pnpmpath = await require('./link.cjs')(path.join(realDirname, 'bin'), 'pnpm') + process.exitCode = 1 + const injectionpath = path.join(realDirname, 'pnpm-injection.cjs') + console.log(process.execPath) + + // spawn(process.execPath, ['--require', injectionpath, pnpmpath, ...process.argv.slice(2)], { + // stdio: 'inherit' + // }).on('exit', (code, signal) => { + // if (signal) { + // process.kill(process.pid, signal) + // } else if (code !== null) { + // process.exit(code) + // } + // }) + } + main() \ No newline at end of file diff --git a/lib/shadow/pnpm-injection.cjs b/lib/shadow/pnpm-injection.cjs new file mode 100644 index 00000000..85d3862c --- /dev/null +++ b/lib/shadow/pnpm-injection.cjs @@ -0,0 +1,20 @@ +/* eslint-disable no-console */ +// THIS MUST BE CJS TO WORK WITH --require +'use strict' + +const path = require('path') + +try { + // due to update-notifier pkg being ESM only we actually spawn a subprocess sadly + require('child_process').spawnSync(process.execPath, [ + path.join(__dirname, 'update-notifier.mjs') + ], { + stdio: 'inherit' + }) +} catch (e) { + // ignore if update notification fails +} + +// console.log('HERE: ', process.argv[1]) + +require.cache[process.argv[1]] = { exports: {} } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index ede12c72..bcdca39f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,12 @@ { "name": "@socketsecurity/cli", - "version": "0.9.0", + "version": "0.9.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@socketsecurity/cli", - "version": "0.9.0", - "hasInstallScript": true, + "version": "0.9.3", "license": "MIT", "dependencies": { "@apideck/better-ajv-errors": "^0.3.6", From c1e86eecc2562acb022f00a28c26d3e2b42b9c48 Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Tue, 28 May 2024 17:04:32 -0700 Subject: [PATCH 2/2] wip --- lib/shadow/pnpm-cli.cjs | 18 +++++++++++++++++- lib/shadow/pnpm-injection.cjs | 35 +++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/lib/shadow/pnpm-cli.cjs b/lib/shadow/pnpm-cli.cjs index 0ea8b300..e92ad284 100644 --- a/lib/shadow/pnpm-cli.cjs +++ b/lib/shadow/pnpm-cli.cjs @@ -3,6 +3,7 @@ 'use strict' const { spawn } = require('child_process') const { realpathSync } = require('fs') +const fs = require('fs') const path = require('path') const realFilename = realpathSync(__filename) @@ -14,9 +15,24 @@ async function main () { const pnpmpath = await require('./link.cjs')(path.join(realDirname, 'bin'), 'pnpm') process.exitCode = 1 const injectionpath = path.join(realDirname, 'pnpm-injection.cjs') - console.log(process.execPath) + const pnpmModulePath = `${process.env.PNPM_HOME}/global/5/.pnpm/pnpm@9.1.0/node_modules/pnpm/dist/pnpm.cjs` + + // require.cache[process.argv[1]] = { exports: {} } + // require.cache[pnpmModulePath] = { get exports() {console.log(123)} } + // require('module').Module.runMain = () => {} + + // Read module file + // await fs.readFile(pnpmModulePath, 'utf-8' , (err, data) => { + // if (err) throw err; + // console.log(data); + // }); + + // Require the installed pnpm module + require(pnpmModulePath) // spawn(process.execPath, ['--require', injectionpath, pnpmpath, ...process.argv.slice(2)], { + // spawn(process.execPath, ['--require', injectionpath, pnpmModulePath, ...process.argv.slice(2)], { + // spawn(process.execPath, ['--require', pnpmModulePath, ...process.argv.slice(2)], { // stdio: 'inherit' // }).on('exit', (code, signal) => { // if (signal) { diff --git a/lib/shadow/pnpm-injection.cjs b/lib/shadow/pnpm-injection.cjs index 85d3862c..a14dde2e 100644 --- a/lib/shadow/pnpm-injection.cjs +++ b/lib/shadow/pnpm-injection.cjs @@ -2,19 +2,30 @@ // THIS MUST BE CJS TO WORK WITH --require 'use strict' -const path = require('path') +// const path = require('path') -try { - // due to update-notifier pkg being ESM only we actually spawn a subprocess sadly - require('child_process').spawnSync(process.execPath, [ - path.join(__dirname, 'update-notifier.mjs') - ], { - stdio: 'inherit' - }) -} catch (e) { - // ignore if update notification fails +// try { +// // due to update-notifier pkg being ESM only we actually spawn a subprocess sadly +// // require('child_process').spawnSync(process.execPath, [ +// // path.join(__dirname, 'update-notifier.mjs') +// // ], { +// // stdio: 'inherit' +// // }) +// } catch (e) { +// // ignore if update notification fails +// } + +// require.cache[process.argv[1]] = { exports: {} } + +const pnpmInjection = async (depGraph) => { + if (process.env.SOCKET_PNPM_WRAPPER) { + + } } -// console.log('HERE: ', process.argv[1]) +const dryRun = Boolean(process.env.SOCKET_PNPM_WRAPPER) -require.cache[process.argv[1]] = { exports: {} } \ No newline at end of file +module.exports = { + pnpmInjection, + dryRun +} \ No newline at end of file