[feat] index.js: use async read/write

This commit is contained in:
olemorud
2023-03-14 15:20:54 +01:00
parent 850281ad51
commit bb243c024a

View File

@@ -2,31 +2,43 @@ const path = require('path');
const fs = require('node:fs'); const fs = require('node:fs');
const core = require('@actions/core'); const core = require('@actions/core');
// C:\Users\ => C\:\\Users\\ // escapeRegExp :: string => string
// escape all characters with special meanings in regexp
const escapeRegExp = (s) => const escapeRegExp = (s) =>
s.replace(/[:.*+?^${}()|\/[\]\\]/g, "\\$&"); s.replace(/[:.*+?^${}()|\/[\]\\]/g, "\\$&");
// variable :: string => RegExp
// ${{ key }}, ${{var}}, ${{ aggqq43g3qg4 }} // create regex to match ${{ key }}
const variable = (key) => const variable = (key) =>
new RegExp("\\${{\\s*?" + key + "\\s*?}}", "g"); new RegExp("\\${{\\s*?" + key + "\\s*?}}", "g");
// templatePath :: string
// default value set in /action.yml
const root = core.getInput('root', { required: false });
const templatePath = path.join(__dirname, "gcc_matcher.jsontemplate"); const templatePath = path.join(__dirname, "gcc_matcher.jsontemplate");
const parsed = // matcherPath :: string
fs.readFileSync(templatePath, "ascii")
.replace(variable("BASE"), escapeRegExp(root));
const matcherPath = path.join(__dirname, "gcc_matcher.json"); const matcherPath = path.join(__dirname, "gcc_matcher.json");
fs.writeFileSync(matcherPath, parsed); // parse :: IO => IO => Error | null
const parse = (templatePath) => (matcherPath) => {
fs.readFile(templatePath, 'utf-8', (err, content) => {
if (err) throw err;
console.log('::add-matcher::' + matcherPath); const root = core.getInput('root', { required: false });
/* for testing */ const parsed = content.replace(variable("BASE"), escapeRegExp(root));
fs.writeFile(matcherPath, parsed, (err) => {
if (err) throw err;
console.log('::add-matcher::' + matcherPath);
});
});
}
// main:
parse(templatePath)(matcherPath);
// for testing
exports.escapeRegExp = escapeRegExp exports.escapeRegExp = escapeRegExp
exports.variable = variable; exports.variable = variable;