Skip to content

Commit

Permalink
feat(prettier-plugin-sh): build languages from linguist definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Oct 2, 2019
1 parent 24f657d commit ffdab4e
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 254 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
.changelog
.type-coverage
coverage
node_modules
lib
node_modules
packages/sh/src/languages.ts
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage
fixtures
lib
node_modules
packages/sh/src/languages.ts
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"packages/*"
],
"scripts": {
"build": "run-p build:*",
"build": "lerna run build && run-p build:*",
"build:r": "r -g \"{'prettier/parser-babylon':'prettierPlugins.babylon','prettier-plugin-pkg':'prettierPlugins.pkg','prettier-plugin-sh':'prettierPlugins.sh'}\" -x mvdan-sh -p",
"build:ts": "tsc -b",
"lint": "run-p lint:*",
Expand All @@ -26,7 +26,7 @@
"@1stg/eslint-config": "^0.12.18",
"@1stg/husky-config": "^0.3.0",
"@1stg/lint-staged": "^0.8.3",
"@1stg/prettier-config": "^0.3.4",
"@1stg/prettier-config": "^0.4.0",
"@1stg/remark-config": "^0.2.2",
"@1stg/rollup-config": "^0.12.0",
"@1stg/tsconfig": "^0.6.0",
Expand All @@ -37,10 +37,12 @@
"@pkgr/imagemin": "^0.1.0",
"@types/jest": "^24.0.18",
"@types/lodash": "^4.14.141",
"@types/js-yaml": "^3.12.1",
"@types/prettier": "^1.18.2",
"eslint": "^6.5.1",
"husky": "^3.0.7",
"husky": "^3.0.8",
"jest": "^24.9.0",
"js-yaml": "^3.13.1",
"lerna": "^3.16.4",
"lerna-changelog": "^0.8.2",
"lint-staged": "^9.4.1",
Expand All @@ -50,6 +52,7 @@
"react": "^16.10.1",
"rollup": "^1.22.0",
"ts-jest": "^24.1.0",
"ts-node": "^8.4.1",
"tslint": "^5.20.0",
"type-coverage": "^2.3.0",
"typescript": "^3.6.3"
Expand All @@ -63,9 +66,10 @@
]
},
"eslintIgnore": [
"!.*.js",
"**/CHANGELOG.md",
"lib",
"!.*.js"
"packages/sh/src/languages.ts"
],
"jest": {
"preset": "ts-jest",
Expand Down
6 changes: 4 additions & 2 deletions packages/sh/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
![banner](https://raw.githubusercontent.com/rx-ts/prettier/master/assets/sh.png)
<p align="center">
<img src="https://raw.githubusercontent.com/rx-ts/prettier/master/assets/sh.png" height="50" />
</p>

# prettier-plugin-sh ![npm bundle size](https://img.shields.io/bundlephobia/min/prettier-plugin-sh) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/prettier-plugin-sh)

> An opinionated `shellscript、Dockerfile、properties、gitignore、dotenv、hosts、jvmoptions... DocumentFormat` formatter plugin for [Prettier][]
> An opinionated `shellscript、Dockerfile、properties、gitignore、dotenv、hosts、jvmoptions...` formatter plugin for [Prettier][]
Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing, taking various rules into account.

Expand Down
92 changes: 92 additions & 0 deletions packages/sh/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* eslint-disable node/no-extraneous-import */
import fs from 'fs'
import { get } from 'https'
import { safeLoad } from 'js-yaml'
import { pick } from 'lodash'
import prettier, { SupportLanguage } from 'prettier'

const linguistLanguages =
'https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml'

export interface LinguistLanguage {
type: string
group: string
color: string
aliases: string[]
extensions: string[]
filenames: string[]
tm_scope: string
ace_mode: string
codemirror_mode: string
codemirror_mime_type: string
language_id: number
}

const EXTRA_LANGUAGES: SupportLanguage[] = [
{
name: 'JvmOptions',
parsers: ['sh'],
extensions: ['.vmoptions'],
filenames: ['jvm.options'],
vscodeLanguageIds: ['jvmoptions'],
},
{
name: 'hosts',
parsers: ['sh'],
filenames: ['hosts'],
vscodeLanguageIds: ['hosts'],
},
{
name: 'dotenv',
parsers: ['sh'],
extensions: ['.env'],
vscodeLanguageIds: ['dotenv'],
},
]

const getShLanguages = (languages: Record<string, LinguistLanguage>) =>
Object.entries(languages)
.reduce<SupportLanguage[]>((acc, [name, language]) => {
const { ace_mode: aceMode } = language
if (
!['dockerfile', 'gitignore', 'ini', 'properties', 'sh'].includes(
aceMode,
)
) {
return acc
}
acc.push({
name,
parsers: ['sh'],
...pick(language, 'group', 'aliases', 'extensions', 'filenames'),
tmScope: language.tm_scope,
aceMode,
codemirrorMode: language.codemirror_mode,
codemirrorMimeType: language.codemirror_mime_type,
linguistLanguageId: language.language_id,
vscodeLanguageIds: [aceMode === 'sh' ? 'shellscript' : aceMode],
})
return acc
}, [])
.concat(EXTRA_LANGUAGES)

get(linguistLanguages, res => {
let rawText = ''
res.on('data', (data: Buffer) => {
rawText += data.toString()
})
res.on('end', () => {
const languages = getShLanguages(safeLoad(rawText))
fs.writeFileSync(
'src/languages.ts',
prettier.format(
`import { SupportLanguage } from 'prettier'
export const languages = ${JSON.stringify(languages)} as SupportLanguage[]`,
{
parser: 'typescript',
},
),
)
})
})
13 changes: 10 additions & 3 deletions packages/sh/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "prettier-plugin-sh",
"version": "0.1.0",
"description": "An opinionated shellscript formatter plugin for Prettier",
"description": "An opinionated `shellscript、Dockerfile、properties、gitignore、dotenv、hosts、jvmoptions...` formatter plugin for Prettier",
"repository": "git@github.com/rx-ts/prettier.git",
"homepage": "https://github.com/rx-ts/prettier/blob/master/packages/sh",
"author": "JounQin <admin@1stg.me>",
Expand All @@ -14,15 +14,22 @@
"unpkg": "lib/umd",
"types": "lib",
"files": [
"lib",
"typings.d.ts"
"lib"
],
"keywords": [
"dockerfile",
"ini",
"properties",
"sh",
"shell",
"shellscript",
"plugin",
"prettier",
"prettier-plugin"
],
"scripts": {
"build": "ts-node -O '{\"module\":\"commonjs\"}' build"
},
"peerDependencies": {
"prettier": "^1.18.2"
},
Expand Down

0 comments on commit ffdab4e

Please sign in to comment.