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

View File

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

View File

@ -30,8 +30,7 @@
"lodash.get": "^4.4.2", "lodash.get": "^4.4.2",
"set-value": "^4.1.0", "set-value": "^4.1.0",
"source-map-support": "0.5.19", "source-map-support": "0.5.19",
"supports-color": "9.2.1", "supports-color": "9.2.1"
"which": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.12.10", "@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 D = require('../../build/debug');
const debug = D('@flecks/core/server'); const debug = D('@flecks/core/server');
const debugSilly = debug.extend('silly'); const debugSilly = debug.extend('silly');
exports.binaryPath = (binary) => ( const {
new Promise((resolve, reject) => { FLECKS_CORE_ROOT = process.cwd(),
exec(`npx which ${binary}`, (error, stdout) => { } = process.env;
if (error) {
reject(error); exports.binaryPath = async (binary, root = FLECKS_CORE_ROOT) => {
return; // eslint-disable-next-line no-eval
} const resolved = dirname(await realpath(eval('require').resolve(join(root, 'package.json'))));
resolve(stdout.trim()); 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;
}
catch (error) {
parts.pop();
}
}
throw new Error(`Binary '${binary}' not found! (root: ${root})`);
};
exports.processCode = (child) => new Promise((resolve, reject) => { exports.processCode = (child) => new Promise((resolve, reject) => {
child.on('error', 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 = { exports.hooks = {
'@flecks/core.config': () => ({ '@flecks/core.config': () => ({
@ -25,16 +33,24 @@ exports.hooks = {
*/ */
url: undefined, url: undefined,
}), }),
'@flecks/build.config.alter': (configs) => { '@flecks/build.config.alter': async (configs) => {
const {server: config} = configs; const electronPath = await binaryPath('electron', '@flecks/electron');
if (config) { const {server} = configs;
const plugin = config.plugins.find(({pluginName}) => pluginName === 'StartServerPlugin'); if (server) {
// Extremely hackish, c'est la vie. const plugin = server.plugins.find(({pluginName}) => pluginName === 'StartServerPlugin');
if (plugin) { if (plugin) {
const relativePath = relative(server.output.path, electronPath);
const {exec} = plugin.options; const {exec} = plugin.options;
plugin.options.exec = (compilation) => { plugin.options.exec = (compilation) => {
plugin.options.args = [join(config.output.path, compilation.getPath(exec))]; const assetPath = compilation.getPath(exec);
return join('..', '..', 'node_modules', '.bin', 'electron'); 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 = {}} = {}) { export async function buildChild(path, {args = [], opts = {}} = {}) {
return spawnWith( return spawnWith(
[await binaryPath('flecks'), 'build', ...args], [await binaryPath('flecks', '@flecks/build'), 'build', ...args],
{ {
...opts, ...opts,
env: { env: {

View File

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