diff --git a/.gitignore b/.gitignore index 05923927..e68f4e2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target .DS_Store +/docs diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 9e2c1685..00000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "walnut_monorepo"] - path = walnut_monorepo - url = git@github.com:teamwalnut/walnut_monorepo.git diff --git a/src/build.rs b/src/build.rs index b4754f64..15f02de8 100644 --- a/src/build.rs +++ b/src/build.rs @@ -72,7 +72,7 @@ pub fn get_compiler_args( rescript_version } else { let bsc_path = match bsc_path { - Some(bsc_path) => bsc_path, + Some(bsc_path) => helpers::get_abs_path(&bsc_path), None => helpers::get_bsc(&package_root, workspace_root.to_owned()), }; helpers::get_rescript_version(&bsc_path) @@ -138,7 +138,7 @@ pub fn initialize_build( let project_root = helpers::get_abs_path(path); let workspace_root = helpers::get_workspace_root(&project_root); let bsc_path = match bsc_path { - Some(bsc_path) => bsc_path, + Some(bsc_path) => helpers::get_abs_path(&bsc_path), None => helpers::get_bsc(&project_root, workspace_root.to_owned()), }; let root_config_name = packages::read_package_name(&project_root)?; diff --git a/src/build/clean.rs b/src/build/clean.rs index 0cc4142e..ba53e4ff 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -70,16 +70,31 @@ pub fn clean_mjs_files(build_state: &BuildState) { .packages .get(&build_state.root_config_name) .expect("Could not find root package"); - Some(( - std::path::PathBuf::from(package.path.to_string()) - .join(&source_file.implementation.path) - .to_string_lossy() - .to_string(), - root_package.config.get_suffix(), - )) + + Some( + root_package + .config + .get_package_specs() + .iter() + .filter_map(|spec| { + if spec.in_source { + Some(( + std::path::PathBuf::from(package.path.to_string()) + .join(&source_file.implementation.path) + .to_string_lossy() + .to_string(), + root_package.config.get_suffix(spec), + )) + } else { + None + } + }) + .collect::>(), + ) } _ => None, }) + .flatten() .collect::>(); rescript_file_locations @@ -323,7 +338,7 @@ pub fn cleanup_after_build(build_state: &BuildState) { }); } -pub fn clean(path: &str, show_progress: bool, bsc_path: Option) -> Result<()> { +pub fn clean(path: &str, show_progress: bool, bsc_path: Option, build_dev_deps: bool) -> Result<()> { let project_root = helpers::get_abs_path(path); let workspace_root = helpers::get_workspace_root(&project_root); let packages = packages::make( @@ -332,11 +347,11 @@ pub fn clean(path: &str, show_progress: bool, bsc_path: Option) -> Resul &workspace_root, show_progress, // Always clean dev dependencies - true, + build_dev_deps, )?; let root_config_name = packages::read_package_name(&project_root)?; let bsc_path = match bsc_path { - Some(bsc_path) => bsc_path, + Some(bsc_path) => helpers::get_abs_path(&bsc_path), None => helpers::get_bsc(&project_root, workspace_root.to_owned()), }; diff --git a/src/build/compile.rs b/src/build/compile.rs index 3a13fbe1..73260ec6 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -430,23 +430,47 @@ pub fn compiler_args( false => vec![], }; + let package_name_arg = vec!["-bs-package-name".to_string(), config.name.to_owned()]; + let implementation_args = if is_interface { debug!("Compiling interface file: {}", &module_name); vec![] } else { debug!("Compiling file: {}", &module_name); - - vec![ - "-bs-package-name".to_string(), - config.name.to_owned(), - "-bs-package-output".to_string(), - format!( - "{}:{}:{}", - root_config.get_module(), - Path::new(file_path).parent().unwrap().to_str().unwrap(), - root_config.get_suffix() - ), - ] + let specs = root_config.get_package_specs(); + + specs + .iter() + .map(|spec| { + return vec![ + "-bs-package-output".to_string(), + format!( + "{}:{}:{}", + spec.module, + if spec.in_source { + Path::new(file_path) + .parent() + .unwrap() + .to_str() + .unwrap() + .to_string() + } else { + format!( + "lib/{}", + Path::join( + Path::new(&spec.get_out_of_source_dir()), + Path::new(file_path).parent().unwrap() + ) + .to_str() + .unwrap() + ) + }, + root_config.get_suffix(spec), + ), + ]; + }) + .flatten() + .collect() }; vec![ @@ -463,6 +487,7 @@ pub fn compiler_args( // this is the default // we should probably parse the right ones from the package config // vec!["-w".to_string(), "a".to_string()], + package_name_arg, implementation_args, // vec![ // "-I".to_string(), @@ -588,6 +613,7 @@ fn compile_file( &Some(packages), build_dev_deps, ); + let to_mjs = Command::new(bsc_path) .current_dir(helpers::canonicalize_string_path(&build_path_abs.to_owned()).unwrap()) .args(to_mjs_args) @@ -699,26 +725,31 @@ fn compile_file( } // copy js file - match &module.source_type { - SourceType::SourceFile(SourceFile { - implementation: Implementation { path, .. }, - .. - }) => { - let source = helpers::get_source_file_from_rescript_file( - &std::path::Path::new(&package.path).join(path), - &root_package.config.get_suffix(), - ); - let destination = helpers::get_source_file_from_rescript_file( - &std::path::Path::new(&package.get_build_path()).join(path), - &root_package.config.get_suffix(), - ); - - if source.exists() { - let _ = std::fs::copy(&source, &destination).expect("copying source file failed"); + root_package.config.get_package_specs().iter().for_each(|spec| { + if spec.in_source { + match &module.source_type { + SourceType::SourceFile(SourceFile { + implementation: Implementation { path, .. }, + .. + }) => { + let source = helpers::get_source_file_from_rescript_file( + &std::path::Path::new(&package.path).join(path), + &root_package.config.get_suffix(spec), + ); + let destination = helpers::get_source_file_from_rescript_file( + &std::path::Path::new(&package.get_build_path()).join(path), + &root_package.config.get_suffix(spec), + ); + + if source.exists() { + let _ = + std::fs::copy(&source, &destination).expect("copying source file failed"); + } + } + _ => (), } } - _ => (), - } + }); if helpers::contains_ascii_characters(&err) { if package.is_pinned_dep || package.is_local_dep { diff --git a/src/build/packages.rs b/src/build/packages.rs index d458ccc7..d85f93f7 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -67,6 +67,14 @@ pub fn get_build_path(canonical_path: &str) -> String { format!("{}/lib/bs", canonical_path) } +pub fn get_js_path(canonical_path: &str) -> String { + format!("{}/lib/js", canonical_path) +} + +pub fn get_es6_path(canonical_path: &str) -> String { + format!("{}/lib/es6", canonical_path) +} + pub fn get_ocaml_build_path(canonical_path: &str) -> String { format!("{}/lib/ocaml", canonical_path) } @@ -80,6 +88,14 @@ impl Package { get_build_path(&self.path) } + pub fn get_js_path(&self) -> String { + get_js_path(&self.path) + } + + pub fn get_es6_path(&self) -> String { + get_es6_path(&self.path) + } + pub fn get_mlmap_path(&self) -> String { self.get_build_path() + "/" @@ -494,17 +510,19 @@ pub fn get_source_files( }; let path_dir = Path::new(&source.dir); - if (build_dev_deps && type_ == &Some("dev".to_string())) || type_ != &Some("dev".to_string()) { - match read_folders(filter, package_dir, path_dir, recurse) { + match (build_dev_deps, type_) { + (false, Some(type_)) if type_ == "dev" => (), + _ => match read_folders(filter, package_dir, path_dir, recurse) { Ok(files) => map.extend(files), + Err(_e) => log::error!( "Could not read folder: {:?}. Specified in dependency: {}, located {:?}...", path_dir.to_path_buf().into_os_string(), package_name, package_dir ), - } - } + }, + }; map } @@ -594,8 +612,48 @@ pub fn parse_packages(build_state: &mut BuildState) { } let build_path_abs = package.get_build_path(); let bs_build_path = package.get_ocaml_build_path(); - helpers::create_build_path(&build_path_abs); - helpers::create_build_path(&bs_build_path); + helpers::create_path(&build_path_abs); + helpers::create_path(&bs_build_path); + let root_config = build_state + .get_package(&build_state.root_config_name) + .expect("cannot find root config"); + + root_config.config.get_package_specs().iter().for_each(|spec| { + if !spec.in_source { + // we don't want to calculate this if we don't have out of source specs + // we do this twice, but we almost never have multiple package specs + // so this optimization is less important + let relative_dirs: AHashSet = match &package.source_files { + Some(source_files) => source_files + .keys() + .map(|source_file| { + Path::new(source_file) + .parent() + .expect("parent dir not found") + .to_owned() + }) + .collect(), + _ => AHashSet::new(), + }; + if spec.is_common_js() { + helpers::create_path(&package.get_js_path()); + relative_dirs.iter().for_each(|path_buf| { + helpers::create_path_for_path(&Path::join( + &PathBuf::from(package.get_js_path()), + path_buf, + )) + }) + } else { + helpers::create_path(&package.get_es6_path()); + relative_dirs.iter().for_each(|path_buf| { + helpers::create_path_for_path(&Path::join( + &PathBuf::from(package.get_es6_path()), + path_buf, + )) + }) + } + } + }); package.namespace.to_suffix().iter().for_each(|namespace| { // generate the mlmap "AST" file for modules that have a namespace configured diff --git a/src/build/parse.rs b/src/build/parse.rs index 023a3294..b4fddd72 100644 --- a/src/build/parse.rs +++ b/src/build/parse.rs @@ -325,9 +325,7 @@ fn generate_ast( ); // generate the dir of the ast_path (it mirrors the source file dir) - helpers::create_build_path( - &(package.get_build_path() + "/" + &ast_path.parent().unwrap().to_string_lossy()), - ); + helpers::create_path(&(package.get_build_path() + "/" + &ast_path.parent().unwrap().to_string_lossy())); /* Create .ast */ let result = if let Some(res_to_ast) = Some( diff --git a/src/build/read_compile_state.rs b/src/build/read_compile_state.rs index 27fd1a7c..4bfc3e29 100644 --- a/src/build/read_compile_state.rs +++ b/src/build/read_compile_state.rs @@ -103,7 +103,9 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState { last_modified: last_modified.to_owned(), ast_file_path, is_root: *package_is_root, - suffix: root_package.config.get_suffix(), + suffix: root_package + .config + .get_suffix(root_package.config.get_package_specs().first().unwrap()), }, ); let _ = ast_rescript_file_locations.insert(res_file_path); diff --git a/src/config.rs b/src/config.rs index 53e13535..7bcbda2e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -111,6 +111,27 @@ pub struct PackageSpec { pub suffix: Option, } +impl PackageSpec { + pub fn get_out_of_source_dir(&self) -> String { + match self.module.as_str() { + "commonjs" => "js", + _ => "es6", + } + .to_string() + } + + pub fn is_common_js(&self) -> bool { + match self.module.as_str() { + "commonjs" => true, + _ => false, + } + } + + pub fn get_suffix(&self) -> Option { + self.suffix.to_owned() + } +} + #[derive(Deserialize, Debug, Clone)] #[serde(untagged)] pub enum Error { @@ -182,13 +203,13 @@ pub struct Config { pub suffix: Option, #[serde(rename = "pinned-dependencies")] pub pinned_dependencies: Option>, - #[serde(rename = "bs-dependencies")] + #[serde(rename = "dependencies", alias = "bs-dependencies")] pub bs_dependencies: Option>, - #[serde(rename = "bs-dev-dependencies")] + #[serde(rename = "bs-dev-dependencies", alias = "dev-dependencies")] pub bs_dev_dependencies: Option>, #[serde(rename = "ppx-flags")] pub ppx_flags: Option>>, - #[serde(rename = "bsc-flags")] + #[serde(rename = "bsc-flags", alias = "compiler-flags")] pub bsc_flags: Option>>, pub reason: Option, pub namespace: Option, @@ -417,36 +438,29 @@ impl Config { } } - pub fn get_module(&self) -> String { - match &self.package_specs { - Some(OneOrMore::Single(PackageSpec { module, .. })) => module.to_string(), - Some(OneOrMore::Multiple(vec)) => match vec.first() { - Some(PackageSpec { module, .. }) => module.to_string(), - None => "commonjs".to_string(), - }, - _ => "commonjs".to_string(), + pub fn get_gentype_arg(&self) -> Vec { + match &self.gentype_config { + Some(_) => vec!["-bs-gentype".to_string()], + None => vec![], } } - pub fn get_suffix(&self) -> String { - match &self.package_specs { - Some(OneOrMore::Single(PackageSpec { suffix, .. })) => suffix.to_owned(), - Some(OneOrMore::Multiple(vec)) => match vec.first() { - Some(PackageSpec { suffix, .. }) => suffix.to_owned(), - None => None, - }, - - _ => None, + pub fn get_package_specs(&self) -> Vec { + match self.package_specs.clone() { + None => vec![PackageSpec { + module: "commonjs".to_string(), + in_source: true, + suffix: Some(".js".to_string()), + }], + Some(OneOrMore::Single(spec)) => vec![spec], + Some(OneOrMore::Multiple(vec)) => vec, } - .or(self.suffix.to_owned()) - .unwrap_or(".js".to_string()) } - pub fn get_gentype_arg(&self) -> Vec { - match &self.gentype_config { - Some(_) => vec!["-bs-gentype".to_string()], - None => vec![], - } + pub fn get_suffix(&self, spec: &PackageSpec) -> String { + spec.get_suffix() + .or(self.suffix.clone()) + .unwrap_or(".js".to_string()) } } @@ -468,8 +482,11 @@ mod tests { "#; let config = serde_json::from_str::(json).unwrap(); - assert_eq!(config.get_suffix(), ".mjs"); - assert_eq!(config.get_module(), "es6"); + let specs = config.get_package_specs(); + assert_eq!(specs.len(), 1); + let spec = specs.first().unwrap(); + assert_eq!(spec.module, "es6"); + assert_eq!(config.get_suffix(spec), ".mjs"); } #[test] @@ -590,6 +607,134 @@ mod tests { ); } + #[test] + fn test_get_suffix() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs" + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!( + config.get_suffix(&config.get_package_specs().first().unwrap()), + ".mjs" + ); + } + + #[test] + fn test_dependencies() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs", + "bs-dependencies": [ "@testrepo/main" ] + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!(config.bs_dependencies, Some(vec!["@testrepo/main".to_string()])); + } + + #[test] + fn test_dependencies_alias() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs", + "dependencies": [ "@testrepo/main" ] + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!(config.bs_dependencies, Some(vec!["@testrepo/main".to_string()])); + } + + #[test] + fn test_dev_dependencies() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs", + "bs-dev-dependencies": [ "@testrepo/main" ] + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!( + config.bs_dev_dependencies, + Some(vec!["@testrepo/main".to_string()]) + ); + } + + #[test] + fn test_dev_dependencies_alias() { + let json = r#" + { + "name": "testrepo", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ], + "suffix": ".mjs", + "dev-dependencies": [ "@testrepo/main" ] + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!( + config.bs_dev_dependencies, + Some(vec!["@testrepo/main".to_string()]) + ); + } + #[test] fn test_check_if_rescript11_or_higher() { assert_eq!(check_if_rescript11_or_higher("11.0.0"), Ok(true)); diff --git a/src/helpers.rs b/src/helpers.rs index e6d40961..c4a73430 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -139,13 +139,17 @@ pub fn contains_ascii_characters(str: &str) -> bool { false } -pub fn create_build_path(build_path: &str) { +pub fn create_path(path: &str) { fs::DirBuilder::new() .recursive(true) - .create(PathBuf::from(build_path.to_string())) + .create(PathBuf::from(path.to_string())) .unwrap(); } +pub fn create_path_for_path(path: &Path) { + fs::DirBuilder::new().recursive(true).create(path).unwrap(); +} + pub fn get_bsc(root_path: &str, workspace_root: Option) -> String { let subfolder = match (std::env::consts::OS, std::env::consts::ARCH) { ("macos", "aarch64") => "darwinarm64", diff --git a/src/main.rs b/src/main.rs index 4e4d4ad2..3ff47687 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,7 +118,7 @@ fn main() -> Result<()> { std::process::exit(1) } lock::Lock::Aquired(_) => match command { - Command::Clean => build::clean::clean(&folder, show_progress, args.bsc_path), + Command::Clean => build::clean::clean(&folder, show_progress, args.bsc_path, args.dev), Command::Build => { match build::build( &filter, @@ -149,6 +149,7 @@ fn main() -> Result<()> { args.after_build, args.create_sourcedirs, args.dev, + args.bsc_path, ); Ok(()) diff --git a/src/watcher.rs b/src/watcher.rs index 3846e2f9..6b99db75 100644 --- a/src/watcher.rs +++ b/src/watcher.rs @@ -54,9 +54,17 @@ async fn async_watch( after_build: Option, create_sourcedirs: bool, build_dev_deps: bool, + bsc_path: Option, ) -> notify::Result<()> { - let mut build_state = build::initialize_build(None, filter, show_progress, path, None, build_dev_deps) - .expect("Can't initialize build"); + let mut build_state = build::initialize_build( + None, + filter, + show_progress, + path, + bsc_path.clone(), + build_dev_deps, + ) + .expect("Can't initialize build"); let mut needs_compile_type = CompileType::Incremental; // create a mutex to capture if ctrl-c was pressed let ctrlc_pressed = Arc::new(Mutex::new(false)); @@ -91,6 +99,20 @@ async fn async_watch( } for event in events { + // if there is a file named rewatch.lock in the events path, we can quit the watcher + if let Some(_) = event.paths.iter().find(|path| path.ends_with("rewatch.lock")) { + match event.kind { + EventKind::Remove(_) => { + if show_progress { + println!("\nExiting... (lockfile removed)"); + } + clean::cleanup_after_build(&build_state); + return Ok(()); + } + _ => (), + } + } + let paths = event .paths .iter() @@ -214,9 +236,15 @@ async fn async_watch( } CompileType::Full => { let timing_total = Instant::now(); - build_state = - build::initialize_build(None, filter, show_progress, path, None, build_dev_deps) - .expect("Can't initialize build"); + build_state = build::initialize_build( + None, + filter, + show_progress, + path, + bsc_path.clone(), + build_dev_deps, + ) + .expect("Can't initialize build"); let _ = build::incremental_build( &mut build_state, None, @@ -260,6 +288,7 @@ pub fn start( after_build: Option, create_sourcedirs: bool, build_dev_deps: bool, + bsc_path: Option, ) { futures::executor::block_on(async { let queue = Arc::new(FifoQueue::>::new()); @@ -280,6 +309,7 @@ pub fn start( after_build, create_sourcedirs, build_dev_deps, + bsc_path, ) .await { diff --git a/sync_upstream.sh b/sync_upstream.sh new file mode 100755 index 00000000..b58c312e --- /dev/null +++ b/sync_upstream.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +rm -rf testrepo/node_modules +cp -rf . ../rescript/rewatch +cd testrepo +yarn install +cd .. +rm -rf ../rescript/rewatch/.git +rm -rf ../rescript/rewatch/docs +rm -rf ../rescript/rewatch/.github +cd ../rescript/rewatch +rm .tmuxinator.yaml +rm package.json +rm release.sh +rm sync_upstream.sh +# reset yarn.lock to the most recent commited version +git checkout -- testrepo/yarn.lock +cd testrepo +yarn install diff --git a/testrepo/package.json b/testrepo/package.json index c09461a3..7f50287c 100644 --- a/testrepo/package.json +++ b/testrepo/package.json @@ -11,6 +11,9 @@ "packages/with-dev-deps" ] }, + "dependencies": { + "rescript": "11.1.4" + }, "scripts": { "build": "../target/release/rewatch build .", "build:rescript": "rescript build -with-deps", diff --git a/testrepo/packages/dep01/package.json b/testrepo/packages/dep01/package.json index e60d8095..9209fbba 100644 --- a/testrepo/packages/dep01/package.json +++ b/testrepo/packages/dep01/package.json @@ -12,7 +12,6 @@ "author": "", "license": "MIT", "dependencies": { - "@testrepo/dep02": "*", - "rescript": "*" + "@testrepo/dep02": "*" } } diff --git a/testrepo/packages/dep02/package.json b/testrepo/packages/dep02/package.json index 8dc05ab0..ee0c5676 100644 --- a/testrepo/packages/dep02/package.json +++ b/testrepo/packages/dep02/package.json @@ -10,8 +10,5 @@ "rescript" ], "author": "", - "license": "MIT", - "dependencies": { - "rescript": "*" - } + "license": "MIT" } diff --git a/testrepo/packages/main/package.json b/testrepo/packages/main/package.json index 5bf3dca7..10954e07 100644 --- a/testrepo/packages/main/package.json +++ b/testrepo/packages/main/package.json @@ -12,7 +12,6 @@ "author": "", "license": "MIT", "dependencies": { - "@testrepo/dep01": "*", - "rescript": "*" + "@testrepo/dep01": "*" } } diff --git a/testrepo/packages/new-namespace/package.json b/testrepo/packages/new-namespace/package.json index 1fb4e262..8e30fe2b 100644 --- a/testrepo/packages/new-namespace/package.json +++ b/testrepo/packages/new-namespace/package.json @@ -5,8 +5,5 @@ "rescript" ], "author": "", - "license": "MIT", - "dependencies": { - "rescript": "*" - } + "license": "MIT" } diff --git a/testrepo/yarn.lock b/testrepo/yarn.lock index cc5d0224..eca95713 100644 --- a/testrepo/yarn.lock +++ b/testrepo/yarn.lock @@ -7,7 +7,7 @@ resolved "https://registry.yarnpkg.com/@rescript/core/-/core-1.6.1.tgz#159670c94d64a2b8236f46be2bf09a007b1ece08" integrity sha512-vyb5k90ck+65Fgui+5vCja/mUfzKaK3kOPT4Z6aAJdHLH1eljEi1zKhXroCiCtpNLSWp8k4ulh1bdB5WS0hvqA== -rescript@*: +rescript@*, rescript@11.1.4: version "11.1.4" resolved "https://registry.yarnpkg.com/rescript/-/rescript-11.1.4.tgz#9a42ebc4fc5363707e39cef5b3188160b63bee42" integrity sha512-0bGU0bocihjSC6MsE3TMjHjY0EUpchyrREquLS8VsZ3ohSMD+VHUEwimEfB3kpBI1vYkw3UFZ3WD8R28guz/Vw== diff --git a/tests/compile.sh b/tests/compile.sh index 65d599bb..dd39bf5b 100755 --- a/tests/compile.sh +++ b/tests/compile.sh @@ -91,7 +91,7 @@ else exit 1 fi -rewatch clean &> /dev/null +rewatch clean --dev &> /dev/null file_count=$(find ./packages/with-dev-deps -name *.mjs | wc -l) if [ "$file_count" -eq 0 ]; then diff --git a/tests/lock.sh b/tests/lock.sh index 019e6166..4522a7a0 100755 --- a/tests/lock.sh +++ b/tests/lock.sh @@ -14,20 +14,21 @@ else fi exit_watcher() { - # we need to kill the parent process (rewatch) - kill $(pgrep -P $!); + # Try to find child process, if not found just kill the process directly + rm lib/rewatch.lock } -rewatch watch &>/dev/null & -success "Watcher Started" +rewatch_bg watch > /dev/null 2>&1 & sleep 1 -if rewatch watch 2>&1 | grep 'Could not start Rewatch:' &> /dev/null; +if rewatch watch | grep 'Could not start Rewatch:' &> /dev/null; then + # rm output.txt success "Lock is correctly set" exit_watcher else + # rm output.txt error "Not setting lock correctly" exit_watcher exit 1 @@ -36,7 +37,7 @@ fi sleep 1 touch tmp.txt -rewatch watch &> tmp.txt & +rewatch_bg watch > tmp.txt 2>&1 & success "Watcher Started" sleep 1 @@ -51,4 +52,4 @@ else exit_watcher fi -rm tmp.txt +rm tmp.txt \ No newline at end of file diff --git a/tests/suite-ci.sh b/tests/suite-ci.sh index bcec5677..60970745 100755 --- a/tests/suite-ci.sh +++ b/tests/suite-ci.sh @@ -2,23 +2,35 @@ # Make sure we are in the right directory cd $(dirname $0) +# Get rewatch executable location from the first argument or use default +if [ -n "$1" ]; then + REWATCH_EXECUTABLE="$1" +else + REWATCH_EXECUTABLE="../target/release/rewatch" +fi +export REWATCH_EXECUTABLE + source ./utils.sh -bold "Check if build exists" -if test -f ../target/release/rewatch; -then - success "Build exists" -else - error "Build does not exist. Exiting..." - exit 1 -fi +bold "Rescript version" +(cd ../testrepo && ./node_modules/.bin/rescript -v) + +# we need to reset the yarn.lock and package.json to the original state +# so there is not diff in git. The CI will install new ReScript package +bold "Reset package.json and yarn.lock" +git checkout ../testrepo/yarn.lock &> /dev/null +git checkout ../testrepo/package.json &> /dev/null +success "Reset package.json and yarn.lock" bold "Make sure the testrepo is clean" -if git diff --exit-code ../testrepo &> /dev/null; +if git diff --exit-code ../testrepo &> diff.txt; then + rm diff.txt success "Testrepo has no changes" else error "Testrepo is not clean to start with" + cat diff.txt + rm diff.txt exit 1 fi diff --git a/tests/utils.sh b/tests/utils.sh index e02b2808..f2ee211d 100644 --- a/tests/utils.sh +++ b/tests/utils.sh @@ -3,7 +3,8 @@ overwrite() { echo -e "\r\033[1A\033[0K$@"; } success() { echo -e "- ✅ \033[32m$1\033[0m"; } error() { echo -e "- 🛑 \033[31m$1\033[0m"; } bold() { echo -e "\033[1m$1\033[0m"; } -rewatch() { RUST_BACKTRACE=1 ../target/release/rewatch --no-timing=true $@; } +rewatch() { RUST_BACKTRACE=1 $REWATCH_EXECUTABLE --no-timing=true $@; } +rewatch_bg() { RUST_BACKTRACE=1 nohup $REWATCH_EXECUTABLE --no-timing=true $@; } replace() { if [[ $OSTYPE == 'darwin'* ]]; diff --git a/tests/watch.sh b/tests/watch.sh index 20546d86..e3a24c09 100755 --- a/tests/watch.sh +++ b/tests/watch.sh @@ -13,10 +13,10 @@ fi exit_watcher() { # we need to kill the parent process (rewatch) - kill $(pgrep -P $!); + rm lib/rewatch.lock } -rewatch watch &>/dev/null & +rewatch_bg watch > /dev/null 2>&1 & success "Watcher Started" echo 'Js.log("added-by-test")' >> ./packages/main/src/Main.res