forked from jhipster/generator-jhipster
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import prettier from 'prettier'; | ||
import prettierPluginJava from 'prettier-plugin-java'; | ||
import prettierPluginProperties from 'prettier-plugin-properties'; | ||
import prettierPluginPackagejson from 'prettier-plugin-packagejson'; | ||
|
||
export default async ({ | ||
relativeFilePath, | ||
filePath, | ||
fileContents, | ||
prettierOptions, | ||
prettierPackageJson, | ||
prettierJava, | ||
prettierProperties, | ||
}) => { | ||
try { | ||
const resolvedDestinationFileOptions = await prettier.resolveConfig(relativeFilePath); | ||
const fileOptions = { | ||
// Config from disk | ||
...resolvedDestinationFileOptions, | ||
plugins: [], | ||
// for better errors | ||
filepath: relativeFilePath, | ||
...prettierOptions, | ||
}; | ||
if (prettierPackageJson && filePath.endsWith('package.json')) { | ||
fileOptions.plugins.push(prettierPluginPackagejson); | ||
} | ||
if (prettierJava && filePath.endsWith('.java')) { | ||
fileOptions.plugins.push(prettierPluginJava); | ||
} | ||
if (prettierProperties) { | ||
fileOptions.plugins.push(prettierPluginProperties); | ||
} | ||
const data = await prettier.format(fileContents, fileOptions); | ||
return { fileContents: data }; | ||
} catch (error) { | ||
let errorMessage; | ||
if (fileContents) { | ||
errorMessage = `Error parsing file ${relativeFilePath}: ${error} | ||
At: ${fileContents | ||
.split('\n') | ||
.map((value, idx) => `${idx + 1}: ${value}`) | ||
.join('\n')}`; | ||
} else { | ||
errorMessage = `Unknown prettier error: ${error}`; | ||
} | ||
return { errorMessage }; | ||
} | ||
}; |