Skip to content

Commit

Permalink
Merge pull request #8 from CloudCannon/fix/hugo-environments
Browse files Browse the repository at this point in the history
Fix/hugo environments
  • Loading branch information
rphillips-cc authored Mar 19, 2024
2 parents 1a33ec8 + 9f582e8 commit 986dca2
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
test/integration/**/public/
test/integration/**/resources/
test/integration/**/.hugo_build.lock
.hugo_build_lock
9 changes: 6 additions & 3 deletions src/generators/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import { getData } from './data.js';
import { getConfig } from '../config.js';
import { getCollectionsAndConfig } from './collections.js';

async function getHugoUrls() {
async function getHugoUrls(hugoConfig) {
log('⏳ Listing files from Hugo...');

const { source } = pathHelper.getPaths();
const cmdArgs = ['list', 'all', ...(source ? ['--source', source] : [])];
const { environment } = hugoConfig;
const cmdArgs = ['list', 'all', '--environment', environment || 'production'];
cmdArgs.push(...(source ? ['--source', source] : []));

const raw = await runProcess('hugo', cmdArgs);
const startIndex = raw.search(/^path,/m); // hugo logs warnings before this point
if (startIndex < 0) {
Expand All @@ -30,7 +33,7 @@ async function getHugoUrls() {

export async function getInfo(hugoConfig, options) {
const config = await getConfig(hugoConfig);
const hugoUrls = await getHugoUrls();
const hugoUrls = await getHugoUrls(hugoConfig);

pathHelper.getSupportedLanguages(hugoConfig);

Expand Down
6 changes: 5 additions & 1 deletion src/helpers/hugo-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function configSort(fileArray) {

export async function getConfigPaths(flags = {}) {
const sourceDir = flags.source || '';
const environment = flags.environment || 'production'; // or just use root
const environment = flags.environment || process.env.HUGO_ENVIRONMENT || 'production';

const configDir = flags.configDir || 'config';
const configDirEnvironment = join(sourceDir, configDir, environment);
Expand Down Expand Up @@ -122,6 +122,10 @@ export async function getHugoConfig(flags = {}) {
configObject.source = flags.source;
}

if (flags.environment) {
configObject.environment = flags.environment;
}

if (flags.destination) {
configObject.destination = flags.destination;
}
Expand Down
8 changes: 6 additions & 2 deletions test/integration/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const CHECKED_KEYS = [ // Skips time and generator
'source_editor'
];

async function run(fixture, npxArgs = []) {
const expectedPath = join('test/integration', `${fixture}.json`);
async function run(fixture, npxArgs = [], expectedOutputFile) {
const expectedPath = join('test/integration', expectedOutputFile || `${fixture}.json`);
const expectedRaw = await readFile(expectedPath);
const expected = JSON.parse(expectedRaw);

Expand Down Expand Up @@ -66,6 +66,10 @@ describe('integration should generate info.json', function () {
await run('yaml-config');
});

it('with YAML config file and specified environment', async function () {
await run('yaml-config', ['--environment', 'staging'], 'yaml-config-staging.json');
});

it('with collections_config_override', async function () {
await run('collections-config-override');
});
Expand Down
Loading

0 comments on commit 986dca2

Please sign in to comment.