From e6559bdc7c87d83feacd52fc8672d3ae4982145f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=A8=E5=8D=93?= Date: Mon, 5 Sep 2022 11:37:32 +0800 Subject: [PATCH] Convert yml to json only if yml is changed --- build/yaml-to-json.mjs | 33 +++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 build/yaml-to-json.mjs 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",