Skip to content

Commit e0465db

Browse files
Merge pull request #11 from react-R/add-node_modules-rbuildignore
Scaffolding: Add node_modules to .Rbuildignore, creating it if necessary
2 parents e670956 + ce7094e commit e0465db

File tree

5 files changed

+28
-22
lines changed

5 files changed

+28
-22
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Suggests:
3838
rmarkdown,
3939
shiny,
4040
V8,
41-
knitr
41+
knitr,
42+
usethis
4243
RoxygenNote: 6.1.1
4344
VignetteBuilder: knitr

R/scaffold.R

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
#' R package.
55
#'
66
#' @param name Name of widget
7-
#' @param npmPkg Optional \href{https://npmjs.com/}{NPM} package upon which this
8-
#' widget is based, a named list with two elements: \code{name} and
9-
#' \href{https://docs.npmjs.com/files/package.json#dependencies}{version}. If
10-
#' you specify this parameter the package will be added to the
11-
#' \code{dependency} section of the generated \code{package.json}.
7+
#' @param npmPkgs Optional \href{https://npmjs.com/}{NPM} packages upon which
8+
#' this widget is based which will be used to populate \code{package.json}.
9+
#' Should be a named list of names to
10+
#' \href{https://docs.npmjs.com/files/package.json#dependencies}{versions}.
1211
#' @param edit Automatically open the widget's JavaScript source file after
1312
#' creating the scaffolding.
1413
#'
1514
#' @note This function must be executed from the root directory of the package
1615
#' you wish to add the widget to.
1716
#'
1817
#' @export
19-
scaffoldReactWidget <- function(name, npmPkg = NULL, edit = interactive()){
18+
scaffoldReactWidget <- function(name, npmPkgs = NULL, edit = interactive()){
2019
if (!file.exists('DESCRIPTION')){
2120
stop(
2221
"You need to create a package to house your widget first!",
@@ -29,19 +28,26 @@ scaffoldReactWidget <- function(name, npmPkg = NULL, edit = interactive()){
2928
package <- read.dcf('DESCRIPTION')[[1,"Package"]]
3029
addWidgetConstructor(name, package, edit)
3130
addWidgetYAML(name, edit)
32-
addPackageJSON(toDepJSON(npmPkg))
31+
addPackageJSON(toDepJSON(npmPkgs))
3332
addWebpackConfig(name)
3433
addWidgetJS(name, edit)
3534
addExampleApp(name)
35+
36+
usethis::use_build_ignore(c("node_modules", "srcjs"))
37+
usethis::use_git_ignore(c("node_modules", "srcjs"))
38+
lapply(c("htmltools", "htmlwidgets", "reactR"), usethis::use_package)
39+
3640
message("To install dependencies from npm run: yarn install")
3741
message("To build JavaScript run: yarn run webpack --mode=development")
3842
}
3943

40-
toDepJSON <- function(npmPkg) {
41-
if (is.null(npmPkg)) {
44+
toDepJSON <- function(npmPkgs) {
45+
if (is.null(npmPkgs)) {
4246
""
47+
} else if (!length(names(npmPkgs))) {
48+
stop("Must specify npm package names in the names attributes of npmPkgs")
4349
} else {
44-
sprintf('"%s": "%s"', npmPkg$name, npmPkg$version)
50+
paste0(sprintf('"%s": "%s"', names(npmPkgs), npmPkgs), collapse = ",\n")
4551
}
4652
}
4753

@@ -101,8 +107,8 @@ addWidgetYAML <- function(name, edit){
101107
if (edit) fileEdit(file_)
102108
}
103109

104-
addPackageJSON <- function(npmPkg) {
105-
tpl <- renderTemplate(slurp('templates/widget_package.json.txt'), list(npmPkg = npmPkg))
110+
addPackageJSON <- function(npmPkgs) {
111+
tpl <- renderTemplate(slurp('templates/widget_package.json.txt'), list(npmPkgs = npmPkgs))
106112
if (!file.exists('package.json')) {
107113
cat(tpl, file = 'package.json')
108114
message('Created package.json')

inst/templates/widget_package.json.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"dependencies": {
4-
${npmPkg}
4+
${npmPkgs}
55
},
66
"devDependencies": {
77
"webpack": "^4.27.1",

man/scaffoldReactWidget.Rd

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/intro_htmlwidgets.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ usethis::create_package("~/sparklines")
4949
# Inject the widget templating
5050
withr::with_dir(
5151
"~/sparklines",
52-
reactR::scaffoldReactWidget("sparklines", list(name = "react-sparklines", version = "^1.7.0"))
52+
reactR::scaffoldReactWidget("sparklines", list("react-sparklines" = "^1.7.0"))
5353
)
5454
```
5555

5656
## Building and installing
5757

5858
### Building the JavaScript
5959

60-
The next step is to navigate to the newly-created `sparklines` directory in your terminal. Then, run the following commands:
60+
The next step is to navigate to the newly-created `sparklines` project and run the following R commands:
6161

6262
```{r}
6363
system("yarn install")

0 commit comments

Comments
 (0)