diff --git a/packages/build/build/build.js b/packages/build/build/build.js index 494499d..0ffc183 100644 --- a/packages/build/build/build.js +++ b/packages/build/build/build.js @@ -44,7 +44,12 @@ module.exports = class Build extends Flecks { .filter((e) => e); } - static async buildRuntime(originalConfig, platforms, flecks = {}) { + static async buildRuntime({ + flecks = {}, + originalConfig, + platforms, + root, + }) { const cleanConfig = JSON.parse(JSON.stringify(originalConfig)); // Dealias the config keys. const dealiasedConfig = Object.fromEntries( @@ -57,7 +62,7 @@ module.exports = class Build extends Flecks { ]; }), ); - const resolver = new Resolver({root: FLECKS_CORE_ROOT}); + const resolver = new Resolver({root}); const {paths, roots} = await explicate({ paths: Object.keys(originalConfig), platforms, @@ -129,6 +134,7 @@ module.exports = class Build extends Flecks { config: configParameter, flecks: configFlecks, platforms = ['server'], + root = FLECKS_CORE_ROOT, } = {}, ) { // Load or use parameterized configuration. @@ -136,7 +142,7 @@ module.exports = class Build extends Flecks { let configType = 'parameter'; if (!configParameter) { // eslint-disable-next-line no-param-reassign - [configType, originalConfig] = await loadConfig(); + [configType, originalConfig] = await loadConfig(root); } else { originalConfig = JSON.parse(JSON.stringify(configParameter)); @@ -147,9 +153,15 @@ module.exports = class Build extends Flecks { resolver, roots, runtime, - } = await this.buildRuntime(originalConfig, platforms, configFlecks); + } = await this.buildRuntime({ + flecks: configFlecks, + originalConfig, + platforms, + root, + }); const flecks = await super.from(runtime); flecks.platforms = platforms; + flecks.root = root; flecks.roots = roots; flecks.resolver = resolver; flecks.loadBuildFiles(); @@ -181,7 +193,7 @@ module.exports = class Build extends Flecks { if (!fleck) { throw new Error(`Unknown build config: '${config}'`); } - const rootConfig = await this.resolver.resolve(join(FLECKS_CORE_ROOT, 'build', config)); + const rootConfig = await this.resolver.resolve(join(this.root, 'build', config)); if (rootConfig) { return rootConfig; } diff --git a/packages/build/build/load-config.js b/packages/build/build/load-config.js index a339c4b..a5a75e8 100644 --- a/packages/build/build/load-config.js +++ b/packages/build/build/load-config.js @@ -5,14 +5,10 @@ const D = require('@flecks/core/build/debug'); const debug = D('@flecks/build/build/load-config'); -const { - FLECKS_CORE_ROOT = process.cwd(), -} = process.env; - -module.exports = async function loadConfig() { +module.exports = async function loadConfig(root) { try { const {load} = require('js-yaml'); - const filename = join(FLECKS_CORE_ROOT, 'build', 'flecks.yml'); + const filename = join(root, 'build', 'flecks.yml'); const buffer = await readFile(filename, 'utf8'); debug('parsing configuration from YML...'); return ['YML', load(buffer, {filename})]; @@ -21,7 +17,7 @@ module.exports = async function loadConfig() { if ('ENOENT' !== error.code) { throw error; } - const {name} = require(join(FLECKS_CORE_ROOT, 'package.json')); + const {name} = require(join(root, 'package.json')); const barebones = { '@flecks/build': {}, '@flecks/core': {}, @@ -30,7 +26,7 @@ module.exports = async function loadConfig() { if (barebones[name]) { delete barebones[name]; } - barebones[`${name}:${FLECKS_CORE_ROOT}`] = {}; + barebones[`${name}:${root}`] = {}; return ['barebones', barebones]; } }; diff --git a/packages/build/package.json b/packages/build/package.json index 133c74f..0b050ca 100644 --- a/packages/build/package.json +++ b/packages/build/package.json @@ -11,7 +11,7 @@ "clean": "rm -rf dist node_modules yarn.lock", "lint": "eslint --config ./build/eslint.config.js .", "postversion": "npm run build", - "test": "webpack --config ../core/build/build.test.webpack.config.js --mode production && mocha --colors ./dist/test.js --timeout 10000", + "test": "webpack --config ./build/build.test.webpack.config.js --mode production && mocha --colors ./dist/test.js --timeout 10000", "test:watch": "webpack watch --config ./build/build.test.webpack.config.js --mode development & mocha --parallel --watch --watch-files ./dist/test.js --colors ./dist/test.js --timeout 10000" }, "bin": { diff --git a/packages/build/test/server/build.js b/packages/build/test/server/build.js new file mode 100644 index 0000000..d409947 --- /dev/null +++ b/packages/build/test/server/build.js @@ -0,0 +1,56 @@ +import {join} from 'path'; + +import {expect} from 'chai'; + +const { + FLECKS_CORE_ROOT = process.cwd(), +} = process.env; + +const Build = __non_webpack_require__(join(FLECKS_CORE_ROOT, 'build', 'build')); +const loadConfig = __non_webpack_require__(join(FLECKS_CORE_ROOT, 'build', 'load-config')); + +const buildRoot = join(FLECKS_CORE_ROOT, 'test', 'server', 'build', 'root'); + +it('defaults config', async () => { + expect(await loadConfig(FLECKS_CORE_ROOT)) + .to.deep.equal([ + 'barebones', + { + [`@flecks/build:${FLECKS_CORE_ROOT}`]: {}, + '@flecks/core': {}, + '@flecks/fleck': {}, + }, + ]); +}); + +it('loads config', async () => { + expect(await loadConfig(buildRoot)) + .to.deep.equal(['YML', {one: {}, 'two:./two': {}}]); +}); + +it('configures from environment', async () => { + const {env} = process; + env.FLECKS_ENV__one__foo = '{"boo": 2}'; + env.FLECKS_ENV__two__bar = 'yo'; + expect( + Build.environmentConfiguration({ + one: {foo: {boo: 1}}, + two: {bar: 'hi'}, + }), + ) + .to.deep.equal({ + one: {foo: {boo: 2}}, + two: {bar: 'yo'}, + }); +}); + +it('dealiases config', async () => { + expect(await Build.buildRuntime({ + originalConfig: {'two:./two': {foo: 2}}, + platforms: [], + root: buildRoot, + })) + .to.nested.include({ + 'runtime.config.two.foo': 2, + }); +}); diff --git a/packages/build/test/server/build/root/build/flecks.yml b/packages/build/test/server/build/root/build/flecks.yml new file mode 100644 index 0000000..457610c --- /dev/null +++ b/packages/build/test/server/build/root/build/flecks.yml @@ -0,0 +1,2 @@ +one: {} +'two:./two': {} \ No newline at end of file diff --git a/packages/build/test/server/build/root/two/index.js b/packages/build/test/server/build/root/two/index.js new file mode 100644 index 0000000..e69de29 diff --git a/packages/build/test/server/build/root/two/package.json b/packages/build/test/server/build/root/two/package.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/packages/build/test/server/build/root/two/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/packages/build/test/server/yml/build/flecks.yml b/packages/build/test/server/yml/build/flecks.yml new file mode 100644 index 0000000..2d1adf9 --- /dev/null +++ b/packages/build/test/server/yml/build/flecks.yml @@ -0,0 +1,4 @@ +a: {} +bar: {} +foo: {} +two: {}