Skip to content

Commit

Permalink
Merge branch 'main' into better-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
James M. Greene authored and GitHub committed Aug 10, 2022
2 parents 9f6ed02 + 15f519f commit 90b7c04
Show file tree
Hide file tree
Showing 48 changed files with 657 additions and 234 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/check-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ on:
permissions:
contents: read

# This allows a subsequently queued workflow run to interrupt previous runs
concurrency:
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
cancel-in-progress: true

jobs:
check-dist:
runs-on: ubuntu-latest
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/check-formatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Checking formatting

on:
push:
branches:
- main
pull_request:

permissions:
contents: read

# This allows a subsequently queued workflow run to interrupt previous runs
concurrency:
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.JS
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Verify formatting
run: npm run format:check
3 changes: 3 additions & 0 deletions .github/workflows/draft-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
branches:
- main

permissions:
contents: write

jobs:
draft-release:
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ on:
- main
pull_request:

permissions:
contents: read

# This allows a subsequently queued workflow run to interrupt previous runs
concurrency:
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions .prettierrc.yaml → .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Prettier (formatter) configuration
---
printWidth: 80
printWidth: 120
tabWidth: 2
useTabs: false
semi: false
singleQuote: true
trailingComma: none
bracketSpacing: false
bracketSpacing: true
arrowParens: avoid
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ runs:
main: 'dist/index.js'
inputs:
static_site_generator:
description: 'Optional static site generator to attempt to configure (nuxt, next or gatsby)'
description: 'Optional static site generator to attempt to configure: "nuxt", "next", or "gatsby"'
required: false
generator_config_file:
description: 'Optional file path to static site generator configuration file'
required: false
token:
description: 'GitHub token'
Expand Down
87 changes: 45 additions & 42 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14559,7 +14559,7 @@ 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}) {
constructor({ configurationFile, blankConfigurationFile, properties }) {
// Save field
this.configurationFile = configurationFile
this.properties = properties
Expand All @@ -14584,16 +14584,32 @@ class ConfigParser {
// 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')
var defaultExport = ast.body.find(node => node.type === 'ExportDefaultDeclaration')

// Direct default export
if (defaultExport && defaultExport.declaration.type === 'ObjectExpression') {
core.info('Found configuration object in direct default export declaration')
return defaultExport.declaration
}

// Indirect default export
else if (defaultExport && defaultExport.declaration.type === 'Identifier') {
const identifierName = defaultExport.declaration.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 default export declaration')
return identifierDefinition.declarations[0].init
}
}

// Try to find a module export
var moduleExport = ast.body.find(
node =>
Expand All @@ -14608,19 +14624,13 @@ class ConfigParser {
)

// Direct module export
if (
moduleExport &&
moduleExport.expression.right.type === 'ObjectExpression'
) {
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'
) {
else if (moduleExport && moduleExport.expression.right.type === 'Identifier') {
const identifierName = moduleExport && moduleExport.expression.right.name
const identifierDefinition = ast.body.find(
node =>
Expand Down Expand Up @@ -14648,9 +14658,7 @@ class ConfigParser {
// 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
)
object.properties.find(node => node.key.type === 'Identifier' && node.key.name === name)

// Return the property's value (if found) or null
if (property) {
Expand All @@ -14670,9 +14678,7 @@ class ConfigParser {
return `${properties[startIndex]}: ${JSON.stringify(propertyValue)}`
} else {
return (
`${properties[startIndex]}: {` +
this.getPropertyDeclaration(properties, startIndex + 1, propertyValue) +
'}'
`${properties[startIndex]}: {` + this.getPropertyDeclaration(properties, startIndex + 1, propertyValue) + '}'
)
}
}
Expand Down Expand Up @@ -14711,7 +14717,7 @@ class ConfigParser {
var depth = 0
const properties = propertyName.split('.')
var lastNode = configurationObject
while (1) {
while (true) {
// Find the node for the current property
var propertyNode = this.findProperty(lastNode, properties[depth])

Expand Down Expand Up @@ -14750,11 +14756,7 @@ class ConfigParser {
// Create nested properties in the configuration file
else {
// Build the declaration to inject
const declaration = this.getPropertyDeclaration(
properties,
depth,
propertyValue
)
const declaration = this.getPropertyDeclaration(properties, depth, propertyValue)

// The last node identified is an object expression, so do the assignment
if (lastNode.type === 'ObjectExpression') {
Expand Down Expand Up @@ -14801,7 +14803,7 @@ class ConfigParser {
}
}

module.exports = {ConfigParser}
module.exports = { ConfigParser }


/***/ }),
Expand All @@ -14817,6 +14819,7 @@ function getRequiredVars() {
repositoryNwo: process.env.GITHUB_REPOSITORY,
githubToken: core.getInput('token'),
staticSiteGenerator: core.getInput('static_site_generator'),
generatorConfigFile: core.getInput('generator_config_file'),
enablement: core.getInput('enablement') !== 'false'
}
}
Expand All @@ -14833,7 +14836,7 @@ function getContext() {
return requiredVars
}

module.exports = {getContext}
module.exports = { getContext }


/***/ }),
Expand All @@ -14859,15 +14862,15 @@ module.exports = outputPagesBaseUrl
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const core = __nccwpck_require__(2186)
const {ConfigParser} = __nccwpck_require__(8395)
const { ConfigParser } = __nccwpck_require__(8395)

// Return the settings to be passed to a {ConfigParser} for a given
// static site generator and a Pages path value to inject
function getConfigParserSettings(staticSiteGenerator, path) {
// Return the settings to be passed to a {ConfigParser} for a given static site generator,
// optional configuration file path, and a Pages path value to inject
function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, path }) {
switch (staticSiteGenerator) {
case 'nuxt':
return {
configurationFile: './nuxt.config.js',
configurationFile: generatorConfigFile || './nuxt.config.js',
blankConfigurationFile: __nccwpck_require__.ab + "nuxt.js",
properties: {
// Configure a base path on the router
Expand All @@ -14885,7 +14888,7 @@ function getConfigParserSettings(staticSiteGenerator, path) {
}

return {
configurationFile: './next.config.js',
configurationFile: generatorConfigFile || './next.config.js',
blankConfigurationFile: __nccwpck_require__.ab + "next.js",
properties: {
// Configure a base path
Expand All @@ -14898,7 +14901,7 @@ function getConfigParserSettings(staticSiteGenerator, path) {
}
case 'gatsby':
return {
configurationFile: './gatsby-config.js',
configurationFile: generatorConfigFile || './gatsby-config.js',
blankConfigurationFile: __nccwpck_require__.ab + "gatsby.js",
properties: {
// Configure a path prefix
Expand All @@ -14911,10 +14914,10 @@ function getConfigParserSettings(staticSiteGenerator, path) {
}

// Inject Pages configuration in a given static site generator's configuration file
function setPagesPath({staticSiteGenerator, path}) {
function setPagesPath({ staticSiteGenerator, generatorConfigFile, path }) {
try {
// Parse the configuration file and try to inject the Pages configuration in it
const settings = getConfigParserSettings(staticSiteGenerator, path)
const settings = getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, path })
new ConfigParser(settings).injectAll()
} catch (error) {
// Logging
Expand All @@ -14925,7 +14928,7 @@ function setPagesPath({staticSiteGenerator, path}) {
}
}

module.exports = {getConfigParserSettings, setPagesPath}
module.exports = { getConfigParserSettings, setPagesPath }


/***/ }),
Expand Down Expand Up @@ -16417,13 +16420,13 @@ const outputPagesBaseUrl = __nccwpck_require__(7527)

async function main() {
try {
const { repositoryNwo, githubToken, enablement, staticSiteGenerator } = getContext()
const { repositoryNwo, githubToken, enablement, staticSiteGenerator, generatorConfigFile } = getContext()

const pageObject = await findOrCreatePagesSite({ repositoryNwo, githubToken, enablement })
const siteUrl = new URL(pageObject.html_url)

if (staticSiteGenerator) {
setPagesPath({ staticSiteGenerator, path: siteUrl.pathname })
setPagesPath({ staticSiteGenerator, generatorConfigFile, path: siteUrl.pathname })
}
outputPagesBaseUrl(siteUrl)
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"description": "A GitHub Action to enable Pages and extract various metadata about a site. It can also be used to configure various static site generators we support as starter workflows.",
"main": "./dist/index.js",
"scripts": {
"prepare": "ncc build src/index.js -o dist --source-map --license licenses.txt",
"format": "prettier --write 'src/**/*.js'",
"format:check": "prettier --check 'src/**/*.js'",
"prepare": "npm run format && ncc build src/index.js -o dist --source-map --license licenses.txt",
"test": "jest"
},
"repository": {
Expand Down
16 changes: 5 additions & 11 deletions src/api-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ describe('apiClient', () => {
})

it('handles a 409 response when the page already exists', async () => {
jest
.spyOn(axios, 'post')
.mockImplementationOnce(() => Promise.reject({ response: { status: 409 } }))
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.reject({ response: { status: 409 } }))

// Simply assert that no error is raised
const result = await apiClient.enablePagesSite({
Expand All @@ -67,9 +65,7 @@ describe('apiClient', () => {
})

it('re-raises errors on failure status codes', async () => {
jest
.spyOn(axios, 'post')
.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))

let erred = false
try {
Expand Down Expand Up @@ -98,9 +94,7 @@ describe('apiClient', () => {
})

it('re-raises errors on failure status codes', async () => {
jest
.spyOn(axios, 'get')
.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
jest.spyOn(axios, 'get').mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))

let erred = false
try {
Expand Down Expand Up @@ -203,7 +197,8 @@ describe('apiClient', () => {

it('makes second request to get page if create fails because of existence', async () => {
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
jest.spyOn(axios, 'get')
jest
.spyOn(axios, 'get')
.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
.mockImplementationOnce(() => Promise.resolve({ status: 200, data: PAGE_OBJECT }))
jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.reject({ response: { status: 409 } }))
Expand All @@ -217,5 +212,4 @@ describe('apiClient', () => {
expect(axios.post).toHaveBeenCalledTimes(1)
})
})

})
Loading

0 comments on commit 90b7c04

Please sign in to comment.