summaryrefslogtreecommitdiff
path: root/deno_typescript/ops.rs
diff options
context:
space:
mode:
Diffstat (limited to 'deno_typescript/ops.rs')
-rw-r--r--deno_typescript/ops.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/deno_typescript/ops.rs b/deno_typescript/ops.rs
index e45349591..0680d07b3 100644
--- a/deno_typescript/ops.rs
+++ b/deno_typescript/ops.rs
@@ -41,11 +41,16 @@ struct ReadFile {
should_create_new_source_file: bool,
}
-pub fn read_file(_s: &mut TSState, v: Value) -> Result<Value, ErrBox> {
+pub fn read_file(s: &mut TSState, v: Value) -> Result<Value, ErrBox> {
let v: ReadFile = serde_json::from_value(v)?;
let (module_name, source_code) = if v.file_name.starts_with("$asset$/") {
let asset = v.file_name.replace("$asset$/", "");
- let source_code = crate::get_asset2(&asset)?.to_string();
+
+ let source_code = match s.custom_assets.get(&asset) {
+ Some(asset_path) => std::fs::read_to_string(&asset_path)?,
+ None => crate::get_asset2(&asset)?.to_string(),
+ };
+
(asset, source_code)
} else {
assert!(!v.file_name.starts_with("$assets$"), "you meant $asset$");
@@ -112,6 +117,27 @@ pub fn resolve_module_names(
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
+struct FetchAssetArgs {
+ name: String,
+}
+
+pub fn fetch_asset(s: &mut TSState, v: Value) -> Result<Value, ErrBox> {
+ let args: FetchAssetArgs = serde_json::from_value(v)?;
+
+ if let Some(asset_path) = s.custom_assets.get(&args.name) {
+ let source_code = std::fs::read_to_string(&asset_path)?;
+ return Ok(json!(source_code));
+ }
+
+ if let Some(source_code) = crate::get_asset(&args.name) {
+ Ok(json!(source_code))
+ } else {
+ panic!("op_fetch_asset bad asset {}", args.name)
+ }
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
struct Exit {
code: i32,
}