Skip to content

Commit ce7094e

Browse files
Merge pull request #12 from react-R/scaffold-multiple-npm-libs
Scaffold multiple npm libs
2 parents 7b819a3 + 1076bb3 commit ce7094e

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

R/scaffold.R

Lines changed: 13 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,7 +28,7 @@ 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)
@@ -42,11 +41,13 @@ scaffoldReactWidget <- function(name, npmPkg = NULL, edit = interactive()){
4241
message("To build JavaScript run: yarn run webpack --mode=development")
4342
}
4443

45-
toDepJSON <- function(npmPkg) {
46-
if (is.null(npmPkg)) {
44+
toDepJSON <- function(npmPkgs) {
45+
if (is.null(npmPkgs)) {
4746
""
47+
} else if (!length(names(npmPkgs))) {
48+
stop("Must specify npm package names in the names attributes of npmPkgs")
4849
} else {
49-
sprintf('"%s": "%s"', npmPkg$name, npmPkg$version)
50+
paste0(sprintf('"%s": "%s"', names(npmPkgs), npmPkgs), collapse = ",\n")
5051
}
5152
}
5253

@@ -106,8 +107,8 @@ addWidgetYAML <- function(name, edit){
106107
if (edit) fileEdit(file_)
107108
}
108109

109-
addPackageJSON <- function(npmPkg) {
110-
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))
111112
if (!file.exists('package.json')) {
112113
cat(tpl, file = 'package.json')
113114
message('Created package.json')

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/reactR)](https://cran.r-project.org/package=reactR)
77
[![Travis-CI Build
88
Status](https://travis-ci.org/react-R/reactR.svg?branch=master)](https://travis-ci.org/react-R/reactR)
9+
[![Slack Status](https://reactr-slackin.herokuapp.com/badge.svg)](https://reactr-slackin.herokuapp.com/)
910

1011
`reactR` provides a set of convenience functions for using
1112
[`React`](https://facebook.github.io/react) in `R` with `htmlwidget`
@@ -92,3 +93,7 @@ We welcome contributors and would love your participation. Please note
9293
that this project is released with a [Contributor Code of
9394
Conduct](CONDUCT.md). By participating in this project you agree to
9495
abide by the terms.
96+
97+
## Community
98+
99+
We operate a [Slack](https://slack.com) workspace you are [more than welcome to join](http://reactr-slackin.herokuapp.com/).

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)