class Configuration
Loads and validates Web Packages settings from a project's package.json file.
Definitions
def self.load(root)
Load the configuration for a project.
Signature
-
parameter
rootString | Pathname The project directory containing
package.json.-
returns
Configuration The validated project configuration.
-
raises
ConfigurationError If
package.jsonis missing, malformed or invalid.
Implementation
def self.load(root)
root = Pathname.new(root).expand_path
package_path = root + "package.json"
unless package_path.file?
raise ConfigurationError, "Could not find package.json in #{root}!"
end
package_json = JSON.parse(package_path.read)
new(root, package_json)
rescue JSON::ParserError => error
raise ConfigurationError, "Could not parse #{package_path}: #{error.message}"
end
def initialize(root, package_json)
Initialize a configuration from parsed package metadata.
Signature
-
parameter
rootString | Pathname The project root directory.
-
parameter
package_jsonHash The parsed contents of
package.json.-
raises
ConfigurationError If the configuration is invalid.
Implementation
def initialize(root, package_json)
@root = Pathname.new(root).expand_path
@package_json = package_json
unless @package_json.is_a?(Hash)
raise ConfigurationError, "package.json must contain an object!"
end
configuration = @package_json.fetch("web-packages", {})
unless configuration.is_a?(Hash)
raise ConfigurationError, "web-packages configuration must be an object!"
end
@package_root = expand_within_root(configuration.fetch("packageRoot", "node_modules"), "packageRoot")
@output = configuration.fetch("output", DEFAULT_OUTPUT)
@base = configuration.fetch("base", DEFAULT_BASE)
@packages = load_packages(configuration["packages"])
unless @base.is_a?(String) && @base.end_with?("/")
raise ConfigurationError, "web-packages base must be a string ending in '/'!"
end
output_path
end
attr :root
Signature
-
attribute
Pathname The expanded project root directory.
attr :package_json
Signature
-
attribute
Hash The parsed contents of
package.json.
attr :package_root
Signature
-
attribute
Pathname The directory containing installed Node.js packages.
attr :base
Signature
-
attribute
String The public URL prefix for static packages.
attr :packages
Signature
-
attribute
Hash(String, Package) The packages selected for static deployment.
def output_path(override = nil)
Resolve the configured output directory.
Signature
-
parameter
overrideString | Nil An optional project-relative output directory.
-
returns
Pathname The expanded output directory.
-
raises
ConfigurationError If the output directory escapes the project root.
Implementation
def output_path(override = nil)
expand_within_root(override || @output, "output", allow_root: false)
end