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

Proper error handling for missing app.dx/app.json #200

Merged
merged 1 commit into from
Oct 14, 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
13 changes: 12 additions & 1 deletion src/app/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getExistingFile } from "../utils/file-utils.ts";
import { ESCAPE_SEQUENCES } from "datex-core-legacy/utils/logger.ts";
import { isDenoForUIX, version as UIX_VERSION } from "../utils/version.ts";
import { _updateProjectRootURL } from "datex-core-legacy/utils/global_values.ts"
import { handleError, KnownError } from "datex-core-legacy/utils/error-handling.ts";

export const command_line_options = new CommandLineOptions("UIX", "Fullstack Web Framework with DATEX Integration.\nVisit https://unyt.org/uix for more information", "../RUN.md");

Expand Down Expand Up @@ -74,7 +75,17 @@ export async function updateRootPath(allowFail = false) {
const config_path = getExistingFile(_path, './app.dx', './app.json', './src/app.dx', './src/app.json');

if (!config_path && !allowFail) {
throw "Could not find an app.dx or app.json config file in " + _path.normal_pathname
handleError(
new KnownError(
`The directory does not appear to be a UIX project.\n(No app.dx or app.json file was found in ${_path.normal_pathname})`,
[
"Initialize a new project by running 'uix --init'",
"Change directory to a project that contains an 'app.dx' or 'src/app.dx' file",
"Change directory to a project that contains an 'app.json' or 'src/app.json' file",
"Create an 'app.dx' file with the project fields 'name' and 'description'"
]
)
);
}

rootPath = (config_path ? new Path(config_path).parent_dir : null) as Path.File;
Expand Down
41 changes: 33 additions & 8 deletions src/app/config-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Datex } from "datex-core-legacy/mod.ts";
import type { AppPlugin } from "./app-plugin.ts";
import { logger } from "../utils/global-values.ts";
import { normalizedAppOptions } from "./options.ts";
import { handleError, KnownError } from "datex-core-legacy/utils/error-handling.ts";

const default_importmap = "https://dev.cdn.unyt.org/importmap.json";
const arg_import_map_string = command_line_options.option("import-map", {type:"string", description: "Import map path"});
Expand All @@ -16,10 +17,22 @@ const arg_import_map = (arg_import_map_string?.startsWith("http://")||arg_import
undefined
)

export async function applyPlugins(plugins: AppPlugin[], rootPath:Path, appOptions: normalizedAppOptions) {
export async function applyPlugins(plugins: AppPlugin[], rootPath: Path, appOptions: normalizedAppOptions) {
const config_path = getExistingFile(rootPath, './app.dx', './app.json');

if (!config_path) throw "Could not find an app.dx or app.json config file in " + new Path(rootPath).normal_pathname
if (!config_path) {
handleError(
new KnownError(
`The directory does not appear to be a UIX project.\n(No app.dx or app.json file was found in ${new Path(rootPath).normal_pathname}.)`,
[
"Initialize a new project by running 'uix --init'",
"Change directory to a project that contains an 'app.dx' file",
"Change directory to a project that contains an 'app.json' file",
"Create an 'app.dx' file with the project fields 'name' and 'description'"
]
)
);
}

// handle plugins (only if in dev environment, not on host, TODO: better solution)
if (plugins?.length && !Deno.env.get("UIX_HOST_ENDPOINT")) {
Expand All @@ -36,8 +49,8 @@ export async function applyPlugins(plugins: AppPlugin[], rootPath:Path, appOptio
/**
* get combined config of app.dx and deno.json and command line args
*/
export async function getAppOptions(root_path:URL) {
const config_path = getExistingFile(root_path, './app.dx', './app.json');
export async function getAppOptions(rootPath:URL) {
const config_path = getExistingFile(rootPath, './app.dx', './app.json');
let config:Record<string,unknown> = {}

if (config_path) {
Expand All @@ -48,13 +61,25 @@ export async function getAppOptions(root_path:URL) {
}
config = Object.fromEntries(Datex.DatexObject.entries(<Record<string, unknown>>raw_config));
}
else throw "Could not find an app.dx or app.json config file in " + new Path(root_path).normal_pathname;
else {
handleError(
new KnownError(
`The directory does not appear to be a UIX project.\n(No app.dx or app.json file was found in ${new Path(rootPath).normal_pathname})`,
[
"Initialize a new project by running 'uix --init'",
"Change directory to a project that contains an 'app.dx' file",
"Change directory to a project that contains an 'app.json' file",
"Create an 'app.dx' file with the project fields 'name' and 'description'"
]
)
);
}

// overwrite --import-map path
if (arg_import_map) config.import_map_path = arg_import_map;

// set import map from deno.json if exists
const deno_path = getExistingFile(root_path, './deno.json', './deno.jsonc');
const deno_path = getExistingFile(rootPath, './deno.json', './deno.jsonc');

if (deno_path) {

Expand All @@ -80,11 +105,11 @@ export async function getAppOptions(root_path:URL) {
}
// _publicImportMap path
else if (deno._publicImportMap) {
config.import_map_path = new URL(deno._publicImportMap, root_path);
config.import_map_path = new URL(deno._publicImportMap, rootPath);
}
// importMap path
else if (deno.importMap) {
config.import_map_path = new URL(deno.importMap, root_path);
config.import_map_path = new URL(deno.importMap, rootPath);
}
}
} catch {}
Expand Down