Skip to content

Commit

Permalink
Better node package
Browse files Browse the repository at this point in the history
  • Loading branch information
j3lte committed Nov 24, 2023
1 parent 8bacbe4 commit 5217045
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 170 deletions.
268 changes: 124 additions & 144 deletions deno.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/build-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ await build({
skipSourceOutput: true,
// scriptModule: false,
shims: {
deno: true,
// deno: true,
},
test: false,
typeCheck: false,
Expand Down
47 changes: 28 additions & 19 deletions scripts/update-node-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,43 @@ async function update_npm_build_script(): Promise<void> {
await Deno.writeTextFile(path, updatedFile);
}

async function update_npm_package(packageName: string, fileContent: string): Promise<string> {
try {
const npmJSON = await fetch(`https://registry.npmjs.org/${packageName}`).then((r) => r.json());
const latest = npmJSON["dist-tags"].latest;

// Replace the version number in Pastebin.ts
const updatedFile = fileContent.replace(
new RegExp(`npm:${packageName}@\\d+\\.\\d+\\.\\d+`),
`npm:${packageName}@${latest}`,
);

return updatedFile;
} catch (error) {
console.error(`Error updating version in ${packageName}`);
console.error(error);
}

return fileContent;
}

async function update_npm_packages(): Promise<void> {
const pastebinPath = `${getDirPath()}/../src/node/Pastebin.ts`;

const pastebinFile = await Deno.readTextFile(pastebinPath);

try {
// Find the version number in "npm:[email protected]"
const pastebinVersion = (
pastebinFile.match(/npm:node-fetch@(\d+\.\d+\.\d+)/) as string[]
)[1];

const npmJSON = await fetch("https://registry.npmjs.org/node-fetch").then((r) => r.json());
const latest = npmJSON["dist-tags"].latest;
// update node-fetch and fast-xml-parser
const updatedFile = await update_npm_package("node-fetch", pastebinFile);
const updatedFile2 = await update_npm_package("fast-xml-parser", updatedFile);

if (pastebinVersion === latest) {
console.log("No changes to node/Pastebin.ts package needed.");
if (pastebinFile === updatedFile2) {
console.log("No changes to npm packages needed.");
return;
} else {
console.log(`Updating npm packages from ${pastebinVersion} to ${latest}`);

// Replace the version number in Pastebin.ts
const updatedFile = pastebinFile.replace(
/npm:node-fetch@\d+\.\d+\.\d+/,
`npm:node-fetch@${latest}`,
);

await Deno.writeTextFile(pastebinPath, updatedFile);
}

console.log("Updating npm packages in Pastebin.ts");
await Deno.writeTextFile(pastebinPath, updatedFile2);
} catch (error) {
console.error("Error updating version in Pastebin.ts");
console.error(error);
Expand Down
19 changes: 18 additions & 1 deletion src/deno/Pastebin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
// Copyright 2023 J.W. Lagendijk. All rights reserved. MIT license.

import { parse } from "https://deno.land/x/[email protected]/mod.ts";

import { AbstractPastebin } from "../lib/Pastebin.ts";
import { ICreatePasteFileOptions, ICreatePasteTextOptions } from "../lib/interfaces.ts";
import {
ICreatePasteFileOptions,
ICreatePasteTextOptions,
IPastebinOptions,
} from "../lib/interfaces.ts";

export class Pastebin extends AbstractPastebin {
constructor(config?: IPastebinOptions | string | null) {
super(config);
// This is probably not the best way to do this, but it works
super.parseXML = this.parseXml;
}

/**
* Create a paste from a file
*
Expand Down Expand Up @@ -40,4 +52,9 @@ export class Pastebin extends AbstractPastebin {

return this.createPaste(pasteOpts);
}

parseXml = (xml: string): Record<string, string> => {
const data = parse(xml) as unknown as Record<string, string>;
return data;
};
}
11 changes: 7 additions & 4 deletions src/lib/Pastebin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2023 J.W. Lagendijk. All rights reserved. MIT license.

import { parse } from "https://deno.land/x/[email protected]/mod.ts";

import {
ExpirationTime,
FormatType,
Expand Down Expand Up @@ -341,17 +339,22 @@ export abstract class AbstractPastebin {
}

// Parse
parseXML(_xml: string): Record<string, string> {
throw new Error("Not implemented!");
}

#parseUser(xml: string): User {
const data = parse(xml) as { user?: User };
const data = this.parseXML(xml) as { user?: User };
if (isUndefined(data) || isNull(data) || isUndefined(data.user)) {
throw new Error("No data returned to _parseUser!");
}
return data.user;
}

#parsePastes(xml: string): Paste[] {
const { root: data } = parse(`<root>${xml}</root>`) as unknown as { root: { paste: Paste[] } };
const { root: data } = this.parseXML(`<root>${xml}</root>`) as unknown as {
root: { paste: Paste[] };
};
if (isUndefined(data) || isNull(data) || isUndefined(data.paste)) {
throw new Error("No data returned to _parsePastes!");
}
Expand Down
19 changes: 18 additions & 1 deletion src/node/Pastebin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Copyright 2023 J.W. Lagendijk. All rights reserved. MIT license.

import { AbstractPastebin } from "../lib/Pastebin.ts";
import { ICreatePasteFileOptions, ICreatePasteTextOptions } from "../lib/interfaces.ts";
import {
ICreatePasteFileOptions,
ICreatePasteTextOptions,
IPastebinOptions,
} from "../lib/interfaces.ts";

import fs from "node:fs/promises";
import { Buffer } from "node:buffer";
import fetch from "npm:[email protected]";
import { XMLParser } from "npm:[email protected]";

export class Pastebin extends AbstractPastebin {
constructor(config?: IPastebinOptions | string | null) {
super(config);
// This is probably not the best way to do this, but it works
super.parseXML = this.parseXml;
}

fetch = fetch as unknown as typeof globalThis.fetch;

async createPasteFromFile(
Expand Down Expand Up @@ -39,4 +50,10 @@ export class Pastebin extends AbstractPastebin {

return this.createPaste(pasteOpts);
}

parseXml = (xml: string): Record<string, string> => {
const parser = new XMLParser();
const data = parser.parse(xml);
return data;
};
}

0 comments on commit 5217045

Please sign in to comment.