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