Skip to content

Commit

Permalink
Comments 💭 and getDataPath patch
Browse files Browse the repository at this point in the history
  • Loading branch information
denyncrawford committed Jan 26, 2022
1 parent bd8e41d commit bcf721a
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 20 deletions.
11 changes: 5 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[submodule "default"]
path = modules/astrodon-templates/templates/default
url = git://github.com/astrodon/astrodon-default-template.git

[submodule "vue"]
path = modules/astrodon-templates/vue
url = git://github.com/astrodon/astrodon-vue-template.git
path = modules/astrodon-templates/vue
url = https://github.com/astrodon/astrodon-vue-template
[submodule "default"]
path = modules/astrodon-templates/default
url = https://github.com/astrodon/astrodon-default-template
2 changes: 2 additions & 0 deletions examples/compiled_vue_app/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const app = await App.new();

await app.registerWindow({ title: "Compile example", url: indexPath });

console.log(await app.getDataPath());

setInterval(() => {
app.send(`Hello Tauri: ${Math.random()}`);
}, 500);
Expand Down
2 changes: 2 additions & 0 deletions modules/astrodon-build/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export class Builder {
}
}

// Pack and unpack methods should be exported: Future work

export const unpackAssets = async (
data: bundle,
output: string,
Expand Down
19 changes: 19 additions & 0 deletions modules/astrodon-cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { dirname, join, toFileUrl } from "https://deno.land/std/path/mod.ts";
import { Builder } from "../../astrodon-build/mod.ts";
import { Logger } from "../utils.ts";

// Build options are located in the astrodon.config.ts file and can be passed to the cli build command

interface BuildOptions {
name: string;
entry: string;
Expand All @@ -11,7 +13,12 @@ interface BuildOptions {

const buildLogger = new Logger("build");

// Builds astrodon projects from CLI

export const build = async (options: BuildOptions) => {

// Getting options and filling in default values

options = Object.assign(options, await getBuidOptions());

buildLogger.log(`Building ${options.name} with this configurations:`, "info");
Expand All @@ -24,22 +31,32 @@ export const build = async (options: BuildOptions) => {
buildLogger.log(option, "info");
});

// Entry file is the main file of the project

const entry = Deno.realPathSync(options.entry);
const customAssetsPath = options.assets;

// Initializing the builder from @astrodon-builder

const builder = new Builder(dirname(entry));
const assetsPath = customAssetsPath
? customAssetsPath
: join(dirname(entry), "renderer", "src");

// Package assets into a TypeScript bundle

buildLogger.log("Packaging assets...");

await Builder.packageAssets(assetsPath, join(options.out, "snapshot.b.ts"));

// Pre-bundling code generation

buildLogger.log("Creating temporal entry file...");

await builder.preBundle("mod.ts");

// Compiling the project into an executable

buildLogger.log("Compiling...");

await builder.compile(join(options.out, options.name), {
Expand All @@ -49,6 +66,8 @@ export const build = async (options: BuildOptions) => {
buildLogger.log("🎉 Build process was successful!");
};

// Handler for getting the build options

const getBuidOptions = async (): Promise<BuildOptions> => {
try {
const { default: configFile } = await import(
Expand Down
2 changes: 2 additions & 0 deletions modules/astrodon-cli/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const init = async (options: InitOptions) => {
const formattedUrl = templateUrl.endsWith("/")
? templateUrl.slice(0, -1)
: templateUrl;

// For checking if the template is valid we pre-fetch the manifest to prevent cloning from untrusted repositories
const { data: templateManifest } = await axiod.get(
`${formattedUrl}/raw/main/template_manifest.json`,
// deno-lint-ignore no-explicit-any
Expand Down
5 changes: 5 additions & 0 deletions modules/astrodon-cli/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ import { build } from "./commands/build.ts";
import { init } from "./commands/init.ts";
import { join } from "https://deno.land/std/path/mod.ts";

// CLI configuration

await new Command()
.name(meta.name)
.version(meta.version)
.global()
.description(`Project manager for Astrodon`)
.command("help", new HelpCommand().global())
.command("completions", new CompletionsCommand())
// Start of CLI commands
.command(
"build",
new Command()
//Build command
.description("Build the app.")
.allowEmpty(false)
.option("-i, --entry [type:string]", "Entry point for the app.", {
Expand All @@ -34,6 +38,7 @@ await new Command()
.command(
"init",
new Command()
//Init command
.description("Initialize a new project.")
.allowEmpty(false)
.option("-t, --template [type:string]", "Template to use.", {
Expand Down
9 changes: 6 additions & 3 deletions modules/astrodon-cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface States {
info: string;
}

// Logger is an agnostic logger that can be used in any module of the CLI to show messages with different states.

export class Logger {
constructor(private readonly module: keyof AcceptedModules) {}

Expand All @@ -20,7 +22,8 @@ export class Logger {
error: red(`❌`),
info: yellow(`ℹ`),
};
console.log(`${states[type]} ${yellow(`[astrodon ${this.module}]:`)} ${message}`);
console.log(
`${states[type]} ${yellow(`[astrodon ${this.module}]:`)} ${message}`,
);
}

}
}
1 change: 1 addition & 0 deletions modules/astrodon-templates/default
Submodule default added at fb5f91
1 change: 1 addition & 0 deletions modules/astrodon-templates/vue
Submodule vue added at 71e8db
26 changes: 16 additions & 10 deletions modules/astrodon/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plug } from "./deps.ts";
import { dirname, Plug } from "./deps.ts";
import {
getAppOptions,
getAppPathByContext,
Expand Down Expand Up @@ -103,22 +103,28 @@ export class App {
this.windows.push(window);
}

public getDataPath() {
// Gets the app data folder by checking the binPath

public async getDataPath() {
const osSlash = Deno.build.os == "windows" ? "\\" : "/";
const homePath = Deno.env.get("HOME") || Deno.env.get("APPDATA") ||
Deno.cwd();
const binPath = getAppPathByContext(this.globalContext);
const customBinary = Deno.env.get("CUSTOM_BINARY");
const binPath = customBinary
? dirname(await Deno.realPath(customBinary))
: getAppPathByContext(this.globalContext);
const signaTurePath = `${meta.name}${osSlash}${meta.version}`;
const removedHome = binPath.replace(homePath, "");
const root = Deno.build.os == "windows"
? removedHome.split("\\")[1]
: removedHome.split("/")[1];

if (root == meta.name) {
if (removedHome.startsWith(`${osSlash}${signaTurePath}`)) {
const root = removedHome.split(osSlash)[1];
return binPath.substring(0, homePath.length + root.length + 1);
}

if (!binPath.includes(meta.name)) return binPath;
const astrodonIndex = binPath.indexOf(meta.name) - 1;
return binPath.substring(0, astrodonIndex);
if (!binPath.includes(signaTurePath)) return binPath;

const astrodonIndex = binPath.indexOf(signaTurePath);
return binPath.substring(0, astrodonIndex - 1);
}

public run(): void {
Expand Down
16 changes: 15 additions & 1 deletion modules/astrodon/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export const getAppOptions = async (): Promise<AppOptions> => {
}
};

// Note: This is running at runtime, maybe we can use it the build process?

/**
* Gets the path of the entry url
* Also uncompress assets if it's on production
Expand All @@ -99,20 +101,26 @@ export const prepareUrl = async (
url: string,
context: AppContext,
): Promise<string> => {
// Checks if url is remote and returns it
if (url.startsWith("http")) return url;
// If url is local, checks if is in production or explicitly set to prevent unpack on instance configurations
const production = window.astrodonProduction;
const preventUnpack = window?.astrodonAppConfig?.build?.preventUnpack;
if (!production || production && preventUnpack) return url;
// If url is local, checks if assets are already in memory
const assets = window.astrodonAssets;
if (!assets) return url;
console.log("assets");
// Gets binary directory from binary location
const dir = getAppPathByContext(context);
// assets are always located two levels up from the binary
const assetsFolder = join(dir, "../../assets");
const existFolder = await exists(assetsFolder);
if (!existFolder) {
await ensureDir(assetsFolder);
await unpackAssets(assets, assetsFolder);
}
// creates file url from assets in appData folder
// Note: This is a temporary solution, we should map the original assets folder to the appData folder
return `file://${
join(
assetsFolder,
Expand All @@ -123,6 +131,12 @@ export const prepareUrl = async (
}`;
};

/**
* Retrieve the binary path, this is placed outside to be used in other modules
* This is defines where the binary is located by the context of the app
* This is intentionally dynamic, so we can run app both locally or remotely
*/

export const getAppPathByContext = (context: AppContext) =>
join(
Deno.env.get("APPDATA") || Deno.env.get("HOME") || Deno.cwd(),
Expand Down

0 comments on commit bcf721a

Please sign in to comment.