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

Feat/version pinning #18

Merged
merged 3 commits into from
May 20, 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.5",
"version": "1.1.6",
"description": "A script that builds the script that builds your site",
"main": "src/index.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Static = require('./lib/static');

const Parser = require('./helpers/parser');
const BuildOptions = require('./options/ssg-options');
const VersionOptions = require('./options/version-options');

module.exports = {
Astro: ReaderNpm,
Expand All @@ -30,5 +31,6 @@ module.exports = {
SvelteKit: ReaderNpm,

Parser,
BuildOptions
BuildOptions,
VersionOptions
};
51 changes: 49 additions & 2 deletions src/lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const versionOptions = require('../options/version-options');

module.exports = class Compiler {
static hookCommands(path) {
return [
Expand All @@ -8,8 +10,53 @@ module.exports = class Compiler {
];
}

static getPreinstallCommands() {
return Compiler.hookCommands('.cloudcannon/preinstall');
static getVersionCommands(buildConfig) {
return Object.entries(versionOptions).flatMap(([name, options]) => {
if (buildConfig[name] && buildConfig[name] !== options.default) {
const command = options.getVersionCommand(buildConfig[name]);
if (!command) {
return [];
}

return [
`echo "$ ${command}"`,
command
];
}

return [];
});
}

static getCheckCommands() {
const result = Object.entries(versionOptions).flatMap(([name, options]) => {
const shortName = name.slice(0, -7);
return [
`DETECTED_${shortName}_VERSION=${options.checkCommand}`,
`echo "[🏷${shortName}:\${DETECTED_${shortName}_VERSION}]"`
];
});

result.push(
"DETECTED_NPM_VERSION=$((npm -v 2> /dev/null || echo 'unknown') | sed \"s/[][]//g\")",
"DETECTED_YARN_VERSION=$(yarn -v 2> /dev/null || echo 'unknown')",
'DETECTED_BUNDLE_VERSION=$((bundle -v 2> /dev/null || echo \'unknown\') | sed "s/[][]//g" | sed "s/^Bundler version //g")',

/* eslint-disable no-template-curly-in-string */
'echo "[🏷npm:${DETECTED_NPM_VERSION}]"',
'echo "[🏷yarn:${DETECTED_YARN_VERSION}]"',
'echo "[🏷bundler:${DETECTED_BUNDLE_VERSION}]"'
/* eslint-enable no-template-curly-in-string */
);

return result;
}

static getPreinstallCommands(buildConfig = {}) {
return [
...Compiler.getVersionCommands(buildConfig),
...Compiler.hookCommands('.cloudcannon/preinstall')
];
}

static getPrebuildCommands() {
Expand Down
16 changes: 2 additions & 14 deletions src/lib/eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ const Compiler = require('./compiler');
const { parseOptions } = require('../helpers/parser');
const { addEchoCommand } = require('../helpers/commands');

function getCheckCommands() {
return [
'DETECTED_NPM_VERSION=$(npm -v | sed \'s/[][]//g\')',
'DETECTED_NODE_VERSION=$(node -v | sed \'s/[][]//g\' | sed \'s/^v//\')',

// eslint-disable-next-line no-template-curly-in-string
'echo "[🏷npm:${DETECTED_NPM_VERSION}]"',
// eslint-disable-next-line no-template-curly-in-string
'echo "[🏷node:${DETECTED_NODE_VERSION}]"'
];
}

function getVersioningCommands() {
return [
'export CC_ELEVENTY_VERSION=`npm list @11ty/eleventy | grep @11ty/eleventy | awk -F "@" \'{print $NF}\'`',
Expand Down Expand Up @@ -111,10 +99,10 @@ module.exports = class Eleventy {
const outputPath = buildConfig.output.replace(/^\//, '');

return [
...Compiler.getPreinstallCommands(),
...Compiler.getPreinstallCommands(buildConfig),
...getInstallCommands(buildConfig),
...Compiler.getPrebuildCommands(),
...getCheckCommands(),
...Compiler.getCheckCommands(),
...getBuildCommands(buildConfig).reduce(addEchoCommand, []),
...Compiler.getPostbuildCommands(),
...Compiler.getOutputCommands(outputPath, buildConfig.preserveOutput),
Expand Down
26 changes: 12 additions & 14 deletions src/lib/hugo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ const Compiler = require('./compiler');
const Parser = require('../helpers/parser');
const { addEchoCommand } = require('../helpers/commands');

function getCheckCommands() {
return [
"DETECTED_HUGO_VERSION=$(hugo version | sed 's/[][]//g' | sed 's/^hugo v//' | cut -d ' ' -f 1)",

// eslint-disable-next-line no-template-curly-in-string
'echo "[🏷hugo:${DETECTED_HUGO_VERSION}]"'
];
}

function getBuildCommands(buildConfig) {
const tag = buildConfig.use_beta_plugin ? '@next' : '';
let pluginTag;
if (buildConfig.manage_plugin_manually) {
pluginTag = '';
} else if (buildConfig.use_beta_plugin) {
pluginTag = '@next';
} else {
pluginTag = '@latest';
}
const buildOptions = Parser.parseOptions('hugo', buildConfig);

return [
`echo '$ hugo ${buildOptions}'`,
`hugo ${buildOptions}`,
'__CURRENT_NVM_VERSION=$(nvm current)',
'nvm use default > /dev/null',
`echo '$ npx cloudcannon-hugo${tag} ${buildOptions}'`,
`npx cloudcannon-hugo${tag} ${buildOptions}`,
`echo '$ npx cloudcannon-hugo${pluginTag} ${buildOptions}'`,
`npx cloudcannon-hugo${pluginTag} ${buildOptions}`,
'nvm use "$__CURRENT_NVM_VERSION" > /dev/null',
'unset __CURRENT_NVM_VERSION'
];
Expand All @@ -47,11 +45,11 @@ module.exports = class Hugo {
).replace(/^\//, '');

return [
...Compiler.getPreinstallCommands(),
...Compiler.getPreinstallCommands(buildConfig),
...getInstallCommands(buildConfig).reduce(addEchoCommand, []),
...Compiler.getLegacyPrebuildCommands(),
...Compiler.getPrebuildCommands(),
...getCheckCommands(),
...Compiler.getCheckCommands(),
...getBuildCommands(buildConfig),
...Compiler.getPostbuildCommands(),
...Compiler.getOutputCommands(outputPath, buildConfig.preserveOutput),
Expand Down
16 changes: 2 additions & 14 deletions src/lib/jekyll.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ module.exports = class Jekyll {
`echo "$ export JEKYLL_ENV=\\"${environment}\\""`,
`export JEKYLL_ENV="${environment}"`,

'if [ -f ".ruby-version" ]; then',
' echo "$ ruby -v (Using .ruby-version)"',
' ruby -v || (echo "\\nPlease use one of the following versions:\\n" && rbenv versions 2> /dev/null && false)',
'fi',

'DETECTED_BUNDLE_VERSION=$(bundle -v | sed \'s/[][]//g\' | sed \'s/^Bundler version //g\')',
'DETECTED_RUBY_VERSION=$(ruby -v | sed \'s/[][]//g\' | sed \'s/^ruby //g\' | cut -d \' \' -f 1)',

// eslint-disable-next-line no-template-curly-in-string
'echo "[🏷bundler:${DETECTED_BUNDLE_VERSION}]"',
// eslint-disable-next-line no-template-curly-in-string
'echo "[🏷ruby:${DETECTED_RUBY_VERSION}]"',

`if [ -d "${options.bundleFolderPath}" ]; then`,
` echo "$ rm -rf ${options.bundleFolderPath}"`,
` rm -rf ${options.bundleFolderPath}`,
Expand All @@ -63,7 +50,7 @@ module.exports = class Jekyll {
' USE_BUNDLE=true',
'fi',

...Compiler.getPreinstallCommands(),
...Compiler.getPreinstallCommands(buildConfig),

'if [ "$USE_BUNDLE" = true ]; then',
' echo "$ bundle version"',
Expand All @@ -84,6 +71,7 @@ module.exports = class Jekyll {
'else',
...Compiler.getLegacyPrebuildCommands(),
...Compiler.getPrebuildCommands(),
...Compiler.getCheckCommands(),
` echo "$ jekyll build ${buildOptions}";`,
` jekyll build ${buildOptions}`,
'fi',
Expand Down
33 changes: 10 additions & 23 deletions src/lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,21 @@ const { join } = require('path');
const Compiler = require('./compiler');
const { addEchoCommand } = require('../helpers/commands');

function getCheckCommands() {
return [
"DETECTED_NPM_VERSION=$((npm -v 2> /dev/null || echo 'unknown') | sed \"s/[][]//g\")",
'DETECTED_NODE_VERSION=$((node -v 2> /dev/null || echo \'unknown\') | sed "s/[][]//g" | sed "s/^v//")',
'DETECTED_DENO_VERSION=$((deno -V 2> /dev/null || echo \'unknown\') | sed "s/[][]//g" | sed "s/^deno //")',
"DETECTED_YARN_VERSION=$(yarn -v 2> /dev/null || echo 'unknown')",
'DETECTED_BUNDLE_VERSION=$((bundle -v 2> /dev/null || echo \'unknown\') | sed "s/[][]//g" | sed "s/^Bundler version //g")',
'DETECTED_RUBY_VERSION=$((ruby -v 2> /dev/null || echo \'unknown\') | sed "s/[][]//g" | sed "s/^ruby //g" | cut -d " " -f 1)',

/* eslint-disable no-template-curly-in-string */
'echo "[🏷npm:${DETECTED_NPM_VERSION}]"',
'echo "[🏷node:${DETECTED_NODE_VERSION}]"',
'echo "[🏷deno:${DETECTED_DENO_VERSION}]"',
'echo "[🏷yarn:${DETECTED_YARN_VERSION}]"',
'echo "[🏷ruby-bundler:${DETECTED_BUNDLE_VERSION}]"',
'echo "[🏷ruby:${DETECTED_RUBY_VERSION}]"'
/* eslint-enable no-template-curly-in-string */
];
}

function getInstallCommands(buildConfig) {
return buildConfig.install_command
? [buildConfig.install_command, `cd ${Compiler.getInputPath()}`]
: [];
}

function getBuildCommands(buildConfig, buildOutputPath) {
const pluginTag = buildConfig.use_beta_plugin ? '@next' : '';
let pluginTag;
if (buildConfig.manage_plugin_manually) {
pluginTag = '';
} else if (buildConfig.use_beta_plugin) {
pluginTag = '@next';
} else {
pluginTag = '@latest';
}

return [
...(buildConfig.build_command
Expand All @@ -54,10 +41,10 @@ module.exports = class Reader {
const buildOutputPath = join('.', buildConfig.output_path || 'public');

return [
...Compiler.getPreinstallCommands(),
...Compiler.getPreinstallCommands(buildConfig),
...getInstallCommands(buildConfig).reduce(addEchoCommand, []),
...Compiler.getPrebuildCommands(),
...getCheckCommands(),
...Compiler.getCheckCommands(),
...getBuildCommands(buildConfig, buildOutputPath),
...Compiler.getPostbuildCommands(),
...Compiler.getOutputCommands(
Expand Down
21 changes: 8 additions & 13 deletions src/lib/static.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
const { join } = require('path');
const Compiler = require('./compiler');
const Reader = require('./reader');

module.exports = class Static {
static runScriptCommands(buildConfig) {
const outputPath = join(Compiler.getInputPath(), buildConfig.source || '');

return [
...Compiler.getPreinstallCommands(),
...Compiler.getLegacyPrebuildCommands(),
...Compiler.getPrebuildCommands(),
...Compiler.getOutputCommands(outputPath, buildConfig.preserveOutput),
...Compiler.getPostbuildCommands(),
...Compiler.getExportCommands()
];
static runScriptCommands(buildConfig = {}) {
return Reader.runScriptCommands({
...buildConfig,
output_path: buildConfig.source || '',
install_command: '',
build_command: ''
});
}
};
Loading
Loading