Skip to content

Commit

Permalink
use lumeland/init to upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jun 4, 2024
1 parent 1d593c3 commit 57e6aec
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 172 deletions.
80 changes: 3 additions & 77 deletions cli/upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,10 @@
import { log } from "../core/utils/log.ts";
import {
getCurrentVersion,
getLatestDevelopmentVersion,
getLatestVersion,
} from "../core/utils/lume_version.ts";
import {
readDenoConfig,
updateLumeVersion,
writeDenoConfig,
} from "../core/utils/deno_config.ts";
import { upgrade } from "../deps/init.ts";

interface Options {
dev?: boolean;
version?: string;
}
export default function ({ dev, version }: Options) {
return upgrade(dev, version);
}

/** Upgrade the Lume installation to the latest version */
export async function upgrade(dev: boolean | string = false, version?: string) {
const latest = version
? version
: dev
? await getLatestDevelopmentVersion()
: await getLatestVersion();
const url = getVersionUrl(latest, dev);

if (latest === getCurrentVersion()) {
const message = version
? `You're already using this version of Lume:`
: dev
? "You're using the latest version of Lume:"
: "You're using the latest development version of Lume:";

log.info(`${message} <green>${latest}</green>`);
return;
}

log.info(
version
? `Updating Lume to <green>${latest}</green>...`
: `New version available. Updating Lume to <green>${latest}</green>...`,
);

const denoConfig = await readDenoConfig();

if (!denoConfig) {
throw new Error("No Deno config file found");
}

updateLumeVersion(url, denoConfig);

// Remove deno.lock file
try {
await Deno.remove("deno.lock");
} catch {
// Ignore
}

await writeDenoConfig(denoConfig);

log.info("Update successful!");

if (!dev) {
log.info(
`See the changes in <gray>https://github.com/lumeland/lume/blob/${latest}/CHANGELOG.md</gray>`,
);
}
}

function getVersionUrl(version: string, dev: boolean | string = false): URL {
// Prepend automatically "v" to the version if it's missing
if (!dev && !version.startsWith("v")) {
version = `v${version}`;
}

return new URL(
dev
? `https://cdn.jsdelivr.net/gh/lumeland/lume@${version}/`
: `https://deno.land/x/lume@${version}/`,
);
export default function ({ dev, version }: Options) {
return upgrade({ dev, version });
}
72 changes: 0 additions & 72 deletions core/utils/deno_config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { brightGreen, gray } from "../../deps/colors.ts";
import { dirname, extname, join } from "../../deps/path.ts";
import { parse } from "../../deps/jsonc.ts";
import { isUrl } from "../utils/path.ts";

Expand Down Expand Up @@ -55,73 +53,3 @@ export async function readDenoConfig(): Promise<DenoConfigResult | undefined> {
}
}
}

/** Write the Deno configuration file */
export async function writeDenoConfig(options: DenoConfigResult) {
const { file, config, importMap } = options;

if (importMap && !config.importMap) {
config.imports = importMap.imports;
config.scopes = importMap.scopes;
}

if (config.importMap) {
const importMapFile = join(dirname(file), config.importMap);
await Deno.writeTextFile(
importMapFile,
JSON.stringify(importMap, null, 2) + "\n",
);
console.log(brightGreen("Import map file saved:"), importMapFile);
}

if (extname(file) === ".jsonc") {
const save = confirm(
"Saving the deno.jsonc file will overwrite the comments. Continue?",
);

if (!save) {
console.log(
"You have to update your deno.jsonc file manually with the following content:",
);
console.log(gray(JSON.stringify(config, null, 2)));
console.log("Use deno.json to update it automatically without asking.");
return;
}
}
await Deno.writeTextFile(file, JSON.stringify(config, null, 2) + "\n");
console.log("Deno configuration file saved:", gray(file));
}

/** Update the Lume import map and tasks in the Deno configuration file */
export function updateLumeVersion(url: URL, denoConfig: DenoConfigResult) {
denoConfig.importMap ??= { imports: {} };

const { config, importMap } = denoConfig;

const oldUrl = importMap.imports["lume/"];
const newUrl = new URL("./", url).href;
importMap.imports["lume/"] = newUrl;

for (const [specifier, url] of Object.entries(importMap.imports)) {
if (url.startsWith(oldUrl)) {
importMap.imports[specifier] = url.replace(oldUrl, newUrl);
}
}

// Configure lume tasks
const tasks = config.tasks || {};
if (!tasks.lume || !tasks.lume.includes(`echo "import 'lume/cli.ts'"`)) {
tasks.lume = `echo "import 'lume/cli.ts'" | deno run -A -`;
tasks.build = "deno task lume";
tasks.serve = "deno task lume -s";
}
config.tasks = tasks;

// Configure the compiler options
const compilerOptions = config.compilerOptions || {};
compilerOptions.types = compilerOptions.types || [];
if (!compilerOptions.types.includes("lume/types.ts")) {
compilerOptions.types.push("lume/types.ts");
}
config.compilerOptions = compilerOptions;
}
16 changes: 0 additions & 16 deletions core/utils/lume_version.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/** Return the latest stable version from the deno.land/x repository */
export async function getLatestVersion(): Promise<string> {
const response = await fetch("https://cdn.deno.land/lume/meta/versions.json");
const versions = await response.json();
return versions.latest;
}

/** Return the hash of the latest commit from the GitHub repository */
export async function getLatestDevelopmentVersion(): Promise<string> {
const response = await fetch(
`https://api.github.com/repos/lumeland/lume/commits/main`,
);
const commits = await response.json();
return commits.sha;
}

/** Return the current installed version */
export function getCurrentVersion(
url = new URL(import.meta.resolve("../")),
Expand Down
12 changes: 12 additions & 0 deletions deps/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const res = await fetch(
`https://cdn.deno.land/lume_init/meta/versions.json`,
);
const versions = await res.json();
const { run } = await import(
`https://deno.land/x/lume_init@${versions.latest}/mod.ts`
);
const { default: upgrade } = await import(
`https://deno.land/x/lume_init@${versions.latest}/upgrade.ts`
);

export { run, upgrade };
9 changes: 2 additions & 7 deletions init.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { run } from "./deps/init.ts";

console.warn(
"This module is deprecated. Use `deno run -A https://lume.land/init.ts` instead.",
);

const res = await fetch(
`https://cdn.deno.land/lume_init/meta/versions.json`,
);
const versions = await res.json();
const { run } = await import(
`https://deno.land/x/lume_init@${versions.latest}/mod.ts`
);
run();

0 comments on commit 57e6aec

Please sign in to comment.