Skip to content

Commit

Permalink
Support additional configuration
Browse files Browse the repository at this point in the history
Support provision of additional configuration through a JSON file. It includes dedicated context mappings, custom descriptions , custom variables and description prefixes for the templates. E.g.:
```
{
  "templateSetName": "Vue",
  "contextMapping": {
    "vue-script-vuex": "VUE_COMPONENT_DESCRIPTOR",
    "vue-script-router": "VUE_SCRIPT",
    "vue-script": "VUE_SCRIPT",
    "vue-template": "VUE_TEMPLATE",
    "vue": "VUE_TOP_LEVEL"
  },
  "templates": {
    "vanim": {
      "description": "Vue transition component with JavaScript hooks"
    },
    "vanimhook-js": {
      "descriptionPrefix": "Vue",
      "context": [
        "VUE_COMPONENT_DESCRIPTOR"
      ]
    },
    "vprops": {
      "variables": {
        "Number": {
          "name": "type",
          "defaultValue": "Number"
        },
        "0": {
          "name": "default",
          "defaultValue": "0"
        }
      },
      "context": [
        "VUE_COMPONENT_DESCRIPTOR"
      ]
    },
    "vsrc": {
      "descriptionPrefix": "Vue"
    }
  }
}
```
  • Loading branch information
piotrtomiak authored Sep 1, 2022
1 parent 248356d commit 30c2248
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,27 @@ function mapContext(string) {
'html': "HTML",
'typescript': "TypeScript"
};
return map[string] || string;
return map[string] || config.contextMapping[string] || string;
}

function capitalize(string) {
return string[0].toUpperCase() + string.substr(1)
}

function convertSnippet(snippet, context) {
let vars = {};
const templateConfig = config.templates[snippet.prefix] ?? {}

let body = typeof snippet.body === 'string' ? snippet.body : snippet.body.join("\n");
body = body.replace(/\\t/, '');
let replacement = (str, match, name, value) => {
const shortMatch = typeof value === "number" || value === undefined;
name = !name.match(/\d*/) || shortMatch || !value.match(/^[_a-zA-Z0-9\-]+$/g) ? name : value;
value = shortMatch ? undefined : value;

const varConfig = templateConfig.variables?.[name]
name = varConfig?.name ?? name
value = varConfig?.defaultValue ?? (shortMatch ? undefined : value);

const original = name;
name = name === "0" ? "END" : name;
name = name.replace("-", "_");
Expand All @@ -48,21 +57,37 @@ function convertSnippet(snippet, context) {
body = body.replace(/\${(([_a-zA-Z0-9\-]+)(?:\s*:\s*\${([^}]+)})?)}/g, replacement);
body = body.replace(/\${(([_a-zA-Z0-9\-]+)(?:\s*:\s*([^}]+))?)}/g, replacement);

const description = templateConfig.description ??
(templateConfig.descriptionPrefix ? templateConfig.descriptionPrefix + " " + snippet.description : capitalize(snippet.description))

let templateText = `
<template name="${xmlEscape(snippet.prefix)}" value="${xmlEscape(body)}" description="${xmlEscape(snippet.description)}" toReformat="true" toShortenFQNames="true">`;
<template name="${xmlEscape(snippet.prefix)}"
value="${xmlEscape(body)}"
description="${xmlEscape(description)}"
toReformat="true" toShortenFQNames="true">`;
for (const variable of Object.keys(vars)) {
if (variable === "END") continue;

templateText += `
<variable name="${variable}" expression="" defaultValue="&quot;${vars[variable]}&quot;" alwaysStopAt="true" />`;
<variable name="${variable}" expression="" defaultValue="&quot;${vars[variable]}&quot;" alwaysStopAt="true"/>`;
}
templateText += `
if (templateConfig.context) {
templateText += `
<context>`
for (context of templateConfig.context) {
templateText += `
<option name="${context}" value="true"/>`;
}
templateText += `
</context>`
} else {
templateText += `
<context>
<option name="${mapContext(context)}" value="true" />
</context>
</template>`;
<option name="${mapContext(context)}" value="true"/>
</context>`;
}

templateText += "";
templateText += "\n </template>\n";
return templateText;
}
function convertFile(input, file) {
Expand All @@ -77,13 +102,18 @@ function convertFile(input, file) {
}

const input = process.argv[2];
const config = {
contextMapping: {},
templates: {},
...(process.argv[3] ? require(process.argv[3]) : {})
}

if (!input) {
printUsageAndExit()
}

fs.readdir(input, (err, files) => {
let templateSetText = `<templateSet group="${path.basename(path.dirname(input))}">`;
let templateSetText = `<?xml version="1.0" encoding="UTF-8"?>\n\n<templateSet group="${config.templateSetName ?? path.basename(path.dirname(input))}">`;
files.forEach(file => {
templateSetText += convertFile(input, file)
});
Expand Down

0 comments on commit 30c2248

Please sign in to comment.