Skip to content

Commit

Permalink
feat: add rescript project
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickard Natt och Dag committed Nov 16, 2020
1 parent c2ad3a6 commit d92a612
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 8 deletions.
68 changes: 61 additions & 7 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ serde_json = "1.0"
structopt = "0.3.13"
handlebars = "3.5.1"
dialoguer = "0.7.1"
copy_dir = "0.1.2"
colored = "2.0.0"
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,19 @@ Environment variables are set inside GitHub repository settings -> Secrets.
```
supreme github-actions
```

### ReScript

Create a [ReScript](http://rescript-lang.org/) project with
[Tailwind](https://tailwindcss.com/).

```
supreme rescript my-project-name
```

You'll get some instructions after the project has been created

- `cd my-project-name`
- `npm install`
- `npm start` (start the compiler)
- `npm run server` (in another terminal window, start development server)
1 change: 1 addition & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod add;
pub mod github_actions;
pub mod rescript;
53 changes: 53 additions & 0 deletions src/commands/rescript.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::utils::helpers;
use colored::*;
use copy_dir::copy_dir;
use handlebars::Handlebars;
use helpers::Result;
use serde_json::json;
use std::{env, fs};

fn update_file(dir: &str, filename: &str) -> Result<()> {
let reg = Handlebars::new();
let file = &fs::read(format!("{}/{}", dir, filename))?;
let file: String = String::from_utf8_lossy(file).parse()?;
let output = reg.render_template(&file, &json!({ "name": dir }))?;

fs::write(&format!("{}/{}", dir, filename), output)?;

Ok(())
}

pub fn run(name: String) -> Result<()> {
let rescript_template = concat!(env!("CARGO_MANIFEST_DIR"), "/src/templates/rescript");
copy_dir(rescript_template, &name)?;

// Update files with template names
update_file(&name, "package.json")?;
update_file(&name, "bsconfig.json")?;
update_file(&name, "README.md")?;
update_file(&name, "public/index.html")?;

println!(
"
{title}
--------------------
Install dependencies
* cd {name}
* {install}
Start the app by opening two terminal tabs and
running the following commands:
* {compiler} (start compiler)
* {server} (start development server on port 3000)
",
title = "ReScript setup completed".green(),
name = &name.green(),
compiler = "npm start".blue(),
server = "npm run server".blue(),
install = "npm install".blue(),
);

Ok(())
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod commands;
mod utils;

use commands::{add, github_actions};
use commands::{add, github_actions, rescript};
use structopt::StructOpt;
use utils::helpers::Result;

Expand Down Expand Up @@ -31,6 +31,9 @@ enum Cli {
#[structopt(long = "no-npm", short = "n")]
no_npm: bool,
},

/// Create a ReScript project
Rescript { name: String },
}

fn main() -> Result<()> {
Expand All @@ -43,6 +46,7 @@ fn main() -> Result<()> {
Cli::Add(AddCommand::Nvm) => add::nvm()?,
Cli::Add(AddCommand::Prettier) => add::prettier()?,
Cli::GithubActions { no_npm } => github_actions::run(no_npm)?,
Cli::Rescript { name } => rescript::run(name)?,
};

Ok(())
Expand Down
47 changes: 47 additions & 0 deletions src/templates/rescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# {{ name }}

## Run Project

```sh
npm install
npm start
# in another tab
npm run server
```

When both processes are running, open a browser at http://localhost:3000

## Build for Production

```sh
npm run clean
npm run build
npm run webpack:production
```

This will replace the development artifact `build/Index.js` for an optimized
version as well as copy `public/index.html` into `build/`. You can then deploy the
contents of the `build` directory (`index.html` and `Index.js`).

If you make use of routing (via `ReasonReact.Router` or similar logic) ensure
that server-side routing handles your routes or that 404's are directed back to
`index.html` (which is how the dev server is set up).

**To enable dead code elimination**, change `bsconfig.json`'s `package-specs`
`module` from `"commonjs"` to `"es6"`. Then re-run the above 2 commands. This
will allow Webpack to remove unused code.
'

## Build to Now

This project includes building straight to [Now](https://zeit.co/) after Travis has validated
tests and created a release. There are some steps that need to be taken to enable the setup.

1. Get a token from your [Now dashboard](https://zeit.co/account/tokens)
1. Set the token as `NOW_TOKEN` in Travis
1. Uncomment the Now build steps in `.travis.yml`
1. Add `now-build` to `package.json` scripts. Now runs this script during it build process:

```
"now-build": "npm run build && npm run webpack:production"
```
26 changes: 26 additions & 0 deletions src/templates/rescript/bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "{{ name }}",
"reason": {
"react-jsx": 3
},
"sources": [
{
"dir": "src",
"subdirs": true
}
],
"package-specs": [
{
"module": "commonjs",
"in-source": true
}
],
"suffix": ".bs.js",
"namespace": true,
"bs-dependencies": ["reason-react"],
"refmt": 3,
"warnings": {
"number": "+A-9-40-42-48",
"error": "+A-3-40-42-44-102"
}
}
39 changes: 39 additions & 0 deletions src/templates/rescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "{{ name }}",
"version": "0.1.0",
"scripts": {
"build": "bsb -make-world",
"start": "cross-env BS_WATCH_CLEAR=true bsb -make-world -w",
"clean": "bsb -clean-world",
"test": "is-ci test:ci test:watch",
"test:ci": "jest",
"test:watch": "jest --watch",
"webpack": "webpack -w",
"webpack:production": "NODE_ENV=production webpack",
"server": "webpack serve"
},
"keywords": [
"ReScript"
],
"author": "",
"license": "MIT",
"dependencies": {
"react": "17.0.1",
"react-dom": "17.0.1",
"reason-react": "0.9.1"
},
"devDependencies": {
"autoprefixer": "10.0.2",
"bs-platform": "8.3.3",
"cross-env": "7.0.2",
"css-loader": "5.0.1",
"html-webpack-plugin": "4.5.0",
"is-ci-cli": "2.1.2",
"postcss-loader": "4.0.4",
"style-loader": "2.0.0",
"tailwindcss": "1.9.6",
"webpack": "5.4.0",
"webpack-cli": "4.2.0",
"webpack-dev-server": "3.11.0"
}
}
3 changes: 3 additions & 0 deletions src/templates/rescript/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: [require("tailwindcss"), require("autoprefixer")],
};
Loading

0 comments on commit d92a612

Please sign in to comment.