From 7a33dbb6cb2b511b972d8dd1aa839a4dbddfbb45 Mon Sep 17 00:00:00 2001 From: Edward Bebbington Date: Mon, 9 Nov 2020 12:49:17 +0000 Subject: [PATCH 1/4] update dependencies --- tests/integration/up-to-date-deps/deps.ts | 8 ++++---- tests/integration/up-to-date-deps/original_deps.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/integration/up-to-date-deps/deps.ts b/tests/integration/up-to-date-deps/deps.ts index 4ae3906..8425cde 100644 --- a/tests/integration/up-to-date-deps/deps.ts +++ b/tests/integration/up-to-date-deps/deps.ts @@ -1,13 +1,13 @@ import { Drash } from "https://deno.land/x/drash@v1.3.0/mod.ts"; // up to date -import * as fs from "https://deno.land/std@0.76.0/fs/mod.ts"; // up to date +import * as fs from "https://deno.land/std@0.77.0/fs/mod.ts"; // up to date -import * as colors from "https://deno.land/std@0.76.0/fmt/colors.ts"; //up to date +import * as colors from "https://deno.land/std@0.77.0/fmt/colors.ts"; //up to date import * as Cliffy from "https://x.nest.land/cliffy@0.15.0/mod.ts"; //up to date -import * as log from "https://deno.land/std@0.76.0/log/mod.ts"; //up to date +import * as log from "https://deno.land/std@0.77.0/log/mod.ts"; //up to date -export { v4 } from "https://deno.land/std@0.76.0/uuid/mod.ts"; //up to date +export { v4 } from "https://deno.land/std@0.77.0/uuid/mod.ts"; //up to date export { Cliffy, colors, Drash, fs, log }; diff --git a/tests/integration/up-to-date-deps/original_deps.ts b/tests/integration/up-to-date-deps/original_deps.ts index 4ae3906..8425cde 100644 --- a/tests/integration/up-to-date-deps/original_deps.ts +++ b/tests/integration/up-to-date-deps/original_deps.ts @@ -1,13 +1,13 @@ import { Drash } from "https://deno.land/x/drash@v1.3.0/mod.ts"; // up to date -import * as fs from "https://deno.land/std@0.76.0/fs/mod.ts"; // up to date +import * as fs from "https://deno.land/std@0.77.0/fs/mod.ts"; // up to date -import * as colors from "https://deno.land/std@0.76.0/fmt/colors.ts"; //up to date +import * as colors from "https://deno.land/std@0.77.0/fmt/colors.ts"; //up to date import * as Cliffy from "https://x.nest.land/cliffy@0.15.0/mod.ts"; //up to date -import * as log from "https://deno.land/std@0.76.0/log/mod.ts"; //up to date +import * as log from "https://deno.land/std@0.77.0/log/mod.ts"; //up to date -export { v4 } from "https://deno.land/std@0.76.0/uuid/mod.ts"; //up to date +export { v4 } from "https://deno.land/std@0.77.0/uuid/mod.ts"; //up to date export { Cliffy, colors, Drash, fs, log }; From 495164fa664d94436fb8b081baab0a2e10e05f8b Mon Sep 17 00:00:00 2001 From: Edward Bebbington Date: Mon, 9 Nov 2020 13:09:41 +0000 Subject: [PATCH 2/4] [refactor] Refactor core logic --- src/commands/check.ts | 16 +-- src/commands/info.ts | 9 +- src/commands/update.ts | 22 ++-- src/interfaces/module.ts | 8 +- src/services/deno_service.ts | 39 ++++--- src/services/module_service.ts | 188 ++++++++++++++++++--------------- src/services/nest_service.ts | 4 +- tests/integration/info_test.ts | 14 +-- 8 files changed, 165 insertions(+), 135 deletions(-) diff --git a/src/commands/check.ts b/src/commands/check.ts index 70cf639..51ff71d 100644 --- a/src/commands/check.ts +++ b/src/commands/check.ts @@ -10,12 +10,16 @@ import ModuleService from "../services/module_service.ts"; */ export async function check(dependencies: string[]): Promise { // Create objects for each dep, with its name and version - const modules = await ModuleService.constructModulesDataFromDeps( - dependencies, - "check", - ); + const allModules = await ModuleService.constructModulesDataFromDeps(); + const selectedModules = allModules.filter(module => { + if (dependencies.length) { // only return selected modules of selecting is set + return dependencies.indexOf(module.name) > -1 + } else { + return true + } + }) - if (modules === false || typeof modules === "boolean") { + if (selectedModules.length === 0) { LoggerService.logError( "Modules specified do not exist in your dependencies.", ); @@ -26,7 +30,7 @@ export async function check(dependencies: string[]): Promise { LoggerService.logInfo("Comparing versions..."); let depsCanBeUpdated: boolean = false; const listOfModuleNamesToBeUpdated: string[] = []; - modules.forEach((module) => { + selectedModules.forEach((module) => { if (module.importedVersion !== module.latestRelease) { depsCanBeUpdated = true; listOfModuleNamesToBeUpdated.push(module.name); diff --git a/src/commands/info.ts b/src/commands/info.ts index af4b589..7cb289b 100644 --- a/src/commands/info.ts +++ b/src/commands/info.ts @@ -25,25 +25,24 @@ export async function info(modules: string[]): Promise { const name = moduleToGetInfoOn; let description; let denoLandUrl; - let gitHubUrl; + let repositoryUrl; let latestVersion; if (isStd) { latestVersion = await DenoService.getLatestModuleRelease("std"); description = "Cannot retrieve descriptions for std modules"; denoLandUrl = "https://deno.land/std@" + latestVersion + "/" + name; - gitHubUrl = "https://github.com/denoland/deno/tree/master/std/" + name; + repositoryUrl = "https://github.com/denoland/deno/tree/master/std/" + name; } if (isThirdParty) { description = await DenoService.getThirdPartyDescription(name); - gitHubUrl = "https://github.com/" + - await DenoService.getThirdPartyRepoAndOwner(name); + repositoryUrl = await DenoService.getThirdPartyRepoURL(name) latestVersion = await DenoService.getLatestModuleRelease(name); denoLandUrl = "https://deno.land/x/" + name + "@" + latestVersion; } const importLine = "import * as " + name + ' from "' + denoLandUrl + '";'; LoggerService.logInfo( - `Information on ${name}\n\n - Name: ${name}\n - Description: ${description}\n - deno.land Link: ${denoLandUrl}\n - GitHub Repository: ${gitHubUrl}\n - Import Statement: ${importLine}\n - Latest Version: ${latestVersion}` + + `Information on ${name}\n\n - Name: ${name}\n - Description: ${description}\n - deno.land Link: ${denoLandUrl}\n - Repository: ${repositoryUrl}\n - Import Statement: ${importLine}\n - Latest Version: ${latestVersion}` + "\n", ); } diff --git a/src/commands/update.ts b/src/commands/update.ts index 18d4b7c..96b7825 100644 --- a/src/commands/update.ts +++ b/src/commands/update.ts @@ -10,12 +10,16 @@ import ModuleService from "../services/module_service.ts"; */ export async function update(dependencies: string[]): Promise { // Create objects for each dep, with its name and version - const modules = await ModuleService.constructModulesDataFromDeps( - dependencies, - "update", - ); + const allModules = await ModuleService.constructModulesDataFromDeps(); + const modules = allModules.filter(module => { + if (dependencies.length) { // only return selected modules of selecting is set + return dependencies.indexOf(module.name) > -1 + } else { + return true + } + }) - if (modules === false || typeof modules === "boolean") { + if (modules.length === 0) { LoggerService.logError( "Modules specified do not exist in your dependencies.", ); @@ -29,6 +33,8 @@ export async function update(dependencies: string[]): Promise { let depsContent: string = new TextDecoder().decode( Deno.readFileSync(usersWorkingDir + "/deps.ts"), ); // no need for a try/catch. The user needs a deps.ts file + + // Update the file content modules.forEach((module) => { // only re-write modules that need to be updated if (module.importedVersion === module.latestRelease) { @@ -39,12 +45,6 @@ export async function update(dependencies: string[]): Promise { "std@" + module.importedVersion + "/" + module.name, "std@" + module.latestRelease + "/" + module.name, ); - // `v` is not supported for std imports anymore - if (module.importedVersion.indexOf("v") > -1) { - LoggerService.logWarn( - `You are importing a version of ${module.name} prefixed with "v". deno.land does not support this and will throw a 403 error.`, - ); - } } else { depsContent = depsContent.replace( module.name + "@" + module.importedVersion, diff --git a/src/interfaces/module.ts b/src/interfaces/module.ts index 9bf9033..213737d 100644 --- a/src/interfaces/module.ts +++ b/src/interfaces/module.ts @@ -8,10 +8,10 @@ * importedVersion * The version imported for the given module * - * moduleURL + * importUrl * The import URL of the module * - * resposityURL + * repositoryUrl * The URL for the code repository for the module. * This is always a github.com URL for a deno.land module. * @@ -25,8 +25,8 @@ export default interface IModule { std: boolean; name: string; importedVersion: string; - moduleURL: string; - repositoryURL: string; + importUrl: string; + repositoryUrl: string; latestRelease: string; description: string; } diff --git a/src/services/deno_service.ts b/src/services/deno_service.ts index 7512258..25b82ad 100644 --- a/src/services/deno_service.ts +++ b/src/services/deno_service.ts @@ -1,3 +1,10 @@ +type ModuleMeta = { + upload_options: { + repository: string, // eg "drashland/drash" + type: string // eg "github" + } +} + export default class DenoService { /** * Url for Deno's CDN link @@ -71,28 +78,34 @@ export default class DenoService { public static async getThirdPartyRepoAndOwner( importedModuleName: string, ): Promise { + const meta = await DenoService.getModuleMeta(importedModuleName) + const repository = meta.upload_options.repository; + return repository; + } + + public static async getThirdPartyRepoURL( + importedModuleName: string, + ): Promise { + const meta = await DenoService.getModuleMeta(importedModuleName); + const repoURL = `https://${meta.upload_options.type}.com/${meta.upload_options.repository}`; + return repoURL + } + + private static async getModuleMeta (moduleName: string): Promise { const latestRelease = await DenoService.getLatestModuleRelease( - importedModuleName, + moduleName, ); const res = await fetch( - DenoService.DENO_CDN_URL + importedModuleName + "/versions/" + + DenoService.DENO_CDN_URL + moduleName + "/versions/" + latestRelease + "/meta/meta.json", ); + // there's more data but we only care about the stuff below const json: { upload_options: { repository: string; + type: string }; } = await res.json(); - const repository = json.upload_options.repository; - return repository; - } - - public static async getThirdPartyRepoURL( - importedModuleName: string, - ): Promise { - const repoAndOwner = await DenoService.getThirdPartyRepoAndOwner( - importedModuleName, - ); - return "https://github.com/" + repoAndOwner; + return json } } diff --git a/src/services/module_service.ts b/src/services/module_service.ts index afb5596..beefb09 100644 --- a/src/services/module_service.ts +++ b/src/services/module_service.ts @@ -3,6 +3,9 @@ import { colours, LoggerService } from "../../deps.ts"; import DenoService from "../services/deno_service.ts"; import NestService from "../services/nest_service.ts"; +const supportedUrls = ["https://deno.land/", "https://x.nest.land"] +const importVersionRegex = /(v)?[0-9\.]+[0-9\.]+[0-9]/g + export default class ModuleService { /** * Keeps the latest release string in-line with the imported version. @@ -41,15 +44,9 @@ export default class ModuleService { * 2. Adds a the github url to each object using Deno's database.json and the modules name * 3. Adds the latest version to each object using the github url * - * @param modulesForPurpose - Specific modules instead of checking all. Leave empty if all - * @param purpose - The purpose. Purely for logging purposes, eg "check" or "update" - * * @returns An array of objects, with each object containing information about each module */ - public static async constructModulesDataFromDeps( - modulesForPurpose: string[], - purpose: string, - ): Promise { + public static async constructModulesDataFromDeps(): Promise { // Solely read the users `deps.ts` file LoggerService.logInfo("Reading deps.ts to gather your dependencies..."); const usersWorkingDir: string = Deno.realPathSync("."); @@ -57,99 +54,116 @@ export default class ModuleService { Deno.readFileSync(usersWorkingDir + "/deps.ts"), ); // no need for a try/catch. The user needs a deps.ts file - // Turn lines that import from a url into a nice array - const listOfDeps: string[] = depsContent.split("\n").filter((line) => - line.indexOf("https://deno.land") !== -1 || - line.indexOf("https://x.nest.land") !== -1 - ); + // Only select lines we support eg versioning and an actual import line + const listOfDeps: string[] = depsContent.split("\n").filter((line) => { + const regexMatch = line.match(importVersionRegex) + const usesVersioning = regexMatch && regexMatch.length > 0 + let hasSupportedUrl = false + supportedUrls.forEach(supportedUrl => { + if (line.indexOf(supportedUrl) > -1) { + hasSupportedUrl = true + } + }) + const supported = hasSupportedUrl && usesVersioning === true + return supported + }) // Collate data for each module imported - const modules: Array = []; + const allModules: Array = []; for (const dep of listOfDeps) { - const isDeno = dep.indexOf("https://deno.land/") >= 0; - const isNest = dep.indexOf("https://x.nest.land/") >= 0; - - // Get if is std - const isStd: boolean = dep.indexOf("https://deno.land/std") >= 0 || - dep.indexOf("https://x.nest.land/std") >= 0; - // Get URL - const moduleURL: string = dep.substring( - dep.lastIndexOf(isDeno ? "https://deno.land/" : "https://x.nest.land/"), - dep.lastIndexOf(".ts") + 3, // to include the `.ts` - ); - - // Get the imported version - const importVersionRegex = /(v)?[0-9\.]+[0-9\.]+[0-9]/g; - const importVersionRegexResult = dep.match(importVersionRegex); - const importedVersion: string = importVersionRegexResult !== null && - importVersionRegexResult.length > 0 - ? importVersionRegexResult[0] - : ""; - if (!importedVersion) { - LoggerService.logError( - "The following line is not versioned. To update, your dependencies must be versioned." + - "\n" + - " " + dep, - ); - Deno.exit(1); + if (dep.indexOf("https://x.nest.land/") > -1) { + const data = await ModuleService.constructDataForNestImport(dep) + allModules.push(data) } - const RegistryService = isDeno ? DenoService : NestService; - - // Get the module name - const name: string = isStd - ? (dep.split("@" + importedVersion + "/")[1]).split("/")[0] - : dep.substring( - dep.lastIndexOf( - isDeno ? "https://deno.land/x/" : "https://x.nest.land/", - ) + 20, - dep.lastIndexOf("@"), - ); - - // Leave the module out if it isn't specified - if (modulesForPurpose.length && modulesForPurpose.indexOf(name) === -1) { - continue; + if (dep.indexOf("https://deno.land/") > -1) { + const data = await ModuleService.constructDataForDenoImport(dep) + allModules.push(data) } + } - // Get the repository url (always Github for deno.land but could be something else for nest.land) - const repositoryURL: string = isStd - ? "https://github.com/denoland/deno/tree/master/std/" + name - : await RegistryService.getThirdPartyRepoURL(name); - - // Get the latest release - make sure the string is the same format as imported version eg using a "v" - const latestRelease: string = isStd - ? ModuleService.standardiseVersion( - importedVersion, - await RegistryService.getLatestModuleRelease("std"), - ) - : ModuleService.standardiseVersion( - importedVersion, - await RegistryService.getLatestModuleRelease(name), - ); + return allModules; + } - // Get the description - const description: string = !isStd - ? await RegistryService.getThirdPartyDescription(name) - : colours.red( - "Descriptions for std modules are not currently supported", - ); + private static getImportedVersionFromImportLine (importLine: string): string { + const importVersionRegexResult = importLine.match(importVersionRegex); + const importedVersion: string = importVersionRegexResult !== null && + importVersionRegexResult.length > 0 + ? importVersionRegexResult[0] + : ""; + return importedVersion + } - // Save the module - modules.push({ - std: isStd, - repositoryURL, - moduleURL, - latestRelease, + private static async constructDataForNestImport (importLine: string): Promise { + const isStd = importLine.indexOf("/std") > -1 + const importUrl: string = importLine.substring( + importLine.indexOf("https://"), + importLine.indexOf(".ts") + 3, // to include the `.ts` + ); + const importedVersion = ModuleService.getImportedVersionFromImportLine(importLine) + let name = "" + if (isStd) { + const nameAndFile = importLine.split("@" + importedVersion + "/")[1] + name = nameAndFile.split("/")[0] + } else { + const nameAndVersionAndFile = importLine.split("https://x.nest.land/")[1] + name = nameAndVersionAndFile.split("@")[0] + } + const repositoryUrl = await NestService.getThirdPartyRepoURL(name) + const latestRelease = ModuleService.standardiseVersion( importedVersion, - name, - description, - }); + await NestService.getLatestModuleRelease(name) + ) + const description = await NestService.getThirdPartyDescription(name) + return { + description, + latestRelease, + repositoryUrl, + name, + std: isStd, + importedVersion, + importUrl } + } - if (!modules.length) { - return false; + private static async constructDataForDenoImport (importLine: string): Promise { + const isStd = importLine.indexOf("/std") > -1 + const importUrl: string = importLine.substring( + importLine.indexOf("https://"), + importLine.indexOf(".ts") + 3, // to include the `.ts` + ); + const importedVersion = ModuleService.getImportedVersionFromImportLine(importLine) + let name = ""; + if (isStd) { + const nameAndFile = importLine.split("@" + importedVersion + "/")[1] + name = nameAndFile.split("/")[0] + } else { + const nameAndVersionAndFile = importLine.split("https://deno.land/x/")[1] + name = nameAndVersionAndFile.split("@")[0] + } + const repositoryUrl: string = isStd + ? "https://github.com/denoland/deno/tree/master/std/" + name + : await DenoService.getThirdPartyRepoURL(name); + // Get the latest release - make sure the string is the same format as imported version eg using a "v" + const latestRelease = ModuleService.standardiseVersion( + importedVersion, + await DenoService.getLatestModuleRelease(isStd ? "std" : name), + ) + // Get the description + const description: string = !isStd + ? await DenoService.getThirdPartyDescription(name) + : colours.red( + "Descriptions for std modules are not currently supported", + ); + return { + description, + latestRelease, + repositoryUrl, + name, + std: isStd, + importedVersion, + importUrl } - return modules; } } diff --git a/src/services/nest_service.ts b/src/services/nest_service.ts index db52a45..8b58db4 100644 --- a/src/services/nest_service.ts +++ b/src/services/nest_service.ts @@ -75,7 +75,7 @@ export default class NestService { NestService.NEST_API_URL + "package/" + importedModuleName, ); const json: nestModule = await res.json(); - const repositoryURL = json.repository; - return repositoryURL; + const repositoryUrl = json.repository; + return repositoryUrl; } } diff --git a/tests/integration/info_test.ts b/tests/integration/info_test.ts index 7c29834..8a531d4 100644 --- a/tests/integration/info_test.ts +++ b/tests/integration/info_test.ts @@ -61,7 +61,7 @@ Deno.test({ " - Name: drash\n" + " - Description: A REST microframework for Deno's HTTP server with zero dependencies.\n" + ` - deno.land Link: https://deno.land/x/drash@${latestDrashRelease}\n` + - " - GitHub Repository: https://github.com/drashland/deno-drash\n" + + " - Repository: https://github.com/drashland/deno-drash\n" + ` - Import Statement: import * as drash from \"https://deno.land/x/drash@${latestDrashRelease}\";\n` + ` - Latest Version: ${latestDrashRelease}\n` + "\n", @@ -96,7 +96,7 @@ Deno.test({ " - Name: fs\n" + " - Description: Cannot retrieve descriptions for std modules\n" + ` - deno.land Link: https://deno.land/std@${latestStdRelease}/fs\n` + - " - GitHub Repository: https://github.com/denoland/deno/tree/master/std/fs\n" + + " - Repository: https://github.com/denoland/deno/tree/master/std/fs\n" + ` - Import Statement: import * as fs from \"https://deno.land/std@${latestStdRelease}/fs\";\n` + ` - Latest Version: ${latestStdRelease}\n` + "\n", @@ -139,17 +139,17 @@ Deno.test({ - Name: fs - Description: Cannot retrieve descriptions for std modules - - deno.land Link: https://deno.land/std@0.76.0/fs - - GitHub Repository: https://github.com/denoland/deno/tree/master/std/fs - - Import Statement: import * as fs from "https://deno.land/std@0.76.0/fs"; - - Latest Version: 0.76.0 + - deno.land Link: https://deno.land/std@${latestStdRelease}/fs + - Repository: https://github.com/denoland/deno/tree/master/std/fs + - Import Statement: import * as fs from "https://deno.land/std@${latestStdRelease}/fs"; + - Latest Version: ${latestStdRelease} ${colours.blue("INFO")} Information on drash - Name: drash - Description: A REST microframework for Deno's HTTP server with zero dependencies. - deno.land Link: https://deno.land/x/drash@${latestDrashRelease} - - GitHub Repository: https://github.com/drashland/deno-drash + - Repository: https://github.com/drashland/deno-drash - Import Statement: import * as drash from "https://deno.land/x/drash@${latestDrashRelease}"; - Latest Version: ${latestDrashRelease} From 15f29173d6ddc19944dea2b3e3c9362c1d19d6ef Mon Sep 17 00:00:00 2001 From: Edward Bebbington Date: Mon, 9 Nov 2020 13:09:54 +0000 Subject: [PATCH 3/4] deno fmt --- src/commands/check.ts | 8 +-- src/commands/info.ts | 5 +- src/commands/update.ts | 8 +-- src/services/deno_service.ts | 25 +++---- src/services/module_service.ts | 123 +++++++++++++++++---------------- 5 files changed, 89 insertions(+), 80 deletions(-) diff --git a/src/commands/check.ts b/src/commands/check.ts index 51ff71d..9d6a422 100644 --- a/src/commands/check.ts +++ b/src/commands/check.ts @@ -11,13 +11,13 @@ import ModuleService from "../services/module_service.ts"; export async function check(dependencies: string[]): Promise { // Create objects for each dep, with its name and version const allModules = await ModuleService.constructModulesDataFromDeps(); - const selectedModules = allModules.filter(module => { + const selectedModules = allModules.filter((module) => { if (dependencies.length) { // only return selected modules of selecting is set - return dependencies.indexOf(module.name) > -1 + return dependencies.indexOf(module.name) > -1; } else { - return true + return true; } - }) + }); if (selectedModules.length === 0) { LoggerService.logError( diff --git a/src/commands/info.ts b/src/commands/info.ts index 7cb289b..3ff52f3 100644 --- a/src/commands/info.ts +++ b/src/commands/info.ts @@ -32,11 +32,12 @@ export async function info(modules: string[]): Promise { description = "Cannot retrieve descriptions for std modules"; denoLandUrl = "https://deno.land/std@" + latestVersion + "/" + name; - repositoryUrl = "https://github.com/denoland/deno/tree/master/std/" + name; + repositoryUrl = "https://github.com/denoland/deno/tree/master/std/" + + name; } if (isThirdParty) { description = await DenoService.getThirdPartyDescription(name); - repositoryUrl = await DenoService.getThirdPartyRepoURL(name) + repositoryUrl = await DenoService.getThirdPartyRepoURL(name); latestVersion = await DenoService.getLatestModuleRelease(name); denoLandUrl = "https://deno.land/x/" + name + "@" + latestVersion; } diff --git a/src/commands/update.ts b/src/commands/update.ts index 96b7825..2f3cc3d 100644 --- a/src/commands/update.ts +++ b/src/commands/update.ts @@ -11,13 +11,13 @@ import ModuleService from "../services/module_service.ts"; export async function update(dependencies: string[]): Promise { // Create objects for each dep, with its name and version const allModules = await ModuleService.constructModulesDataFromDeps(); - const modules = allModules.filter(module => { + const modules = allModules.filter((module) => { if (dependencies.length) { // only return selected modules of selecting is set - return dependencies.indexOf(module.name) > -1 + return dependencies.indexOf(module.name) > -1; } else { - return true + return true; } - }) + }); if (modules.length === 0) { LoggerService.logError( diff --git a/src/services/deno_service.ts b/src/services/deno_service.ts index 25b82ad..fca7c8a 100644 --- a/src/services/deno_service.ts +++ b/src/services/deno_service.ts @@ -1,9 +1,9 @@ type ModuleMeta = { upload_options: { - repository: string, // eg "drashland/drash" - type: string // eg "github" - } -} + repository: string; // eg "drashland/drash" + type: string; // eg "github" + }; +}; export default class DenoService { /** @@ -78,7 +78,7 @@ export default class DenoService { public static async getThirdPartyRepoAndOwner( importedModuleName: string, ): Promise { - const meta = await DenoService.getModuleMeta(importedModuleName) + const meta = await DenoService.getModuleMeta(importedModuleName); const repository = meta.upload_options.repository; return repository; } @@ -87,25 +87,26 @@ export default class DenoService { importedModuleName: string, ): Promise { const meta = await DenoService.getModuleMeta(importedModuleName); - const repoURL = `https://${meta.upload_options.type}.com/${meta.upload_options.repository}`; - return repoURL + const repoURL = + `https://${meta.upload_options.type}.com/${meta.upload_options.repository}`; + return repoURL; } - private static async getModuleMeta (moduleName: string): Promise { + private static async getModuleMeta(moduleName: string): Promise { const latestRelease = await DenoService.getLatestModuleRelease( - moduleName, + moduleName, ); const res = await fetch( - DenoService.DENO_CDN_URL + moduleName + "/versions/" + + DenoService.DENO_CDN_URL + moduleName + "/versions/" + latestRelease + "/meta/meta.json", ); // there's more data but we only care about the stuff below const json: { upload_options: { repository: string; - type: string + type: string; }; } = await res.json(); - return json + return json; } } diff --git a/src/services/module_service.ts b/src/services/module_service.ts index beefb09..172031b 100644 --- a/src/services/module_service.ts +++ b/src/services/module_service.ts @@ -3,8 +3,8 @@ import { colours, LoggerService } from "../../deps.ts"; import DenoService from "../services/deno_service.ts"; import NestService from "../services/nest_service.ts"; -const supportedUrls = ["https://deno.land/", "https://x.nest.land"] -const importVersionRegex = /(v)?[0-9\.]+[0-9\.]+[0-9]/g +const supportedUrls = ["https://deno.land/", "https://x.nest.land"]; +const importVersionRegex = /(v)?[0-9\.]+[0-9\.]+[0-9]/g; export default class ModuleService { /** @@ -56,66 +56,69 @@ export default class ModuleService { // Only select lines we support eg versioning and an actual import line const listOfDeps: string[] = depsContent.split("\n").filter((line) => { - const regexMatch = line.match(importVersionRegex) - const usesVersioning = regexMatch && regexMatch.length > 0 - let hasSupportedUrl = false - supportedUrls.forEach(supportedUrl => { + const regexMatch = line.match(importVersionRegex); + const usesVersioning = regexMatch && regexMatch.length > 0; + let hasSupportedUrl = false; + supportedUrls.forEach((supportedUrl) => { if (line.indexOf(supportedUrl) > -1) { - hasSupportedUrl = true + hasSupportedUrl = true; } - }) - const supported = hasSupportedUrl && usesVersioning === true - return supported - }) + }); + const supported = hasSupportedUrl && usesVersioning === true; + return supported; + }); // Collate data for each module imported const allModules: Array = []; for (const dep of listOfDeps) { - if (dep.indexOf("https://x.nest.land/") > -1) { - const data = await ModuleService.constructDataForNestImport(dep) - allModules.push(data) + const data = await ModuleService.constructDataForNestImport(dep); + allModules.push(data); } if (dep.indexOf("https://deno.land/") > -1) { - const data = await ModuleService.constructDataForDenoImport(dep) - allModules.push(data) + const data = await ModuleService.constructDataForDenoImport(dep); + allModules.push(data); } } return allModules; } - private static getImportedVersionFromImportLine (importLine: string): string { + private static getImportedVersionFromImportLine(importLine: string): string { const importVersionRegexResult = importLine.match(importVersionRegex); const importedVersion: string = importVersionRegexResult !== null && - importVersionRegexResult.length > 0 - ? importVersionRegexResult[0] - : ""; - return importedVersion + importVersionRegexResult.length > 0 + ? importVersionRegexResult[0] + : ""; + return importedVersion; } - private static async constructDataForNestImport (importLine: string): Promise { - const isStd = importLine.indexOf("/std") > -1 + private static async constructDataForNestImport( + importLine: string, + ): Promise { + const isStd = importLine.indexOf("/std") > -1; const importUrl: string = importLine.substring( - importLine.indexOf("https://"), - importLine.indexOf(".ts") + 3, // to include the `.ts` + importLine.indexOf("https://"), + importLine.indexOf(".ts") + 3, // to include the `.ts` + ); + const importedVersion = ModuleService.getImportedVersionFromImportLine( + importLine, ); - const importedVersion = ModuleService.getImportedVersionFromImportLine(importLine) - let name = "" + let name = ""; if (isStd) { - const nameAndFile = importLine.split("@" + importedVersion + "/")[1] - name = nameAndFile.split("/")[0] + const nameAndFile = importLine.split("@" + importedVersion + "/")[1]; + name = nameAndFile.split("/")[0]; } else { - const nameAndVersionAndFile = importLine.split("https://x.nest.land/")[1] - name = nameAndVersionAndFile.split("@")[0] + const nameAndVersionAndFile = importLine.split("https://x.nest.land/")[1]; + name = nameAndVersionAndFile.split("@")[0]; } - const repositoryUrl = await NestService.getThirdPartyRepoURL(name) + const repositoryUrl = await NestService.getThirdPartyRepoURL(name); const latestRelease = ModuleService.standardiseVersion( - importedVersion, - await NestService.getLatestModuleRelease(name) - ) - const description = await NestService.getThirdPartyDescription(name) + importedVersion, + await NestService.getLatestModuleRelease(name), + ); + const description = await NestService.getThirdPartyDescription(name); return { description, latestRelease, @@ -123,39 +126,43 @@ export default class ModuleService { name, std: isStd, importedVersion, - importUrl - } + importUrl, + }; } - private static async constructDataForDenoImport (importLine: string): Promise { - const isStd = importLine.indexOf("/std") > -1 + private static async constructDataForDenoImport( + importLine: string, + ): Promise { + const isStd = importLine.indexOf("/std") > -1; const importUrl: string = importLine.substring( - importLine.indexOf("https://"), - importLine.indexOf(".ts") + 3, // to include the `.ts` + importLine.indexOf("https://"), + importLine.indexOf(".ts") + 3, // to include the `.ts` + ); + const importedVersion = ModuleService.getImportedVersionFromImportLine( + importLine, ); - const importedVersion = ModuleService.getImportedVersionFromImportLine(importLine) let name = ""; if (isStd) { - const nameAndFile = importLine.split("@" + importedVersion + "/")[1] - name = nameAndFile.split("/")[0] + const nameAndFile = importLine.split("@" + importedVersion + "/")[1]; + name = nameAndFile.split("/")[0]; } else { - const nameAndVersionAndFile = importLine.split("https://deno.land/x/")[1] - name = nameAndVersionAndFile.split("@")[0] + const nameAndVersionAndFile = importLine.split("https://deno.land/x/")[1]; + name = nameAndVersionAndFile.split("@")[0]; } const repositoryUrl: string = isStd - ? "https://github.com/denoland/deno/tree/master/std/" + name - : await DenoService.getThirdPartyRepoURL(name); + ? "https://github.com/denoland/deno/tree/master/std/" + name + : await DenoService.getThirdPartyRepoURL(name); // Get the latest release - make sure the string is the same format as imported version eg using a "v" const latestRelease = ModuleService.standardiseVersion( - importedVersion, - await DenoService.getLatestModuleRelease(isStd ? "std" : name), - ) + importedVersion, + await DenoService.getLatestModuleRelease(isStd ? "std" : name), + ); // Get the description const description: string = !isStd - ? await DenoService.getThirdPartyDescription(name) - : colours.red( - "Descriptions for std modules are not currently supported", - ); + ? await DenoService.getThirdPartyDescription(name) + : colours.red( + "Descriptions for std modules are not currently supported", + ); return { description, latestRelease, @@ -163,7 +170,7 @@ export default class ModuleService { name, std: isStd, importedVersion, - importUrl - } + importUrl, + }; } } From 8d6d27789e5ec0b450acf35361411e60302674ee Mon Sep 17 00:00:00 2001 From: Edward Bebbington Date: Mon, 9 Nov 2020 13:14:25 +0000 Subject: [PATCH 4/4] [#88-refactor] Remove dead code --- src/services/deno_service.ts | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/services/deno_service.ts b/src/services/deno_service.ts index fca7c8a..337c22a 100644 --- a/src/services/deno_service.ts +++ b/src/services/deno_service.ts @@ -66,23 +66,6 @@ export default class DenoService { return description; } - /** - * Fetches the owner and repository name, for the given module - * - * await getThirdPartyRepoAndOwner("drash"); // "drashland/deno-drash" - * - * @param importedModuleName - The imported module in which we want to get the repository for on github - * - * @returns The owner and repo name, eg "/" - */ - public static async getThirdPartyRepoAndOwner( - importedModuleName: string, - ): Promise { - const meta = await DenoService.getModuleMeta(importedModuleName); - const repository = meta.upload_options.repository; - return repository; - } - public static async getThirdPartyRepoURL( importedModuleName: string, ): Promise {