mirror of
https://github.com/actions/configure-pages.git
synced 2026-04-03 16:22:24 +00:00
Merge pull request #14 from paper-spa/fallback
Rewrite the AST logic to be more complete
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
|
/src/fixtures/tmp
|
||||||
|
|
||||||
# Editors
|
# Editors
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|||||||
2
dist/gatsby.js
vendored
Normal file
2
dist/gatsby.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// Default Pages configuration for Gatsby
|
||||||
|
module.exports = {}
|
||||||
593
dist/index.js
vendored
593
dist/index.js
vendored
@@ -14000,107 +14000,6 @@ function plural(ms, msAbs, n, name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/***/ }),
|
|
||||||
|
|
||||||
/***/ 3259:
|
|
||||||
/***/ (function(module) {
|
|
||||||
|
|
||||||
void function(global) {
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
// ValueError :: String -> Error
|
|
||||||
function ValueError(message) {
|
|
||||||
var err = new Error(message);
|
|
||||||
err.name = 'ValueError';
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
// defaultTo :: a,a? -> a
|
|
||||||
function defaultTo(x, y) {
|
|
||||||
return y == null ? x : y;
|
|
||||||
}
|
|
||||||
|
|
||||||
// create :: Object -> String,*... -> String
|
|
||||||
function create(transformers) {
|
|
||||||
return function(template) {
|
|
||||||
var args = Array.prototype.slice.call(arguments, 1);
|
|
||||||
var idx = 0;
|
|
||||||
var state = 'UNDEFINED';
|
|
||||||
|
|
||||||
return template.replace(
|
|
||||||
/([{}])\1|[{](.*?)(?:!(.+?))?[}]/g,
|
|
||||||
function(match, literal, _key, xf) {
|
|
||||||
if (literal != null) {
|
|
||||||
return literal;
|
|
||||||
}
|
|
||||||
var key = _key;
|
|
||||||
if (key.length > 0) {
|
|
||||||
if (state === 'IMPLICIT') {
|
|
||||||
throw ValueError('cannot switch from ' +
|
|
||||||
'implicit to explicit numbering');
|
|
||||||
}
|
|
||||||
state = 'EXPLICIT';
|
|
||||||
} else {
|
|
||||||
if (state === 'EXPLICIT') {
|
|
||||||
throw ValueError('cannot switch from ' +
|
|
||||||
'explicit to implicit numbering');
|
|
||||||
}
|
|
||||||
state = 'IMPLICIT';
|
|
||||||
key = String(idx);
|
|
||||||
idx += 1;
|
|
||||||
}
|
|
||||||
var value = defaultTo('', lookup(args, key.split('.')));
|
|
||||||
|
|
||||||
if (xf == null) {
|
|
||||||
return value;
|
|
||||||
} else if (Object.prototype.hasOwnProperty.call(transformers, xf)) {
|
|
||||||
return transformers[xf](value);
|
|
||||||
} else {
|
|
||||||
throw ValueError('no transformer named "' + xf + '"');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function lookup(_obj, _path) {
|
|
||||||
var obj = _obj;
|
|
||||||
var path = _path;
|
|
||||||
if (!/^\d+$/.test(path[0])) {
|
|
||||||
path = ['0'].concat(path);
|
|
||||||
}
|
|
||||||
for (var idx = 0; idx < path.length; idx += 1) {
|
|
||||||
var key = path[idx];
|
|
||||||
obj = typeof obj[key] === 'function' ? obj[key]() : obj[key];
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
// format :: String,*... -> String
|
|
||||||
var format = create({});
|
|
||||||
|
|
||||||
// format.create :: Object -> String,*... -> String
|
|
||||||
format.create = create;
|
|
||||||
|
|
||||||
// format.extend :: Object,Object -> ()
|
|
||||||
format.extend = function(prototype, transformers) {
|
|
||||||
var $format = create(transformers);
|
|
||||||
prototype.format = function() {
|
|
||||||
var args = Array.prototype.slice.call(arguments);
|
|
||||||
args.unshift(this);
|
|
||||||
return $format.apply(global, args);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/* istanbul ignore else */
|
|
||||||
if (true) {
|
|
||||||
module.exports = format;
|
|
||||||
} else {}
|
|
||||||
|
|
||||||
}.call(this, this);
|
|
||||||
|
|
||||||
|
|
||||||
/***/ }),
|
/***/ }),
|
||||||
|
|
||||||
/***/ 9318:
|
/***/ 9318:
|
||||||
@@ -14531,206 +14430,275 @@ exports.debug = debug; // for test
|
|||||||
|
|
||||||
const fs = __nccwpck_require__(7147)
|
const fs = __nccwpck_require__(7147)
|
||||||
const espree = __nccwpck_require__(6910)
|
const espree = __nccwpck_require__(6910)
|
||||||
const format = __nccwpck_require__(3259)
|
|
||||||
const core = __nccwpck_require__(2186)
|
const core = __nccwpck_require__(2186)
|
||||||
|
|
||||||
// Parse the AST
|
/*
|
||||||
|
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 (and work).
|
||||||
|
|
||||||
|
Supported configuration initializations:
|
||||||
|
|
||||||
|
(1) Default export:
|
||||||
|
|
||||||
|
export default {
|
||||||
|
// configuration object here
|
||||||
|
}
|
||||||
|
|
||||||
|
(2) Direct module export:
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
// configuration object here
|
||||||
|
}
|
||||||
|
|
||||||
|
(3) Indirect module export:
|
||||||
|
|
||||||
|
const config = // configuration object here
|
||||||
|
module.exports = config
|
||||||
|
*/
|
||||||
|
|
||||||
|
class ConfigParser {
|
||||||
|
// Ctor
|
||||||
|
// - configurationFile: path to the configuration file
|
||||||
|
// - blankConfigurationFile: a blank configuration file to use if non was previously found
|
||||||
|
constructor({configurationFile, blankConfigurationFile, properties}) {
|
||||||
|
// Save field
|
||||||
|
this.configurationFile = configurationFile
|
||||||
|
this.properties = properties
|
||||||
|
|
||||||
|
// If the configuration file does not exist, initialize it with the blank configuration file
|
||||||
|
if (!fs.existsSync(this.configurationFile)) {
|
||||||
|
core.info('Use default blank configuration')
|
||||||
|
const blankConfiguration = fs.readFileSync(blankConfigurationFile, 'utf8')
|
||||||
|
fs.writeFileSync(this.configurationFile, blankConfiguration, {
|
||||||
|
encoding: 'utf8'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the configuration file
|
||||||
|
core.info('Read existing configuration')
|
||||||
|
this.configuration = fs.readFileSync(this.configurationFile, 'utf8')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the configuration object in an AST.
|
||||||
|
// Look for a default export, a direct module export or an indirect module
|
||||||
|
// export (in that order).
|
||||||
|
//
|
||||||
|
// Return the configuration object or null.
|
||||||
|
findConfigurationObject(ast) {
|
||||||
|
// Try to find a default export
|
||||||
|
var defaultExport = ast.body.find(
|
||||||
|
node =>
|
||||||
|
node.type === 'ExportDefaultDeclaration' &&
|
||||||
|
node.declaration.type === 'ObjectExpression'
|
||||||
|
)
|
||||||
|
if (defaultExport) {
|
||||||
|
core.info('Found configuration object in default export declaration')
|
||||||
|
return defaultExport.declaration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to find a module export
|
||||||
|
var moduleExport = ast.body.find(
|
||||||
|
node =>
|
||||||
|
node.type === 'ExpressionStatement' &&
|
||||||
|
node.expression.type === 'AssignmentExpression' &&
|
||||||
|
node.expression.operator === '=' &&
|
||||||
|
node.expression.left.type === 'MemberExpression' &&
|
||||||
|
node.expression.left.object.type === 'Identifier' &&
|
||||||
|
node.expression.left.object.name === 'module' &&
|
||||||
|
node.expression.left.property.type === 'Identifier' &&
|
||||||
|
node.expression.left.property.name === 'exports'
|
||||||
|
)
|
||||||
|
|
||||||
|
// Direct module export
|
||||||
|
if (
|
||||||
|
moduleExport &&
|
||||||
|
moduleExport.expression.right.type === 'ObjectExpression'
|
||||||
|
) {
|
||||||
|
core.info('Found configuration object in direct module export')
|
||||||
|
return moduleExport.expression.right
|
||||||
|
}
|
||||||
|
|
||||||
|
// Indirect module export
|
||||||
|
else if (
|
||||||
|
moduleExport &&
|
||||||
|
moduleExport.expression.right.type === 'Identifier'
|
||||||
|
) {
|
||||||
|
const identifierName = moduleExport && moduleExport.expression.right.name
|
||||||
|
const identifierDefinition = ast.body.find(
|
||||||
|
node =>
|
||||||
|
node.type === 'VariableDeclaration' &&
|
||||||
|
node.declarations.length == 1 &&
|
||||||
|
node.declarations[0].type === 'VariableDeclarator' &&
|
||||||
|
node.declarations[0].id.type === 'Identifier' &&
|
||||||
|
node.declarations[0].id.name === identifierName &&
|
||||||
|
node.declarations[0].init.type === 'ObjectExpression'
|
||||||
|
)
|
||||||
|
if (identifierDefinition) {
|
||||||
|
core.info('Found configuration object in indirect module export')
|
||||||
|
return identifierDefinition.declarations[0].init
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No configuration object found
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find a property with a given name on a given object.
|
||||||
|
//
|
||||||
|
// Return the matching property or null.
|
||||||
|
findProperty(object, name) {
|
||||||
|
// Try to find a property matching a given name
|
||||||
|
const property =
|
||||||
|
object.type === 'ObjectExpression' &&
|
||||||
|
object.properties.find(
|
||||||
|
node => node.key.type === 'Identifier' && node.key.name === name
|
||||||
|
)
|
||||||
|
|
||||||
|
// Return the property's value (if found) or null
|
||||||
|
if (property) {
|
||||||
|
return property.value
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, propertyValue) {
|
||||||
|
if (startIndex === properties.length - 1) {
|
||||||
|
return `${properties[startIndex]}: ${JSON.stringify(propertyValue)}`
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
`${properties[startIndex]}: {` +
|
||||||
|
this.getPropertyDeclaration(properties, startIndex + 1, propertyValue) +
|
||||||
|
'}'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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}`)
|
||||||
|
|
||||||
|
// Parse the AST out of the configuration file
|
||||||
const espreeOptions = {
|
const espreeOptions = {
|
||||||
ecmaVersion: 6,
|
ecmaVersion: 6,
|
||||||
sourceType: 'module',
|
sourceType: 'module',
|
||||||
range: true
|
range: true
|
||||||
}
|
}
|
||||||
|
const ast = espree.parse(this.configuration, espreeOptions)
|
||||||
|
|
||||||
class ConfigParser {
|
// Find the configuration object
|
||||||
constructor(staticSiteConfig) {
|
var configurationObject = this.findConfigurationObject(ast)
|
||||||
this.pathPropertyNuxt = `router: {\n base: '{0}'\n }`
|
if (!configurationObject) {
|
||||||
this.pathPropertyNext = `basePath: '{0}'`
|
throw 'Could not find a configuration object in the configuration file'
|
||||||
this.pathPropertyGatsby = `pathPrefix: '{0}'`
|
|
||||||
this.configskeleton = `export default {\n {0}\n}`
|
|
||||||
this.staticSiteConfig = staticSiteConfig
|
|
||||||
this.config = fs.existsSync(this.staticSiteConfig.filePath)
|
|
||||||
? fs.readFileSync(this.staticSiteConfig.filePath, 'utf8')
|
|
||||||
: null
|
|
||||||
this.validate()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
validate() {
|
// A property may be nested in the configuration file. Split the property name with `.`
|
||||||
if (!this.config) {
|
// then walk the configuration object one property at a time.
|
||||||
core.info(`original raw configuration was empty:\n${this.config}`)
|
var depth = 0
|
||||||
core.info('Generating a default configuration to start from...')
|
const properties = propertyName.split('.')
|
||||||
|
var lastNode = configurationObject
|
||||||
|
while (1) {
|
||||||
|
// Find the node for the current property
|
||||||
|
var propertyNode = this.findProperty(lastNode, properties[depth])
|
||||||
|
|
||||||
// Update the `config` property with a default configuration file
|
// Update last node
|
||||||
this.config = this.generateConfigFile()
|
if (propertyNode != null) {
|
||||||
}
|
lastNode = propertyNode
|
||||||
|
depth++
|
||||||
}
|
}
|
||||||
|
|
||||||
generateConfigFile() {
|
// Exit when exiting the current configuration object
|
||||||
switch (this.staticSiteConfig.type) {
|
if (propertyNode == null || depth >= properties.length) {
|
||||||
case 'nuxt':
|
|
||||||
return format(
|
|
||||||
this.configskeleton,
|
|
||||||
format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
|
|
||||||
)
|
|
||||||
break
|
break
|
||||||
case 'next':
|
|
||||||
return format(
|
|
||||||
this.configskeleton,
|
|
||||||
format(this.pathPropertyNext, this.staticSiteConfig.newPath)
|
|
||||||
)
|
|
||||||
break
|
|
||||||
case 'gatsby':
|
|
||||||
return format(
|
|
||||||
this.configskeleton,
|
|
||||||
format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
|
|
||||||
)
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw 'Unknown config type'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generateConfigProperty() {
|
// If the configuration file is defining the property we are after, update it.
|
||||||
switch (this.staticSiteConfig.type) {
|
if (depth == properties.length) {
|
||||||
case 'nuxt':
|
// The last node identified is an object expression, so do the assignment
|
||||||
return format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
|
if (lastNode.type === 'ObjectExpression') {
|
||||||
break
|
this.configuration =
|
||||||
case 'next':
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
return format(this.pathPropertyNext, this.staticSiteConfig.newPath)
|
JSON.stringify(propertyValue) +
|
||||||
break
|
this.configuration.slice(lastNode.range[1])
|
||||||
case 'gatsby':
|
}
|
||||||
return format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
|
|
||||||
break
|
// A misc object was found in the configuration file (e.g. an array, a string, a boolean,
|
||||||
default:
|
// a number, etc.), just replace the whole range by our declaration
|
||||||
throw 'Unknown config type'
|
else {
|
||||||
|
this.configuration =
|
||||||
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
|
JSON.stringify(propertyValue) +
|
||||||
|
this.configuration.slice(lastNode.range[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parse() {
|
// Create nested properties in the configuration file
|
||||||
core.info(`original configuration:\n${this.config}`)
|
else {
|
||||||
const ast = espree.parse(this.config, espreeOptions)
|
// Build the declaration to inject
|
||||||
|
const declaration = this.getPropertyDeclaration(
|
||||||
// Find the default export declaration node
|
properties,
|
||||||
var exportNode = ast.body.find(node => node.type === 'ExpressionStatement')
|
depth,
|
||||||
if (exportNode) {
|
propertyValue
|
||||||
var property = this.getPropertyModuleExport(exportNode)
|
|
||||||
} else {
|
|
||||||
exportNode = ast.body.find(
|
|
||||||
node => node.type === 'ExportDefaultDeclaration'
|
|
||||||
)
|
|
||||||
if (!exportNode) throw 'Unable to find default export'
|
|
||||||
var property = this.getPropertyExportDefault(exportNode)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (property) {
|
|
||||||
switch (this.staticSiteConfig.type) {
|
|
||||||
case 'nuxt':
|
|
||||||
this.parseNuxt(property)
|
|
||||||
break
|
|
||||||
case 'next':
|
|
||||||
case 'gatsby':
|
|
||||||
this.parseNextGatsby(property)
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw 'Unknown config type'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
core.info(`parsed configuration:\n${this.config}`)
|
|
||||||
fs.writeFileSync(this.staticSiteConfig.filePath, this.config)
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
getPropertyModuleExport(exportNode) {
|
|
||||||
var propertyNode = exportNode.expression.right.properties.find(
|
|
||||||
node =>
|
|
||||||
node.key.type === 'Identifier' &&
|
|
||||||
node.key.name === this.staticSiteConfig.pathName
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!propertyNode) {
|
// The last node identified is an object expression, so do the assignment
|
||||||
core.info(
|
if (lastNode.type === 'ObjectExpression') {
|
||||||
'Unable to find property, insert it : ' +
|
// The object is blank (no properties) so replace the whole range by a new object containing the declaration
|
||||||
this.staticSiteConfig.pathName
|
if (lastNode.properties.length === 0) {
|
||||||
)
|
this.configuration =
|
||||||
if (exportNode.expression.right.properties.length > 0) {
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
this.config =
|
'{' +
|
||||||
this.config.slice(
|
declaration +
|
||||||
0,
|
'}' +
|
||||||
exportNode.expression.right.properties[0].range[0]
|
this.configuration.slice(lastNode.range[1])
|
||||||
) +
|
|
||||||
this.generateConfigProperty() +
|
|
||||||
',\n' +
|
|
||||||
this.config.slice(exportNode.expression.right.properties[0].range[0])
|
|
||||||
core.info('new config = \n' + this.config)
|
|
||||||
} else {
|
|
||||||
this.config =
|
|
||||||
this.config.slice(0, exportNode.expression.right.range[0] + 1) +
|
|
||||||
'\n ' +
|
|
||||||
this.generateConfigProperty() +
|
|
||||||
'\n' +
|
|
||||||
this.config.slice(exportNode.expression.right.range[1] - 1)
|
|
||||||
core.info('new config = \n' + this.config)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return propertyNode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getPropertyExportDefault(exportNode) {
|
// The object contains other properties, prepend our new one at the beginning
|
||||||
var propertyNode = exportNode.declaration.properties.find(
|
else {
|
||||||
node =>
|
this.configuration =
|
||||||
node.key.type === 'Identifier' &&
|
this.configuration.slice(0, lastNode.properties[0].range[0]) +
|
||||||
node.key.name === this.staticSiteConfig.pathName
|
declaration +
|
||||||
)
|
',' +
|
||||||
|
this.configuration.slice(lastNode.properties[0].range[0])
|
||||||
if (!propertyNode) {
|
|
||||||
core.info(
|
|
||||||
'Unable to find property, insert it ' + this.staticSiteConfig.pathName
|
|
||||||
)
|
|
||||||
if (exportNode.declaration.properties.length > 0) {
|
|
||||||
this.config =
|
|
||||||
this.config.slice(0, exportNode.declaration.properties[0].range[0]) +
|
|
||||||
this.generateConfigProperty() +
|
|
||||||
',\n' +
|
|
||||||
this.config.slice(exportNode.declaration.properties[0].range[0])
|
|
||||||
core.info('new config = \n' + this.config)
|
|
||||||
} else {
|
|
||||||
this.config =
|
|
||||||
this.config.slice(0, exportNode.declaration.range[0] + 1) +
|
|
||||||
'\n ' +
|
|
||||||
this.generateConfigProperty() +
|
|
||||||
'\n' +
|
|
||||||
this.config.slice(exportNode.declaration.range[1] - 1)
|
|
||||||
core.info('new config = \n' + this.config)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return propertyNode
|
// A misc object was found in the configuration file (e.g. an array, a string, a boolean,
|
||||||
}
|
// a number, etc.), just replace the whole range by our declaration
|
||||||
|
else {
|
||||||
parseNuxt(propertyNode) {
|
this.configuration =
|
||||||
// Find the base node
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
if (propertyNode && propertyNode.value.type === 'ObjectExpression') {
|
'{' +
|
||||||
var baseNode = propertyNode.value.properties.find(
|
declaration +
|
||||||
node =>
|
'}' +
|
||||||
node.key.type === 'Identifier' &&
|
this.configuration.slice(lastNode.range[1])
|
||||||
node.key.name === this.staticSiteConfig.subPathName
|
|
||||||
) //'base')
|
|
||||||
if (baseNode) {
|
|
||||||
// Swap the base value by a hardcoded string and print it
|
|
||||||
this.config =
|
|
||||||
this.config.slice(0, baseNode.value.range[0]) +
|
|
||||||
`'${this.staticSiteConfig.newPath}'` +
|
|
||||||
this.config.slice(baseNode.value.range[1])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parseNextGatsby(pathNode) {
|
// Logging
|
||||||
if (pathNode) {
|
core.info(`Writing new configuration:\n${this.configuration}`)
|
||||||
this.config =
|
|
||||||
this.config.slice(0, pathNode.value.range[0]) +
|
// Finally write the new configuration in the file
|
||||||
`'${this.staticSiteConfig.newPath}'` +
|
fs.writeFileSync(this.configurationFile, this.configuration, {
|
||||||
this.config.slice(pathNode.value.range[1])
|
encoding: 'utf8'
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -14753,7 +14721,8 @@ function getRequiredVars() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function getContext() {
|
// Return the context object
|
||||||
|
function getContext() {
|
||||||
const requiredVars = getRequiredVars()
|
const requiredVars = getRequiredVars()
|
||||||
for (const variable in requiredVars) {
|
for (const variable in requiredVars) {
|
||||||
if (requiredVars[variable] === undefined) {
|
if (requiredVars[variable] === undefined) {
|
||||||
@@ -14764,6 +14733,8 @@ module.exports = function getContext() {
|
|||||||
return requiredVars
|
return requiredVars
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = {getContext}
|
||||||
|
|
||||||
|
|
||||||
/***/ }),
|
/***/ }),
|
||||||
|
|
||||||
@@ -14810,7 +14781,7 @@ module.exports = enablePages
|
|||||||
|
|
||||||
const core = __nccwpck_require__(2186)
|
const core = __nccwpck_require__(2186)
|
||||||
const axios = __nccwpck_require__(6545)
|
const axios = __nccwpck_require__(6545)
|
||||||
const setPagesPath = __nccwpck_require__(4770)
|
const {setPagesPath} = __nccwpck_require__(4770)
|
||||||
|
|
||||||
async function getPagesBaseUrl({
|
async function getPagesBaseUrl({
|
||||||
repositoryNwo,
|
repositoryNwo,
|
||||||
@@ -14854,44 +14825,58 @@ module.exports = getPagesBaseUrl
|
|||||||
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
|
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
|
||||||
|
|
||||||
const core = __nccwpck_require__(2186)
|
const core = __nccwpck_require__(2186)
|
||||||
const axios = __nccwpck_require__(6545)
|
|
||||||
const {ConfigParser} = __nccwpck_require__(8395)
|
const {ConfigParser} = __nccwpck_require__(8395)
|
||||||
|
|
||||||
async function setPagesPath({staticSiteGenerator, path}) {
|
// Return the settings to be passed to a {ConfigParser} for a given
|
||||||
try {
|
// static site generator and a Pages path value to inject
|
||||||
|
function getConfigParserSettings(staticSiteGenerator, path) {
|
||||||
switch (staticSiteGenerator) {
|
switch (staticSiteGenerator) {
|
||||||
case 'nuxt':
|
case 'nuxt':
|
||||||
var ssConfig = {
|
return {
|
||||||
filePath: './nuxt.config.js',
|
configurationFile: './nuxt.config.js',
|
||||||
type: 'nuxt',
|
blankConfigurationFile: __nccwpck_require__.ab + "nuxt.js",
|
||||||
pathName: 'router',
|
properties: {
|
||||||
subPathName: 'base',
|
'router.base': path
|
||||||
newPath: path
|
}
|
||||||
}
|
}
|
||||||
break
|
|
||||||
case 'next':
|
case 'next':
|
||||||
var ssConfig = {
|
// Next does not want a trailing slash
|
||||||
filePath: './next.config.js',
|
if (path.endsWith('/')) {
|
||||||
type: 'next',
|
path = path.slice(0, -1)
|
||||||
pathName: 'basePath',
|
|
||||||
newPath: path
|
|
||||||
}
|
|
||||||
break
|
|
||||||
case 'gatsby':
|
|
||||||
var ssConfig = {
|
|
||||||
filePath: './gatsby-config.js',
|
|
||||||
type: 'gatsby',
|
|
||||||
pathName: 'pathPrefix',
|
|
||||||
newPath: path
|
|
||||||
}
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw 'Unknown config type'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let configParser = new ConfigParser(ssConfig)
|
return {
|
||||||
configParser.parse()
|
configurationFile: './next.config.js',
|
||||||
|
blankConfigurationFile: __nccwpck_require__.ab + "next.js",
|
||||||
|
properties: {
|
||||||
|
basePath: path,
|
||||||
|
|
||||||
|
// Disable server side image optimization too
|
||||||
|
// https://nextjs.org/docs/api-reference/next/image#unoptimized
|
||||||
|
'experimental.images.unoptimized': true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 'gatsby':
|
||||||
|
return {
|
||||||
|
configurationFile: './gatsby-config.js',
|
||||||
|
blankConfigurationFile: __nccwpck_require__.ab + "gatsby.js",
|
||||||
|
properties: {
|
||||||
|
pathPrefix: path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
throw `Unsupported static site generator: ${staticSiteGenerator}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inject Pages configuration in a given static site generator's configuration file
|
||||||
|
function setPagesPath({staticSiteGenerator, path}) {
|
||||||
|
try {
|
||||||
|
// Parse the configuration file and try to inject the Pages configuration in it
|
||||||
|
const settings = getConfigParserSettings(staticSiteGenerator, path)
|
||||||
|
new ConfigParser(settings).injectAll()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Logging
|
||||||
core.warning(
|
core.warning(
|
||||||
`We were unable to determine how to inject the site metadata into your config. Generated URLs may be incorrect. The base URL for this site should be ${path}. Please ensure your framework is configured to generate relative links appropriately.`,
|
`We were unable to determine how to inject the site metadata into your config. Generated URLs may be incorrect. The base URL for this site should be ${path}. Please ensure your framework is configured to generate relative links appropriately.`,
|
||||||
error
|
error
|
||||||
@@ -14899,7 +14884,7 @@ async function setPagesPath({staticSiteGenerator, path}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = setPagesPath
|
module.exports = {getConfigParserSettings, setPagesPath}
|
||||||
|
|
||||||
|
|
||||||
/***/ }),
|
/***/ }),
|
||||||
@@ -16386,7 +16371,7 @@ const enablePages = __nccwpck_require__(5424)
|
|||||||
const getPagesBaseUrl = __nccwpck_require__(9965)
|
const getPagesBaseUrl = __nccwpck_require__(9965)
|
||||||
|
|
||||||
// All variables we need from the runtime are loaded here
|
// All variables we need from the runtime are loaded here
|
||||||
const getContext = __nccwpck_require__(1319)
|
const {getContext} = __nccwpck_require__(1319)
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
|
|||||||
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
18
dist/licenses.txt
vendored
18
dist/licenses.txt
vendored
@@ -572,24 +572,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
string-format
|
|
||||||
(WTFPL OR MIT)
|
|
||||||
|
|
||||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
||||||
Version 2, December 2004
|
|
||||||
|
|
||||||
Copyright (c) 2018 David Chambers <dc@davidchambers.me>
|
|
||||||
|
|
||||||
Everyone is permitted to copy and distribute verbatim or modified
|
|
||||||
copies of this license document, and changing it is allowed as long
|
|
||||||
as the name is changed.
|
|
||||||
|
|
||||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
||||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
|
||||||
|
|
||||||
0. You just DO WHAT THE FUCK YOU WANT TO.
|
|
||||||
|
|
||||||
|
|
||||||
supports-color
|
supports-color
|
||||||
MIT
|
MIT
|
||||||
MIT License
|
MIT License
|
||||||
|
|||||||
3
dist/next.js
vendored
Normal file
3
dist/next.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
// Default Pages configuration for Next
|
||||||
|
const nextConfig = {}
|
||||||
|
module.exports = nextConfig
|
||||||
2
dist/nuxt.js
vendored
Normal file
2
dist/nuxt.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// Default Pages configuration for Nuxt
|
||||||
|
export default {}
|
||||||
24
package-lock.json
generated
24
package-lock.json
generated
@@ -17,7 +17,8 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@vercel/ncc": "^0.34.0",
|
"@vercel/ncc": "^0.34.0",
|
||||||
"jest": "^28.1.1"
|
"jest": "^28.1.1",
|
||||||
|
"prettier": "^2.7.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@actions/core": {
|
"node_modules/@actions/core": {
|
||||||
@@ -3041,6 +3042,21 @@
|
|||||||
"node": ">=8"
|
"node": ">=8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/prettier": {
|
||||||
|
"version": "2.7.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz",
|
||||||
|
"integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==",
|
||||||
|
"dev": true,
|
||||||
|
"bin": {
|
||||||
|
"prettier": "bin-prettier.js"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.13.0"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/prettier/prettier?sponsor=1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/pretty-format": {
|
"node_modules/pretty-format": {
|
||||||
"version": "28.1.1",
|
"version": "28.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz",
|
||||||
@@ -5878,6 +5894,12 @@
|
|||||||
"find-up": "^4.0.0"
|
"find-up": "^4.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"prettier": {
|
||||||
|
"version": "2.7.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz",
|
||||||
|
"integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"pretty-format": {
|
"pretty-format": {
|
||||||
"version": "28.1.1",
|
"version": "28.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz",
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@vercel/ncc": "^0.34.0",
|
"@vercel/ncc": "^0.34.0",
|
||||||
"jest": "^28.1.1"
|
"jest": "^28.1.1",
|
||||||
|
"prettier": "^2.7.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
src/blank-configurations/gatsby.js
Normal file
2
src/blank-configurations/gatsby.js
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// Default Pages configuration for Gatsby
|
||||||
|
module.exports = {}
|
||||||
3
src/blank-configurations/next.js
Normal file
3
src/blank-configurations/next.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
// Default Pages configuration for Next
|
||||||
|
const nextConfig = {}
|
||||||
|
module.exports = nextConfig
|
||||||
2
src/blank-configurations/nuxt.js
Normal file
2
src/blank-configurations/nuxt.js
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// Default Pages configuration for Nuxt
|
||||||
|
export default {}
|
||||||
@@ -1,205 +1,274 @@
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const espree = require('espree')
|
const espree = require('espree')
|
||||||
const format = require('string-format')
|
|
||||||
const core = require('@actions/core')
|
const core = require('@actions/core')
|
||||||
|
|
||||||
// Parse the AST
|
/*
|
||||||
|
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 (and work).
|
||||||
|
|
||||||
|
Supported configuration initializations:
|
||||||
|
|
||||||
|
(1) Default export:
|
||||||
|
|
||||||
|
export default {
|
||||||
|
// configuration object here
|
||||||
|
}
|
||||||
|
|
||||||
|
(2) Direct module export:
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
// configuration object here
|
||||||
|
}
|
||||||
|
|
||||||
|
(3) Indirect module export:
|
||||||
|
|
||||||
|
const config = // configuration object here
|
||||||
|
module.exports = config
|
||||||
|
*/
|
||||||
|
|
||||||
|
class ConfigParser {
|
||||||
|
// Ctor
|
||||||
|
// - configurationFile: path to the configuration file
|
||||||
|
// - blankConfigurationFile: a blank configuration file to use if non was previously found
|
||||||
|
constructor({configurationFile, blankConfigurationFile, properties}) {
|
||||||
|
// Save field
|
||||||
|
this.configurationFile = configurationFile
|
||||||
|
this.properties = properties
|
||||||
|
|
||||||
|
// If the configuration file does not exist, initialize it with the blank configuration file
|
||||||
|
if (!fs.existsSync(this.configurationFile)) {
|
||||||
|
core.info('Use default blank configuration')
|
||||||
|
const blankConfiguration = fs.readFileSync(blankConfigurationFile, 'utf8')
|
||||||
|
fs.writeFileSync(this.configurationFile, blankConfiguration, {
|
||||||
|
encoding: 'utf8'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the configuration file
|
||||||
|
core.info('Read existing configuration')
|
||||||
|
this.configuration = fs.readFileSync(this.configurationFile, 'utf8')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the configuration object in an AST.
|
||||||
|
// Look for a default export, a direct module export or an indirect module
|
||||||
|
// export (in that order).
|
||||||
|
//
|
||||||
|
// Return the configuration object or null.
|
||||||
|
findConfigurationObject(ast) {
|
||||||
|
// Try to find a default export
|
||||||
|
var defaultExport = ast.body.find(
|
||||||
|
node =>
|
||||||
|
node.type === 'ExportDefaultDeclaration' &&
|
||||||
|
node.declaration.type === 'ObjectExpression'
|
||||||
|
)
|
||||||
|
if (defaultExport) {
|
||||||
|
core.info('Found configuration object in default export declaration')
|
||||||
|
return defaultExport.declaration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to find a module export
|
||||||
|
var moduleExport = ast.body.find(
|
||||||
|
node =>
|
||||||
|
node.type === 'ExpressionStatement' &&
|
||||||
|
node.expression.type === 'AssignmentExpression' &&
|
||||||
|
node.expression.operator === '=' &&
|
||||||
|
node.expression.left.type === 'MemberExpression' &&
|
||||||
|
node.expression.left.object.type === 'Identifier' &&
|
||||||
|
node.expression.left.object.name === 'module' &&
|
||||||
|
node.expression.left.property.type === 'Identifier' &&
|
||||||
|
node.expression.left.property.name === 'exports'
|
||||||
|
)
|
||||||
|
|
||||||
|
// Direct module export
|
||||||
|
if (
|
||||||
|
moduleExport &&
|
||||||
|
moduleExport.expression.right.type === 'ObjectExpression'
|
||||||
|
) {
|
||||||
|
core.info('Found configuration object in direct module export')
|
||||||
|
return moduleExport.expression.right
|
||||||
|
}
|
||||||
|
|
||||||
|
// Indirect module export
|
||||||
|
else if (
|
||||||
|
moduleExport &&
|
||||||
|
moduleExport.expression.right.type === 'Identifier'
|
||||||
|
) {
|
||||||
|
const identifierName = moduleExport && moduleExport.expression.right.name
|
||||||
|
const identifierDefinition = ast.body.find(
|
||||||
|
node =>
|
||||||
|
node.type === 'VariableDeclaration' &&
|
||||||
|
node.declarations.length == 1 &&
|
||||||
|
node.declarations[0].type === 'VariableDeclarator' &&
|
||||||
|
node.declarations[0].id.type === 'Identifier' &&
|
||||||
|
node.declarations[0].id.name === identifierName &&
|
||||||
|
node.declarations[0].init.type === 'ObjectExpression'
|
||||||
|
)
|
||||||
|
if (identifierDefinition) {
|
||||||
|
core.info('Found configuration object in indirect module export')
|
||||||
|
return identifierDefinition.declarations[0].init
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No configuration object found
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find a property with a given name on a given object.
|
||||||
|
//
|
||||||
|
// Return the matching property or null.
|
||||||
|
findProperty(object, name) {
|
||||||
|
// Try to find a property matching a given name
|
||||||
|
const property =
|
||||||
|
object.type === 'ObjectExpression' &&
|
||||||
|
object.properties.find(
|
||||||
|
node => node.key.type === 'Identifier' && node.key.name === name
|
||||||
|
)
|
||||||
|
|
||||||
|
// Return the property's value (if found) or null
|
||||||
|
if (property) {
|
||||||
|
return property.value
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, propertyValue) {
|
||||||
|
if (startIndex === properties.length - 1) {
|
||||||
|
return `${properties[startIndex]}: ${JSON.stringify(propertyValue)}`
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
`${properties[startIndex]}: {` +
|
||||||
|
this.getPropertyDeclaration(properties, startIndex + 1, propertyValue) +
|
||||||
|
'}'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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}`)
|
||||||
|
|
||||||
|
// Parse the AST out of the configuration file
|
||||||
const espreeOptions = {
|
const espreeOptions = {
|
||||||
ecmaVersion: 6,
|
ecmaVersion: 6,
|
||||||
sourceType: 'module',
|
sourceType: 'module',
|
||||||
range: true
|
range: true
|
||||||
}
|
}
|
||||||
|
const ast = espree.parse(this.configuration, espreeOptions)
|
||||||
|
|
||||||
class ConfigParser {
|
// Find the configuration object
|
||||||
constructor(staticSiteConfig) {
|
var configurationObject = this.findConfigurationObject(ast)
|
||||||
this.pathPropertyNuxt = `router: {\n base: '{0}'\n }`
|
if (!configurationObject) {
|
||||||
this.pathPropertyNext = `basePath: '{0}'`
|
throw 'Could not find a configuration object in the configuration file'
|
||||||
this.pathPropertyGatsby = `pathPrefix: '{0}'`
|
|
||||||
this.configskeleton = `export default {\n {0}\n}`
|
|
||||||
this.staticSiteConfig = staticSiteConfig
|
|
||||||
this.config = fs.existsSync(this.staticSiteConfig.filePath)
|
|
||||||
? fs.readFileSync(this.staticSiteConfig.filePath, 'utf8')
|
|
||||||
: null
|
|
||||||
this.validate()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
validate() {
|
// A property may be nested in the configuration file. Split the property name with `.`
|
||||||
if (!this.config) {
|
// then walk the configuration object one property at a time.
|
||||||
core.info(`original raw configuration was empty:\n${this.config}`)
|
var depth = 0
|
||||||
core.info('Generating a default configuration to start from...')
|
const properties = propertyName.split('.')
|
||||||
|
var lastNode = configurationObject
|
||||||
|
while (1) {
|
||||||
|
// Find the node for the current property
|
||||||
|
var propertyNode = this.findProperty(lastNode, properties[depth])
|
||||||
|
|
||||||
// Update the `config` property with a default configuration file
|
// Update last node
|
||||||
this.config = this.generateConfigFile()
|
if (propertyNode != null) {
|
||||||
}
|
lastNode = propertyNode
|
||||||
|
depth++
|
||||||
}
|
}
|
||||||
|
|
||||||
generateConfigFile() {
|
// Exit when exiting the current configuration object
|
||||||
switch (this.staticSiteConfig.type) {
|
if (propertyNode == null || depth >= properties.length) {
|
||||||
case 'nuxt':
|
|
||||||
return format(
|
|
||||||
this.configskeleton,
|
|
||||||
format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
|
|
||||||
)
|
|
||||||
break
|
break
|
||||||
case 'next':
|
|
||||||
return format(
|
|
||||||
this.configskeleton,
|
|
||||||
format(this.pathPropertyNext, this.staticSiteConfig.newPath)
|
|
||||||
)
|
|
||||||
break
|
|
||||||
case 'gatsby':
|
|
||||||
return format(
|
|
||||||
this.configskeleton,
|
|
||||||
format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
|
|
||||||
)
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw 'Unknown config type'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generateConfigProperty() {
|
// If the configuration file is defining the property we are after, update it.
|
||||||
switch (this.staticSiteConfig.type) {
|
if (depth == properties.length) {
|
||||||
case 'nuxt':
|
// The last node identified is an object expression, so do the assignment
|
||||||
return format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
|
if (lastNode.type === 'ObjectExpression') {
|
||||||
break
|
this.configuration =
|
||||||
case 'next':
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
return format(this.pathPropertyNext, this.staticSiteConfig.newPath)
|
JSON.stringify(propertyValue) +
|
||||||
break
|
this.configuration.slice(lastNode.range[1])
|
||||||
case 'gatsby':
|
}
|
||||||
return format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
|
|
||||||
break
|
// A misc object was found in the configuration file (e.g. an array, a string, a boolean,
|
||||||
default:
|
// a number, etc.), just replace the whole range by our declaration
|
||||||
throw 'Unknown config type'
|
else {
|
||||||
|
this.configuration =
|
||||||
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
|
JSON.stringify(propertyValue) +
|
||||||
|
this.configuration.slice(lastNode.range[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parse() {
|
// Create nested properties in the configuration file
|
||||||
core.info(`original configuration:\n${this.config}`)
|
else {
|
||||||
const ast = espree.parse(this.config, espreeOptions)
|
// Build the declaration to inject
|
||||||
|
const declaration = this.getPropertyDeclaration(
|
||||||
// Find the default export declaration node
|
properties,
|
||||||
var exportNode = ast.body.find(node => node.type === 'ExpressionStatement')
|
depth,
|
||||||
if (exportNode) {
|
propertyValue
|
||||||
var property = this.getPropertyModuleExport(exportNode)
|
|
||||||
} else {
|
|
||||||
exportNode = ast.body.find(
|
|
||||||
node => node.type === 'ExportDefaultDeclaration'
|
|
||||||
)
|
|
||||||
if (!exportNode) throw 'Unable to find default export'
|
|
||||||
var property = this.getPropertyExportDefault(exportNode)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (property) {
|
|
||||||
switch (this.staticSiteConfig.type) {
|
|
||||||
case 'nuxt':
|
|
||||||
this.parseNuxt(property)
|
|
||||||
break
|
|
||||||
case 'next':
|
|
||||||
case 'gatsby':
|
|
||||||
this.parseNextGatsby(property)
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw 'Unknown config type'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
core.info(`parsed configuration:\n${this.config}`)
|
|
||||||
fs.writeFileSync(this.staticSiteConfig.filePath, this.config)
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
getPropertyModuleExport(exportNode) {
|
|
||||||
var propertyNode = exportNode.expression.right.properties.find(
|
|
||||||
node =>
|
|
||||||
node.key.type === 'Identifier' &&
|
|
||||||
node.key.name === this.staticSiteConfig.pathName
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!propertyNode) {
|
// The last node identified is an object expression, so do the assignment
|
||||||
core.info(
|
if (lastNode.type === 'ObjectExpression') {
|
||||||
'Unable to find property, insert it : ' +
|
// The object is blank (no properties) so replace the whole range by a new object containing the declaration
|
||||||
this.staticSiteConfig.pathName
|
if (lastNode.properties.length === 0) {
|
||||||
)
|
this.configuration =
|
||||||
if (exportNode.expression.right.properties.length > 0) {
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
this.config =
|
'{' +
|
||||||
this.config.slice(
|
declaration +
|
||||||
0,
|
'}' +
|
||||||
exportNode.expression.right.properties[0].range[0]
|
this.configuration.slice(lastNode.range[1])
|
||||||
) +
|
|
||||||
this.generateConfigProperty() +
|
|
||||||
',\n' +
|
|
||||||
this.config.slice(exportNode.expression.right.properties[0].range[0])
|
|
||||||
core.info('new config = \n' + this.config)
|
|
||||||
} else {
|
|
||||||
this.config =
|
|
||||||
this.config.slice(0, exportNode.expression.right.range[0] + 1) +
|
|
||||||
'\n ' +
|
|
||||||
this.generateConfigProperty() +
|
|
||||||
'\n' +
|
|
||||||
this.config.slice(exportNode.expression.right.range[1] - 1)
|
|
||||||
core.info('new config = \n' + this.config)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return propertyNode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getPropertyExportDefault(exportNode) {
|
// The object contains other properties, prepend our new one at the beginning
|
||||||
var propertyNode = exportNode.declaration.properties.find(
|
else {
|
||||||
node =>
|
this.configuration =
|
||||||
node.key.type === 'Identifier' &&
|
this.configuration.slice(0, lastNode.properties[0].range[0]) +
|
||||||
node.key.name === this.staticSiteConfig.pathName
|
declaration +
|
||||||
)
|
',' +
|
||||||
|
this.configuration.slice(lastNode.properties[0].range[0])
|
||||||
if (!propertyNode) {
|
|
||||||
core.info(
|
|
||||||
'Unable to find property, insert it ' + this.staticSiteConfig.pathName
|
|
||||||
)
|
|
||||||
if (exportNode.declaration.properties.length > 0) {
|
|
||||||
this.config =
|
|
||||||
this.config.slice(0, exportNode.declaration.properties[0].range[0]) +
|
|
||||||
this.generateConfigProperty() +
|
|
||||||
',\n' +
|
|
||||||
this.config.slice(exportNode.declaration.properties[0].range[0])
|
|
||||||
core.info('new config = \n' + this.config)
|
|
||||||
} else {
|
|
||||||
this.config =
|
|
||||||
this.config.slice(0, exportNode.declaration.range[0] + 1) +
|
|
||||||
'\n ' +
|
|
||||||
this.generateConfigProperty() +
|
|
||||||
'\n' +
|
|
||||||
this.config.slice(exportNode.declaration.range[1] - 1)
|
|
||||||
core.info('new config = \n' + this.config)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return propertyNode
|
// A misc object was found in the configuration file (e.g. an array, a string, a boolean,
|
||||||
}
|
// a number, etc.), just replace the whole range by our declaration
|
||||||
|
else {
|
||||||
parseNuxt(propertyNode) {
|
this.configuration =
|
||||||
// Find the base node
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
if (propertyNode && propertyNode.value.type === 'ObjectExpression') {
|
'{' +
|
||||||
var baseNode = propertyNode.value.properties.find(
|
declaration +
|
||||||
node =>
|
'}' +
|
||||||
node.key.type === 'Identifier' &&
|
this.configuration.slice(lastNode.range[1])
|
||||||
node.key.name === this.staticSiteConfig.subPathName
|
|
||||||
) //'base')
|
|
||||||
if (baseNode) {
|
|
||||||
// Swap the base value by a hardcoded string and print it
|
|
||||||
this.config =
|
|
||||||
this.config.slice(0, baseNode.value.range[0]) +
|
|
||||||
`'${this.staticSiteConfig.newPath}'` +
|
|
||||||
this.config.slice(baseNode.value.range[1])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parseNextGatsby(pathNode) {
|
// Logging
|
||||||
if (pathNode) {
|
core.info(`Writing new configuration:\n${this.configuration}`)
|
||||||
this.config =
|
|
||||||
this.config.slice(0, pathNode.value.range[0]) +
|
// Finally write the new configuration in the file
|
||||||
`'${this.staticSiteConfig.newPath}'` +
|
fs.writeFileSync(this.configurationFile, this.configuration, {
|
||||||
this.config.slice(pathNode.value.range[1])
|
encoding: 'utf8'
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,104 +1,156 @@
|
|||||||
const {ConfigParser} = require('./config-parser')
|
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const assert = require('assert')
|
|
||||||
|
|
||||||
const srcFolder = `${process.cwd()}/src/fixtures`
|
const {ConfigParser} = require('./config-parser')
|
||||||
const tmpFolder = `${process.cwd()}/src/fixtures/tmp`
|
const {getTempFolder, compareFiles} = require('./test-helpers')
|
||||||
const expectedFolder = `${process.cwd()}/src/fixtures/expected`
|
|
||||||
|
|
||||||
const repoPath = '/amazing-new-repo/'
|
// Get the temp folder
|
||||||
|
const tempFolder = getTempFolder()
|
||||||
|
|
||||||
|
// Cases to test
|
||||||
const cases = [
|
const cases = [
|
||||||
[
|
//
|
||||||
'next.config.js',
|
// Default export
|
||||||
|
//
|
||||||
|
|
||||||
{
|
{
|
||||||
filePath: `${tmpFolder}/next.config.js`,
|
property: 'property',
|
||||||
type: 'next',
|
source: `export default {}`,
|
||||||
pathName: 'basePath',
|
expected: `export default { property: "value" }`
|
||||||
newPath: repoPath
|
},
|
||||||
}
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'next.config.old.js',
|
|
||||||
{
|
{
|
||||||
filePath: `${tmpFolder}/next.config.old.js`,
|
property: 'property',
|
||||||
type: 'next',
|
source: `export default { property: 0 }`, // property exists and is a number
|
||||||
pathName: 'basePath',
|
expected: `export default { property: "value" }`
|
||||||
newPath: repoPath
|
},
|
||||||
}
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'next.config.old.missing.js',
|
|
||||||
{
|
{
|
||||||
filePath: `${tmpFolder}/next.config.old.missing.js`,
|
property: 'property',
|
||||||
type: 'next',
|
source: `export default { property: false }`, // property exists and is a boolean
|
||||||
pathName: 'basePath',
|
expected: `export default { property: "value" }`
|
||||||
newPath: repoPath
|
},
|
||||||
}
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'gatsby-config.js',
|
|
||||||
{
|
{
|
||||||
filePath: `${tmpFolder}/gatsby-config.js`,
|
property: 'property',
|
||||||
type: 'gatsby',
|
source: `export default { property: "test" }`, // property exists and is a string
|
||||||
pathName: 'pathPrefix',
|
expected: `export default { property: "value" }`
|
||||||
newPath: repoPath
|
},
|
||||||
}
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'gatsby-config.old.js',
|
|
||||||
{
|
{
|
||||||
filePath: `${tmpFolder}/gatsby-config.old.js`,
|
property: 'property',
|
||||||
type: 'gatsby',
|
source: `export default { property: [1,2] }`, // property exists and is an array
|
||||||
pathName: 'pathPrefix',
|
expected: `export default { property: "value" }`
|
||||||
newPath: repoPath
|
},
|
||||||
}
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'nuxt.config.js',
|
|
||||||
{
|
{
|
||||||
filePath: `${tmpFolder}/nuxt.config.js`,
|
property: 'property',
|
||||||
type: 'nuxt',
|
source: `export default { property: null }`, // property exists and is null
|
||||||
pathName: 'router',
|
expected: `export default { property: "value" }`
|
||||||
subPathName: 'base',
|
},
|
||||||
newPath: repoPath
|
|
||||||
}
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'nuxt.config.missing.js',
|
|
||||||
{
|
{
|
||||||
filePath: `${tmpFolder}/nuxt.config.missing.js`,
|
property: 'property',
|
||||||
type: 'nuxt',
|
source: `export default { property: {}}`, // property exists and is an object
|
||||||
pathName: 'router',
|
expected: `export default { property: "value" }`
|
||||||
subPathName: 'base',
|
},
|
||||||
newPath: repoPath
|
|
||||||
}
|
// Deep properties (injection 1)
|
||||||
],
|
|
||||||
[
|
|
||||||
'nuxt.config.old.js',
|
|
||||||
{
|
{
|
||||||
filePath: `${tmpFolder}/nuxt.config.old.js`,
|
property: 'property.b.c',
|
||||||
type: 'nuxt',
|
source: `export default {}`,
|
||||||
pathName: 'router',
|
expected: `export default { property: { b: { c: "value" }}}`
|
||||||
subPathName: 'base',
|
},
|
||||||
newPath: repoPath
|
{
|
||||||
|
property: 'property.b.c',
|
||||||
|
source: `export default { property: 0 }`, // property exists and is a number
|
||||||
|
expected: `export default { property: { b: { c: "value" }}}`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
property: 'property.b.c',
|
||||||
|
source: `export default { property: {}}`, // property exists and is an object
|
||||||
|
expected: `export default { property: { b: { c: "value" }}}`
|
||||||
|
},
|
||||||
|
|
||||||
|
// Deep properties (injection 2)
|
||||||
|
{
|
||||||
|
property: 'property.b.c',
|
||||||
|
source: `export default { property: { b: 0 }}`, // property exists and is a number
|
||||||
|
expected: `export default { property: { b: { c: "value" }}}`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
property: 'property.b.c',
|
||||||
|
source: `export default { property: { b: {}}}`, // property exists and is an object
|
||||||
|
expected: `export default { property: { b: { c: "value" }}}`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
property: 'property.b.c',
|
||||||
|
source: `export default { property: { b: { hello: 123}}}`, // property exists and is a non-empty object
|
||||||
|
expected: `export default { property: { b: { c: "value", hello: 123 }}}`
|
||||||
|
},
|
||||||
|
|
||||||
|
// Deep properties (existing properties)
|
||||||
|
{
|
||||||
|
property: 'a1.a2',
|
||||||
|
source: `export default { a2: false, a1: { a3: [12]}}`, // property exists and is a non-empty object
|
||||||
|
expected: `export default { a2: false, a1: { a2: "value", a3: [12]}}`
|
||||||
|
},
|
||||||
|
|
||||||
|
//
|
||||||
|
// Direct module exports
|
||||||
|
//
|
||||||
|
{
|
||||||
|
property: 'property',
|
||||||
|
source: `module.exports = {}`,
|
||||||
|
expected: `module.exports = { property: "value"}`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
property: 'property',
|
||||||
|
source: `module.exports = { p1: 0}`,
|
||||||
|
expected: `module.exports = { property: "value", p1: 0}`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
property: 'a.b.c',
|
||||||
|
source: `module.exports = { p1: 0}`,
|
||||||
|
expected: `module.exports = { a: { b: { c: "value" }}, p1: 0}`
|
||||||
|
},
|
||||||
|
|
||||||
|
//
|
||||||
|
// Indirect module exports
|
||||||
|
//
|
||||||
|
{
|
||||||
|
property: 'property',
|
||||||
|
source: `const config = {}; module.exports = config`,
|
||||||
|
expected: `const config = { property: "value"}; module.exports = config`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
property: 'property',
|
||||||
|
source: `var config = {}; module.exports = config`,
|
||||||
|
expected: `var config = { property: "value"}; module.exports = config`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
property: 'a.b.c',
|
||||||
|
source: `var config = {}; module.exports = config`,
|
||||||
|
expected: `var config = { a: { b: { c: "value"}}}; module.exports = config`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
property: 'a.b.c',
|
||||||
|
source: `var config = { a: { b: [], c: "hello"}}; module.exports = config`,
|
||||||
|
expected: `var config = { a: { b: { c: "value"}, c: "hello"}}; module.exports = config`
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
|
||||||
|
|
||||||
describe('configParser', () => {
|
describe('config-parser', () => {
|
||||||
test.each(cases)('%p parsed correctly', (fileName, configuration) => {
|
cases.forEach(({property, source, expected}, index) => {
|
||||||
srcFileName = `${srcFolder}/${fileName}`
|
it(`Inject path properly for case #${index}`, () => {
|
||||||
tmpFileName = `${tmpFolder}/${fileName}`
|
// Write the source file
|
||||||
expectedFileName = `${expectedFolder}/${fileName}`
|
const sourceFile = `${tempFolder}/source.js`
|
||||||
fs.mkdirSync(tmpFolder, {recursive: true})
|
fs.writeFileSync(sourceFile, source, {encoding: 'utf8'})
|
||||||
fs.copyFileSync(srcFileName, tmpFileName)
|
|
||||||
const parser = new ConfigParser(configuration)
|
|
||||||
parser.parse()
|
|
||||||
|
|
||||||
var expectedContent = fs.readFileSync(expectedFileName).toString()
|
// Write the expected file
|
||||||
var actualContent = fs.readFileSync(tmpFileName).toString()
|
const expectedFile = `${tempFolder}/expected.js`
|
||||||
assert.equal(actualContent, expectedContent)
|
fs.writeFileSync(expectedFile, expected, {encoding: 'utf8'})
|
||||||
fs.rmSync(tmpFileName)
|
|
||||||
|
// Update the settings and do the injection
|
||||||
|
new ConfigParser({
|
||||||
|
configurationFile: sourceFile
|
||||||
|
}).inject(property, 'value')
|
||||||
|
|
||||||
|
// Compare the files
|
||||||
|
compareFiles(sourceFile, expectedFile)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ function getRequiredVars() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function getContext() {
|
// Return the context object
|
||||||
|
function getContext() {
|
||||||
const requiredVars = getRequiredVars()
|
const requiredVars = getRequiredVars()
|
||||||
for (const variable in requiredVars) {
|
for (const variable in requiredVars) {
|
||||||
if (requiredVars[variable] === undefined) {
|
if (requiredVars[variable] === undefined) {
|
||||||
@@ -19,3 +20,5 @@ module.exports = function getContext() {
|
|||||||
core.debug('all variables are set')
|
core.debug('all variables are set')
|
||||||
return requiredVars
|
return requiredVars
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = {getContext}
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
import { resolve } from 'path'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
pathPrefix: '/amazing-new-repo/',/* test */
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
pathPrefix: '/amazing-new-repo/'
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
|
|
||||||
import { resolve } from 'path'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
basePath: '/amazing-new-repo/'/* test */,
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
module.exports={
|
|
||||||
basePath: '/amazing-new-repo/'/* test */,
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
module.exports={
|
|
||||||
basePath: '/amazing-new-repo/'
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import { resolve } from 'path'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
target: 'static',
|
|
||||||
router: {
|
|
||||||
base: '/amazing-new-repo/'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import { resolve } from 'path'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
router: {
|
|
||||||
base: '/amazing-new-repo/'
|
|
||||||
},
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
target: 'static'
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
|
|
||||||
module.exports={
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
target: 'static',
|
|
||||||
router: {
|
|
||||||
base: '/amazing-new-repo/'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import { resolve } from 'path'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
pathPrefix: '/prefix',/* test */
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
pathPrefix: '/prefix'
|
|
||||||
}
|
|
||||||
2
src/fixtures/gatsby/blank.expected.js
Normal file
2
src/fixtures/gatsby/blank.expected.js
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// Default Pages configuration for Gatsby
|
||||||
|
module.exports = { pathPrefix: "/docs/" }
|
||||||
1
src/fixtures/gatsby/blank.js
Normal file
1
src/fixtures/gatsby/blank.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// This file is not read by the test suite
|
||||||
8
src/fixtures/gatsby/default.expected.js
Normal file
8
src/fixtures/gatsby/default.expected.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
module.exports = {
|
||||||
|
pathPrefix: "/docs/",
|
||||||
|
siteMetadata: {
|
||||||
|
title: `My Gatsby Site`,
|
||||||
|
siteUrl: `https://www.yourdomain.tld`,
|
||||||
|
},
|
||||||
|
plugins: [],
|
||||||
|
}
|
||||||
7
src/fixtures/gatsby/default.js
Normal file
7
src/fixtures/gatsby/default.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module.exports = {
|
||||||
|
siteMetadata: {
|
||||||
|
title: `My Gatsby Site`,
|
||||||
|
siteUrl: `https://www.yourdomain.tld`,
|
||||||
|
},
|
||||||
|
plugins: [],
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
|
|
||||||
import { resolve } from 'path'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
basePath: '/gh-pages-test'/* test */,
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
module.exports={
|
|
||||||
basePath: '/gh-pages-test'/* test */,
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
module.exports={
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
6
src/fixtures/next/blank.expected.js
Normal file
6
src/fixtures/next/blank.expected.js
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
// Default Pages configuration for Next
|
||||||
|
const nextConfig = {
|
||||||
|
experimental: {images: {unoptimized: true}},
|
||||||
|
basePath: '/docs'
|
||||||
|
}
|
||||||
|
module.exports = nextConfig
|
||||||
1
src/fixtures/next/blank.js
Normal file
1
src/fixtures/next/blank.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// This file is not read by the test suite
|
||||||
9
src/fixtures/next/default.expected.js
Normal file
9
src/fixtures/next/default.expected.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
/** @type {import('next').NextConfig} */
|
||||||
|
const nextConfig = {
|
||||||
|
experimental: {images: {unoptimized: true}},
|
||||||
|
basePath: '/docs',
|
||||||
|
reactStrictMode: true,
|
||||||
|
swcMinify: true
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = nextConfig
|
||||||
7
src/fixtures/next/default.js
Normal file
7
src/fixtures/next/default.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
/** @type {import('next').NextConfig} */
|
||||||
|
const nextConfig = {
|
||||||
|
reactStrictMode: true,
|
||||||
|
swcMinify: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = nextConfig
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import { resolve } from 'path'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
target: 'static',
|
|
||||||
router: {
|
|
||||||
base: 'some/path'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import { resolve } from 'path'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
target: 'static'
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
|
|
||||||
module.exports={
|
|
||||||
alias: {
|
|
||||||
'style': resolve(__dirname, './assets/style')
|
|
||||||
},
|
|
||||||
target: 'static',
|
|
||||||
router: {
|
|
||||||
base: 'some/path'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
2
src/fixtures/nuxt/blank.expected.js
Normal file
2
src/fixtures/nuxt/blank.expected.js
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// Default Pages configuration for Nuxt
|
||||||
|
export default {router: {base: '/docs/'}}
|
||||||
1
src/fixtures/nuxt/blank.js
Normal file
1
src/fixtures/nuxt/blank.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// This file is not read by the test suite
|
||||||
45
src/fixtures/nuxt/default.expected.js
Normal file
45
src/fixtures/nuxt/default.expected.js
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
export default {
|
||||||
|
// Disable server-side rendering: https://go.nuxtjs.dev/ssr-mode
|
||||||
|
router: { base: "/docs/" },
|
||||||
|
ssr: false,
|
||||||
|
|
||||||
|
// Global page headers: https://go.nuxtjs.dev/config-head
|
||||||
|
head: {
|
||||||
|
title: 'nuxt',
|
||||||
|
htmlAttrs: {
|
||||||
|
lang: 'en'
|
||||||
|
},
|
||||||
|
meta: [
|
||||||
|
{ charset: 'utf-8' },
|
||||||
|
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
|
||||||
|
{ hid: 'description', name: 'description', content: '' },
|
||||||
|
{ name: 'format-detection', content: 'telephone=no' }
|
||||||
|
],
|
||||||
|
link: [
|
||||||
|
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
// Global CSS: https://go.nuxtjs.dev/config-css
|
||||||
|
css: [
|
||||||
|
],
|
||||||
|
|
||||||
|
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
|
||||||
|
plugins: [
|
||||||
|
],
|
||||||
|
|
||||||
|
// Auto import components: https://go.nuxtjs.dev/config-components
|
||||||
|
components: true,
|
||||||
|
|
||||||
|
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
|
||||||
|
buildModules: [
|
||||||
|
],
|
||||||
|
|
||||||
|
// Modules: https://go.nuxtjs.dev/config-modules
|
||||||
|
modules: [
|
||||||
|
],
|
||||||
|
|
||||||
|
// Build Configuration: https://go.nuxtjs.dev/config-build
|
||||||
|
build: {
|
||||||
|
}
|
||||||
|
}
|
||||||
44
src/fixtures/nuxt/default.js
Normal file
44
src/fixtures/nuxt/default.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
export default {
|
||||||
|
// Disable server-side rendering: https://go.nuxtjs.dev/ssr-mode
|
||||||
|
ssr: false,
|
||||||
|
|
||||||
|
// Global page headers: https://go.nuxtjs.dev/config-head
|
||||||
|
head: {
|
||||||
|
title: 'nuxt',
|
||||||
|
htmlAttrs: {
|
||||||
|
lang: 'en'
|
||||||
|
},
|
||||||
|
meta: [
|
||||||
|
{ charset: 'utf-8' },
|
||||||
|
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
|
||||||
|
{ hid: 'description', name: 'description', content: '' },
|
||||||
|
{ name: 'format-detection', content: 'telephone=no' }
|
||||||
|
],
|
||||||
|
link: [
|
||||||
|
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
// Global CSS: https://go.nuxtjs.dev/config-css
|
||||||
|
css: [
|
||||||
|
],
|
||||||
|
|
||||||
|
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
|
||||||
|
plugins: [
|
||||||
|
],
|
||||||
|
|
||||||
|
// Auto import components: https://go.nuxtjs.dev/config-components
|
||||||
|
components: true,
|
||||||
|
|
||||||
|
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
|
||||||
|
buildModules: [
|
||||||
|
],
|
||||||
|
|
||||||
|
// Modules: https://go.nuxtjs.dev/config-modules
|
||||||
|
modules: [
|
||||||
|
],
|
||||||
|
|
||||||
|
// Build Configuration: https://go.nuxtjs.dev/config-build
|
||||||
|
build: {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
const core = require('@actions/core')
|
const core = require('@actions/core')
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const setPagesPath = require('./set-pages-path')
|
const {setPagesPath} = require('./set-pages-path')
|
||||||
|
|
||||||
async function getPagesBaseUrl({
|
async function getPagesBaseUrl({
|
||||||
repositoryNwo,
|
repositoryNwo,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
const core = require('@actions/core')
|
const core = require('@actions/core')
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
//const { expect, jest } = require('@jest/globals')
|
|
||||||
|
|
||||||
const getPagesBaseUrl = require('./get-pages-base-url')
|
const getPagesBaseUrl = require('./get-pages-base-url')
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ const enablePages = require('./enable-pages')
|
|||||||
const getPagesBaseUrl = require('./get-pages-base-url')
|
const getPagesBaseUrl = require('./get-pages-base-url')
|
||||||
|
|
||||||
// All variables we need from the runtime are loaded here
|
// All variables we need from the runtime are loaded here
|
||||||
const getContext = require('./context')
|
const {getContext} = require('./context')
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,42 +1,56 @@
|
|||||||
const core = require('@actions/core')
|
const core = require('@actions/core')
|
||||||
const axios = require('axios')
|
|
||||||
const {ConfigParser} = require('./config-parser')
|
const {ConfigParser} = require('./config-parser')
|
||||||
|
|
||||||
async function setPagesPath({staticSiteGenerator, path}) {
|
// Return the settings to be passed to a {ConfigParser} for a given
|
||||||
try {
|
// static site generator and a Pages path value to inject
|
||||||
|
function getConfigParserSettings(staticSiteGenerator, path) {
|
||||||
switch (staticSiteGenerator) {
|
switch (staticSiteGenerator) {
|
||||||
case 'nuxt':
|
case 'nuxt':
|
||||||
var ssConfig = {
|
return {
|
||||||
filePath: './nuxt.config.js',
|
configurationFile: './nuxt.config.js',
|
||||||
type: 'nuxt',
|
blankConfigurationFile: `${__dirname}/blank-configurations/nuxt.js`,
|
||||||
pathName: 'router',
|
properties: {
|
||||||
subPathName: 'base',
|
'router.base': path
|
||||||
newPath: path
|
}
|
||||||
}
|
}
|
||||||
break
|
|
||||||
case 'next':
|
case 'next':
|
||||||
var ssConfig = {
|
// Next does not want a trailing slash
|
||||||
filePath: './next.config.js',
|
if (path.endsWith('/')) {
|
||||||
type: 'next',
|
path = path.slice(0, -1)
|
||||||
pathName: 'basePath',
|
|
||||||
newPath: path
|
|
||||||
}
|
|
||||||
break
|
|
||||||
case 'gatsby':
|
|
||||||
var ssConfig = {
|
|
||||||
filePath: './gatsby-config.js',
|
|
||||||
type: 'gatsby',
|
|
||||||
pathName: 'pathPrefix',
|
|
||||||
newPath: path
|
|
||||||
}
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw 'Unknown config type'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let configParser = new ConfigParser(ssConfig)
|
return {
|
||||||
configParser.parse()
|
configurationFile: './next.config.js',
|
||||||
|
blankConfigurationFile: `${__dirname}/blank-configurations/next.js`,
|
||||||
|
properties: {
|
||||||
|
basePath: path,
|
||||||
|
|
||||||
|
// Disable server side image optimization too
|
||||||
|
// https://nextjs.org/docs/api-reference/next/image#unoptimized
|
||||||
|
'experimental.images.unoptimized': true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 'gatsby':
|
||||||
|
return {
|
||||||
|
configurationFile: './gatsby-config.js',
|
||||||
|
blankConfigurationFile: `${__dirname}/blank-configurations/gatsby.js`,
|
||||||
|
properties: {
|
||||||
|
pathPrefix: path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
throw `Unsupported static site generator: ${staticSiteGenerator}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inject Pages configuration in a given static site generator's configuration file
|
||||||
|
function setPagesPath({staticSiteGenerator, path}) {
|
||||||
|
try {
|
||||||
|
// Parse the configuration file and try to inject the Pages configuration in it
|
||||||
|
const settings = getConfigParserSettings(staticSiteGenerator, path)
|
||||||
|
new ConfigParser(settings).injectAll()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Logging
|
||||||
core.warning(
|
core.warning(
|
||||||
`We were unable to determine how to inject the site metadata into your config. Generated URLs may be incorrect. The base URL for this site should be ${path}. Please ensure your framework is configured to generate relative links appropriately.`,
|
`We were unable to determine how to inject the site metadata into your config. Generated URLs may be incorrect. The base URL for this site should be ${path}. Please ensure your framework is configured to generate relative links appropriately.`,
|
||||||
error
|
error
|
||||||
@@ -44,4 +58,4 @@ async function setPagesPath({staticSiteGenerator, path}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = setPagesPath
|
module.exports = {getConfigParserSettings, setPagesPath}
|
||||||
|
|||||||
53
src/set-pages-path.test.js
Normal file
53
src/set-pages-path.test.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
const fs = require('fs')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
|
const {getConfigParserSettings} = require('./set-pages-path')
|
||||||
|
const {ConfigParser} = require('./config-parser')
|
||||||
|
const {getTempFolder, compareFiles} = require('./test-helpers')
|
||||||
|
|
||||||
|
// Get the temp folder
|
||||||
|
const tempFolder = getTempFolder()
|
||||||
|
|
||||||
|
// Test suite
|
||||||
|
describe('configParser', () => {
|
||||||
|
// Iterate over the static site generators
|
||||||
|
;['next', 'nuxt', 'gatsby'].forEach(staticSiteGenerator => {
|
||||||
|
// Folder containing the fixtures for a given static site generator
|
||||||
|
const fixtureFolder = `${__dirname}/fixtures/${staticSiteGenerator}`
|
||||||
|
|
||||||
|
// Iterate over the fixtures
|
||||||
|
fs.readdirSync(fixtureFolder).forEach(configurationFile => {
|
||||||
|
// Ignore expectation
|
||||||
|
if (configurationFile.endsWith('.expected.js')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
it(`Inject path properly for ${staticSiteGenerator} in ${configurationFile}`, async () => {
|
||||||
|
// Get settings for the static site generator
|
||||||
|
const settings = getConfigParserSettings(staticSiteGenerator, '/docs/')
|
||||||
|
|
||||||
|
// Copy the source fixture to a temp file
|
||||||
|
const fixtureSourceFile = `${fixtureFolder}/${configurationFile}`
|
||||||
|
const fixtureTargetFile = `${tempFolder}/${configurationFile}`
|
||||||
|
if (configurationFile != 'blank.js') {
|
||||||
|
fs.copyFileSync(fixtureSourceFile, fixtureTargetFile)
|
||||||
|
} else if (fs.existsSync(fixtureTargetFile)) {
|
||||||
|
fs.rmSync(fixtureTargetFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the settings and do the injection
|
||||||
|
settings.configurationFile = fixtureTargetFile
|
||||||
|
new ConfigParser(settings).injectAll()
|
||||||
|
|
||||||
|
// Read the expected file
|
||||||
|
const expectedFile = `${fixtureFolder}/${path.basename(
|
||||||
|
configurationFile,
|
||||||
|
'.js'
|
||||||
|
)}.expected.js`
|
||||||
|
|
||||||
|
// Compare the actual and expected files
|
||||||
|
compareFiles(settings.configurationFile, expectedFile)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
37
src/test-helpers.js
Normal file
37
src/test-helpers.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
const fs = require('fs')
|
||||||
|
const prettier = require('prettier')
|
||||||
|
const assert = require('assert')
|
||||||
|
|
||||||
|
// Create and return the path to a temp folder
|
||||||
|
function getTempFolder() {
|
||||||
|
const tmpFolder = `${__dirname}/fixtures/tmp`
|
||||||
|
if (!fs.existsSync(tmpFolder)) {
|
||||||
|
fs.mkdirSync(tmpFolder, {recursive: true})
|
||||||
|
}
|
||||||
|
return tmpFolder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read a JavaScript file and return it formatted
|
||||||
|
function formatFile(file) {
|
||||||
|
const fileContent = fs.readFileSync(file, 'utf8')
|
||||||
|
return prettier.format(fileContent, {
|
||||||
|
parser: 'espree',
|
||||||
|
|
||||||
|
// Prettier options
|
||||||
|
printWidth: 80,
|
||||||
|
tabWidth: 2,
|
||||||
|
useTabs: false,
|
||||||
|
semi: false,
|
||||||
|
singleQuote: true,
|
||||||
|
trailingComma: 'none',
|
||||||
|
bracketSpacing: false,
|
||||||
|
arrowParens: 'avoid'
|
||||||
|
}).trim()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare two JavaScript files
|
||||||
|
function compareFiles(actualFile, expectedFile) {
|
||||||
|
assert.equal(formatFile(actualFile), formatFile(expectedFile))
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {getTempFolder, formatFile, compareFiles}
|
||||||
Reference in New Issue
Block a user