Skip to content

Commit

Permalink
Unpkg support, more options
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Aug 5, 2022
1 parent 7374338 commit de3a4fb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
image:
file: .gitpod.Dockerfile

tasks:
- name: Install deno-outdated for testing
init: deno task install

vscode:
extensions:
- denoland.vscode-deno
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# deno-outdated

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/LeoDog896/deno-outdated)

```bash
Expand Down
9 changes: 6 additions & 3 deletions change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { regexes } from "./regex.ts";
import { updateOrSelf } from "./update.ts";
import replaceAsync from "https://esm.sh/[email protected]";

export async function findAndReplace(source: string): Promise<string> {
export async function findAndReplace(
source: string,
flag = "i-deno-outdated",
): Promise<string> {
const lines = await Promise.all(
source.split("\n").map(str =>
source.split("\n").map((str) =>
regexes.reduce(
async (prev, regex) => {
const newPrev = await prev;
if (newPrev.includes("i-deno-outdated")) {
if (newPrev.includes(flag)) {
return newPrev;
}
return replaceAsync(
Expand Down
31 changes: 23 additions & 8 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ export async function recursiveReaddir(path: string, ignore: string[]) {
return files;
}

async function update(quiet: boolean, ignore: string[] = []) {
async function update(
quiet: boolean,
ignore: string[] = [],
lineIgnore: string,
) {
let count = 0;
// TODO .gitignore
const files = await recursiveReaddir(Deno.cwd(), [...ignore, ".git"])
const files = await recursiveReaddir(Deno.cwd(), [...ignore, ".git"]);
for (const file of files) {
if (ignore.includes(basename(file))) continue;

const originalSource = await Deno.readTextFile(file);
const newSource = await findAndReplace(originalSource);
const newSource = await findAndReplace(originalSource, lineIgnore);

if (newSource !== originalSource) {
await Deno.writeTextFile(
Expand All @@ -40,7 +44,7 @@ async function update(quiet: boolean, ignore: string[] = []) {
}
}

return count
return count;
}

await new Command()
Expand All @@ -52,11 +56,22 @@ await new Command()
.option("-i --ignore [ignore:string[]]", "list of files to ignore", {
separator: " ",
})
.option(
"-l --line-ignore [line-ignore:string]",
"The text of the comment to ignore",
{
default: "i-deno-outdated",
},
)
.description(
"Check for outdated dependencies for deno.land/x and other various 3rd party vendors",
)
.action(async ({ quiet, ignore }) => {
const count = await update(quiet, Array.isArray(ignore) ? ignore : [])
if (!quiet) console.log(`Updated ${count} files.`);
.action(async ({ quiet, ignore, lineIgnore }) => {
const count = await update(
quiet,
Array.isArray(ignore) ? ignore : [],
typeof lineIgnore === "string" ? lineIgnore : "i-deno-outdated",
);
if (!quiet) console.log(`Updated ${count} file${count === 1 ? "" : "s"}`);
})
.parse(Deno.args);
4 changes: 4 additions & 0 deletions regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const regexes: Regex[] = [
validate: /https?:\/\/cdn.jsdelivr.net\/[^ "'`]+/g,
removal: /@[\d\.]+/g,
},
{
validate: /https?:\/\/unpkg.com\/[^ "'`]+/g,
removal: /@[\w\d+\.]+/g,
},
];

/**
Expand Down
6 changes: 3 additions & 3 deletions test/change.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { findAndReplace } from "../change.ts";
import {
assert,
assert,
assertEquals,
assertNotEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
Expand All @@ -9,7 +9,7 @@ Deno.test("Source code translation works", async () => {
const source = "const x = 'https://deno.land/[email protected]/testing/asserts.ts'"; // i-deno-outdated
const redirect = await findAndReplace(source);

console.log(redirect)
console.log(redirect);

assertNotEquals(redirect, source);
});
Expand All @@ -24,7 +24,7 @@ const x = 'https://deno.land/[email protected]/testing/asserts.ts' // i-deno-outdated
const sourceLines = source.split("\n");
const lines = redirect.split("\n");

assert(!lines[0].includes("%22")) // ensure that the escape character bug does not exist
assert(!lines[0].includes("%22")); // ensure that the escape character bug does not exist
assertNotEquals(lines[0], sourceLines[0]);
assertEquals(lines[1], sourceLines[1]);

Expand Down

0 comments on commit de3a4fb

Please sign in to comment.