Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): add upgrade command (#203)
- Loading branch information
Showing
9 changed files
with
622 additions
and
0 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export { | ||
blue, | ||
bold, | ||
cyan, | ||
dim, | ||
green, | ||
italic, | ||
|
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./provider/deno_land.ts"; | ||
export * from "./provider/nest_land.ts"; | ||
export * from "./provider.ts"; | ||
export * from "./upgrade_command.ts"; |
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 |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { blue, bold, cyan, green, red, yellow } from "../deps.ts"; | ||
import { ValidationError } from "../_errors.ts"; | ||
import { Table } from "../../table/table.ts"; | ||
|
||
export interface Versions { | ||
latest: string; | ||
versions: Array<string>; | ||
} | ||
|
||
export interface UpgradeOptions { | ||
name: string; | ||
from?: string; | ||
to: string; | ||
args?: Array<string>; | ||
main?: string; | ||
} | ||
|
||
export abstract class Provider { | ||
abstract readonly name: string; | ||
protected readonly maxListSize: number = 25; | ||
private maxCols = 8; | ||
|
||
abstract getVersions(name: string): Promise<Versions>; | ||
|
||
abstract getRepositoryUrl(name: string): string; | ||
|
||
abstract getRegistryUrl(name: string, version: string): string; | ||
|
||
async isOutdated( | ||
name: string, | ||
currentVersion: string, | ||
targetVersion: string, | ||
): Promise<boolean> { | ||
const { latest, versions } = await this.getVersions(name); | ||
|
||
if (targetVersion === "latest") { | ||
targetVersion = latest; | ||
} | ||
|
||
// Check if requested version exists. | ||
if (targetVersion && !versions.includes(targetVersion)) { | ||
throw new ValidationError( | ||
`The provided version ${ | ||
bold(red(targetVersion)) | ||
} is not found.\n\n ${ | ||
cyan( | ||
`Visit ${ | ||
blue(this.getRepositoryUrl(name)) | ||
} for available releases or run again with the ${(yellow( | ||
"-l", | ||
))} or ${(yellow("--list-versions"))} command.`, | ||
) | ||
}`, | ||
); | ||
} | ||
|
||
// Check if requested version is already the latest available version. | ||
if (latest && latest === currentVersion && latest === targetVersion) { | ||
console.warn( | ||
yellow( | ||
`You're already using the latest available version ${currentVersion} of ${name}.`, | ||
), | ||
); | ||
return false; | ||
} | ||
|
||
// Check if requested version is already installed. | ||
if (targetVersion && currentVersion === targetVersion) { | ||
console.warn( | ||
yellow(`You're already using version ${currentVersion} of ${name}.`), | ||
); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
async upgrade( | ||
{ name, from, to, main = `${name}.ts`, args = [] }: UpgradeOptions, | ||
): Promise<void> { | ||
if (to === "latest") { | ||
const { latest } = await this.getVersions(name); | ||
to = latest; | ||
} | ||
const registry: string = new URL(main, this.getRegistryUrl(name, to)).href; | ||
|
||
const cmd = [Deno.execPath(), "install"]; | ||
if (args.length) { | ||
cmd.push(...args, "--force", "--name", name, registry); | ||
} else { | ||
cmd.push("--no-check", "--quiet", "--force", "--name", name, registry); | ||
} | ||
|
||
const process = Deno.run({ cmd, stdout: "piped", stderr: "piped" }); | ||
|
||
const [status, stderr] = await Promise.all([ | ||
process.status(), | ||
process.stderrOutput(), | ||
process.output(), | ||
]); | ||
|
||
if (!status.success) { | ||
process.close(); | ||
await Deno.stderr.write(stderr); | ||
throw new Error( | ||
`Failed to upgrade ${name} from ${from} to version ${to}!`, | ||
); | ||
} | ||
process.close(); | ||
|
||
console.info( | ||
`Successfully upgraded ${name} from ${from} to version ${to}! (${ | ||
this.getRegistryUrl(name, to) | ||
})`, | ||
); | ||
} | ||
|
||
public async listVersions( | ||
name: string, | ||
currentVersion?: string, | ||
): Promise<void> { | ||
const { versions } = await this.getVersions(name); | ||
this.printVersions(versions, currentVersion); | ||
} | ||
|
||
protected printVersions( | ||
versions: Array<string>, | ||
currentVersion?: string, | ||
{ maxCols = this.maxCols, indent = 0 }: { | ||
maxCols?: number; | ||
indent?: number; | ||
} = {}, | ||
): void { | ||
versions = versions.slice(); | ||
if (versions?.length) { | ||
versions = versions.map((version: string) => | ||
currentVersion && currentVersion === version | ||
? green(`* ${version}`) | ||
: ` ${version}` | ||
); | ||
|
||
if (versions.length > this.maxListSize) { | ||
const table = new Table().indent(indent); | ||
const rowSize = Math.ceil(versions.length / maxCols); | ||
const colSize = Math.min(versions.length, maxCols); | ||
let versionIndex = 0; | ||
for (let colIndex = 0; colIndex < colSize; colIndex++) { | ||
for (let rowIndex = 0; rowIndex < rowSize; rowIndex++) { | ||
if (!table[rowIndex]) { | ||
table[rowIndex] = []; | ||
} | ||
table[rowIndex][colIndex] = versions[versionIndex++]; | ||
} | ||
} | ||
console.log(table.toString()); | ||
} else { | ||
console.log( | ||
versions.map((version) => " ".repeat(indent) + version).join("\n"), | ||
); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Provider, Versions } from "../provider.ts"; | ||
|
||
export interface DenoLandProviderOptions { | ||
name?: string; | ||
} | ||
|
||
export class DenoLandProvider extends Provider { | ||
name = "deno.land"; | ||
private readonly repositoryUrl = "https://deno.land/x/"; | ||
private readonly registryUrl = "https://deno.land/x/"; | ||
private readonly moduleName?: string; | ||
|
||
constructor({ name }: DenoLandProviderOptions = {}) { | ||
super(); | ||
this.moduleName = name; | ||
} | ||
|
||
async getVersions( | ||
name: string, | ||
): Promise<Versions> { | ||
const response = await fetch( | ||
`https://cdn.deno.land/${this.moduleName ?? name}/meta/versions.json`, | ||
); | ||
if (!response.ok) { | ||
throw new Error( | ||
"couldn't fetch the latest version - try again after sometime", | ||
); | ||
} | ||
|
||
return await response.json(); | ||
} | ||
|
||
getRepositoryUrl(name: string): string { | ||
return new URL(`${this.moduleName ?? name}/`, this.repositoryUrl).href; | ||
} | ||
|
||
getRegistryUrl(name: string, version: string): string { | ||
return new URL(`${this.moduleName ?? name}@${version}/`, this.registryUrl) | ||
.href; | ||
} | ||
} |
Oops, something went wrong.