Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change 11ty plugin injection commands to support upcoming @11ty/eleventy v3 changes #16

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/commands.js
Original file line number Diff line number Diff line change
@@ -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
};
62 changes: 47 additions & 15 deletions src/lib/eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)}`
];
}

Expand All @@ -77,7 +109,7 @@ module.exports = class Eleventy {

return [
...Compiler.getPreinstallCommands(),
...getInstallCommands(buildConfig).reduce(addEchoCommand, []),
...getInstallCommands(buildConfig),
...Compiler.getPrebuildCommands(),
...getCheckCommands(),
...getBuildCommands(buildConfig).reduce(addEchoCommand, []),
Expand Down
112 changes: 74 additions & 38 deletions tests/eleventy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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"',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
Loading