Skip to content

Commit

Permalink
Merge pull request #7565 from quarto-dev/chore/cleanup-bad-imports
Browse files Browse the repository at this point in the history
Chore/cleanup bad imports
  • Loading branch information
cscheid authored Nov 13, 2023
2 parents 045fd3f + e366733 commit ed58a5b
Show file tree
Hide file tree
Showing 27 changed files with 355 additions and 348 deletions.
28 changes: 5 additions & 23 deletions src/command/create/artifacts/artifact-shared.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
* artifact-shared.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/
* artifact-shared.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/

import { capitalizeTitle } from "../../../core/text.ts";
import { quartoConfig } from "../../../core/quarto.ts";
Expand All @@ -16,24 +15,7 @@ import { basename, dirname, join, relative } from "path/mod.ts";
import { ensureDirSync, walkSync } from "fs/mod.ts";
import { renderEjs } from "../../../core/ejs.ts";
import { safeExistsSync } from "../../../core/path.ts";

export interface CreateDirective {
displayType: string;
name: string;
directory: string;
template: string;
options: Record<string, unknown>;
}

export interface CreateDirectiveData extends Record<string, string> {
name: string;
filesafename: string;
classname: string;
title: string;
author: string;
version: string;
quartoversion: string;
}
import { CreateDirective, CreateDirectiveData } from "../cmd-types.ts";

// File paths that include this string will get fixed up
// and the value from the ejs data will be substituted
Expand Down
23 changes: 11 additions & 12 deletions src/command/create/artifacts/extension.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/*
* extension.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/

import { ArtifactCreator, CreateContext } from "../cmd.ts";
* extension.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/

import {
ArtifactCreator,
CreateContext,
CreateDirective,
ejsData,
renderAndCopyArtifacts,
} from "./artifact-shared.ts";
} from "../cmd-types.ts";

import { ejsData, renderAndCopyArtifacts } from "./artifact-shared.ts";

import { resourcePath } from "../../../core/resources.ts";

Expand Down Expand Up @@ -46,7 +45,7 @@ const kExtensionSubtypes: Record<string, string[]> = {
"format": ["html", "pdf", "docx", "revealjs", "typst"],
};

const kExtensionValues = kExtensionTypes.filter((t) => typeof (t) === "object")
const kExtensionValues = kExtensionTypes.filter((t) => typeof t === "object")
.map((t) => (t as { name: string; value: string }).value);

export const extensionArtifactCreator: ArtifactCreator = {
Expand Down Expand Up @@ -183,7 +182,7 @@ async function createArtifact(
// Find the type using the template
const createType = typeFromTemplate(createDirective.template);
const extType = kExtensionTypes.find((type) => {
if (typeof (type) === "object") {
if (typeof type === "object") {
return type.value === createType;
} else {
return false;
Expand Down
17 changes: 9 additions & 8 deletions src/command/create/artifacts/project.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* project.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/
* project.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/

import { ArtifactCreator, CreateContext } from "../cmd.ts";

import { CreateDirective } from "./artifact-shared.ts";
import {
ArtifactCreator,
CreateContext,
CreateDirective,
} from "../cmd-types.ts";

import { capitalizeTitle } from "../../../core/text.ts";
import { kMarkdownEngine } from "../../../execute/types.ts";
Expand Down
68 changes: 68 additions & 0 deletions src/command/create/cmd-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* cmd-types.ts
*
* Copyright (C) 2020-2023 Posit Software, PBC
*/

export interface CreateContext {
cwd: string;
options: Record<string, unknown>;
}

export interface CreateResult {
// Path to the directory or document
path: string;

// Files to open
openfiles: string[];
}

export interface ArtifactCreator {
// The name that is displayed to users
displayName: string;

// The identifier for this artifact type
type: string;

// artifact creators are passed any leftover args from the create command
// and may use those arguments to populate the options
resolveOptions: (args: string[]) => Record<string, unknown>;

// this will always be called, giving the artifact creator
// a change to finalize / transform options
finalizeOptions: (options: CreateContext) => CreateDirective;

// As long as prompting is allowed, allow the artifact creator prompting to populate
// the options. This will be called until it return undefined, at which point
// the artifact will be created using the options
// deno-lint-ignore no-explicit-any
nextPrompt: (options: CreateContext) => any | undefined; // TODO: this any is a nightmare

// Creates the artifact using the specified options
// Returns the path to the created artifact
createArtifact: (
directive: CreateDirective,
quiet?: boolean,
) => Promise<CreateResult>;

// Set this to false to exclude this artifact type from the create command
enabled?: boolean;
}

export interface CreateDirective {
displayType: string;
name: string;
directory: string;
template: string;
options: Record<string, unknown>;
}

export interface CreateDirectiveData extends Record<string, string> {
name: string;
filesafename: string;
classname: string;
title: string;
author: string;
version: string;
quartoversion: string;
}
64 changes: 6 additions & 58 deletions src/command/create/cmd.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
* cmd.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/
* cmd.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/

import { extensionArtifactCreator } from "./artifacts/extension.ts";
import { projectArtifactCreator } from "./artifacts/project.ts";
Expand All @@ -12,61 +11,11 @@ import { kEditorInfos, scanForEditors } from "./editor.ts";
import { isInteractiveTerminal } from "../../core/platform.ts";
import { runningInCI } from "../../core/ci-info.ts";

import { CreateDirective } from "./artifacts/artifact-shared.ts";

