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

fix(#2987): relative config handling with --config flag #3001

Merged
merged 5 commits into from
Apr 6, 2022
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
5 changes: 5 additions & 0 deletions .changeset/dirty-trains-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix relative config handling with the `--config` flag
15 changes: 12 additions & 3 deletions packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path from 'path';
import { pathToFileURL, fileURLToPath } from 'url';
import { mergeConfig as mergeViteConfig } from 'vite';
import { z } from 'zod';
import load from '@proload/core';
import load, { ProloadError } from '@proload/core';
import loadTypeScript from '@proload/plugin-tsm';
import postcssrc from 'postcss-load-config';
import { arraify, isObject } from './util.js';
Expand Down Expand Up @@ -379,11 +379,20 @@ export async function loadConfig(configOptions: LoadConfigOptions): Promise<Astr

if (flags?.config) {
userConfigPath = /^\.*\//.test(flags.config) ? flags.config : `./${flags.config}`;
userConfigPath = fileURLToPath(new URL(userConfigPath, pathToFileURL(root)));
userConfigPath = fileURLToPath(new URL(userConfigPath, appendForwardSlash(pathToFileURL(root).toString())));
}

// Automatically load config file using Proload
// If `userConfigPath` is `undefined`, Proload will search for `astro.config.[cm]?[jt]s`
const config = await load('astro', { mustExist: false, cwd: root, filePath: userConfigPath });
let config;
try {
config = await load('astro', { mustExist: !!userConfigPath, cwd: root, filePath: userConfigPath });
} catch (err) {
if (err instanceof ProloadError && flags.config) {
throw new Error(`Unable to resolve --config "${flags.config}"! Does the file exist?`);
}
throw err;
}
if (config) {
userConfig = config.value;
}
Expand Down
54 changes: 54 additions & 0 deletions packages/astro/test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,60 @@ describe('config', () => {
});
});

describe('relative path', () => {
it('can be passed via relative --config', async () => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
const configFileURL = 'my-config.mjs';
const { local } = await cliServerLogSetup([
'--root',
fileURLToPath(projectRootURL),
'--config',
configFileURL,
]);

const localURL = new URL(local);
expect(localURL.port).to.equal('8080');
});
})

describe('relative path with leading ./', () => {
it('can be passed via relative --config', async () => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
const configFileURL = './my-config.mjs';
const { local } = await cliServerLogSetup([
'--root',
fileURLToPath(projectRootURL),
'--config',
configFileURL,
]);

const localURL = new URL(local);
expect(localURL.port).to.equal('8080');
});
})

describe('incorrect path', () => {
it('fails and exits when config does not exist', async () => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
const configFileURL = './does-not-exist.mjs';
let exit = 0;
try {
await cliServerLogSetup([
'--root',
fileURLToPath(projectRootURL),
'--config',
configFileURL,
]);
} catch (e) {
if (e.message.includes('Unable to resolve --config')) {
exit = 1;
}
}

expect(exit).to.equal(1, "Throws helpful error message when --config does not exist");
});
})

describe('port', () => {
it('can be specified in astro.config.mjs', async () => {
expect(portFixture.config.server.port).to.deep.equal(5006);
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/test/fixtures/astro-basic/my-config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
server: {
port: 8080,
},
}
22 changes: 16 additions & 6 deletions packages/astro/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,32 @@ export function cli(/** @type {string[]} */ ...args) {

export async function parseCliDevStart(proc) {
let stdout = '';
let stderr = '';

for await (const chunk of proc.stdout) {
stdout += chunk;

if (chunk.includes('Local')) break;
}
if (!stdout) {
for await (const chunk of proc.stderr) {
stderr += chunk;
break;
}
}

proc.kill();
stdout = stripAnsi(stdout);
stderr = stripAnsi(stderr);

if (stderr) {
throw new Error(stderr);
}

const messages = stdout
.split('\n')
.filter((ln) => !!ln.trim())
.map((ln) => ln.replace(/[🚀┃]/g, '').replace(/\s+/g, ' ').trim());

return { messages };
}

Expand All @@ -172,11 +185,8 @@ export async function cliServerLogSetup(flags = [], cmd = 'dev') {

const { messages } = await parseCliDevStart(proc);

const localRaw = (messages[1] ?? '').includes('Local') ? messages[1] : undefined;
const networkRaw = (messages[2] ?? '').includes('Network') ? messages[2] : undefined;

const local = localRaw?.replace(/Local\s*/g, '');
const network = networkRaw?.replace(/Network\s*/g, '');
const local = messages.find(msg => msg.includes('Local'))?.replace(/Local\s*/g, '');
const network = messages.find(msg => msg.includes('Network'))?.replace(/Network\s*/g, '');

return { local, network };
}
Expand Down