diff --git a/build/yaml-to-json.mjs b/build/yaml-to-json.mjs new file mode 100644 index 0000000..95dab6c --- /dev/null +++ b/build/yaml-to-json.mjs @@ -0,0 +1,33 @@ +import { execSync } from 'child_process'; +import { existsSync, statSync } from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + + +function isGrammarOutdate(fileName) { + const thisFile = fileURLToPath(import.meta.url); + const json = path.join(path.dirname(thisFile), '..', 'syntaxes', fileName); + if (!existsSync(json)) { + return true; + } + const yaml = json.replace('.json', '.yml'); + const jsonState = statSync(json); + const yamlState = statSync(yaml); + if (yamlState.mtime.toString() > jsonState.mtime.toString()) { + console.log(`${yaml} changed, Grammar is outdate, re-generate it.`); + return true; + } + return false; +} + +function yamlToJson() { + if (isGrammarOutdate('cmake.tmLanguage.json')) { + execSync('npm run grammar-cmake'); + } + + if (isGrammarOutdate('cmakecache.tmLanguage.json')) { + execSync('npm run grammar-cmakecache'); + } +} + +yamlToJson(); diff --git a/package.json b/package.json index c95f93b..9d54efc 100644 --- a/package.json +++ b/package.json @@ -97,7 +97,7 @@ "vscode:prepublish": "npm run compile", "grammar-cmake": "npx js-yaml ./syntaxes/cmake.tmLanguage.yml > ./syntaxes/cmake.tmLanguage.json", "grammar-cmakecache": "npx js-yaml ./syntaxes/cmakecache.tmLanguage.yml > ./syntaxes/cmakecache.tmLanguage.json", - "grammar": "npm run grammar-cmake && npm run grammar-cmakecache", + "grammar": "node ./build/yaml-to-json.mjs", "compile": "npm run grammar && tsc -p ./", "watch": "npm run grammar && tsc -watch -p ./", "pretest": "npm run compile && npm run lint",