import { Command } from "cliffy/command/mod.ts";
import {
prompt,
Select,
SelectValueOptions,
} from "cliffy/prompt/mod.ts";
import { prompt, Select, SelectValueOptions } from "cliffy/prompt/mod.ts";
import { readLines } from "io/mod.ts";
import { info } from "log/mod.ts";

export interface CreateContext {
cwd: string;
options: Record<string, unknown>;
}

export interface CreateResult {
// Path to the directory or document
path: string;

// Files to open
openfiles: string[];
}

export interface ArtifactCreator {
// The name that is displayed to users
displayName: string;

// The identifier for this artifact type
type: string;

// artifact creators are passed any leftover args from the create command
// and may use those arguments to populate the options
resolveOptions: (args: string[]) => Record<string, unknown>;

// this will always be called, giving the artifact creator
// a change to finalize / transform options
finalizeOptions: (options: CreateContext) => CreateDirective;

// As long as prompting is allowed, allow the artifact creator prompting to populate
// the options. This will be called until it return undefined, at which point
// the artifact will be created using the options
// deno-lint-ignore no-explicit-any
nextPrompt: (options: CreateContext) => any | undefined; // TODO: this any is a nightmare

// Creates the artifact using the specified options
// Returns the path to the created artifact
createArtifact: (
directive: CreateDirective,
quiet?: boolean,
) => Promise<CreateResult>;

// Set this to false to exclude this artifact type from the create command
enabled?: boolean;
}
import { ArtifactCreator, CreateDirective, CreateResult } from "./cmd-types.ts";

// The registered artifact creators
const kArtifactCreators: ArtifactCreator[] = [
Expand Down Expand Up @@ -210,7 +159,6 @@ const resolveArtifact = async (type?: string, prompt?: boolean) => {
};
};


// Wrapper that will provide keyboard selection hint (if necessary)
async function promptSelect(
message: string,
Expand Down
11 changes: 5 additions & 6 deletions src/command/create/editor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/*
* cmd.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/
* cmd.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/

import { CreateResult } from "./cmd.ts";
import { CreateResult } from "./cmd-types.ts";

import { which } from "../../core/path.ts";
import { isRStudioTerminal, isVSCodeTerminal } from "../../core/platform.ts";
Expand Down
18 changes: 18 additions & 0 deletions src/command/render/output-shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* output-shared.ts
*
* Copyright (C) 2023 Posit Software, PBC
*/

import { dirname, isAbsolute, relative } from "path/mod.ts";

export function normalizeOutputPath(input: string, output: string) {
if (isAbsolute(output)) {
return output;
} else {
return relative(
dirname(input),
output,
);
}
}
2 changes: 1 addition & 1 deletion src/command/render/output-tex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { OutputRecipe } from "./types.ts";
import { pdfEngine } from "../../config/pdf.ts";
import { execProcess } from "../../core/process.ts";
import { parseFormatString } from "../../core/pandoc/pandoc-formats.ts";
import { normalizeOutputPath } from "./output.ts";
import { normalizeOutputPath } from "./output-shared.ts";

export interface PdfGenerator {
generate: (
Expand Down
2 changes: 1 addition & 1 deletion src/command/render/output-typst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { writeFileToStdout } from "../../core/console.ts";
import { dirAndStem, expandPath } from "../../core/path.ts";
import { kStdOut, replacePandocOutputArg } from "./flags.ts";
import { OutputRecipe, RenderOptions } from "./types.ts";
import { normalizeOutputPath } from "./output.ts";
import { normalizeOutputPath } from "./output-shared.ts";
import {
typstCompile,
validateRequiredTypstVersion,
Expand Down
11 changes: 0 additions & 11 deletions src/command/render/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,4 @@ export function outputRecipe(
}
}

export function normalizeOutputPath(input: string, output: string) {
if (isAbsolute(output)) {
return output;
} else {
return relative(
dirname(input),
output,
);
}
}

const kOutExt = "out";
2 changes: 1 addition & 1 deletion src/core/handlers/mermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { Element } from "../deno-dom.ts";
import { convertFromYaml } from "../lib/yaml-schema/from-yaml.ts";
import { readYamlFromString } from "../yaml.ts";
import { pandocHtmlBlock, pandocRawStr } from "../pandoc/codegen.ts";
import { LocalizedError } from "../lib/error.ts";
import { LocalizedError } from "../lib/located-error.ts";
import { warning } from "log/mod.ts";
import { FormatDependency } from "../../config/types.ts";
import { mappedDiff } from "../mapped-text.ts";
Expand Down
28 changes: 0 additions & 28 deletions src/core/lib/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Copyright (C) 2020-2022 Posit Software, PBC
*/

import { mappedIndexToLineCol, MappedString } from "./mapped-text.ts";

export class InternalError extends Error {
constructor(
message: string,
Expand Down Expand Up @@ -46,32 +44,6 @@ export class ErrorEx extends Error {
public readonly printStack: boolean;
}

export class LocalizedError extends Error {
constructor(
name: string,
message: string,
source: MappedString,
position = 0,
printName = true,
printStack = true,
) {
const fileName = source.map(position)?.originalString?.fileName;
if (fileName) {
const { line, column } = mappedIndexToLineCol(source)(position);
message = `In file ${fileName} (${line + 1}:${column + 1}):
${message}`;
}

super(message);
this.name = name;
this.printName = printName;
this.printStack = printStack;
}

public readonly printName: boolean;
public readonly printStack: boolean;
}

export function asErrorEx(e: unknown) {
if (e instanceof ErrorEx) {
return e;
Expand Down
Loading

0 comments on commit ed58a5b

Please sign in to comment.