Convert yml to json only if yml is changed

This commit is contained in:
全卓 2022-09-05 11:37:32 +08:00
parent 8005b3dce5
commit e6559bdc7c
2 changed files with 34 additions and 1 deletions

33
build/yaml-to-json.mjs Normal file
View File

@ -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();

View File

@ -97,7 +97,7 @@
"vscode:prepublish": "npm run compile", "vscode:prepublish": "npm run compile",
"grammar-cmake": "npx js-yaml ./syntaxes/cmake.tmLanguage.yml > ./syntaxes/cmake.tmLanguage.json", "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-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 ./", "compile": "npm run grammar && tsc -p ./",
"watch": "npm run grammar && tsc -watch -p ./", "watch": "npm run grammar && tsc -watch -p ./",
"pretest": "npm run compile && npm run lint", "pretest": "npm run compile && npm run lint",