parse config files

pull/5370/head
filip mertens 2 years ago
parent 5b31c0483a
commit 2c8eeb25a3
  1. 103
      apps/remix-ide/src/app/plugins/code-format.ts
  2. 1
      package.json
  3. 2
      yarn.lock

@ -7,6 +7,8 @@ import * as ts from 'prettier/parser-typescript'
import * as babel from 'prettier/parser-babel'
import * as espree from 'prettier/parser-espree'
import path from 'path'
import yaml from 'js-yaml'
import toml from 'toml'
const profile = {
name: 'codeFormatter',
@ -28,10 +30,6 @@ export class CodeFormat extends Plugin {
const content = await this.call('fileManager', 'readFile', file)
if (!content) return
let parserName = ''
// parse TOML file
// parse YAML file
// parse JSON file
let options: Options = {
}
switch (path.extname(file)) {
@ -62,24 +60,76 @@ export class CodeFormat extends Plugin {
parserName = 'json'
break
}
const possibleFileNames = [
'.prettierrc',
'.prettierrc.json',
'.prettierrc.yaml',
'.prettierrc.yml',
'.prettierrc.toml',
'prettier.js',
'prettier.cjs',
'prettier.config.js',
'prettier.config.cjs',
'prettier.config.mjs',
'prettier.config.ts',
]
// find first file that exists
const prettierConfigFile = possibleFileNames.find(async fileName => await this.call('fileManager', 'exists', fileName))
const possibleFileNames = [
'.prettierrc',
'.prettierrc.json',
'.prettierrc.yaml',
'.prettierrc.yml',
'.prettierrc.toml',
'.prettierrc.js',
'.prettierrc.cjs',
'prettier.config.js',
'prettier.config.cjs',
'.prettierrc.json5',
]
console.log(prettierConfigFile)
const prettierConfigFile = await findAsync(possibleFileNames, async (fileName) => {
const exists = await this.call('fileManager', 'exists', fileName)
return exists
})
let parsed = null
if (prettierConfigFile) {
let prettierConfig = await this.call('fileManager', 'readFile', prettierConfigFile)
if (prettierConfig) {
if (prettierConfigFile.endsWith('.yaml') || prettierConfigFile.endsWith('.yml')) {
parsed = yaml.load(prettierConfig)
} else if (prettierConfigFile.endsWith('.toml')) {
parsed = toml.parse(prettierConfig)
} else if (prettierConfigFile.endsWith('.json') || prettierConfigFile.endsWith('.json5')) {
parsed = JSON.parse(prettierConfig)
} else if (prettierConfigFile === '.prettierrc') {
try {
parsed = JSON.parse(prettierConfig)
} catch (e) {
// do nothing
}
if (!parsed) {
try {
parsed = yaml.load(prettierConfig)
} catch (e) {
// do nothing
}
}
} else if (prettierConfigFile.endsWith('.js') || prettierConfigFile.endsWith('.cjs')) {
// remove any comments
prettierConfig = prettierConfig.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '')
// add quotes to keys
prettierConfig = prettierConfig.replace(/([a-zA-Z0-9_]+)(\s*):/g, '"$1"$2:')
// remove comma from last key
prettierConfig = prettierConfig.replace(/,(\s*})/g, '$1')
// remove any semi-colons
prettierConfig = prettierConfig.replace(/;/g, '')
// convert single quotes to double quotes
prettierConfig = prettierConfig.replace(/'/g, '"')
try {
parsed = JSON.parse(prettierConfig.replace('module.exports = ', '').replace('module.exports=', ''))
} catch (e) {
// do nothing
}
}
}
}
if(!parsed && prettierConfigFile) {
this.call('notification', 'toast', `Error parsing prettier config file: ${prettierConfigFile}`)
}
// merge options
if (parsed) {
options = {
...options,
...parsed,
}
}
const result = prettier.format(content, {
plugins: [sol as any, ts, babel, espree],
parser: parserName,
@ -93,12 +143,9 @@ export class CodeFormat extends Plugin {
}
function getRange(index, node) {
if (node.range) {
return node.range[index];
}
if (node.expression && node.expression.range) {
return node.expression.range[index];
}
return null;
async function findAsync(arr, asyncCallback) {
const promises = arr.map(asyncCallback);
const results = await Promise.all(promises);
const index = results.findIndex(result => result);
return arr[index];
}

@ -194,6 +194,7 @@
"isbinaryfile": "^3.0.2",
"isomorphic-git": "^1.8.2",
"jquery": "^3.3.1",
"js-yaml": "^4.1.0",
"jszip": "^3.6.0",
"latest-version": "^5.1.0",
"merge": "^2.1.1",

@ -14731,7 +14731,7 @@ js-yaml@4.0.0:
dependencies:
argparse "^2.0.1"
js-yaml@4.1.0:
js-yaml@4.1.0, js-yaml@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==

Loading…
Cancel
Save