Skip to content

Commit

Permalink
fix: why is npm so broken?
Browse files Browse the repository at this point in the history
(and why is THAT ONE GUY on every issue making things worse?)
  • Loading branch information
cha0s committed Feb 7, 2024
1 parent 49cc23f commit f16cd6e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 32 deletions.
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/build/build/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'),
'.',
];
Expand Down
3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 28 additions & 12 deletions packages/core/src/server/process.js
Original file line number Diff line number Diff line change
@@ -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;
}
resolve(stdout.trim());
});
})
);
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;
}
catch (error) {
parts.pop();
}
}
throw new Error(`Binary '${binary}' not found! (root: ${root})`);
};

exports.processCode = (child) => new Promise((resolve, reject) => {
child.on('error', reject);
Expand Down
32 changes: 24 additions & 8 deletions packages/electron/build/flecks.bootstrap.js
Original file line number Diff line number Diff line change
@@ -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': () => ({
Expand All @@ -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;
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/server/test/server/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
8 changes: 3 additions & 5 deletions packages/web/build/flecks.bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit f16cd6e

Please sign in to comment.