refactor: async

This commit is contained in:
cha0s 2022-03-01 10:14:38 -06:00
parent 18bb7b961b
commit 73f36ab3c0
14 changed files with 568 additions and 534 deletions

View File

@ -2,13 +2,18 @@ const neutrino = require('neutrino');
const R = require('../bootstrap/require');
const {targetNeutrino} = require('../server/commands');
const D = require('../debug');
const {default: Flecks} = require('../server/flecks');
const debug = D('@flecks/core/.eslintrc.js');
const {
FLECKS_CORE_BUILD_TARGET = 'fleck',
} = process.env;
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
const config = R(process.env[targetNeutrino(FLECKS_CORE_BUILD_TARGET)]);
flecks.invokeFlat('@flecks/core/build', FLECKS_CORE_BUILD_TARGET, config);

View File

@ -29,9 +29,11 @@ const buildList = FLECKS_CORE_BUILD_LIST
.map((name) => name.trim())
.filter((e) => e);
const flecks = Flecks.bootstrap();
export default (async () => {
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
const buildConfigs = async () => {
debug('gathering configs');
let targets = flatten(flecks.invokeFlat('@flecks/core/targets'));
if (buildList.length > 0) {
@ -68,6 +70,4 @@ const buildConfigs = async () => {
await new Promise(() => {});
}
return webpackConfigs;
};
export default buildConfigs();
})();

View File

@ -79,6 +79,7 @@ else {
const program = new Command();
program.enablePositionalOptions();
// Bootstrap.
(async () => {
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
@ -106,4 +107,5 @@ else {
}
// Parse commandline.
program.parse(process.argv);
})();
}

View File

@ -1,11 +1,11 @@
import D from 'debug';
const D = require('debug');
const {
VSCODE_INSPECTOR_OPTIONS,
} = process.env;
let hasInitialized;
export default (name) => {
module.exports = (name) => {
if (!hasInitialized) {
// VSCode has a problem showing colors when formatting objects.
if (VSCODE_INSPECTOR_OPTIONS) {

View File

@ -5,16 +5,18 @@ const {join} = require('path');
const banner = require('@neutrinojs/banner');
const copy = require('@neutrinojs/copy');
module.exports = require('@flecks/fleck/server/build/fleck.neutrinorc');
module.exports = (async () => {
// eslint-disable-next-line global-require
const config = await require('@flecks/fleck/server/build/fleck.neutrinorc');
module.exports.use.push(banner({
config.use.push(banner({
banner: '#!/usr/bin/env node',
include: /^cli\.js$/,
pluginId: 'shebang',
raw: true,
}));
}));
module.exports.use.push(({config}) => {
config.use.push(({config}) => {
config
.plugin('executable')
.use(class Executable {
@ -30,9 +32,9 @@ module.exports.use.push(({config}) => {
}
});
});
});
module.exports.use.push(
config.use.push(
copy({
copyUnmodified: true,
patterns: [
@ -42,4 +44,8 @@ module.exports.use.push(
},
],
}),
);
);
return config;
})();

View File

@ -5,16 +5,18 @@ const {join} = require('path');
const banner = require('@neutrinojs/banner');
const copy = require('@neutrinojs/copy');
module.exports = require('@flecks/fleck/server/build/fleck.neutrinorc');
module.exports = (async () => {
// eslint-disable-next-line global-require
const config = await require('@flecks/fleck/server/build/fleck.neutrinorc');
module.exports.use.push(banner({
config.use.push(banner({
banner: '#!/usr/bin/env node',
include: /^cli\.js$/,
pluginId: 'shebang',
raw: true,
}));
}));
module.exports.use.push(({config}) => {
config.use.push(({config}) => {
config
.plugin('executable')
.use(class Executable {
@ -30,9 +32,9 @@ module.exports.use.push(({config}) => {
}
});
});
});
module.exports.use.push(
config.use.push(
copy({
copyUnmodified: true,
patterns: [
@ -42,4 +44,8 @@ module.exports.use.push(
},
],
}),
);
);
return config;
})();

View File

@ -1,4 +1,4 @@
import {execSync, spawn} from 'child_process';
import {exec, spawn} from 'child_process';
import {mkdir} from 'fs/promises';
import {tmpdir} from 'os';
import {join} from 'path';
@ -7,23 +7,25 @@ import {D} from '@flecks/core';
const debug = D('@flecks/docker/container');
const containerIsRunning = (name) => {
try {
const output = execSync(
const containerIsRunning = async (name) => (
new Promise((r, e) => {
exec(
`docker container inspect -f '{{.State.Running}}' ${name}`,
{stdio: 'pipe'},
).toString();
if ('true\n' === output) {
return true;
}
}
catch (e) {
(error, stdout) => {
if (error) {
if (1 !== e.status) {
throw e;
e(error);
}
else {
r(false);
}
}
return false;
};
r('true\n' === stdout);
},
);
})
);
export default async (flecks, key, config) => {
const {id} = flecks.get('@flecks/core');

View File

@ -12,20 +12,21 @@ const {
const debug = D('@flecks/fleck/fleck.neutrino.js');
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
const config = require('../../../../core/src/bootstrap/fleck.neutrinorc');
const compiler = flecks.invokeFleck(
module.exports = (async () => {
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
const compiler = flecks.invokeFleck(
'@flecks/fleck/compiler',
flecks.get('@flecks/fleck.compiler'),
);
if (compiler) {
);
if (compiler) {
config.use.unshift(compiler);
}
else {
}
else {
config.use.unshift((neutrino) => {
neutrino.config.plugins.delete('start-server');
});
@ -36,19 +37,19 @@ else {
cleanStaleWebpackAssets: false,
},
}));
}
}
// Augment the compiler with babel config from flecksrc.
config.use.push((neutrino) => {
// Augment the compiler with babel config from flecksrc.
config.use.push((neutrino) => {
const rcBabel = flecks.babel();
debug('.flecksrc: babel: %O', rcBabel);
neutrino.config.module
.rule('compile')
.use('babel')
.tap((options) => babelmerge(options, ...rcBabel.map(([, babel]) => babel)));
});
});
config.use.push((neutrino) => {
config.use.push((neutrino) => {
// Test entrypoint.
const testPaths = glob.sync(join(FLECKS_CORE_ROOT, 'test/*.js'));
for (let i = 0; i < flecks.platforms.length; ++i) {
@ -58,6 +59,7 @@ config.use.push((neutrino) => {
const testEntry = neutrino.config.entry('test').clear();
testPaths.forEach((path) => testEntry.add(path));
}
});
});
module.exports = config;
return config;
})();

View File

@ -1,13 +1,13 @@
/* eslint-disable import/no-extraneous-dependencies */
const config = require('@flecks/fleck/server/build/fleck.neutrinorc');
// eslint-disable-next-line import/no-extraneous-dependencies
const copy = require('@neutrinojs/copy');
/* eslint-enable import/no-extraneous-dependencies */
config.use.push(({config}) => {
module.exports = (async () => {
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
const config = await require('@flecks/fleck/server/build/fleck.neutrinorc');
config.use.push(({config}) => {
config.entryPoints.delete('build/template');
});
config.use.push(
});
config.use.push(
copy({
copyUnmodified: true,
patterns: [
@ -17,6 +17,6 @@ config.use.push(
},
],
}),
);
module.exports = config;
);
return config;
})();

View File

@ -14,12 +14,12 @@ const {
const debug = D('@flecks/http/http.neutrino.js');
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
// Neutrino configuration.
const config = {
module.exports = (async () => {
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
// Neutrino configuration.
const config = {
options: {
output: 'dist',
root: FLECKS_CORE_ROOT,
@ -32,18 +32,18 @@ const config = {
FLECKS_CORE_BUILD_TARGET: 'client',
}]);
},
targets(flecks),
await targets(flecks),
],
};
// Compile code.
const compiler = flecks.invokeFleck(
};
// Compile code.
const compiler = flecks.invokeFleck(
'@flecks/http/server/compiler',
flecks.get('@flecks/http/server.compiler'),
);
if (compiler) {
);
if (compiler) {
config.use.push(compiler);
}
else {
}
else {
// Use neutrino's web middleware by default.
config.use.push(web({
clean: false,
@ -61,12 +61,12 @@ else {
},
},
}));
}
// Configure dev server.
config.use.push(devServer(flecks));
// Build the client runtime.
config.use.push(runtime(flecks));
// Output configuration.
config.use.push(outputs());
module.exports = config;
}
// Configure dev server.
config.use.push(devServer(flecks));
// Build the client runtime.
config.use.push(await runtime(flecks));
// Output configuration.
config.use.push(outputs());
return config;
})();

View File

@ -1,4 +1,4 @@
const {realpathSync} = require('fs');
const {realpath} = require('fs/promises');
const {
dirname,
join,
@ -10,10 +10,16 @@ const glob = require('glob');
const debug = D('@flecks/http/runtime');
module.exports = (flecks) => (neutrino) => {
module.exports = async (flecks) => {
debug('bootstrapping flecks...');
const httpFlecks = Flecks.bootstrap({platforms: ['client'], without: ['server']});
debug('bootstrapped');
const runtime = await realpath(R.resolve(join(flecks.resolve('@flecks/http'), 'runtime')));
const fullresolve = (fleck, path) => realpath(R.resolve(join(flecks.resolve(fleck), path)));
const entry = await fullresolve('@flecks/http', 'entry');
const importLoader = await fullresolve('@flecks/http', 'import-loader');
const tests = await realpath(R.resolve(join(flecks.resolve('@flecks/http'), 'tests')));
return (neutrino) => {
const {resolver} = httpFlecks;
const paths = Object.entries(resolver);
const source = [
@ -36,7 +42,7 @@ module.exports = (flecks) => (neutrino) => {
// HMR.
source.push('if (module.hot) {');
paths.forEach(([path]) => {
source.push(` module.hot.accept('${path}', () => {`);
source.push(` module.hot.accept('${path}', async () => {`);
source.push(` window.flecks.refresh('${path}', require('${path}'));`);
source.push(` window.flecks.invoke('@flecks/core/hmr', '${path}');`);
source.push(' });');
@ -44,7 +50,6 @@ module.exports = (flecks) => (neutrino) => {
source.push('}');
source.push('');
// Create runtime.
const runtime = realpathSync(R.resolve(join(flecks.resolve('@flecks/http'), 'runtime')));
neutrino.config.module
.rule(runtime)
.test(runtime)
@ -57,13 +62,11 @@ module.exports = (flecks) => (neutrino) => {
.set('@flecks/http/runtime$', runtime);
flecks.runtimeCompiler('http', neutrino);
// Handle runtime import.
const fullresolve = (fleck, path) => realpathSync(R.resolve(join(flecks.resolve(fleck), path)));
const entry = fullresolve('@flecks/http', 'entry');
neutrino.config.module
.rule(entry)
.test(entry)
.use('entry/http')
.loader(fullresolve('@flecks/http', 'import-loader'));
.loader(importLoader);
// Aliases.
const aliases = flecks.aliases();
if (Object.keys(aliases).length > 0) {
@ -101,7 +104,6 @@ module.exports = (flecks) => (neutrino) => {
const testEntry = neutrino.config.entry('test').clear();
testPaths.forEach(([, path]) => testEntry.add(path));
}
const tests = realpathSync(R.resolve(join(flecks.resolve('@flecks/http'), 'tests')));
neutrino.config.module
.rule(tests)
.test(tests)
@ -122,4 +124,5 @@ module.exports = (flecks) => (neutrino) => {
([original, paths]) => `describe('${original}', () => { ${paths.join(' ')} });`,
).join(''),
});
};
};

View File

@ -1,6 +1,6 @@
/* eslint-disable no-param-reassign */
const {dirname, join} = require('path');
const {realpathSync} = require('fs');
const {realpath} = require('fs/promises');
const {require: R} = require('@flecks/core/server');
@ -8,14 +8,16 @@ const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
module.exports = (flecks) => (neutrino) => {
module.exports = async (flecks) => {
const root = await realpath(dirname(R.resolve(join(flecks.resolve('@flecks/http'), 'entry.js'))));
return (neutrino) => {
const {options} = neutrino;
const {output: originalOutput} = options;
neutrino.config.resolve.modules.merge([
join(FLECKS_CORE_ROOT, 'node_modules'),
'node_modules',
]);
options.root = realpathSync(dirname(R.resolve(join(flecks.resolve('@flecks/http'), 'entry.js'))));
options.root = root;
options.source = '.';
options.mains.index = 'entry';
options.mains.tests = {
@ -23,4 +25,5 @@ module.exports = (flecks) => (neutrino) => {
title: 'Testbed',
};
options.output = join(originalOutput, flecks.get('@flecks/http/server.output'));
};
};

View File

@ -1,9 +1,11 @@
const {realpathSync} = require('fs');
const {realpath} = require('fs/promises');
const {join} = require('path');
const {require: R} = require('@flecks/core/server');
module.exports = (flecks) => (neutrino) => {
module.exports = async (flecks) => {
const runtime = await realpath(R.resolve(join(flecks.resolve('@flecks/server'), 'runtime')));
return (neutrino) => {
const {config, resolver} = flecks;
// Inject flecks configuration.
const paths = Object.keys(resolver);
@ -40,7 +42,7 @@ module.exports = (flecks) => (neutrino) => {
source.push(' });');
// Hooks for each fleck.
paths.forEach((path) => {
source.push(` module.hot.accept('${path}', () => {`);
source.push(` module.hot.accept('${path}', async () => {`);
source.push(` global.flecks.refresh('${path}', require('${path}'));`);
source.push(` global.flecks.invoke('@flecks/core/hmr', '${path}');`);
source.push(' });');
@ -48,7 +50,6 @@ module.exports = (flecks) => (neutrino) => {
source.push('}');
// Create runtime.
const entries = neutrino.config.entry('index');
const runtime = realpathSync(R.resolve(join(flecks.resolve('@flecks/server'), 'runtime')));
neutrino.config.module
.rule(runtime)
.test(runtime)
@ -76,4 +77,5 @@ module.exports = (flecks) => (neutrino) => {
// Externalize the rest.
const nodeExternals = R('webpack-node-externals');
neutrino.config.externals(nodeExternals({allowlist}));
};
};

View File

@ -14,25 +14,27 @@ const {
const debug = D('@flecks/server/server.neutrino.js');
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
module.exports = (async () => {
const {
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
const {
hot,
inspect,
profile,
start: isStarting,
} = flecks.get('@flecks/server');
} = flecks.get('@flecks/server');
const entry = (neutrino) => {
const entry = (neutrino) => {
const entries = neutrino.config.entry('index');
entries.delete(join(FLECKS_CORE_ROOT, 'src', 'index'));
entries.add('@flecks/server/entry');
};
};
// Augment the application-starting configuration.
const start = (neutrino) => {
// Augment the application-starting configuration.
const start = (neutrino) => {
if (isStarting) {
neutrino.use(startServer({name: 'index.js'}));
}
@ -59,14 +61,14 @@ const start = (neutrino) => {
options.nodeArgs.push('--trace-uncaught');
return args;
});
};
};
const compiler = flecks.invokeFleck(
const compiler = flecks.invokeFleck(
'@flecks/server/compiler',
flecks.get('@flecks/server.compiler'),
);
);
const config = {
const config = {
options: {
output: 'dist',
root: FLECKS_CORE_ROOT,
@ -75,12 +77,12 @@ const config = {
entry,
start,
],
};
};
if (compiler) {
if (compiler) {
config.use.unshift(compiler);
}
else {
}
else {
config.use.unshift((neutrino) => {
// Default to not starting application on build.
neutrino.config.plugins.delete('start-server');
@ -89,20 +91,20 @@ else {
clean: false,
hot,
}));
}
// Stub out non-server-friendly modules on the server.
const stubs = flecks.stubs();
if (stubs.length > 0) {
}
// Stub out non-server-friendly modules on the server.
const stubs = flecks.stubs();
if (stubs.length > 0) {
config.use.unshift(({config}) => {
stubs.forEach((path) => {
config.resolve.alias
.set(path, '@flecks/core/empty');
});
});
}
// Hardcore hax for module aliasing.
const aliases = flecks.aliases();
if (Object.keys(aliases).length > 0) {
}
// Hardcore hax for module aliasing.
const aliases = flecks.aliases();
if (Object.keys(aliases).length > 0) {
const code = [
`const aliases = ${JSON.stringify(aliases)};`,
'const {Module} = require("module");',
@ -118,17 +120,18 @@ if (Object.keys(aliases).length > 0) {
banner: code,
pluginId: 'aliases-banner',
}));
}
}
// Build the server runtime.
config.use.push(runtime(flecks));
// Build the server runtime.
config.use.push(await runtime(flecks));
// Give the resolver a helping hand.
config.use.push((neutrino) => {
// Give the resolver a helping hand.
config.use.push((neutrino) => {
neutrino.config.resolve.modules.merge([
join(FLECKS_CORE_ROOT, 'node_modules'),
'node_modules',
]);
});
});
module.exports = config;
return config;
})();