fix: why is npm so broken?

(and why is THAT ONE GUY on every issue making things worse?)
This commit is contained in:
cha0s 2024-02-07 15:40:08 -06:00
parent 49cc23f668
commit f16cd6e171
7 changed files with 62 additions and 32 deletions

5
package-lock.json generated
View File

@ -11835,6 +11835,7 @@
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/isexe/-/isexe-3.1.1.tgz",
"integrity": "sha512-LpB/54B+/2J5hqQ7imZHfdU31OlgQqx7ZicVlkm9kzg9/w8GKLEcFfJl/t7DCEDueOyBAD6zCCwTO6Fzs0NoEQ==",
"dev": true,
"license": "ISC",
"engines": {
"node": ">=16"
@ -21007,6 +21008,7 @@
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/which/-/which-4.0.0.tgz",
"integrity": "sha512-GlaYyEb07DPxYCKhKzplCWBJtvxZcZMrL+4UkrTSJHHPyZU4mYYTv3qaOe77H7EODLSSopAUFAc6W8U4yqvscg==",
"dev": true,
"dependencies": {
"isexe": "^3.1.1"
},
@ -21552,8 +21554,7 @@
"lodash.get": "^4.4.2",
"set-value": "^4.1.0",
"source-map-support": "0.5.19",
"supports-color": "9.2.1",
"which": "^4.0.0"
"supports-color": "9.2.1"
},
"devDependencies": {
"@babel/core": "^7.12.10",

View File

@ -224,7 +224,7 @@ exports.commands = (program, flecks) => {
}
const webpackConfig = await flecks.resolveBuildConfig('fleckspack.config.js');
const cmd = [
await binaryPath('webpack'),
await binaryPath('webpack', '@flecks/build'),
...((watch || hot) ? ['watch'] : []),
'--config', webpackConfig,
'--mode', (production && !hot) ? 'production' : 'development',
@ -310,7 +310,7 @@ exports.commands = (program, flecks) => {
.map((pkg) => join(process.cwd(), pkg))
.map(async (cwd) => {
const cmd = [
await binaryPath('eslint'),
await binaryPath('eslint', '@flecks/build'),
'--config', await flecks.resolveBuildConfig('eslint.config.js'),
'.',
];

View File

@ -30,8 +30,7 @@
"lodash.get": "^4.4.2",
"set-value": "^4.1.0",
"source-map-support": "0.5.19",
"supports-color": "9.2.1",
"which": "^4.0.0"
"supports-color": "9.2.1"
},
"devDependencies": {
"@babel/core": "^7.12.10",

View File

@ -1,21 +1,37 @@
const {exec, fork, spawn} = require('child_process');
const {fork, spawn} = require('child_process');
const {
access,
constants: {X_OK},
realpath,
} = require('fs/promises');
const {dirname, join, sep} = require('path');
const D = require('../../build/debug');
const debug = D('@flecks/core/server');
const debugSilly = debug.extend('silly');
exports.binaryPath = (binary) => (
new Promise((resolve, reject) => {
exec(`npx which ${binary}`, (error, stdout) => {
if (error) {
reject(error);
return;
const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
exports.binaryPath = async (binary, root = FLECKS_CORE_ROOT) => {
// eslint-disable-next-line no-eval
const resolved = dirname(await realpath(eval('require').resolve(join(root, 'package.json'))));
const parts = resolved.split(sep);
while (parts.length > 0) {
const path = parts.concat(join('node_modules', '.bin', binary)).join(sep);
try {
// eslint-disable-next-line no-await-in-loop
await access(path, X_OK);
return path;
}
resolve(stdout.trim());
});
})
);
catch (error) {
parts.pop();
}
}
throw new Error(`Binary '${binary}' not found! (root: ${root})`);
};
exports.processCode = (child) => new Promise((resolve, reject) => {
child.on('error', reject);

View File

@ -1,4 +1,12 @@
const {join} = require('path');
const {
basename,
dirname,
extname,
join,
relative,
} = require('path');
const {binaryPath} = require('@flecks/core/src/server');
exports.hooks = {
'@flecks/core.config': () => ({
@ -25,16 +33,24 @@ exports.hooks = {
*/
url: undefined,
}),
'@flecks/build.config.alter': (configs) => {
const {server: config} = configs;
if (config) {
const plugin = config.plugins.find(({pluginName}) => pluginName === 'StartServerPlugin');
// Extremely hackish, c'est la vie.
'@flecks/build.config.alter': async (configs) => {
const electronPath = await binaryPath('electron', '@flecks/electron');
const {server} = configs;
if (server) {
const plugin = server.plugins.find(({pluginName}) => pluginName === 'StartServerPlugin');
if (plugin) {
const relativePath = relative(server.output.path, electronPath);
const {exec} = plugin.options;
plugin.options.exec = (compilation) => {
plugin.options.args = [join(config.output.path, compilation.getPath(exec))];
return join('..', '..', 'node_modules', '.bin', 'electron');
const assetPath = compilation.getPath(exec);
const trimmed = join(dirname(assetPath), basename(assetPath, extname(assetPath)));
plugin.options.args = [
join(
server.output.path,
`${trimmed}.mjs`,
),
];
return relativePath;
};
}
}

View File

@ -30,7 +30,7 @@ export async function createApplication() {
export async function buildChild(path, {args = [], opts = {}} = {}) {
return spawnWith(
[await binaryPath('flecks'), 'build', ...args],
[await binaryPath('flecks', '@flecks/build'), 'build', ...args],
{
...opts,
env: {

View File

@ -3,7 +3,7 @@ const {join} = require('path');
const Build = require('@flecks/build/build/build');
const {regexFromExtensions} = require('@flecks/build/src/server');
const {spawnWith} = require('@flecks/core/src/server');
const {binaryPath, spawnWith} = require('@flecks/core/src/server');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {
@ -161,14 +161,12 @@ exports.hooks = {
return;
}
// Bail if the build isn't watching.
if (!process.argv.find((arg) => '--watch' === arg)) {
if (!process.argv.find((arg) => 'watch' === arg)) {
return;
}
// Otherwise, spawn `webpack-dev-server` (WDS).
const cmd = [
// `npx` doesn't propagate signals!
// 'npx', 'webpack',
join(FLECKS_CORE_ROOT, 'node_modules', '.bin', 'webpack'),
await binaryPath('webpack', '@flecks/build'),
'serve',
'--mode', 'development',
'--hot',