-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start lang crates. * Add version managers. * Add node. * Move helper. * Move node utils. * Move find util. * Update workspace. * Fix ext.
- Loading branch information
Showing
27 changed files
with
379 additions
and
189 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "moon_lang_node" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
moon_lang = { path = "../lang" } |
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,44 @@ | ||
pub mod node; | ||
|
||
use moon_lang::{Language, PackageManager, VersionManager}; | ||
|
||
pub const NODE: Language = Language { | ||
default_version: "16.15.0", | ||
vendor_bins_dir: "node_modules/.bin", | ||
vendor_dir: "node_modules", | ||
}; | ||
|
||
// Package managers | ||
|
||
pub const NPM: PackageManager = PackageManager { | ||
config_filenames: &[".npmrc"], | ||
default_version: "8.10.0", | ||
lock_filenames: &["package-lock.json", "npm-shrinkwrap.json"], | ||
manifest_filename: "package.json", | ||
}; | ||
|
||
pub const PNPM: PackageManager = PackageManager { | ||
config_filenames: &["pnpm-workspace.yaml", ".pnpmfile.cjs"], | ||
default_version: "7.1.5", | ||
lock_filenames: &["pnpm-lock.yaml"], | ||
manifest_filename: "package.json", | ||
}; | ||
|
||
pub const YARN: PackageManager = PackageManager { | ||
config_filenames: &[".yarn", ".yarnrc", ".yarnrc.yml"], | ||
default_version: "3.2.1", | ||
lock_filenames: &["yarn.lock"], | ||
manifest_filename: "package.json", | ||
}; | ||
|
||
// Version managers | ||
|
||
pub const NVMRC: VersionManager = VersionManager { | ||
config_filename: None, | ||
version_filename: ".nvmrc", | ||
}; | ||
|
||
pub const NODENV: VersionManager = VersionManager { | ||
config_filename: None, | ||
version_filename: ".node-version", | ||
}; |
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,118 @@ | ||
use crate::NODE; | ||
use moon_lang::LangError; | ||
use std::env::{self, consts}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
pub fn extend_node_options_env_var(next: String) -> String { | ||
match env::var("NODE_OPTIONS") { | ||
Ok(prev) => format!("{} {}", prev, next), | ||
Err(_) => next, | ||
} | ||
} | ||
|
||
pub fn find_package(starting_dir: &Path, package_name: &str) -> Option<PathBuf> { | ||
let pkg_path = starting_dir.join(NODE.vendor_dir).join(package_name); | ||
|
||
if pkg_path.exists() { | ||
return Some(pkg_path); | ||
} | ||
|
||
match starting_dir.parent() { | ||
Some(dir) => find_package_bin(dir, package_name), | ||
None => None, | ||
} | ||
} | ||
|
||
pub fn find_package_bin(starting_dir: &Path, package_name: &str) -> Option<PathBuf> { | ||
let bin_path = starting_dir | ||
.join(NODE.vendor_bins_dir) | ||
.join(get_bin_name_suffix(package_name, "cmd", true)); | ||
|
||
if bin_path.exists() { | ||
return Some(bin_path); | ||
} | ||
|
||
match starting_dir.parent() { | ||
Some(dir) => find_package_bin(dir, package_name), | ||
None => None, | ||
} | ||
} | ||
|
||
pub fn get_bin_name_suffix(name: &str, windows_ext: &str, flat: bool) -> String { | ||
if cfg!(windows) { | ||
format!("{}.{}", name, windows_ext) | ||
} else if flat { | ||
name.to_owned() | ||
} else { | ||
format!("bin/{}", name) | ||
} | ||
} | ||
|
||
pub fn get_download_file_ext() -> &'static str { | ||
if consts::OS == "windows" { | ||
"zip" | ||
} else { | ||
"tar.gz" | ||
} | ||
} | ||
|
||
// #[allow(unused_assignments)] | ||
pub fn get_download_file_name(version: &str) -> Result<String, LangError> { | ||
let platform; | ||
|
||
if consts::OS == "linux" { | ||
platform = "linux" | ||
} else if consts::OS == "windows" { | ||
platform = "win"; | ||
} else if consts::OS == "macos" { | ||
platform = "darwin" | ||
} else { | ||
return Err(LangError::UnsupportedPlatform( | ||
consts::OS.to_string(), | ||
String::from("Node.js"), | ||
)); | ||
} | ||
|
||
let arch; | ||
|
||
if consts::ARCH == "x86" { | ||
arch = "x86" | ||
} else if consts::ARCH == "x86_64" { | ||
arch = "x64" | ||
} else if consts::ARCH == "arm" { | ||
arch = "arm64" | ||
} else if consts::ARCH == "powerpc64" { | ||
arch = "ppc64le" | ||
} else if consts::ARCH == "s390x" { | ||
arch = "s390x" | ||
} else { | ||
return Err(LangError::UnsupportedArchitecture( | ||
consts::ARCH.to_string(), | ||
String::from("Node.js"), | ||
)); | ||
} | ||
|
||
Ok(format!( | ||
"node-v{version}-{platform}-{arch}", | ||
version = version, | ||
platform = platform, | ||
arch = arch, | ||
)) | ||
} | ||
|
||
pub fn get_download_file(version: &str) -> Result<String, LangError> { | ||
Ok(format!( | ||
"{}.{}", | ||
get_download_file_name(version)?, | ||
get_download_file_ext() | ||
)) | ||
} | ||
|
||
pub fn get_nodejs_url(version: &str, host: &str, path: &str) -> String { | ||
format!( | ||
"{host}/dist/v{version}/{path}", | ||
host = host, | ||
version = version, | ||
path = path, | ||
) | ||
} |
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,7 @@ | ||
[package] | ||
name = "moon_lang" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
thiserror = "1.0.31" |
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,26 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum LangError { | ||
#[error( | ||
"Shashum check has failed for <file>{0}</file>, which was downloaded from <url>{1}</url>." | ||
)] | ||
InvalidShasum( | ||
String, // Download path | ||
String, // URL | ||
), | ||
|
||
#[error( | ||
"Unsupported architecture <symbol>{0}</symbol>. Unable to install <symbol>{1}</symbol>." | ||
)] | ||
UnsupportedArchitecture( | ||
String, // Arch | ||
String, // Tool name | ||
), | ||
|
||
#[error("Unsupported platform <symbol>{0}</symbol>. Unable to install <symbol>{1}</symbol>.")] | ||
UnsupportedPlatform( | ||
String, // Platform | ||
String, // Tool name | ||
), | ||
} |
Oops, something went wrong.