Skip to content

Commit

Permalink
Read --template= argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrikstrid committed Aug 6, 2019
1 parent 02e423e commit 50d31b4
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 69 deletions.
3 changes: 2 additions & 1 deletion npm-cli/src/Bindings.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type stats;
[@bs.send] external isFile: stats => bool = "";

[@bs.val] [@bs.module "process"] external cwd: unit => string = "cwd";
[@bs.val] [@bs.module "process"] external argv: array(string) = "argv";
[@bs.val] external scriptPath: string = "__dirname";
[@bs.val] [@bs.module "child_process"]
external exec: (string, (Js.Nullable.t(_), string, string) => unit) => unit =
Expand All @@ -17,4 +18,4 @@ external exec: (string, (Js.Nullable.t(_), string, string) => unit) => unit =
external download_git: (string, string, option('a) => unit) => unit =
"download-git-repo";

[@bs.module] external walk_sync: string => array(string) = "walk-sync";
[@bs.module] external walk_sync: string => array(string) = "walk-sync";
16 changes: 15 additions & 1 deletion npm-cli/src/Pesy.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 87 additions & 67 deletions npm-cli/src/Pesy.re
Original file line number Diff line number Diff line change
Expand Up @@ -24,79 +24,99 @@ let substituteTemplateValues = s =>
packageNameUpperCamelCase,
);

download_git("github:ulrikstrid/hello-reason-pesy", projectPath, error =>
switch (error) {
| Some(e) => Js.log(e)
| None =>
let files =
walk_sync(projectPath)
->Belt.Array.keep(file_or_dir => statSync(file_or_dir)->isFile);
// TODO: Move the template to the esy or esy-ocaml org
let template =
argv
->Belt.Array.keep(Js.String.includes("--template"))
->Belt.Array.get(0)
->Belt.Option.map(Js.String.replace("--template=", ""))
->Belt.Option.getWithDefault("github:ulrikstrid/hello-reason-pesy");

Belt.Array.forEach(
files,
file => {
let () = readFile(file)->substituteTemplateValues |> write(file);
let download_spinner =
Spinner.start("\x1b[2mDownloading template\x1b[0m " ++ template);
download_git(
template,
projectPath,
error => {
Spinner.stop(download_spinner);
switch (error) {
| Some(e) => Js.log(e)
| None =>
let setup_files_spinner =
Spinner.start("\x1b[2mSetting up template\x1b[0m " ++ template);
let files =
walk_sync(projectPath)
->Belt.Array.keep(file_or_dir => statSync(file_or_dir)->isFile);

renameSync(file, substituteTemplateValues(file));
},
);
Belt.Array.forEach(
files,
file => {
let () = readFile(file)->substituteTemplateValues |> write(file);

let libKebab = packageNameKebabSansScope;
let duneProjectFile = Path.(projectPath / "dune-project");
if (!exists(duneProjectFile)) {
write(
duneProjectFile,
spf({|(lang dune 1.2)
renameSync(file, substituteTemplateValues(file));
},
);

let libKebab = packageNameKebabSansScope;
let duneProjectFile = Path.(projectPath / "dune-project");
if (!exists(duneProjectFile)) {
write(
duneProjectFile,
spf({|(lang dune 1.2)
(name %s)
|}, libKebab),
);
};
);
};

let opamFileName = libKebab ++ ".opam";
let opamFile = Path.(projectPath / opamFileName);
if (!exists(opamFile)) {
write(opamFile, "");
};
let opamFileName = libKebab ++ ".opam";
let opamFile = Path.(projectPath / opamFileName);
if (!exists(opamFile)) {
write(opamFile, "");
};
Spinner.stop(setup_files_spinner);

let id = Spinner.start("\x1b[2mRunning\x1b[0m esy install");
exec("esy i", (e, stdout, stderr) => {
Spinner.stop(id);
if (Js.eqNullable(e, Js.Nullable.null)) {
let id =
Spinner.start(
"\x1b[2mRunning\x1b[0m esy pesy\x1b[2m and \x1b[0m building project dependencies",
);
exec("esy pesy", (e, stdout, stderr) => {
Spinner.stop(id);
if (Js.eqNullable(e, Js.Nullable.null)) {
let id = Spinner.start("\x1b[2mRunning\x1b[0m esy build");
exec("esy build", (e, stdout, stderr) => {
Spinner.stop(id);
if (Js.eqNullable(e, Js.Nullable.null)) {
Printf.printf("You may now run \x1b[32m'esy test'\x1b[0m\n");
} else {
Printf.printf(
"'esy build' \x1b[31mfailed.\x1b[0m Could not build project.\nLogs can be found in pesy.stdout.log and pesy.stderr.log\n",
);
write("pesy.stdout.log", stdout);
write("pesy.stderr.log", stderr);
};
});
} else {
Printf.printf(
"'esy pesy' \x1b[31mfailed.\x1b[0m Dune files could not be created.\n Logs can be found in pesy.stdout.log and pesy.stderr.log\n",
let id = Spinner.start("\x1b[2mRunning\x1b[0m esy install");
exec("esy i", (e, stdout, stderr) => {
Spinner.stop(id);
if (Js.eqNullable(e, Js.Nullable.null)) {
let id =
Spinner.start(
"\x1b[2mRunning\x1b[0m esy pesy\x1b[2m and \x1b[0m building project dependencies",
);
write("pesy.stdout.log", stdout);
write("pesy.stderr.log", stderr);
};
});
} else {
Printf.printf(
"'esy install' \x1b[31mfailed.\x1b[0m Dependencies could not be installed.\nLogs can be found in pesy.stdout.log and pesy.stderr.log\n",
);
write("pesy.stdout.log", stdout);
write("pesy.stderr.log", stderr);
};
});
}
exec("esy pesy", (e, stdout, stderr) => {
Spinner.stop(id);
if (Js.eqNullable(e, Js.Nullable.null)) {
let id = Spinner.start("\x1b[2mRunning\x1b[0m esy build");
exec("esy build", (e, stdout, stderr) => {
Spinner.stop(id);
if (Js.eqNullable(e, Js.Nullable.null)) {
Printf.printf(
"You may now run \x1b[32m'esy test'\x1b[0m\n",
);
} else {
Printf.printf(
"'esy build' \x1b[31mfailed.\x1b[0m Could not build project.\nLogs can be found in pesy.stdout.log and pesy.stderr.log\n",
);
write("pesy.stdout.log", stdout);
write("pesy.stderr.log", stderr);
};
});
} else {
Printf.printf(
"'esy pesy' \x1b[31mfailed.\x1b[0m Dune files could not be created.\n Logs can be found in pesy.stdout.log and pesy.stderr.log\n",
);
write("pesy.stdout.log", stdout);
write("pesy.stderr.log", stderr);
};
});
} else {
Printf.printf(
"'esy install' \x1b[31mfailed.\x1b[0m Dependencies could not be installed.\nLogs can be found in pesy.stdout.log and pesy.stderr.log\n",
);
write("pesy.stdout.log", stdout);
write("pesy.stderr.log", stderr);
};
});
};
},
);

0 comments on commit 50d31b4

Please sign in to comment.