Skip to content

Commit

Permalink
Fix: prettiered and linted (#108)
Browse files Browse the repository at this point in the history
* Prettiered and linted

* Update package.json

* Update package.json
  • Loading branch information
ralfhandl authored Mar 10, 2021
1 parent 408cce9 commit 15fc5fd
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 87 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"env": {
"commonjs": true,
"es2021": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 12
},
"rules": {}
}
19 changes: 10 additions & 9 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"hbenl.vscode-mocha-test-adapter",
"hbenl.vscode-test-explorer",
"redhat.vscode-xml"
]
}
"recommendations": [
"bierner.github-markdown-preview",
"dbaeumer.vscode-eslint",
"eg2.vscode-npm-script",
"esbenp.prettier-vscode",
"hbenl.vscode-mocha-test-adapter",
"hbenl.vscode-test-explorer",
"redhat.vscode-xml"
]
}
10 changes: 8 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@
],
"xml.catalogs": [
"./node_modules/odata-csdl/catalog.xml"
]
}
],
"mochaExplorer.esmLoader": false,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
}
84 changes: 45 additions & 39 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,68 @@
#!/usr/bin/env node
'use strict';
"use strict";

//console.dir(argv);

const csdl = require('odata-csdl');
const lib = require('./csdl2markdown');
const minimist = require('minimist');
const fs = require('fs');
const csdl = require("odata-csdl");
const lib = require("./csdl2markdown");
const minimist = require("minimist");
const fs = require("fs");

var unknown = false;

