Skip to content

Commit

Permalink
Merge pull request #89 from drashland/refactor
Browse files Browse the repository at this point in the history
chore: Refactor
  • Loading branch information
ebebbington authored Nov 10, 2020
2 parents 311e2fb + 8d6d277 commit d225de3
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 155 deletions.
16 changes: 10 additions & 6 deletions src/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import ModuleService from "../services/module_service.ts";
*/
export async function check(dependencies: string[]): Promise<void> {
// 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.",
);
Expand All @@ -26,7 +30,7 @@ export async function check(dependencies: string[]): Promise<void> {
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);
Expand Down
10 changes: 5 additions & 5 deletions src/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ export async function info(modules: string[]): Promise<void> {
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",
);
}
Expand Down
22 changes: 11 additions & 11 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import ModuleService from "../services/module_service.ts";
*/
export async function update(dependencies: string[]): Promise<void> {
// 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.",
);
Expand All @@ -29,6 +33,8 @@ export async function update(dependencies: string[]): Promise<void> {
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) {
Expand All @@ -39,12 +45,6 @@ export async function update(dependencies: string[]): Promise<void> {
"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,
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
43 changes: 20 additions & 23 deletions src/services/deno_service.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -59,40 +66,30 @@ 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 "<owner>/<repo>"
*/
public static async getThirdPartyRepoAndOwner(
public static async getThirdPartyRepoURL(
importedModuleName: string,
): Promise<string> {
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<ModuleMeta> {
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<string> {
const repoAndOwner = await DenoService.getThirdPartyRepoAndOwner(
importedModuleName,
);
return "https://github.com/" + repoAndOwner;
return json;
}
}
Loading

0 comments on commit d225de3

Please sign in to comment.