diff --git a/package.json b/package.json index 6c7f6a4..b314430 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cloudcannon/command-builder", - "version": "1.0.50", + "version": "1.1.0", "description": "A script that builds the script that builds your site", "main": "src/index.js", "scripts": { diff --git a/src/lib/eleventy.js b/src/lib/eleventy.js index ff45cde..2014117 100644 --- a/src/lib/eleventy.js +++ b/src/lib/eleventy.js @@ -1,4 +1,3 @@ -const { join, dirname } = require('path'); const Compiler = require('./compiler'); const { parseOptions } = require('../helpers/parser'); const { addEchoCommand } = require('../helpers/commands'); @@ -22,8 +21,6 @@ function getInstallCommands(buildConfig) { ]; } - const configPath = buildConfig.config || '.eleventy.js'; - const configMovedPath = join(dirname(configPath), 'default-eleventy.config.js'); const pluginTag = buildConfig.use_beta_plugin ? 'next' : 'latest'; // npm pkg set requires npm version >= 7 @@ -37,11 +34,28 @@ function getInstallCommands(buildConfig) { // Install dependencies 'npm install', + ...( + buildConfig.config + ? [ + `if [ -f "${buildConfig.config}" ]; then export ELEVENTY_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' + ] + ), + + 'export ELEVENTY_CONFIG_DIR=`dirname $ELEVENTY_CONFIG`', + 'echo $ELEVENTY_CONFIG', + 'echo $ELEVENTY_CONFIG_DIR', + // Move the site config file to injected config require location - `if [ -f ${configPath} ]; then mv ${configPath} ${configMovedPath}; fi`, + 'if [ -f $ELEVENTY_CONFIG ]; then mv $ELEVENTY_CONFIG "$ELEVENTY_CONFIG_DIR/default-eleventy.config.js"; fi', // Move injected config to the original site config location - `cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js ${configPath}`, + 'cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js $ELEVENTY_CONFIG', // Set environment variable for plugin to read ...(buildConfig.input ? [`export CC_ELEVENTY_INPUT="${buildConfig.input || ''}"`] : []) diff --git a/tests/eleventy.test.js b/tests/eleventy.test.js index 999ff99..ad6904c 100644 --- a/tests/eleventy.test.js +++ b/tests/eleventy.test.js @@ -13,11 +13,22 @@ test('outputs with empty config', () => { 'echo \'$ npm install\'', 'npm install', - 'echo \'$ if [ -f .eleventy.js ]; then mv .eleventy.js default-eleventy.config.js; fi\'', - 'if [ -f .eleventy.js ]; then mv .eleventy.js default-eleventy.config.js; fi', - - 'echo \'$ cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js .eleventy.js\'', - 'cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js .eleventy.js', + "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', 'echo "$ source .cloudcannon/prebuild"', 'if [ -f ".cloudcannon/prebuild" ]; then source .cloudcannon/prebuild; else echo "Not found."; fi', @@ -67,11 +78,22 @@ test('outputs with @next config', () => { 'echo \'$ npm install\'', 'npm install', - 'echo \'$ if [ -f .eleventy.js ]; then mv .eleventy.js default-eleventy.config.js; fi\'', - 'if [ -f .eleventy.js ]; then mv .eleventy.js default-eleventy.config.js; fi', - - 'echo \'$ cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js .eleventy.js\'', - 'cp node_modules/eleventy-plugin-cloudcannon/src/inject-cloudcannon.config.js .eleventy.js', + "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', 'echo \'$ export CC_ELEVENTY_INPUT="src"\'', 'export CC_ELEVENTY_INPUT="src"',