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' )
0 commit comments