Skip to content

Commit

Permalink
fix(cli): do not rely on x.nest.land
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfiszel committed May 24, 2023
1 parent 48aae74 commit ad66bfa
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 20 deletions.
6 changes: 6 additions & 0 deletions cli/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
} from "https://deno.land/[email protected]/streams/mod.ts";
export { DelimiterStream } from "https://deno.land/[email protected]/streams/mod.ts";
export { iterateReader } from "https://deno.land/[email protected]/streams/iterate_reader.ts";
export { writeAllSync } from "https://deno.land/[email protected]/streams/mod.ts";

// other
export { getAvailablePort } from "https://deno.land/x/[email protected]/mod.ts";
Expand All @@ -38,3 +39,8 @@ export { default as objectHash } from "https://deno.land/x/[email protected]/m
export { default as gitignore_parser } from "npm:gitignore-parser";
export { default as JSZip } from "npm:[email protected]";
export * as log from "https://deno.land/[email protected]/log/mod.ts";
export {
stringify as yamlStringify,
parse as yamlParse,
} from "https://deno.land/[email protected]/yaml/mod.ts";
export { open } from "https://deno.land/x/[email protected]/index.ts";
2 changes: 1 addition & 1 deletion cli/flow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// deno-lint-ignore-file no-explicit-any
import { GlobalOptions, isSuperset } from "./types.ts";
import { parse as yamlParse } from "https://deno.land/[email protected]/yaml/mod.ts";

import {
colors,
Expand All @@ -10,6 +9,7 @@ import {
FlowService,
JobService,
Table,
yamlParse,
} from "./deps.ts";
import { requireLogin, resolveWorkspace, validatePath } from "./context.ts";
import { resolve, track_job } from "./script.ts";
Expand Down
3 changes: 1 addition & 2 deletions cli/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { GlobalOptions } from "./types.ts";
import { colors, getAvailablePort, log, Secret, Select } from "./deps.ts";
import { open } from "https://deno.land/x/open/index.ts";
import { colors, getAvailablePort, log, open, Secret, Select } from "./deps.ts";

export async function loginInteractive(remote: string) {
let token: string | undefined;
Expand Down
2 changes: 1 addition & 1 deletion cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CompletionsCommand,
DenoLandProvider,
UpgradeCommand,
log,
} from "./deps.ts";
import flow from "./flow.ts";
import app from "./apps.ts";
Expand All @@ -18,7 +19,6 @@ import folder from "./folder.ts";
import sync from "./sync.ts";
import { tryResolveVersion } from "./context.ts";
import { GlobalOptions } from "./types.ts";
import * as log from "https://deno.land/[email protected]/log/mod.ts";

addEventListener("error", (event) => {
if (event.error) {
Expand Down
4 changes: 2 additions & 2 deletions cli/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
Script,
ScriptService,
Table,
writeAllSync,
yamlParse,
} from "./deps.ts";
import { writeAllSync } from "https://deno.land/[email protected]/streams/mod.ts";
import { parse as yamlParse } from "https://deno.land/[email protected]/yaml/mod.ts";

export interface ScriptFile {
parent_hash?: string;
Expand Down
12 changes: 5 additions & 7 deletions cli/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
FlowModule,
RawScript,
log,
yamlStringify,
yamlParse,
} from "./deps.ts";
import {
getTypeStrFromPath,
Expand All @@ -31,11 +33,7 @@ import { downloadZip } from "./pull.ts";
import { handleScriptMetadata } from "./script.ts";

import { handleFile } from "./script.ts";
import { equal } from "https://deno.land/x/[email protected]/mod.ts";
import {
stringify as yamlStringify,
parse as yamlParse,
} from "https://deno.land/[email protected]/yaml/mod.ts";
import { deepEqual } from "./utils.ts";

type DynFSElement = {
isDirectory: boolean;
Expand Down Expand Up @@ -325,8 +323,8 @@ async function compareDynFSElement(
changes.push({ name: "added", path: k, content: v });
} else if (
m2[k] != v &&
(!k.endsWith(".json") || !equal(JSON.parse(v), JSON.parse(m2[k]))) &&
(!k.endsWith(".yaml") || !equal(yamlParse(v), yamlParse(m2[k])))
(!k.endsWith(".json") || !deepEqual(JSON.parse(v), JSON.parse(m2[k]))) &&
(!k.endsWith(".yaml") || !deepEqual(yamlParse(v), yamlParse(m2[k])))
) {
changes.push({ name: "edited", path: k, after: v, before: m2[k] });
}
Expand Down
10 changes: 3 additions & 7 deletions cli/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// deno-lint-ignore-file no-explicit-any

import { colors, log, path } from "./deps.ts";
import { colors, log, path, yamlParse, yamlStringify } from "./deps.ts";
import { pushApp } from "./apps.ts";
import {
parse as yamlParse,
stringify as yamlStringify,
} from "https://deno.land/[email protected]/yaml/mod.ts";
import { equal } from "https://deno.land/x/[email protected]/equal.ts";
import { pushFolder } from "./folder.ts";
import { pushFlow } from "./flow.ts";
import { pushResource } from "./resource.ts";
Expand All @@ -15,6 +10,7 @@ import { pushVariable } from "./variable.ts";
import * as Diff from "npm:diff";
import { yamlOptions } from "./sync.ts";
import { showDiffs } from "./main.ts";
import { deepEqual } from "./utils.ts";

export interface DifferenceCreate {
type: "CREATE";
Expand Down Expand Up @@ -47,7 +43,7 @@ export function isSuperset(
superset: Record<string, any>
): boolean {
return Object.keys(subset).every((key) => {
const eq = equal(subset[key], superset[key]);
const eq = deepEqual(subset[key], superset[key]);
if (!eq && showDiffs) {
const sub = subset[key];
const supers = superset[key];
Expand Down
76 changes: 76 additions & 0 deletions cli/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Modified from: https://raw.githubusercontent.com/epoberezkin/fast-deep-equal/master/src/index.jst
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck This file is copied from a JS project, so it's not type-safe.
export function deepEqual<T>(a: T, b: T): boolean {
if (a === b) return true;

if (a && b && typeof a === "object" && typeof b === "object") {
if (a.constructor !== b.constructor) return false;

let length, i;
if (Array.isArray(a)) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0; ) {
if (!deepEqual(a[i], b[i])) return false;
}
return true;
}

if (a instanceof Map && b instanceof Map) {
if (a.size !== b.size) return false;
for (i of a.entries()) {
if (!b.has(i[0])) return false;
}
for (i of a.entries()) {
if (!deepEqual(i[1], b.get(i[0]))) return false;
}
return true;
}

if (a instanceof Set && b instanceof Set) {
if (a.size !== b.size) return false;
for (i of a.entries()) {
if (!b.has(i[0])) return false;
}
return true;
}

if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0; ) {
if (a[i] !== b[i]) return false;
}
return true;
}

if (a.constructor === RegExp) {
return a.source === b.source && a.flags === b.flags;
}
if (a.valueOf !== Object.prototype.valueOf) {
return a.valueOf() === b.valueOf();
}
if (a.toString !== Object.prototype.toString) {
return a.toString() === b.toString();
}

const keys = Object.keys(a);
length = keys.length;
if (length !== Object.keys(b).length) return false;

for (i = length; i-- !== 0; ) {
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
}

for (i = length; i-- !== 0; ) {
const key = keys[i];
if (!deepEqual(a[key], b[key])) return false;
}

return true;
}

// true if both NaN, false otherwise
return a !== a && b !== b;
}

0 comments on commit ad66bfa

Please sign in to comment.