-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
196 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
|
@@ -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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
@@ -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!"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
|
@@ -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; | ||
}; | ||
} |