Refactor to inject multiple properties

This commit is contained in:
Yoann Chaudet
2022-07-19 17:03:44 -07:00
parent ad121920a0
commit 3d2f0e5994
8 changed files with 99 additions and 78 deletions
+31 -23
View File
@@ -3,9 +3,9 @@ const espree = require('espree')
const core = require('@actions/core')
/*
Parse a JavaScript based configuration file and initialize or update a given property.
Parse a JavaScript based configuration file and inject arbitrary key/value in it.
This is used to make sure most static site generators can automatically handle
Pages's path based routing.
Pages's path based routing (and work).
Supported configuration initializations:
@@ -30,19 +30,11 @@ Supported configuration initializations:
class ConfigParser {
// Ctor
// - configurationFile: path to the configuration file
// - propertyName: name of the property to update (or set)
// - propertyValue: value of the property to update (or set)
// - blankConfigurationFile: a blank configuration file to use if non was previously found
constructor({
configurationFile,
propertyName,
propertyValue,
blankConfigurationFile
}) {
// Save fields
constructor({configurationFile, blankConfigurationFile, properties}) {
// Save field
this.configurationFile = configurationFile
this.propertyName = propertyName
this.propertyValue = propertyValue
this.properties = properties
// If the configuration file does not exist, initialize it with the blank configuration file
if (!fs.existsSync(this.configurationFile)) {
@@ -143,22 +135,32 @@ class ConfigParser {
// Generate a (nested) property declaration.
// - properties: list of properties to generate
// - startIndex: the index at which to start in the declaration
// - propertyValue: the value of the property
//
// Return a nested property declaration as a string.
getPropertyDeclaration(properties, startIndex) {
getPropertyDeclaration(properties, startIndex, propertyValue) {
if (startIndex === properties.length - 1) {
return `${properties[startIndex]}: "${this.propertyValue}"`
return `${properties[startIndex]}: ${JSON.stringify(propertyValue)}`
} else {
return (
`${properties[startIndex]}: {` +
this.getPropertyDeclaration(properties, startIndex + 1) +
this.getPropertyDeclaration(properties, startIndex + 1, propertyValue) +
'}'
)
}
}
// Parse a configuration file and try to inject Pages settings in it.
inject() {
// Inject all properties into the configuration
injectAll() {
for (var [propertyName, propertyValue] of Object.entries(this.properties)) {
this.inject(propertyName, propertyValue)
}
}
// Inject an arbitrary property into the configuration
// - propertyName: the name of the property (may use . to target nested objects)
// - propertyValue: the value of the property
inject(propertyName, propertyValue) {
// Logging
core.info(`Parsing configuration:\n${this.configuration}`)
@@ -179,7 +181,7 @@ class ConfigParser {
// A property may be nested in the configuration file. Split the property name with `.`
// then walk the configuration object one property at a time.
var depth = 0
const properties = this.propertyName.split('.')
const properties = propertyName.split('.')
var lastNode = configurationObject
while (1) {
// Find the node for the current property
@@ -203,7 +205,7 @@ class ConfigParser {
if (lastNode.type === 'ObjectExpression') {
this.configuration =
this.configuration.slice(0, lastNode.range[0]) +
`"${this.propertyValue}"` +
JSON.stringify(propertyValue) +
this.configuration.slice(lastNode.range[1])
}
@@ -212,7 +214,7 @@ class ConfigParser {
else {
this.configuration =
this.configuration.slice(0, lastNode.range[0]) +
`"${this.propertyValue}"` +
JSON.stringify(propertyValue) +
this.configuration.slice(lastNode.range[1])
}
}
@@ -220,7 +222,11 @@ class ConfigParser {
// Create nested properties in the configuration file
else {
// Build the declaration to inject
const declaration = this.getPropertyDeclaration(properties, depth)
const declaration = this.getPropertyDeclaration(
properties,
depth,
propertyValue
)
// The last node identified is an object expression, so do the assignment
if (lastNode.type === 'ObjectExpression') {
@@ -249,7 +255,9 @@ class ConfigParser {
else {
this.configuration =
this.configuration.slice(0, lastNode.range[0]) +
'{' + declaration + '}' +
'{' +
declaration +
'}' +
this.configuration.slice(lastNode.range[1])
}
}