diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-01-22 20:18:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-22 20:18:01 +0100 |
commit | bd9561f4de8f940ce6ed8b5eedfa84161a749c54 (patch) | |
tree | 20bc23c294aa3031fe4729dc09e03351ec6097f4 /deno_typescript/ops.rs | |
parent | 3c47718959fb38d51e34c64d423151b5326bae3a (diff) |
Reland "Create an old program to be used in snapshot." (#3747)
* read CLI assets from disk during snapshotting
Diffstat (limited to 'deno_typescript/ops.rs')
-rw-r--r-- | deno_typescript/ops.rs | 30 |
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, } |