diff --git a/package.json b/package.json index 898ee7f..31efe79 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cloudcannon/command-builder", - "version": "1.1.3", + "version": "1.1.4", "description": "A script that builds the script that builds your site", "main": "src/index.js", "scripts": { diff --git a/src/helpers/commands.js b/src/helpers/commands.js index 27bf27b..296397c 100644 --- a/src/helpers/commands.js +++ b/src/helpers/commands.js @@ -1,3 +1,8 @@ +function addEchoCommand(memo, command) { + memo.push(`echo '$ ${command}'`, command); + return memo; +} + module.exports = { - addEchoCommand: (memo, command) => [...memo, `echo '$ ${command}'`, command] + addEchoCommand }; diff --git a/src/lib/eleventy.js b/src/lib/eleventy.js index 2014117..4cd9f69 100644 --- a/src/lib/eleventy.js +++ b/src/lib/eleventy.js @@ -14,11 +14,21 @@ function getCheckCommands() { ]; } +function getVersioningCommands() { + return [ + 'export CC_ELEVENTY_VERSION=`npm list @11ty/eleventy | grep @11ty/eleventy | awk -F "@" \'{print $NF}\'`', + 'if [[ -z "$CC_ELEVENTY_VERSION" ]]; then export CC_ELEVENTY_VERSION=unknown; fi', + + // eslint-disable-next-line no-template-curly-in-string + 'echo "[🏷@11ty/eleventy:${CC_ELEVENTY_VERSION}]"' + ]; +} + function getInstallCommands(buildConfig) { if (buildConfig.manage_plugin_manually) { return [ 'npm install' - ]; + ].reduce(addEchoCommand, []); } const pluginTag = buildConfig.use_beta_plugin ? 'next' : 'latest'; @@ -27,46 +37,68 @@ function getInstallCommands(buildConfig) { const installDependency = `npm pkg set dependencies.eleventy-plugin-cloudcannon=${pluginTag}`; const installDependencyFallback = `npm install eleventy-plugin-cloudcannon@${pluginTag}`; - const commands = [ + const installCommands = [ // Add (fallback to install) the integration plugin as dependency `${installDependency} || ${installDependencyFallback}`, // Install dependencies - 'npm install', + 'npm install' + ]; + const pluginCommands = [ ...( buildConfig.config ? [ - `if [ -f "${buildConfig.config}" ]; then export ELEVENTY_CONFIG="${buildConfig.config}"; fi` + `if [ -f "${buildConfig.config}" ]; then CONFIG="${buildConfig.config}"; fi` ] : [ // Find default config path (in reverse order to match priority) - 'if [ -f eleventy.config.cjs ]; then export ELEVENTY_CONFIG=eleventy.config.cjs; fi', - 'if [ -f eleventy.config.js ]; then export ELEVENTY_CONFIG=eleventy.config.js; fi', - 'if [ -f .eleventy.js ]; then export ELEVENTY_CONFIG=.eleventy.js; fi' + 'if [ -f eleventy.config.cjs ]; then CONFIG=eleventy.config.cjs; fi', + 'if [ -f eleventy.config.mjs ]; then CONFIG=eleventy.config.mjs; fi', + 'if [ -f eleventy.config.js ]; then CONFIG=eleventy.config.js; fi', + 'if [ -f .eleventy.js ]; then CONFIG=.eleventy.js; fi' ] ), - 'export ELEVENTY_CONFIG_DIR=`dirname $ELEVENTY_CONFIG`', - 'echo $ELEVENTY_CONFIG', - 'echo $ELEVENTY_CONFIG_DIR', + 'echo $CONFIG', + + 'CONFIG_DIR=`dirname $CONFIG`', + 'echo $CONFIG_DIR', + + 'CONFIG_BASE=`basename $CONFIG`', + 'echo $CONFIG_BASE', + + 'CONFIG_INJECTED="$CONFIG_DIR/inject-cloudcannon.config.cjs"', + 'echo $CONFIG_INJECTED', + + 'export CC_ELEVENTY_CONFIG="$CONFIG_DIR/default-$CONFIG_BASE"', + 'echo $CC_ELEVENTY_CONFIG', // Move the site config file to injected config require location - 'if [ -f $ELEVENTY_CONFIG ]; then mv $ELEVENTY_CONFIG "$ELEVENTY_CONFIG_DIR/default-eleventy.config.js"; fi', + 'if [ -f $CONFIG ]; then mv $CONFIG $CC_ELEVENTY_CONFIG; fi', // Move injected config to the original site config location - 'cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js $ELEVENTY_CONFIG', + 'cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.cjs $CONFIG_INJECTED', // Set environment variable for plugin to read ...(buildConfig.input ? [`export CC_ELEVENTY_INPUT="${buildConfig.input || ''}"`] : []) ]; - return commands; + return [ + ...installCommands.reduce(addEchoCommand, []), + ...getVersioningCommands(), + ...pluginCommands.reduce(addEchoCommand, []) + ]; } function getBuildCommands(buildConfig) { + const alteredConfig = { + ...buildConfig, + config: undefined + }; + return [ - `npx @11ty/eleventy ${parseOptions('eleventy', buildConfig)}` + `npx @11ty/eleventy --config=$CONFIG_INJECTED ${parseOptions('eleventy', alteredConfig)}` ]; } @@ -77,7 +109,7 @@ module.exports = class Eleventy { return [ ...Compiler.getPreinstallCommands(), - ...getInstallCommands(buildConfig).reduce(addEchoCommand, []), + ...getInstallCommands(buildConfig), ...Compiler.getPrebuildCommands(), ...getCheckCommands(), ...getBuildCommands(buildConfig).reduce(addEchoCommand, []), diff --git a/tests/eleventy.test.js b/tests/eleventy.test.js index ad6904c..94085a3 100644 --- a/tests/eleventy.test.js +++ b/tests/eleventy.test.js @@ -13,22 +13,40 @@ test('outputs with empty config', () => { 'echo \'$ npm install\'', 'npm install', - "echo '$ if [ -f eleventy.config.cjs ]; then export ELEVENTY_CONFIG=eleventy.config.cjs; fi'", - 'if [ -f eleventy.config.cjs ]; then export ELEVENTY_CONFIG=eleventy.config.cjs; fi', - "echo '$ if [ -f eleventy.config.js ]; then export ELEVENTY_CONFIG=eleventy.config.js; fi'", - 'if [ -f eleventy.config.js ]; then export ELEVENTY_CONFIG=eleventy.config.js; fi', - "echo '$ if [ -f .eleventy.js ]; then export ELEVENTY_CONFIG=.eleventy.js; fi'", - 'if [ -f .eleventy.js ]; then export ELEVENTY_CONFIG=.eleventy.js; fi', - "echo '$ export ELEVENTY_CONFIG_DIR=`dirname $ELEVENTY_CONFIG`'", - 'export ELEVENTY_CONFIG_DIR=`dirname $ELEVENTY_CONFIG`', - "echo '$ echo $ELEVENTY_CONFIG'", - 'echo $ELEVENTY_CONFIG', - "echo '$ echo $ELEVENTY_CONFIG_DIR'", - 'echo $ELEVENTY_CONFIG_DIR', - "echo '$ if [ -f $ELEVENTY_CONFIG ]; then mv $ELEVENTY_CONFIG \"$ELEVENTY_CONFIG_DIR/default-eleventy.config.js\"; fi'", - 'if [ -f $ELEVENTY_CONFIG ]; then mv $ELEVENTY_CONFIG "$ELEVENTY_CONFIG_DIR/default-eleventy.config.js"; fi', - "echo '$ cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js $ELEVENTY_CONFIG'", - 'cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js $ELEVENTY_CONFIG', + "export CC_ELEVENTY_VERSION=`npm list @11ty/eleventy | grep @11ty/eleventy | awk -F \"@\" '{print $NF}'`", + 'if [[ -z "$CC_ELEVENTY_VERSION" ]]; then export CC_ELEVENTY_VERSION=unknown; fi', + // eslint-disable-next-line no-template-curly-in-string + 'echo "[🏷@11ty/eleventy:${CC_ELEVENTY_VERSION}]"', + "echo '$ if [ -f eleventy.config.cjs ]; then CONFIG=eleventy.config.cjs; fi'", + 'if [ -f eleventy.config.cjs ]; then CONFIG=eleventy.config.cjs; fi', + "echo '$ if [ -f eleventy.config.mjs ]; then CONFIG=eleventy.config.mjs; fi'", + 'if [ -f eleventy.config.mjs ]; then CONFIG=eleventy.config.mjs; fi', + "echo '$ if [ -f eleventy.config.js ]; then CONFIG=eleventy.config.js; fi'", + 'if [ -f eleventy.config.js ]; then CONFIG=eleventy.config.js; fi', + "echo '$ if [ -f .eleventy.js ]; then CONFIG=.eleventy.js; fi'", + 'if [ -f .eleventy.js ]; then CONFIG=.eleventy.js; fi', + "echo '$ echo $CONFIG'", + 'echo $CONFIG', + "echo '$ CONFIG_DIR=`dirname $CONFIG`'", + 'CONFIG_DIR=`dirname $CONFIG`', + "echo '$ echo $CONFIG_DIR'", + 'echo $CONFIG_DIR', + "echo '$ CONFIG_BASE=`basename $CONFIG`'", + 'CONFIG_BASE=`basename $CONFIG`', + "echo '$ echo $CONFIG_BASE'", + 'echo $CONFIG_BASE', + "echo '$ CONFIG_INJECTED=\"$CONFIG_DIR/inject-cloudcannon.config.cjs\"'", + 'CONFIG_INJECTED="$CONFIG_DIR/inject-cloudcannon.config.cjs"', + "echo '$ echo $CONFIG_INJECTED'", + 'echo $CONFIG_INJECTED', + "echo '$ export CC_ELEVENTY_CONFIG=\"$CONFIG_DIR/default-$CONFIG_BASE\"'", + 'export CC_ELEVENTY_CONFIG="$CONFIG_DIR/default-$CONFIG_BASE"', + "echo '$ echo $CC_ELEVENTY_CONFIG'", + 'echo $CC_ELEVENTY_CONFIG', + "echo '$ if [ -f $CONFIG ]; then mv $CONFIG $CC_ELEVENTY_CONFIG; fi'", + 'if [ -f $CONFIG ]; then mv $CONFIG $CC_ELEVENTY_CONFIG; fi', + "echo '$ cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.cjs $CONFIG_INJECTED'", + 'cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.cjs $CONFIG_INJECTED', 'echo "$ source .cloudcannon/prebuild"', 'if [ -f ".cloudcannon/prebuild" ]; then source .cloudcannon/prebuild; else echo "Not found."; fi', @@ -42,8 +60,8 @@ test('outputs with empty config', () => { // eslint-disable-next-line no-template-curly-in-string 'echo "[🏷node:${DETECTED_NODE_VERSION}]"', - 'echo \'$ npx @11ty/eleventy --output _site\'', - 'npx @11ty/eleventy --output _site', + 'echo \'$ npx @11ty/eleventy --config=$CONFIG_INJECTED --output _site\'', + 'npx @11ty/eleventy --config=$CONFIG_INJECTED --output _site', 'echo "$ source .cloudcannon/postbuild"', 'if [ -f ".cloudcannon/postbuild" ]; then source .cloudcannon/postbuild; else echo "Not found."; fi', @@ -78,22 +96,40 @@ test('outputs with @next config', () => { 'echo \'$ npm install\'', 'npm install', - "echo '$ if [ -f eleventy.config.cjs ]; then export ELEVENTY_CONFIG=eleventy.config.cjs; fi'", - 'if [ -f eleventy.config.cjs ]; then export ELEVENTY_CONFIG=eleventy.config.cjs; fi', - "echo '$ if [ -f eleventy.config.js ]; then export ELEVENTY_CONFIG=eleventy.config.js; fi'", - 'if [ -f eleventy.config.js ]; then export ELEVENTY_CONFIG=eleventy.config.js; fi', - "echo '$ if [ -f .eleventy.js ]; then export ELEVENTY_CONFIG=.eleventy.js; fi'", - 'if [ -f .eleventy.js ]; then export ELEVENTY_CONFIG=.eleventy.js; fi', - "echo '$ export ELEVENTY_CONFIG_DIR=`dirname $ELEVENTY_CONFIG`'", - 'export ELEVENTY_CONFIG_DIR=`dirname $ELEVENTY_CONFIG`', - "echo '$ echo $ELEVENTY_CONFIG'", - 'echo $ELEVENTY_CONFIG', - "echo '$ echo $ELEVENTY_CONFIG_DIR'", - 'echo $ELEVENTY_CONFIG_DIR', - "echo '$ if [ -f $ELEVENTY_CONFIG ]; then mv $ELEVENTY_CONFIG \"$ELEVENTY_CONFIG_DIR/default-eleventy.config.js\"; fi'", - 'if [ -f $ELEVENTY_CONFIG ]; then mv $ELEVENTY_CONFIG "$ELEVENTY_CONFIG_DIR/default-eleventy.config.js"; fi', - "echo '$ cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js $ELEVENTY_CONFIG'", - 'cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js $ELEVENTY_CONFIG', + "export CC_ELEVENTY_VERSION=`npm list @11ty/eleventy | grep @11ty/eleventy | awk -F \"@\" '{print $NF}'`", + 'if [[ -z "$CC_ELEVENTY_VERSION" ]]; then export CC_ELEVENTY_VERSION=unknown; fi', + // eslint-disable-next-line no-template-curly-in-string + 'echo "[🏷@11ty/eleventy:${CC_ELEVENTY_VERSION}]"', + "echo '$ if [ -f eleventy.config.cjs ]; then CONFIG=eleventy.config.cjs; fi'", + 'if [ -f eleventy.config.cjs ]; then CONFIG=eleventy.config.cjs; fi', + "echo '$ if [ -f eleventy.config.mjs ]; then CONFIG=eleventy.config.mjs; fi'", + 'if [ -f eleventy.config.mjs ]; then CONFIG=eleventy.config.mjs; fi', + "echo '$ if [ -f eleventy.config.js ]; then CONFIG=eleventy.config.js; fi'", + 'if [ -f eleventy.config.js ]; then CONFIG=eleventy.config.js; fi', + "echo '$ if [ -f .eleventy.js ]; then CONFIG=.eleventy.js; fi'", + 'if [ -f .eleventy.js ]; then CONFIG=.eleventy.js; fi', + "echo '$ echo $CONFIG'", + 'echo $CONFIG', + "echo '$ CONFIG_DIR=`dirname $CONFIG`'", + 'CONFIG_DIR=`dirname $CONFIG`', + "echo '$ echo $CONFIG_DIR'", + 'echo $CONFIG_DIR', + "echo '$ CONFIG_BASE=`basename $CONFIG`'", + 'CONFIG_BASE=`basename $CONFIG`', + "echo '$ echo $CONFIG_BASE'", + 'echo $CONFIG_BASE', + "echo '$ CONFIG_INJECTED=\"$CONFIG_DIR/inject-cloudcannon.config.cjs\"'", + 'CONFIG_INJECTED="$CONFIG_DIR/inject-cloudcannon.config.cjs"', + "echo '$ echo $CONFIG_INJECTED'", + 'echo $CONFIG_INJECTED', + "echo '$ export CC_ELEVENTY_CONFIG=\"$CONFIG_DIR/default-$CONFIG_BASE\"'", + 'export CC_ELEVENTY_CONFIG="$CONFIG_DIR/default-$CONFIG_BASE"', + "echo '$ echo $CC_ELEVENTY_CONFIG'", + 'echo $CC_ELEVENTY_CONFIG', + "echo '$ if [ -f $CONFIG ]; then mv $CONFIG $CC_ELEVENTY_CONFIG; fi'", + 'if [ -f $CONFIG ]; then mv $CONFIG $CC_ELEVENTY_CONFIG; fi', + "echo '$ cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.cjs $CONFIG_INJECTED'", + 'cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.cjs $CONFIG_INJECTED', 'echo \'$ export CC_ELEVENTY_INPUT="src"\'', 'export CC_ELEVENTY_INPUT="src"', @@ -110,8 +146,8 @@ test('outputs with @next config', () => { // eslint-disable-next-line no-template-curly-in-string 'echo "[🏷node:${DETECTED_NODE_VERSION}]"', - 'echo \'$ npx @11ty/eleventy --input src --incremental --ignore-initial --output _site\'', - 'npx @11ty/eleventy --input src --incremental --ignore-initial --output _site', + 'echo \'$ npx @11ty/eleventy --config=$CONFIG_INJECTED --input src --incremental --ignore-initial --output _site\'', + 'npx @11ty/eleventy --config=$CONFIG_INJECTED --input src --incremental --ignore-initial --output _site', 'echo "$ source .cloudcannon/postbuild"', 'if [ -f ".cloudcannon/postbuild" ]; then source .cloudcannon/postbuild; else echo "Not found."; fi', @@ -153,8 +189,8 @@ test('outputs with mange_plugin_manually', () => { // eslint-disable-next-line no-template-curly-in-string 'echo "[🏷node:${DETECTED_NODE_VERSION}]"', - 'echo \'$ npx @11ty/eleventy --output _site\'', - 'npx @11ty/eleventy --output _site', + "echo '$ npx @11ty/eleventy --config=$CONFIG_INJECTED --output _site'", + 'npx @11ty/eleventy --config=$CONFIG_INJECTED --output _site', 'echo "$ source .cloudcannon/postbuild"', 'if [ -f ".cloudcannon/postbuild" ]; then source .cloudcannon/postbuild; else echo "Not found."; fi',