-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathscaffold_widget.R
More file actions
101 lines (92 loc) · 2.66 KB
/
scaffold_widget.R
File metadata and controls
101 lines (92 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#' Create implementation scaffolding for a React.js-based HTML widget
#'
#' Add the minimal code required to implement a React.js-based HTML widget to an
#' R package.
#'
#' @param name Name of widget
#' @param npmPkgs Optional \href{https://www.npmjs.com/}{NPM} packages upon which
#' this widget is based which will be used to populate \code{package.json}.
#' Should be a named list of names to
#' \href{https://docs.npmjs.com/files/package.json/}{versions}.
#' @param edit Automatically open the widget's JavaScript source file after
#' creating the scaffolding.
#'
#' @note This function must be executed from the root directory of the package
#' you wish to add the widget to.
#'
#' @export
scaffoldReactWidget <- function(name, npmPkgs = NULL, edit = interactive()){
assertNameValid(name)
package <- getPackage()
addWidgetConstructor(name, package, edit)
addWidgetYAML(name, edit)
addPackageJSON(toDepJSON(npmPkgs))
addWebpackConfig(name)
addWidgetJS(name, edit)
addExampleApp(name)
usethis::use_build_ignore(c("node_modules", "srcjs", "app.R", "package.json", "webpack.config.js", "yarn.lock"))
usethis::use_git_ignore(c("node_modules"))
lapply(c("htmltools", "htmlwidgets", "reactR"), usethis::use_package)
message("To install dependencies from npm run: yarn install")
message("To build JavaScript run: yarn run webpack --mode=development")
}
addWidgetConstructor <- function(name, package, edit){
file <- renderFile(
sprintf("R/%s.R", name),
"templates/widget_r.txt",
"boilerplate for widget constructor",
list(
name = name,
package = package,
capName = capitalize(name)
)
)
if (edit) fileEdit(file)
}
addWidgetYAML <- function(name, edit){
file <- renderFile(
sprintf('inst/htmlwidgets/%s.yaml', name),
"templates/widget_yaml.txt",
"boilerplate for widget dependencies"
)
if (edit) fileEdit(file)
}
addPackageJSON <- function(npmPkgs) {
renderFile(
'package.json',
'templates/package.json.txt',
'project metadata',
list(npmPkgs = npmPkgs)
)
}
addWebpackConfig <- function(name) {
renderFile(
'webpack.config.js',
'templates/webpack.config.js.txt',
'webpack configuration',
list(
name = name,
outputPath = 'inst/htmlwidgets'
)
)
}
addWidgetJS <- function(name, edit){
file <- renderFile(
sprintf('srcjs/%s.jsx', name),
'templates/widget_js.txt',
'boilerplate for widget JavaScript bindings',
list(name = name)
)
if (edit) fileEdit(file)
}
addExampleApp <- function(name) {
renderFile(
'app.R',
'templates/widget_app.R.txt',
'example app',
list(
name = name,
capName = capitalize(name)
)
)
}