Skip to content

Commit

Permalink
feat: Support multiple --include and --exclude args, and persist …
Browse files Browse the repository at this point in the history
…them to deno.json (#205)
  • Loading branch information
arnauorriols authored Nov 28, 2023
1 parent 48b6a35 commit c5f895d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function parseArgs(args: string[]) {
"config",
"entrypoint",
],
collect: ["grep"],
collect: ["grep", "include", "exclude"],
default: {
static: true,
limit: "100",
Expand Down
14 changes: 13 additions & 1 deletion src/config_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const DEFAULT_FILENAME = "deno.json";
interface ConfigArgs {
project?: string;
entrypoint?: string;
include?: string[];
exclude?: string[];
}

class ConfigFile {
Expand Down Expand Up @@ -70,7 +72,15 @@ class ConfigFile {
// Iterate over the other args as they might include args not yet persisted in the config file
for (const [key, otherValue] of Object.entries(otherConfigArgs)) {
// deno-lint-ignore no-explicit-any
if ((this.args() as any)[key] !== otherValue) {
const thisValue = (this.args() as any)[key];
if (otherValue instanceof Array) {
const thisArrayValue = thisValue as typeof otherValue;
if (thisArrayValue.length !== otherValue.length) {
return false;
} else if (!thisArrayValue.every((x, i) => otherValue[i] === x)) {
return false;
}
} else if (thisValue !== otherValue) {
return false;
}
}
Expand All @@ -81,6 +91,8 @@ class ConfigFile {
// Copy object as normalization is internal to the config file
const normalizedArgs = {
project: args.project,
exclude: args.exclude,
include: args.include,
entrypoint: (args.entrypoint && !isURL(args.entrypoint))
? resolve(args.entrypoint)
// Backoff if entrypoint is URL, the user knows what they're doing
Expand Down
16 changes: 8 additions & 8 deletions src/subcommands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ManifestEntry } from "../utils/api_types.ts";
import { parseEntrypoint } from "../utils/entrypoint.ts";
import { walk } from "../utils/walk.ts";
import TokenProvisioner from "../utils/access_token.ts";
import { Args as RawArgs } from "../args.ts";

const help = `deployctl deploy
Deploy a script with static files to Deno Deploy.
Expand Down Expand Up @@ -50,8 +51,8 @@ export interface Args {
help: boolean;
static: boolean;
prod: boolean;
exclude?: string[];
include?: string[];
exclude: string[];
include: string[];
token: string | null;
project: string | null;
entrypoint: string | null;
Expand All @@ -61,8 +62,7 @@ export interface Args {
saveConfig: boolean;
}

// deno-lint-ignore no-explicit-any
export default async function (rawArgs: Record<string, any>): Promise<void> {
export default async function (rawArgs: RawArgs): Promise<void> {
const positionalEntrypoint: string | null = typeof rawArgs._[0] === "string"
? rawArgs._[0]
: null;
Expand All @@ -78,8 +78,8 @@ export default async function (rawArgs: Record<string, any>): Promise<void> {
? String(rawArgs["entrypoint"])
: null,
importMap: rawArgs["import-map"] ? String(rawArgs["import-map"]) : null,
exclude: rawArgs.exclude?.split(","),
include: rawArgs.include?.split(","),
exclude: rawArgs.exclude.flatMap((e) => e.split(",")),
include: rawArgs.include.flatMap((i) => i.split(",")),
dryRun: !!rawArgs["dry-run"],
config: rawArgs.config ? String(rawArgs.config) : null,
saveConfig: !!rawArgs["save-config"],
Expand Down Expand Up @@ -128,8 +128,8 @@ interface DeployOpts {
importMapUrl: URL | null;
static: boolean;
prod: boolean;
exclude?: string[];
include?: string[];
exclude: string[];
include: string[];
token: string | null;
project: string;
dryRun: boolean;
Expand Down
14 changes: 9 additions & 5 deletions src/utils/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ export async function calculateGitSha1(bytes: Uint8Array) {

function include(
path: string,
include?: string[],
exclude?: string[],
include: string[],
exclude: string[],
): boolean {
if (
include && !include.some((pattern): boolean => path.startsWith(pattern))
include.length &&
!include.some((pattern): boolean => path.startsWith(pattern))
) {
return false;
}
if (exclude && exclude.some((pattern): boolean => path.startsWith(pattern))) {
if (
exclude.length &&
exclude.some((pattern): boolean => path.startsWith(pattern))
) {
return false;
}
return true;
Expand All @@ -35,7 +39,7 @@ export async function walk(
cwd: string,
dir: string,
files: Map<string, string>,
options: { include?: string[]; exclude?: string[] },
options: { include: string[]; exclude: string[] },
): Promise<Record<string, ManifestEntry>> {
const entries: Record<string, ManifestEntry> = {};
for await (const file of Deno.readDir(dir)) {
Expand Down

0 comments on commit c5f895d

Please sign in to comment.