Skip to content

Commit a424d25

Browse files
authored
Install node and nvm (#968)
1 parent beb8310 commit a424d25

File tree

8 files changed

+130
-30
lines changed

8 files changed

+130
-30
lines changed

pgml-apps/cargo-pgml-components/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pgml-apps/cargo-pgml-components/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-pgml-components"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
edition = "2021"
55
authors = ["PostgresML <team@postgresml.org>"]
66
license = "MIT"

pgml-apps/cargo-pgml-components/src/frontend/javascript.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use std::process::Command;
77

88
use convert_case::{Case, Casing};
99

10-
use crate::util::{execute_command, info, unwrap_or_exit, warn};
10+
use crate::frontend::tools::execute_with_nvm;
11+
use crate::util::{info, unwrap_or_exit, warn};
1112

1213
/// The name of the JS file that imports all other JS files
1314
/// created in the modules.
@@ -99,7 +100,7 @@ pub fn bundle() {
99100
assemble_modules();
100101

101102
// Bundle JavaScript.
102-
unwrap_or_exit!(execute_command(
103+
unwrap_or_exit!(execute_with_nvm(
103104
Command::new(JS_COMPILER)
104105
.arg(MODULES_FILE)
105106
.arg("--file")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
export NVM_DIR="$HOME/.nvm"
3+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
4+
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
5+
6+
${@}

pgml-apps/cargo-pgml-components/src/frontend/sass.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::fs::{copy, read_to_string, remove_file, File};
55
use std::io::Write;
66
use std::process::Command;
77

8-
use crate::util::{execute_command, info, unwrap_or_exit, warn};
8+
use crate::frontend::tools::execute_with_nvm;
9+
use crate::util::{info, unwrap_or_exit, warn};
910

1011
/// The name of the SASS file that imports all other SASS files
1112
/// created in the modules.
@@ -77,7 +78,7 @@ pub fn bundle() {
7778
cleanup_old_bundles();
7879

7980
// Build Sass.
80-
unwrap_or_exit!(execute_command(
81+
unwrap_or_exit!(execute_with_nvm(
8182
Command::new(SASS_COMPILER).arg(SASS_FILE).arg(CSS_FILE),
8283
));
8384

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,102 @@
11
//! Tools required by us to build stuff.
22
3-
use crate::util::{error, execute_command, unwrap_or_exit, warn};
3+
use crate::util::{debug1, error, execute_command, unwrap_or_exit, warn};
4+
use std::fs::File;
5+
use std::io::Write;
46
use std::process::{exit, Command};
57

68
/// Required tools.
79
static TOOLS: &[&str] = &["sass", "rollup"];
10+
static NVM_EXEC: &'static str = "/tmp/pgml-components-nvm.sh";
11+
static NVM_SOURCE: &'static str = "https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh";
12+
static NVM_SOURCE_DOWNLOADED: &'static str = "/tmp/pgml-components-nvm-source.sh";
813

914
/// Install any missing tools.
1015
pub fn install() {
11-
if let Err(err) = execute_command(Command::new("node").arg("--version")) {
12-
error("Node is not installed. Install it with nvm or your system package manager.");
13-
debug!("{}", err);
14-
exit(1);
15-
}
16+
install_nvm_entrypoint();
17+
debug!("installed node entrypoint");
18+
install_node();
19+
debug!("installed node");
1620

1721
for tool in TOOLS {
18-
match execute_command(Command::new(tool).arg("--version")) {
22+
match execute_with_nvm(Command::new(tool).arg("--version")) {
1923
Ok(_) => (),
2024
Err(err) => {
21-
debug!("{}", err);
25+
debug1!(err);
2226
warn(&format!("installing {}", tool));
23-
unwrap_or_exit!(execute_command(
27+
unwrap_or_exit!(execute_with_nvm(
2428
Command::new("npm").arg("install").arg("-g").arg(tool)
2529
));
2630
}
2731
}
2832
}
2933
}
34+
35+
/// Execute a command making sure that nvm is available.
36+
pub fn execute_with_nvm(command: &mut Command) -> std::io::Result<String> {
37+
let mut cmd = Command::new(NVM_EXEC);
38+
cmd.arg(command.get_program());
39+
for arg in command.get_args() {
40+
cmd.arg(arg);
41+
}
42+
execute_command(&mut cmd)
43+
}
44+
45+
/// Install the nvm entrypoint we provide into /tmp
46+
fn install_nvm_entrypoint() {
47+
let mut file = unwrap_or_exit!(File::create(NVM_EXEC));
48+
unwrap_or_exit!(writeln!(&mut file, "{}", include_str!("nvm.sh")));
49+
drop(file);
50+
51+
unwrap_or_exit!(execute_command(
52+
Command::new("chmod").arg("+x").arg(NVM_EXEC)
53+
));
54+
}
55+
56+
/// Install node using nvm
57+
fn install_node() {
58+
debug!("installing node");
59+
// Node is already installed.
60+
if let Ok(_) = execute_with_nvm(Command::new("node").arg("--version")) {
61+
debug!("node is available");
62+
return;
63+
}
64+
65+
warn("installing node using nvm");
66+
67+
debug!("node is not available");
68+
69+
if let Err(err) = execute_command(Command::new("nvm").arg("--version")) {
70+
debug!("nvm is not available");
71+
debug1!(err);
72+
// Install Node Version Manager.
73+
if let Err(err) = execute_command(
74+
Command::new("curl")
75+
.arg("-Ls")
76+
.arg(NVM_SOURCE)
77+
.arg("-o")
78+
.arg(NVM_SOURCE_DOWNLOADED),
79+
) {
80+
debug!("curl is not available");
81+
error("couldn't not download nvm from Github, please do so manually before proceeding");
82+
debug1!(err);
83+
exit(1);
84+
} else {
85+
if let Err(err) = execute_command(Command::new("bash").arg(NVM_SOURCE_DOWNLOADED)) {
86+
error("couldn't install nvm, please do so manually before proceeding");
87+
debug1!(err);
88+
exit(1);
89+
} else {
90+
warn("installed nvm");
91+
}
92+
}
93+
}
94+
95+
if let Err(err) = execute_with_nvm(Command::new("nvm").arg("install").arg("stable")) {
96+
error("couldn't install Node, please do so manually before proceeding");
97+
debug1!(err);
98+
exit(1);
99+
} else {
100+
warn("installed node")
101+
}
102+
}

pgml-apps/cargo-pgml-components/src/main.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use clap::{Args, Parser, Subcommand};
44
use std::env::{current_dir, set_current_dir};
5-
use std::fs::{create_dir_all};
5+
use std::fs::create_dir_all;
66
use std::path::Path;
77

88
#[macro_use]
@@ -62,17 +62,23 @@ fn main() {
6262
let cli = Cli::parse();
6363

6464
match cli.subcomand {
65-
CargoSubcommands::PgmlComponents(pgml_commands) => match pgml_commands.command {
66-
Commands::Bundle {} => bundle(pgml_commands.project_path),
67-
Commands::Add(command) => match command {
68-
AddCommands::Component { name } => crate::frontend::components::add(&name, pgml_commands.overwrite),
69-
},
70-
},
65+
CargoSubcommands::PgmlComponents(pgml_commands) => {
66+
validate_project(pgml_commands.project_path);
67+
match pgml_commands.command {
68+
Commands::Bundle {} => bundle(),
69+
Commands::Add(command) => match command {
70+
AddCommands::Component { name } => {
71+
crate::frontend::components::add(&name, pgml_commands.overwrite)
72+
}
73+
},
74+
}
75+
}
7176
}
7277
}
7378

74-
/// Bundle SASS and JavaScript into neat bundle files.
75-
fn bundle(project_path: Option<String>) {
79+
fn validate_project(project_path: Option<String>) {
80+
debug!("validating project directory");
81+
7682
// Validate that the required project paths exist.
7783
let cwd = if let Some(project_path) = project_path {
7884
project_path
@@ -92,9 +98,13 @@ fn bundle(project_path: Option<String>) {
9298
}
9399

94100
unwrap_or_exit!(set_current_dir(path));
101+
}
102+
103+
/// Bundle SASS and JavaScript into neat bundle files.
104+
fn bundle() {
95105
frontend::sass::bundle();
96106
frontend::javascript::bundle();
97107
frontend::components::update_modules();
98108

99-
info("Bundle complete");
109+
info("bundle complete");
100110
}

pgml-apps/cargo-pgml-components/src/util.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use owo_colors::OwoColorize;
22
use std::fs::File;
3-
use std::io::Write;
3+
use std::io::{ErrorKind, Write};
44
use std::path::Path;
5-
use std::process::{exit, Command};
5+
use std::process::Command;
66

77
macro_rules! unwrap_or_exit {
88
($i:expr) => {
@@ -17,6 +17,13 @@ macro_rules! unwrap_or_exit {
1717
};
1818
}
1919

20+
macro_rules! debug1 {
21+
($e:expr) => {
22+
debug!("{}:{}:{} {}", file!(), line!(), column!(), $e);
23+
};
24+
}
25+
26+
pub(crate) use debug1;
2027
pub(crate) use unwrap_or_exit;
2128

2229
pub fn info(value: &str) {
@@ -43,12 +50,14 @@ pub fn execute_command(command: &mut Command) -> std::io::Result<String> {
4350
let stdout = String::from_utf8_lossy(&output.stderr).to_string();
4451

4552
if !output.status.success() {
46-
error!(
53+
let error = String::from_utf8_lossy(&output.stderr).to_string();
54+
debug!(
4755
"{} failed: {}",
4856
command.get_program().to_str().unwrap(),
49-
String::from_utf8_lossy(&output.stderr).to_string(),
57+
error,
5058
);
51-
exit(1);
59+
60+
return Err(std::io::Error::new(ErrorKind::Other, error));
5261
}
5362

5463
if !stderr.is_empty() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy