Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
refactor: use scalar.config.json instead of scalar.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Feb 9, 2024
1 parent c14a559 commit 6a8e8e5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ If you’re tired of passing the file name again and again, just configure it on
scalar init
```

This will create a `scalar.toml` file for you. All commands will use the configured OpenAPI file by default.
This will create a `scalar.config.json` file for you. All commands will use the configured OpenAPI file by default.

## Options

Expand Down
26 changes: 15 additions & 11 deletions packages/cli/src/commands/init/InitCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import kleur from 'kleur'
import fs from 'node:fs'
import path from 'node:path'
import prompts from 'prompts'
import toml from 'toml-js'

import { CONFIG_FILE } from '../../utils'

export function InitCommand() {
const cmd = new Command('init')

cmd.description('Create a new `scalar.toml` file')
cmd.description('Create a new `scalar.config.json` file')
cmd.option('-f, --file [file]', 'your OpenAPI file')
cmd.action(async ({ file }) => {
// Path to `scalar.toml` file
const configFile = path.resolve('scalar.toml')
// Path to `scalar.config.json` file
const configFile = path.resolve(CONFIG_FILE)

// Check if `scalar.toml` already exists
// Check if `scalar.config.json` already exists
if (fs.existsSync(configFile)) {
console.warn(kleur.yellow('A `scalar.toml` file already exists.'))
console.warn(kleur.yellow(`A ${CONFIG_FILE} file already exists.`))
console.log()

const { overwrite } = await prompts({
Expand All @@ -36,7 +37,7 @@ export function InitCommand() {

// Ask for the OpenAPI file
const configuration = {
reference: { file: '' },
references: [],
}

const { input } = file
Expand All @@ -53,12 +54,15 @@ export function InitCommand() {
},
})

configuration.reference.file = input
configuration.references.push({
name: 'API Reference',
path: input,
})

const content = toml.dump(configuration)
const content = JSON.stringify(configuration, null, 2)

console.log()
console.log(kleur.bold().white(' scalar.toml'))
console.log(kleur.bold().white(` ${CONFIG_FILE}`))
console.log()
console.log(
content
Expand All @@ -69,7 +73,7 @@ export function InitCommand() {
)
console.log()

// Create `scalar.toml` file
// Create `scalar.config.json` file
fs.writeFileSync(configFile, content)

console.log(kleur.green('Created a new project configuration.'))
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/src/commands/init/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ScalarCli } from '../../../tests/invoke-cli'
describe('init', () => {
it('creates a config file', () => {
// Delete config file if it exists
const configFile = './scalar.toml'
const configFile = './scalar.config.json'

if (fs.existsSync(configFile)) {
fs.unlinkSync(configFile)
Expand All @@ -33,6 +33,14 @@ describe('init', () => {
expect(fs.readFileSync(configFile, 'utf-8')).toContain(
'./packages/cli/src/commands/validate/valid.json',
)
expect(JSON.parse(fs.readFileSync(configFile, 'utf-8'))).toMatchObject({
references: [
{
name: 'API Reference',
path: './packages/cli/src/commands/validate/valid.json',
},
],
})
expect(exitCode).toBe(0)

fs.unlinkSync(configFile)
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/utils/useGivenFileOrConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import kleur from 'kleur'
import toml from 'toml-js'

import { readFile } from './'

export const CONFIG_FILE = 'scalar.config.json'

export function useGivenFileOrConfiguration(file?: string) {
// If a specific file is given, use it.
if (file) {
Expand All @@ -11,7 +12,7 @@ export function useGivenFileOrConfiguration(file?: string) {

// Try to load the configuration
try {
const configuration = toml.parse(readFile('scalar.toml'))
const configuration = JSON.parse(readFile(CONFIG_FILE))

if (configuration?.reference?.file) {
return configuration.reference.file
Expand Down

0 comments on commit 6a8e8e5

Please sign in to comment.