Skip to content

Commit

Permalink
feat: 支持 tmLanguage 格式文件自动转换为 json 格式
Browse files Browse the repository at this point in the history
  • Loading branch information
vagusX committed Dec 11, 2020
1 parent be9e1d3 commit dc3713b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"scripts": {
"download": "node scripts/update-extensions.js",
"generate": "rm -rf lib && node scripts/generate.js",
"prepublishOnly": "npm run download && npm run generate"
"update": "npm run download && npm run generate",
"pub": "npm run update && npm run release && tnpm publish",
"pub:beta": "npm run update && npm run release -- --prerelease beta && tnpm publish --tag beta",
"release": "standard-version"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -47,6 +50,10 @@
"prettier": "^2.0.5",
"prettier-config-standard": "^1.0.1",
"rimraf": "^3.0.2",
"standard-version": "^9.0.0",
"strip-json-comments": "^3.1.1"
},
"dependencies": {
"plist": "^3.0.1"
}
}
60 changes: 49 additions & 11 deletions scripts/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path')
const { promisify } = require('util')

const { template, templateSettings } = require('lodash')
const plist = require('plist')
const fse = require('fs-extra')
const prettier = require('prettier')
const bluebird = require('bluebird')
Expand Down Expand Up @@ -65,20 +66,26 @@ class Extension {
* use `non-greedy mode`
*/
const languageStr = JSON.stringify(data.languages).replace(
/"configuration":\s*(".+?.json?")/g,
/"configuration":\s*(".+?(?:.json)")/g,
`${ResolvedConfigFiled}:${requireKeyword}($1)`
)
/**
* 处理以下字符串,因为有 tmLanguage 后缀,因此将 `.json` 设置为可选匹配项
* `"path": "./syntaxes/JavaScript.tmLanguage.json"`
* <del>`"path": "./syntaxes/Regular Expressions (JavaScript).tmLanguage"`</del>
* `"path": "./syntaxes/Regular Expressions (JavaScript).tmLanguage"`
* 由于 require 语法导致在 webpack 中需要额外添加 raw-loader 去处理 tmLanguage 文件
* 且会导致 resolvedContent 的内容格式不统一
* 因此目前都通过 fork vscode 插件 [TextMate Languages] 将 tmLanguage 转成 json
* <del>因此目前都通过 fork vscode 插件 [TextMate Languages] 将 tmLanguage 转成 json</del>
* 已使用 `plist` 直接将 tmLanguage 文件转换成 json 格式
*/
const grammarStr = JSON.stringify(data.grammars).replace(
/"path":\s*(".+?[.json]?")/g,
`${ResolvedConfigFiled}:${requireKeyword}($1)`
// 为了处理 tmLanguage 转 json 的问题,将结尾的 `"` 号放在捕获分组之外了
/"path":\s*(".+?(\.json|\.tmLanguage?))"/g,
(_, p1, p2) => {
// 下方 this.copyTextmateFiles 会处理 tmLanguage 转 json 的逻辑
const suffix = p2 === '.tmLanguage' ? p1 + '.json' : p1
return `${ResolvedConfigFiled}:${requireKeyword}(${suffix}")`
}
)

const content = compiled({
Expand Down Expand Up @@ -116,7 +123,7 @@ class Extension {
configuration: './' + targetFilename.trim()
})

return this.copyFileWithoutComments(
return this.copyTextmateFiles(
path.resolve(this.extPath, language.configuration),
path.resolve(extOutDir, targetFilename)
)
Expand Down Expand Up @@ -156,7 +163,7 @@ class Extension {
...grammar,
path: './syntaxes/' + targetFilename.trim()
})
return this.copyFileWithoutComments(
return this.copyTextmateFiles(
path.resolve(this.extPath, grammar.path),
path.resolve(grammarDir, targetFilename)
)
Expand All @@ -168,14 +175,45 @@ class Extension {
)
}

async copyFileWithoutComments(from, to, stripComment = true) {
async copyTextmateFiles(from, to) {
const ext = path.extname(from);
switch (ext) {
case '.json':
this.copyJSONFileWithoutComments(from, to);
break;
case '.tmLanguage':
this.convertTmFileToJson(from, to);
break;
}
}

async copyJSONFileWithoutComments(from, to) {
if (path.extname(from) !== '.json') {
console.warn(`${from} is not a json file skipped`)
return
}
const jsonContent = await promisify(fs.readFile)(from, { encoding: 'utf8' })
const newContent = stripJsonComments(jsonContent, { whitespace: false })
await promisify(fs.writeFile)(to, newContent, { encoding: 'utf8' })
let jsonContent = await promisify(fs.readFile)(from, { encoding: 'utf8' })
jsonContent = stripJsonComments(jsonContent, { whitespace: false })
await promisify(fs.writeFile)(to, jsonContent, { encoding: 'utf8' })
}

async convertTmFileToJson(from, to) {
if (path.extname(from) !== '.tmLanguage') {
return
}

let pListContent = await promisify(fs.readFile)(from, { encoding: 'utf8' })
try {
const plistDesc = plist.parse(pListContent)
pListContent = JSON.stringify(plistDesc, null, 2)
} catch (error) {
console.log(`Parse error for: ${from}`)
console.error(error)
}

// 将目标文件转换成 json 格式
to = to + '.json'
await promisify(fs.writeFile)(to, pListContent, { encoding: 'utf8' })
}

toJSON() {
Expand Down

0 comments on commit dc3713b

Please sign in to comment.