var argv = minimist(process.argv.slice(2), {
string: ["t", "target"],
boolean: ["h", "help"],
alias: {
h: "help",
t: "target"
},
unknown: (arg) => {
if (arg.substring(0, 1) == '-') {
console.error('Unknown option: ' + arg);
unknown = true;
return false;
}
string: ["t", "target"],
boolean: ["h", "help"],
alias: {
h: "help",
t: "target",
},
unknown: (arg) => {
if (arg.substring(0, 1) == "-") {
console.error("Unknown option: " + arg);
unknown = true;
return false;
}
},
});

if (unknown || argv._.length == 0 || argv.h) {
console.log(`Usage: odata-vocab2md <options> <source file>
console.log(`Usage: odata-vocab2md <options> <source file>
Options:
-h, --help show this info
-t, --target target file (default: source file basename + .md)`);
} else {
//TODO: further input parameters reserved for e.g. referenced CSDL documents
// for (var i = 0; i < argv._.length; i++) {
// convert(argv._[i]);
// }
convert(argv._[0]);
//TODO: further input parameters reserved for e.g. referenced CSDL documents
// for (var i = 0; i < argv._.length; i++) {
// convert(argv._[i]);
// }
convert(argv._[0]);
}

function convert(source) {
if (!fs.existsSync(source)) {
console.error('Source file not found: ' + source);
return;
}
if (!fs.existsSync(source)) {
console.error("Source file not found: " + source);
return;
}

const text = fs.readFileSync(source, 'utf8');
//TODO: for JSON input use parser that produces line numbers
const json = text.startsWith('<') ? csdl.xml2json(text, true) : JSON.parse(text);
if (json.$Version < '4.0') {
console.error('Only OData Version 4.0 or greater is supported');
return;
}
const text = fs.readFileSync(source, "utf8");
//TODO: for JSON input use parser that produces line numbers
const json = text.startsWith("<")
? csdl.xml2json(text, true)
: JSON.parse(text);
if (json.$Version < "4.0") {
console.error("Only OData Version 4.0 or greater is supported");
return;
}

const target = argv.target || (source.lastIndexOf('.') <= 0 ? source : source.substring(0, source.lastIndexOf('.'))) + '.md';
console.log(target);
const target =
argv.target ||
(source.lastIndexOf(".") <= 0
? source
: source.substring(0, source.lastIndexOf("."))) + ".md";
console.log(target);

const markdown = lib.csdl2markdown(source, json);
const markdown = lib.csdl2markdown(source, json);

fs.writeFileSync(target, markdown.join('\n'));
}
fs.writeFileSync(target, markdown.join("\n"));
}
2 changes: 1 addition & 1 deletion lib/csdl2markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
lines.push("");
lines.push(schema[voc.Core.Description]);

longDescription = schema[voc.Core.LongDescription];
const longDescription = schema[voc.Core.LongDescription];
if (longDescription) {
lines.push("");
lines.push(...longDescription.split("\n"));
Expand Down
76 changes: 43 additions & 33 deletions lib/transform.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,69 @@
#!/usr/bin/env node
'use strict';
"use strict";

const csdl = require('odata-csdl');
const lib = require('./csdl2markdown');
const fs = require('fs');
const csdl = require("odata-csdl");
const lib = require("./csdl2markdown");
const fs = require("fs");

const vocabFolder = './vocabularies/';
const exampleFolder = './examples/';
const vocabFolder = "./vocabularies/";
const exampleFolder = "./examples/";

const vocabs = {};

fs.readdirSync(vocabFolder).filter(fn => fn.endsWith('.xml')).forEach(filename => {
const vocab = filename.substring(0, filename.lastIndexOf('.'));
console.log(vocab + '.json');
fs.readdirSync(vocabFolder)
.filter((fn) => fn.endsWith(".xml"))
.forEach((filename) => {
const vocab = filename.substring(0, filename.lastIndexOf("."));
console.log(vocab + ".json");

const xml = fs.readFileSync(vocabFolder + filename, 'utf8');
const xml = fs.readFileSync(vocabFolder + filename, "utf8");
const json = csdl.xml2json(xml, true);

vocabs[filename] = json
vocabs[filename] = json;

// swap URLs of latest-version and alternate links
const links = json[vocab]['@Core.Links'];
const latestVersion = links.findIndex(l => l.rel == 'latest-version');
const alternate = links.findIndex(l => l.rel == 'alternate');
const links = json[vocab]["@Core.Links"];
const latestVersion = links.findIndex((l) => l.rel == "latest-version");
const alternate = links.findIndex((l) => l.rel == "alternate");
if (latestVersion != -1 && alternate != -1) {
links[latestVersion].rel = 'alternate';
links[alternate].rel = 'latest-version';
links[latestVersion].rel = "alternate";
links[alternate].rel = "latest-version";
}
fs.writeFileSync(vocabFolder + vocab + '.json', JSON.stringify(json, omitLineNumbers, 4));
});
fs.writeFileSync(
vocabFolder + vocab + ".json",
JSON.stringify(json, omitLineNumbers, 4)
);
});

console.log();

for (const [filename, csdl] of Object.entries(vocabs)) {
const vocab = filename.substring(0, filename.lastIndexOf('.'));
console.log(vocab + '.md');
const vocab = filename.substring(0, filename.lastIndexOf("."));
console.log(vocab + ".md");

const markdown = lib.csdl2markdown(filename, csdl, vocabs);
fs.writeFileSync(vocabFolder + vocab + '.md', markdown.join('\n'));
const markdown = lib.csdl2markdown(filename, csdl, vocabs);
fs.writeFileSync(vocabFolder + vocab + ".md", markdown.join("\n"));
}

console.log();

fs.readdirSync(exampleFolder).filter(fn => fn.endsWith('.xml')).forEach(xmlfile => {
const example = xmlfile.substring(0, xmlfile.lastIndexOf('.'));
console.log(example + '.json');
fs.readdirSync(exampleFolder)
.filter((fn) => fn.endsWith(".xml"))
.forEach((xmlfile) => {
const example = xmlfile.substring(0, xmlfile.lastIndexOf("."));
console.log(example + ".json");

const xml = fs.readFileSync(exampleFolder + xmlfile, 'utf8');
const xml = fs.readFileSync(exampleFolder + xmlfile, "utf8");
const json = csdl.xml2json(xml, false);
fs.writeFileSync(exampleFolder + example + '.json', JSON.stringify(json, null, 4));
});
fs.writeFileSync(
exampleFolder + example + ".json",
JSON.stringify(json, null, 4)
);
});

function omitLineNumbers(key, value) {
if (key.endsWith('@parser.line')) {
return undefined;
}
return value;
}
if (key.endsWith("@parser.line")) {
return undefined;
}
return value;
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "odata-vocabularies",
"version": "0.4.1",
"version": "0.4.2",
"description": "Convert OData vocabularies in CSDL XML or CSDL JSON to GitHub Flavored Markdown",
"homepage": "https://github.com/oasis-tcs/odata-vocabularies/blob/master/lib/README.md",
"bugs": "https://github.com/oasis-tcs/odata-vocabularies/issues",
Expand All @@ -17,11 +17,12 @@
"main": "lib/csdl2markdown.js",
"dependencies": {
"minimist": "^1.2.0",
"odata-csdl": "oasis-tcs/odata-csdl-schemas"
"odata-csdl": "^0.2.4"
},
"devDependencies": {
"c8": "^7.3.1",
"mocha": "^6.1.4"
"eslint": "^7.21.0",
"mocha": "^8.3.1"
},
"scripts": {
"build": "node lib/transform.js",
Expand Down
5 changes: 5 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"mocha": true
}
}

0 comments on commit 15fc5fd

Please sign in to comment.