Skip to content

Commit

Permalink
Enabled custom path for template updates, updated templates
Browse files Browse the repository at this point in the history
  • Loading branch information
blynx committed Jul 16, 2024
1 parent 9bccd33 commit 3e92d82
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ indent_style = tab
insert_final_newline = true
tab_width = 2
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Data Contract Editor

The [Data Contract Editor](https://editor.datacontract.com) is like VS Code, but for [data contracts](https://datacontract.com). It's enterprise-friendly as it stores your data contracts in your browser.
The [Data Contract Editor](https://editor.datacontract.com) is like VS Code, but for [data contracts](https://datacontract.com). It's enterprise-friendly as it stores your data contracts in your browser.

**Features**

Expand Down Expand Up @@ -34,8 +34,15 @@ npm run build

## How to update the templates

Templates are taken from the Data Contract CLI and are fetched directly from the repository.
If you want to update them locally run `npm run update-templates`, whic will also be automatically run in the build.
Templates are taken from the Data Contract CLI and are fetched directly from the repository.
If you want to update them locally run `npm run update-templates`, which will also be automatically run in the build.

Optionally, you can specify a location to the templates folder of a local datacontract-cli repository (or any other location).
Combined with `npm run dev`, the templates will be live reloaded.

```
npm run update-templates --datacontract-template-source="/my/path/to/datacontract-cli/datacontract/templates/"
```

## License

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"dev": "vite src",
"build": "vite build src --outDir ../dist --emptyOutDir",
"update-templates": "npm run load-templates && npm run precompile-templates",
"load-templates": "node scripts/load_templates.js",
"update-templates": "npm run load-templates %npm_config_datacontract-template-source% && npm run precompile-templates",
"load-templates": "node scripts/load_templates.js %npm_config_datacontract-template-source%",
"precompile-templates": "node_modules/nunjucks/bin/precompile templates > src/templates/templates.js",
"lint": "eslint"
},
Expand Down
24 changes: 15 additions & 9 deletions scripts/load_templates.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'node:fs';

/** Utility functions */

async function loadTemplate(sub_path) {
// eslint-disable-next-line no-undef
let base_path = process.env['npm_config_datacontract_template_source'] || 'https://raw.githubusercontent.com/datacontract/datacontract-cli/main/datacontract/templates/';
let path = base_path + sub_path;
if (path.startsWith('http')) {
// eslint-disable-next-line no-undef
return await (await fetch(path)).text();
}
return readFileSync(path) + '';
}

// handle the replacement of all Python constructs into the JS equivalent
function updateTemplateCode(template) {
const updated_template = template
Expand All @@ -21,21 +32,16 @@ function storeTemplate(path, content) {

// download and process a partial
async function processPartial(partial_name) {
const base_path = 'https://raw.githubusercontent.com/datacontract/datacontract-cli/main/datacontract/templates/';
const url = `${base_path}${partial_name}`;
// eslint-disable-next-line no-undef
const raw_template = await fetch(url);
const template = await raw_template.text();
const template = await loadTemplate(partial_name);
const processed_template = updateTemplateCode(template);
return processed_template;
}

/** Main loop */

// fetch the main template, all other work is based on that
// eslint-disable-next-line no-undef
const raw_template = await fetch('https://raw.githubusercontent.com/datacontract/datacontract-cli/main/datacontract/templates/datacontract.html');
const full_main_template = await raw_template.text();
const full_main_template = await loadTemplate('datacontract.html');
// const full_main_template = await raw_template.text();
// cut out the relevant part of the template (which is inside the <main> element)
// note: this is a bit crude, but since this is a template we can't parse it as HTML
const main_template = full_main_template.match(/<main .+>.*<\/main>/s)[0];
Expand Down
2 changes: 0 additions & 2 deletions src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ main {
grid-column: 2;
cursor: col-resize;
}


Loading

0 comments on commit 3e92d82

Please sign in to comment.