Skip to content

Commit

Permalink
Merge pull request #13 from actions/prettier
Browse files Browse the repository at this point in the history
Prettier formatting
  • Loading branch information
James M. Greene authored and GitHub committed Aug 5, 2022
2 parents cc95980 + 06406d7 commit 404d23c
Show file tree
Hide file tree
Showing 27 changed files with 145 additions and 174 deletions.
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
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
40 changes: 12 additions & 28 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 @@ -14585,9 +14585,7 @@ class ConfigParser {
findConfigurationObject(ast) {
// Try to find a default export
var defaultExport = ast.body.find(
node =>
node.type === 'ExportDefaultDeclaration' &&
node.declaration.type === 'ObjectExpression'
node => node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ObjectExpression'
)
if (defaultExport) {
core.info('Found configuration object in default export declaration')
Expand All @@ -14608,19 +14606,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 +14640,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 +14660,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 @@ -14750,11 +14738,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 +14785,7 @@ class ConfigParser {
}
}

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


/***/ }),
Expand Down Expand Up @@ -14833,7 +14817,7 @@ function getContext() {
return requiredVars
}

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


/***/ }),
Expand All @@ -14859,7 +14843,7 @@ 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
Expand Down Expand Up @@ -14911,7 +14895,7 @@ function getConfigParserSettings(staticSiteGenerator, path) {
}

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

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


/***/ }),
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)
})
})

})
32 changes: 8 additions & 24 deletions src/config-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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 @@ -57,9 +57,7 @@ class ConfigParser {
findConfigurationObject(ast) {
// Try to find a default export
var defaultExport = ast.body.find(
node =>
node.type === 'ExportDefaultDeclaration' &&
node.declaration.type === 'ObjectExpression'
node => node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ObjectExpression'
)
if (defaultExport) {
core.info('Found configuration object in default export declaration')
Expand All @@ -80,19 +78,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 @@ -120,9 +112,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 @@ -142,9 +132,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 @@ -222,11 +210,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 @@ -273,4 +257,4 @@ class ConfigParser {
}
}

module.exports = {ConfigParser}
module.exports = { ConfigParser }
10 changes: 5 additions & 5 deletions src/config-parser.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs')

const {ConfigParser} = require('./config-parser')
const {getTempFolder, compareFiles} = require('./test-helpers')
const { ConfigParser } = require('./config-parser')
const { getTempFolder, compareFiles } = require('./test-helpers')

// Get the temp folder
const tempFolder = getTempFolder()
Expand Down Expand Up @@ -134,15 +134,15 @@ const cases = [
]

describe('config-parser', () => {
cases.forEach(({property, source, expected}, index) => {
cases.forEach(({ property, source, expected }, index) => {
it(`Inject path properly for case #${index}`, () => {
// Write the source file
const sourceFile = `${tempFolder}/source.js`
fs.writeFileSync(sourceFile, source, {encoding: 'utf8'})
fs.writeFileSync(sourceFile, source, { encoding: 'utf8' })

// Write the expected file
const expectedFile = `${tempFolder}/expected.js`
fs.writeFileSync(expectedFile, expected, {encoding: 'utf8'})
fs.writeFileSync(expectedFile, expected, { encoding: 'utf8' })

// Update the settings and do the injection
new ConfigParser({
Expand Down
2 changes: 1 addition & 1 deletion src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ function getContext() {
return requiredVars
}

module.exports = {getContext}
module.exports = { getContext }
2 changes: 1 addition & 1 deletion src/fixtures/gatsby/blank.expected.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Default Pages configuration for Gatsby
module.exports = { pathPrefix: "/docs/" }
module.exports = { pathPrefix: '/docs/' }
2 changes: 1 addition & 1 deletion src/fixtures/gatsby/blank.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// This file is not read by the test suite
// This file is not read by the test suite
8 changes: 4 additions & 4 deletions src/fixtures/gatsby/default.expected.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
pathPrefix: "/docs/",
pathPrefix: '/docs/',
siteMetadata: {
title: `My Gatsby Site`,
siteUrl: `https://www.yourdomain.tld`,
siteUrl: `https://www.yourdomain.tld`
},
plugins: [],
}
plugins: []
}
6 changes: 3 additions & 3 deletions src/fixtures/gatsby/default.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
siteMetadata: {
title: `My Gatsby Site`,
siteUrl: `https://www.yourdomain.tld`,
siteUrl: `https://www.yourdomain.tld`
},
plugins: [],
}
plugins: []
}
5 changes: 1 addition & 4 deletions src/fixtures/next/blank.expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Default Pages configuration for Next
const nextConfig = {
experimental: {images: {unoptimized: true}},
basePath: '/docs'
}
const nextConfig = { experimental: { images: { unoptimized: true } }, basePath: '/docs' }
module.exports = nextConfig
2 changes: 1 addition & 1 deletion src/fixtures/next/blank.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// This file is not read by the test suite
// This file is not read by the test suite
Loading

0 comments on commit 404d23c

Please sign in to comment.