refactor: lint, config

This commit is contained in:
cha0s 2023-12-02 22:31:14 -06:00
parent de41c1e01e
commit ab6d1f1720
10 changed files with 97 additions and 84 deletions

View File

@ -44,7 +44,7 @@ export const hooks = {
/** /**
* Make sure you return them as an array expression, like this. * Make sure you return them as an array expression, like this.
*/ */
['mygeneralrc.js', {specifier: (specific) => `${specific}.mygeneralrc.js`}], ['something.config.js', {specifier: (specific) => `something.${specific}.config.js`}],
], ],
/** /**

View File

@ -1,8 +1,31 @@
const configFn = require('../src/server/build/fleck.webpack.config'); const ESLintPlugin = require('eslint-webpack-plugin');
const {executable} = require('../src/server/webpack');
module.exports = (env, argv) => { const configFn = require('../src/server/build/webpack.config');
const config = configFn(env, argv); const {executable} = require('../src/server/webpack');
const eslintConfigFn = require('../src/server/build/default.eslint.config');
const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
module.exports = async (env, argv) => {
const config = await configFn(env, argv);
config.plugins.push(...executable()); config.plugins.push(...executable());
const eslint = await eslintConfigFn();
eslint.settings['import/resolver'].webpack = {
config: {
resolve: config.resolve,
},
};
config.plugins.push(
new ESLintPlugin({
cache: true,
cwd: FLECKS_CORE_ROOT,
emitWarning: argv.mode !== 'production',
failOnError: argv.mode === 'production',
useEslintrc: false,
overrideConfig: eslint,
}),
);
return config; return config;
}; };

View File

@ -36,8 +36,8 @@
"server/build/default.eslint.config.js.map", "server/build/default.eslint.config.js.map",
"server/build/eslint.config.js", "server/build/eslint.config.js",
"server/build/eslint.config.js.map", "server/build/eslint.config.js.map",
"server/build/fleck.webpack.config.js", "server/build/webpack.config.js",
"server/build/fleck.webpack.config.js.map", "server/build/webpack.config.js.map",
"server/build/fleckspack.config.js", "server/build/fleckspack.config.js",
"server/build/fleckspack.config.js.map", "server/build/fleckspack.config.js.map",
"src", "src",

View File

@ -3,7 +3,7 @@ const globals = require('globals');
const R = require('../../require'); const R = require('../../require');
module.exports = (flecks) => { module.exports = async (flecks) => {
const merging = [ const merging = [
{ {
plugins: [R.resolve('@babel/plugin-syntax-dynamic-import')], plugins: [R.resolve('@babel/plugin-syntax-dynamic-import')],
@ -40,7 +40,8 @@ module.exports = (flecks) => {
__non_webpack_require__: true, __non_webpack_require__: true,
}, },
ignorePatterns: [ ignorePatterns: [
'**/dist/**', 'dist/**',
// Not even gonna try.
'build/dox/hooks.js', 'build/dox/hooks.js',
], ],
overrides: [ overrides: [

View File

@ -9,12 +9,10 @@ const {join} = require('path');
const D = require('../../debug'); const D = require('../../debug');
const {default: Flecks} = require('../flecks'); const {default: Flecks} = require('../flecks');
// const R = require('../../require');
const debug = D('@flecks/core/server/build/eslint.config.js'); const debug = D('@flecks/core/server/build/eslint.config.js');
const { const {
FLECKS_CORE_BUILD_TARGET = 'fleck',
FLECKS_CORE_ROOT = process.cwd(), FLECKS_CORE_ROOT = process.cwd(),
FLECKS_CORE_SYNC_FOR_ESLINT = false, FLECKS_CORE_SYNC_FOR_ESLINT = false,
} = process.env; } = process.env;
@ -25,10 +23,10 @@ if (FLECKS_CORE_SYNC_FOR_ESLINT) {
debug('bootstrapping flecks...'); debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap(); const flecks = Flecks.bootstrap();
debug('bootstrapped'); debug('bootstrapped');
const eslintConfigFn = __non_webpack_require__(flecks.buildConfig('default.eslint.config.js', FLECKS_CORE_BUILD_TARGET)); const eslintConfigFn = __non_webpack_require__(flecks.buildConfig('default.eslint.config.js'));
const eslintConfig = eslintConfigFn(flecks); const eslintConfig = await eslintConfigFn(flecks);
const webpackConfigFn = __non_webpack_require__(flecks.buildConfig('webpack.config.js', FLECKS_CORE_BUILD_TARGET)); const webpackConfigFn = __non_webpack_require__(flecks.buildConfig('webpack.config.js', 'fleck'));
const webpackConfig = webpackConfigFn({}, {mode: 'development'}, flecks); const webpackConfig = await webpackConfigFn({}, {mode: 'development'}, flecks);
eslintConfig.settings['import/resolver'].webpack = { eslintConfig.settings['import/resolver'].webpack = {
config: { config: {
resolve: webpackConfig.resolve, resolve: webpackConfig.resolve,

View File

@ -8,12 +8,10 @@ const {
const babelmerge = require('babel-merge'); const babelmerge = require('babel-merge');
const CopyPlugin = require('copy-webpack-plugin'); const CopyPlugin = require('copy-webpack-plugin');
const glob = require('glob'); const glob = require('glob');
const ESLintPlugin = require('eslint-webpack-plugin');
const D = require('../../debug'); const D = require('../../debug');
const R = require('../../require'); const R = require('../../require');
const {defaultConfig, externals, regexFromExtensions} = require('../webpack'); const {defaultConfig, externals, regexFromExtensions} = require('../webpack');
const eslintConfigFn = require('./default.eslint.config');
const { const {
FLECKS_CORE_ROOT = process.cwd(), FLECKS_CORE_ROOT = process.cwd(),
@ -137,22 +135,6 @@ module.exports = (env, argv, flecks) => {
], ],
}, },
); );
const eslint = eslintConfigFn(flecks);
eslint.settings['import/resolver'].webpack = {
config: {
resolve: config.resolve,
},
};
config.plugins.push(
new ESLintPlugin({
cache: true,
cwd: FLECKS_CORE_ROOT,
emitWarning: argv.mode !== 'production',
failOnError: argv.mode === 'production',
useEslintrc: false,
overrideConfig: eslint,
}),
);
// Automatic entry registration. // Automatic entry registration.
files files
.filter(resolveValidModulePath(source)) .filter(resolveValidModulePath(source))

View File

@ -2,6 +2,7 @@ import {spawn} from 'child_process';
import {join, normalize} from 'path'; import {join, normalize} from 'path';
import {Argument} from 'commander'; import {Argument} from 'commander';
import glob from 'glob';
import flatten from 'lodash.flatten'; import flatten from 'lodash.flatten';
import rimraf from 'rimraf'; import rimraf from 'rimraf';
@ -33,6 +34,8 @@ export const spawnWith = (cmd, opts = {}) => {
...(opts.env || {}), ...(opts.env || {}),
}, },
}); });
process.stderr.setMaxListeners(100);
process.stdout.setMaxListeners(100);
child.stderr.pipe(process.stderr); child.stderr.pipe(process.stderr);
child.stdout.pipe(process.stdout); child.stdout.pipe(process.stdout);
return child; return child;
@ -97,32 +100,34 @@ export default (program, flecks) => {
); );
}, },
}; };
commands.lint = { }
description: 'run linter', commands.lint = {
args: [ description: 'run linter',
program.createArgument('[target]', 'target').choices(targets), args: [],
], action: async () => {
action: (targetArgument) => { const promises = [];
const promises = []; const packages = await new Promise((r, e) => {
for (let i = 0; i < targets.length; ++i) { glob(
const target = targets[i]; join('packages', '*'),
if (targetArgument && targetArgument !== target) { (error, result) => (error ? e(error) : r(result)),
// eslint-disable-next-line no-continue );
continue; });
} if (0 === packages.length) {
process.env.FLECKS_CORE_BUILD_TARGET = target; packages.push('.');
}
packages
.map((pkg) => join(process.cwd(), pkg))
.forEach((cwd) => {
const cmd = [ const cmd = [
'npx', 'eslint', 'npx', 'eslint',
'--config', flecks.buildConfig('eslint.config.js', target), '--config', flecks.buildConfig('eslint.config.js'),
'.', '.',
]; ];
promises.push(new Promise((resolve, reject) => { promises.push(new Promise((resolve, reject) => {
const child = spawnWith( const child = spawnWith(
cmd, cmd,
{ {
env: { cwd,
FLECKS_CORE_BUILD_TARGET: target,
},
}, },
); );
child.on('error', reject); child.on('error', reject);
@ -131,28 +136,27 @@ export default (program, flecks) => {
resolve(code); resolve(code);
}); });
})); }));
} });
const promise = Promise.all(promises) const promise = Promise.all(promises)
.then( .then(
(codes) => ( (codes) => (
codes.every((code) => 0 === parseInt(code, 10)) codes.every((code) => 0 === parseInt(code, 10))
? 0 ? 0
: codes.find((code) => code !== 0) : codes.find((code) => code !== 0)
), ),
); );
return { return {
off: () => {}, off: () => {},
on: (type, fn) => { on: (type, fn) => {
if ('error' === type) { if ('error' === type) {
promise.catch(fn); promise.catch(fn);
} }
else if ('exit' === type) { else if ('exit' === type) {
promise.then(fn); promise.then(fn);
} }
}, },
}; };
}, },
}; };
}
return commands; return commands;
}; };

View File

@ -118,13 +118,18 @@ export default class ServerFlecks extends Flecks {
if (!config) { if (!config) {
throw new Error(`Unknown build config '${path}'`); throw new Error(`Unknown build config '${path}'`);
} }
const paths = [`${specific}.${path}`]; const paths = [];
if ('specifier' in config) { if (specific) {
if (false === config.specifier) { if ('specifier' in config) {
paths.pop(); if (false === config.specifier) {
paths.shift();
}
else {
paths.push(config.specifier(specific));
}
} }
else { else {
paths.push(config.specifier(specific)); paths.push(`${specific}.${path}`);
} }
} }
paths.push(path); paths.push(path);

View File

@ -31,7 +31,7 @@ export * from './webpack';
export {webpack}; export {webpack};
export const hooks = { export const hooks = {
'@flecks/core.build': (target, config, env, argv, flecks) => { '@flecks/core.build': async (target, config, env, argv, flecks) => {
const { const {
'eslint.exclude': exclude, 'eslint.exclude': exclude,
profile, profile,
@ -45,7 +45,7 @@ export const hooks = {
} }
if (!exclude.includes(target)) { if (!exclude.includes(target)) {
const eslintConfigFn = R(flecks.buildConfig('default.eslint.config.js', target)); const eslintConfigFn = R(flecks.buildConfig('default.eslint.config.js', target));
const eslint = eslintConfigFn(flecks); const eslint = await eslintConfigFn(flecks);
config.plugins.push( config.plugins.push(
new ESLintPlugin({ new ESLintPlugin({
cache: true, cache: true,
@ -80,11 +80,11 @@ export const hooks = {
* ESLint defaults. The generated `eslint.config.js` just reads from this file so that the * ESLint defaults. The generated `eslint.config.js` just reads from this file so that the
* build can dynamically configure parts of ESLint. * build can dynamically configure parts of ESLint.
*/ */
'default.eslint.config.js', ['default.eslint.config.js', {specifier: false}],
/** /**
* ESLint configuration. See: https://eslint.org/docs/user-guide/configuring/ * ESLint configuration. See: https://eslint.org/docs/user-guide/configuring/
*/ */
'eslint.config.js', ['eslint.config.js', {specifier: false}],
/** /**
* Flecks webpack configuration. See: https://webpack.js.org/configuration/ * Flecks webpack configuration. See: https://webpack.js.org/configuration/
*/ */

View File

@ -1,4 +1,4 @@
const flecksConfigFn = require('@flecks/core/server/build/fleck.webpack.config'); const flecksConfigFn = require('@flecks/core/server/build/webpack.config');
module.exports = async (env, argv, flecks) => { module.exports = async (env, argv, flecks) => {
const config = await flecksConfigFn(env, argv, flecks); const config = await flecksConfigFn(env, argv, flecks);