|
|
|
@ -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]; |
|
|
|
|
} |
|
|
|
|