Skip to content

Commit

Permalink
Merge pull request #528 from fjogeleit/no-compat-mode-input
Browse files Browse the repository at this point in the history
Add input to configure noCompatMode
  • Loading branch information
fjogeleit authored Nov 17, 2022
2 parents 3eb77b1 + 2956f06 commit 7600af2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ jobs:
|workDir | Relative location of the configured `repository` | . | |
|format | Specify the used format parser of your file. WIll be guessed by file extension if not provided and uses YAML as fallback. Supports `YAML` and `JSON` ||
|method | Configures the processing of none existing properties. Possible values: `CreateOrUpdate`, `Update`, `Create` | `CreateOrUpdate` |
|noCompatMode| Removes quotes from reserved words, like Y, N, yes, no, on, etc. | `false` |

#### Methods

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
value:
required: true
description: 'New property value'
noCompatMode:
required: false
description: 'Removes quotes from reserved words, like Y, N, yes, no, on, etc.'
format:
required: false
description: 'Supported file formats, possible values are YAML and JSON, will be guessed by file extension if not provided. Falls back to YAML'
Expand Down
25 changes: 9 additions & 16 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function run(options: Options, actions: Actions): Promise<void> {
const files: ChangedFile[] = []

for (const [file, values] of Object.entries(options.changes)) {
const changedFile = processFile(file, options.format, values, options.workDir, options.method, actions)
const changedFile = processFile(file, values, options, actions)

if (changedFile) {
writeTo(changedFile.content, changedFile.absolutePath, actions)
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function runTest<T extends ContentNode>(options: Options): Promise<
const files: ChangedFile[] = []

for (const [file, values] of Object.entries(options.changes)) {
const changedFile = processFile(file, options.format, values, options.workDir, options.method, new EmptyActions())
const changedFile = processFile(file, values, options, new EmptyActions())
if (changedFile) {
files.push(changedFile)
}
Expand Down Expand Up @@ -221,32 +221,25 @@ export const convertValue = (value: string): string | number | boolean => {
return result[0]
}

export function processFile(
file: string,
format: Format,
values: ValueUpdates,
workDir: string,
method: Method,
actions: Actions
): ChangedFile | null {
const filePath = path.join(process.cwd(), workDir, file)
export function processFile(file: string, values: ValueUpdates, options: Options, actions: Actions): ChangedFile | null {
const filePath = path.join(process.cwd(), options.workDir, file)

actions.debug(`FilePath: ${filePath}, Parameter: ${JSON.stringify({cwd: process.cwd(), workDir, valueFile: file})}`)
actions.debug(`FilePath: ${filePath}, Parameter: ${JSON.stringify({cwd: process.cwd(), workDir: options.workDir, valueFile: file})}`)

format = determineFinalFormat(filePath, format, actions) as Format.JSON | Format.YAML
const format = determineFinalFormat(filePath, options.format, actions) as Format.JSON | Format.YAML

const parser = formatParser[format]

let contentNode = parser.convert(filePath)
let contentString = parser.dump(contentNode)
let contentString = parser.dump(contentNode, {noCompatMode: options.noCompatMode})

const initContent = contentString

actions.debug(`Parsed JSON: ${JSON.stringify(contentNode)}`)

for (const [propertyPath, value] of Object.entries(values)) {
contentNode = replace(value, propertyPath, contentNode, method)
contentString = parser.dump(contentNode)
contentNode = replace(value, propertyPath, contentNode, options.method)
contentString = parser.dump(contentNode, {noCompatMode: options.noCompatMode})
}

actions.debug(`Generated updated ${format.toUpperCase()}
Expand Down
15 changes: 12 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Options {
changes: Changes
format: Format
method: Method
noCompatMode: boolean
}

export class GitHubOptions implements Options {
Expand All @@ -48,11 +49,11 @@ export class GitHubOptions implements Options {
}

get commitChange(): boolean {
return core.getInput('commitChange') === 'true'
return core.getBooleanInput('commitChange')
}

get updateFile(): boolean {
return core.getInput('updateFile') === 'true'
return core.getBooleanInput('updateFile')
}

get targetBranch(): string {
Expand All @@ -68,7 +69,11 @@ export class GitHubOptions implements Options {
}

get createPR(): boolean {
return core.getInput('createPR') === 'true'
return core.getBooleanInput('createPR')
}

get noCompatMode(): boolean {
return core.getBooleanInput('noCompatMode')
}

get token(): string {
Expand Down Expand Up @@ -223,6 +228,10 @@ export class EnvOptions implements Options {
return process.env.CREATE_PR === 'true'
}

get noCompatMode(): boolean {
return process.env.NO_COMPAT_MODE === 'true'
}

get message(): string {
return process.env.MESSAGE || ''
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const YAMLParser = {
convert<T extends ContentNode>(filePath: string): T {
return validateContent<T>(YAML.load(readFile(filePath)) as T, Format.YAML)
},
dump<T extends ContentNode>(content: T): string {
return YAML.dump(content, {lineWidth: -1, noCompatMode: true})
dump<T extends ContentNode>(content: T, options?: {noCompatMode: boolean}): string {
return YAML.dump(content, {lineWidth: -1, noCompatMode: options?.noCompatMode})
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ export enum Format {

export type FormatParser = {
convert<T extends ContentNode>(filePath: string): T
dump<T extends ContentNode>(content: T): string
dump<T extends ContentNode>(content: T, options?: {[key: string]: string | boolean | number}): string
}

0 comments on commit 7600af2

Please sign in to comment.