k_builder-package_json 0.0.2 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.vscode/settings.json +6 -0
- data/k_builder-package_json.gemspec +1 -1
- data/lib/k_builder/package_json.rb +3 -2
- data/lib/k_builder/package_json/configuration.rb +31 -0
- data/lib/k_builder/package_json/package_builder.rb +274 -0
- data/lib/k_builder/package_json/version.rb +1 -1
- metadata +20 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1955feb6eddedfbaf20e83f6192b80f8646b2a1a7c0331fb77d7b7a083382897
|
4
|
+
data.tar.gz: 3e40c92dc419c5d4c3187fe0841aea1f5bec8212b8050b596c6077b4ddf8f618
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1668eacd9e2778018298d89cf569fd2fb8e27cb2c43ae35abac7a6964a8f47ab0ecd4205aeee1daad7bb81696275c6b7d1bf914f550f7a35d14af116b81dbe6b
|
7
|
+
data.tar.gz: 649800ddb5bf9f33ea33d9079ca381d4c07b4f9b9a5092417e847325c3c0c535ece2b4f2c49c33f791cb66616c53091d89d73728bccf0024642cdfdd250b627a
|
data/.rubocop.yml
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'k_builder'
|
3
4
|
require 'k_builder/package_json/version'
|
5
|
+
require 'k_builder/package_json/configuration'
|
6
|
+
require 'k_builder/package_json/package_builder'
|
4
7
|
|
5
8
|
module KBuilder
|
6
9
|
module PackageJson
|
7
10
|
# raise KBuilder::PackageJson::Error, 'Sample message'
|
8
11
|
class Error < StandardError; end
|
9
|
-
|
10
|
-
# Your code goes here...
|
11
12
|
end
|
12
13
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Attach configuration to the KBuilder module
|
4
|
+
module KBuilder
|
5
|
+
module PackageJson
|
6
|
+
PackageGroup = Struct.new(:key, :description, :package_names)
|
7
|
+
|
8
|
+
# Configuration class
|
9
|
+
class Configuration < BaseConfiguration
|
10
|
+
attach_to(self, KBuilder::BaseConfiguration, :package_json)
|
11
|
+
|
12
|
+
attr_accessor :package_groups
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
super()
|
16
|
+
@package_groups = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_package_group(key, description, package_names)
|
20
|
+
package_groups[key] = PackageGroup.new(key, description, package_names)
|
21
|
+
end
|
22
|
+
|
23
|
+
def default_package_groups
|
24
|
+
set_package_group('webpack', 'Webpack V5', %w[webpack webpack-cli webpack-dev-server])
|
25
|
+
set_package_group('swc', 'SWC Transpiler', %w[@swc/cli @swc/core swc-loader])
|
26
|
+
set_package_group('babel', 'Babel Transpiler', %w[@babel/core @babel/cli @babel/preset-env babel-loader])
|
27
|
+
set_package_group('typescript', 'Typescript', %w[typescript ts-loader])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,274 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KBuilder
|
4
|
+
module PackageJson
|
5
|
+
# Configuration currently comes from KBuilder and stores template folders and target folders if configured
|
6
|
+
class PackageBuilder < KBuilder::Builder
|
7
|
+
SETTER_METHODS = %w[dependency_type].freeze
|
8
|
+
|
9
|
+
# In memory representation of the package.json file that is being created
|
10
|
+
attr_writer :package
|
11
|
+
|
12
|
+
def initialize(configuration = nil)
|
13
|
+
super(configuration)
|
14
|
+
|
15
|
+
set_package_file('package.json')
|
16
|
+
set_dependency_type(:development)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Return an array of symbols to represent the fluent setter methods in this builder.
|
20
|
+
def builder_setter_methods
|
21
|
+
SETTER_METHODS
|
22
|
+
end
|
23
|
+
|
24
|
+
# ----------------------------------------------------------------------
|
25
|
+
# Fluent interface
|
26
|
+
# ----------------------------------------------------------------------
|
27
|
+
|
28
|
+
# Change context to production, new dependencies will be for production
|
29
|
+
def production
|
30
|
+
set_dependency_type :production
|
31
|
+
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
# Change context to development, new dependencies will be for development
|
36
|
+
def development
|
37
|
+
set_dependency_type :development
|
38
|
+
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
# Init an NPN package
|
43
|
+
#
|
44
|
+
# run npm init -y
|
45
|
+
#
|
46
|
+
# Note: npm init does not support --silent operation
|
47
|
+
def npm_init
|
48
|
+
rc 'npm init -y'
|
49
|
+
|
50
|
+
load
|
51
|
+
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
# Space separated list of packages
|
56
|
+
def npm_install(packages, options: nil)
|
57
|
+
npm_add_or_install(packages, parse_options(options))
|
58
|
+
|
59
|
+
self
|
60
|
+
end
|
61
|
+
alias npm_i npm_install
|
62
|
+
|
63
|
+
def npm_add(packages, options: nil)
|
64
|
+
npm_add_or_install(packages, parse_options(options, '--package-lock-only --no-package-lock'))
|
65
|
+
|
66
|
+
self
|
67
|
+
end
|
68
|
+
alias npm_a npm_add
|
69
|
+
|
70
|
+
def npm_add_group(key, options: nil)
|
71
|
+
group = get_group(key)
|
72
|
+
|
73
|
+
puts "Adding #{group.description}"
|
74
|
+
|
75
|
+
npm_add(group.package_names, options: options)
|
76
|
+
|
77
|
+
self
|
78
|
+
end
|
79
|
+
alias npm_ag npm_add_group
|
80
|
+
|
81
|
+
# Add a group of NPN packages which get defined in configuration
|
82
|
+
def npm_install_group(key, options: nil)
|
83
|
+
group = get_group(key)
|
84
|
+
|
85
|
+
puts "Installing #{group.description}"
|
86
|
+
|
87
|
+
npm_install(group.package_names, options: options)
|
88
|
+
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
# Load the existing package.json into memory
|
93
|
+
#
|
94
|
+
# ToDo: Would be useful to record the update timestamp on the package
|
95
|
+
# so that we only load if the in memory package is not the latest.
|
96
|
+
#
|
97
|
+
# The reason this can happen, is because external tools such are
|
98
|
+
# npm install are updating the package.json and after this happens
|
99
|
+
# we need to call load, but if there is any bug in the code we may
|
100
|
+
# for get to load, or we may load multiple times.
|
101
|
+
def load
|
102
|
+
raise KBuilder::PackageJson::Error, 'package.json does not exist' unless File.exist?(package_file)
|
103
|
+
|
104
|
+
puts 'loading...'
|
105
|
+
|
106
|
+
content = File.read(package_file)
|
107
|
+
@package = JSON.parse(content)
|
108
|
+
|
109
|
+
self
|
110
|
+
end
|
111
|
+
|
112
|
+
# Write the package.json file
|
113
|
+
def write
|
114
|
+
puts 'writing...'
|
115
|
+
|
116
|
+
content = JSON.pretty_generate(@package)
|
117
|
+
|
118
|
+
File.write(package_file, content)
|
119
|
+
|
120
|
+
self
|
121
|
+
end
|
122
|
+
|
123
|
+
# Remove a script reference by key
|
124
|
+
def remove_script(key)
|
125
|
+
load
|
126
|
+
|
127
|
+
@package['scripts']&.delete(key)
|
128
|
+
|
129
|
+
write
|
130
|
+
|
131
|
+
self
|
132
|
+
end
|
133
|
+
|
134
|
+
# Add a script with key and value (command line to run)
|
135
|
+
def add_script(key, value)
|
136
|
+
load
|
137
|
+
|
138
|
+
@package['scripts'][key] = value
|
139
|
+
|
140
|
+
write
|
141
|
+
|
142
|
+
self
|
143
|
+
end
|
144
|
+
|
145
|
+
# ----------------------------------------------------------------------
|
146
|
+
# Attributes: Think getter/setter
|
147
|
+
#
|
148
|
+
# The following getter/setters can be referenced both inside and outside
|
149
|
+
# of the fluent builder fluent API. They do not implement the fluent
|
150
|
+
# interface unless prefixed by set_.
|
151
|
+
#
|
152
|
+
# set_: Only setters with the prefix _set are considered fluent.
|
153
|
+
# ----------------------------------------------------------------------
|
154
|
+
|
155
|
+
# Package
|
156
|
+
# ----------------------------------------------------------------------
|
157
|
+
|
158
|
+
# Load the package.json into a memory as object
|
159
|
+
def package
|
160
|
+
return @package if defined? @package
|
161
|
+
|
162
|
+
load
|
163
|
+
|
164
|
+
@package
|
165
|
+
end
|
166
|
+
|
167
|
+
# Package.set
|
168
|
+
# ----------------------------------------------------------------------
|
169
|
+
|
170
|
+
# Set a property value in the package
|
171
|
+
def set(key, value)
|
172
|
+
load
|
173
|
+
|
174
|
+
@package[key] = value
|
175
|
+
|
176
|
+
write
|
177
|
+
|
178
|
+
self
|
179
|
+
end
|
180
|
+
|
181
|
+
# Dependency option
|
182
|
+
# ----------------------------------------------------------------------
|
183
|
+
|
184
|
+
# Getter for dependency option
|
185
|
+
def dependency_option
|
186
|
+
dependency_type == :development ? '-D' : '-P'
|
187
|
+
end
|
188
|
+
|
189
|
+
# Dependency type
|
190
|
+
# ----------------------------------------------------------------------
|
191
|
+
|
192
|
+
# Getter for dependency type
|
193
|
+
def dependency_type
|
194
|
+
hash['dependency_type']
|
195
|
+
end
|
196
|
+
|
197
|
+
# Target folder
|
198
|
+
# ----------------------------------------------------------------------
|
199
|
+
|
200
|
+
# Fluent setter for target folder
|
201
|
+
def set_package_file(value)
|
202
|
+
self.package_file = value
|
203
|
+
|
204
|
+
self
|
205
|
+
end
|
206
|
+
|
207
|
+
# Setter for target folder
|
208
|
+
def package_file=(_value)
|
209
|
+
hash['package_file'] = File.join(target_folder, 'package.json')
|
210
|
+
end
|
211
|
+
|
212
|
+
# Getter for target folder
|
213
|
+
def package_file
|
214
|
+
hash['package_file']
|
215
|
+
end
|
216
|
+
|
217
|
+
# -----------------------------------
|
218
|
+
# Helpers
|
219
|
+
# -----------------------------------
|
220
|
+
|
221
|
+
def parse_options(options = nil, required_options = nil)
|
222
|
+
options = [] if options.nil?
|
223
|
+
options = options.split if options.is_a?(String)
|
224
|
+
options.reject(&:empty?)
|
225
|
+
|
226
|
+
required_options = [] if required_options.nil?
|
227
|
+
required_options = required_options.split if required_options.is_a?(String)
|
228
|
+
|
229
|
+
options | required_options
|
230
|
+
end
|
231
|
+
|
232
|
+
def options_any?(options, *any_options)
|
233
|
+
(options & any_options).any?
|
234
|
+
end
|
235
|
+
|
236
|
+
def execute(command)
|
237
|
+
puts "RUN: #{command}"
|
238
|
+
rc command
|
239
|
+
load
|
240
|
+
end
|
241
|
+
|
242
|
+
def npm_add_or_install(packages, options)
|
243
|
+
# if -P or -D is not in the options then use the current builder dependency option
|
244
|
+
options.push dependency_option unless options_any?(options, '-P', '-D')
|
245
|
+
packages = packages.join(' ') if packages.is_a?(Array)
|
246
|
+
command = "npm install #{options.join(' ')} #{packages}"
|
247
|
+
execute command
|
248
|
+
end
|
249
|
+
|
250
|
+
# # Debug method to open the package file in vscode
|
251
|
+
# # ToDo: Maybe remove
|
252
|
+
# def vscode
|
253
|
+
# puts "cd #{output_path}"
|
254
|
+
# puts package_file
|
255
|
+
# rc "code #{package_file}"
|
256
|
+
# end
|
257
|
+
|
258
|
+
# private
|
259
|
+
|
260
|
+
# This is all wrong, but useful for now
|
261
|
+
def context
|
262
|
+
KBuilder.data.to_struct(hash)
|
263
|
+
end
|
264
|
+
|
265
|
+
def get_group(key)
|
266
|
+
group = context.package_json.package_groups[key]
|
267
|
+
|
268
|
+
raise KBuilder::PackageJson::Error, "unknown package group: #{key}" if group.nil?
|
269
|
+
|
270
|
+
group
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: k_builder-package_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Cruwys
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-03-
|
12
|
-
dependencies:
|
11
|
+
date: 2021-03-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: k_builder
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.19
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.19
|
13
27
|
description: " KBuilder-PackageJson provides various fluent builders and code generators
|
14
28
|
for building package.json file\n"
|
15
29
|
email:
|
@@ -22,6 +36,7 @@ files:
|
|
22
36
|
- ".gitignore"
|
23
37
|
- ".rspec"
|
24
38
|
- ".rubocop.yml"
|
39
|
+
- ".vscode/settings.json"
|
25
40
|
- CODE_OF_CONDUCT.md
|
26
41
|
- Gemfile
|
27
42
|
- Guardfile
|
@@ -39,6 +54,8 @@ files:
|
|
39
54
|
- hooks/update-version
|
40
55
|
- k_builder-package_json.gemspec
|
41
56
|
- lib/k_builder/package_json.rb
|
57
|
+
- lib/k_builder/package_json/configuration.rb
|
58
|
+
- lib/k_builder/package_json/package_builder.rb
|
42
59
|
- lib/k_builder/package_json/version.rb
|
43
60
|
homepage: http://appydave.com/gems/k-builder-package-json
|
44
61
|
licenses:
|