Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Python 2 scripts with NodeJS scripts. #95

Merged
merged 7 commits into from
Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ sudo: false

language: node_js
cache: npm
node_js: 8
node_js: 10

install:
- pip install --user -r requirements.txt
- npm install
- 'if [[ "$TRAVIS_BRANCH" = stable ]]; then npm install kaitai-struct-compiler@latest && npm install kaitai-struct@latest && python vendor_build.py; fi'
- 'if [[ "$TRAVIS_BRANCH" = stable ]]; then npm install kaitai-struct-compiler@latest && npm install kaitai-struct@latest && node vendor_build.js; fi'

script:
- '[ "$TRAVIS_BRANCH" = stable ] && export GA_ID=UA-76299550-2 || export GA_ID=UA-76299550-3'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ Online editor / visualizer for Kaitai Struct .ksy files

- Clone stable release: `git clone https://github.com/kaitai-io/ide-kaitai-io.github.io` or
- Devel release: `git clone https://github.com/kaitai-io/ide-devel-kaitai-io.github.io`
- Serve on a webserver (ex. `python -mSimpleHTTPServer` or `python serve.py`)
- Serve on a webserver (ex. `python -mSimpleHTTPServer` or `node serve.js`)
- Go to [http://127.0.0.1:8000/](http://127.0.0.1:8000/)

## compile and run locally

- `git clone --recursive https://github.com/kaitai-io/kaitai_struct_webide`
- `npm install`
- `python serve.py --compile`
- `node serve.js --compile`
- Go to [http://127.0.0.1:8000/](http://127.0.0.1:8000/)

## screenshots
Expand Down
4 changes: 2 additions & 2 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ TSC=node_modules/.bin/tsc

mkdir -p "$OUT_DIR/js"

./genKaitaiFsFiles.py "$OUT_DIR"
node ./genKaitaiFsFiles.js "$OUT_DIR"

"$TSC" --outDir $OUT_DIR/js/ --noEmitOnError
if [ -f "tsconfig.worker.json" ]; then
Expand All @@ -29,4 +29,4 @@ cp -r src/ui/Components/*.html "$OUT_DIR/src/ui/Components"
mkdir -p "$OUT_DIR/src/ui/Parts"
cp -r src/ui/Parts/*.html "$OUT_DIR/src/ui/Parts"

./build.py "$OUT_DIR"
node ./build.js "$OUT_DIR"
86 changes: 86 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const { readFileSync, writeFileSync } = require("fs");
const { exec } = require("child_process");

const { GA_ID, SENTRY_DSN, SENTRY_ENV } = process.env;

const GA_TEMPLATE = GA_ID ? `
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','_ga');

_ga('create', '${GA_ID}', 'auto');
_ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
` : "";

const SENTRY_TEMPLATE = SENTRY_DSN && SENTRY_ENV ? `
<script src="https://cdn.ravenjs.com/3.17.0/raven.min.js" crossorigin="anonymous"></script>
<script>
Raven.config('${SENTRY_DSN}', {
environment: "${SENTRY_ENV}",
release: "{{SENTRY_RELEASE}}",
}).install();
</script>
` : "";

const outDir = process.argv.length > 2 ? process.argv[2] : "out";

function fileAction(fn, action) {
writeFileSync(fn, action(readFileSync(fn, { encoding: "utf-8" })));
}

function appendAfter(str, afterStr, appendStr) {
const i = str.indexOf(afterStr) + afterStr.length;
return str.slice(0, i) + appendStr + str.slice(i);
}

function fetchGitCommitInfo() {
return new Promise((resolve, reject) =>
exec("git log -1 --format=%H,%ct", (err, stdout) => {
if (err) {
reject(err);
} else {
const [commitId, commitTs] = stdout.trim().split(",");
const commitDate = new Date(Number(commitTs)*1000);
resolve({ commitId, commitDate });
}
})
);
}

function formatCommitDate(d) {
return String(d.getUTCFullYear()).padStart(2, '0') + '-'
+ String(d.getUTCMonth() + 1).padStart(2, '0') + '-'
+ String(d.getUTCDate()).padStart(2, '0') + ' '
+ String(d.getUTCHours()).padStart(2, '0') + ':'
+ String(d.getUTCMinutes()).padStart(2, '0') + ':'
+ String(d.getUTCSeconds()).padStart(2, '0');
}

async function main() {
const { commitId, commitDate } = await fetchGitCommitInfo();
const scriptAppend =
SENTRY_TEMPLATE.replace("{{SENTRY_RELEASE}}", commitId) + GA_TEMPLATE;
if (scriptAppend) {
fileAction(outDir + "/index.html", html =>
appendAfter(html, "<!-- SCRIPT_INJECTION_POINT -->", scriptAppend)
);
}
fileAction(outDir + "/js/v1/app.js", html =>
html
.replace(
'kaitaiIde.commitId = "";',
`kaitaiIde.commitId = "${commitId}";`
)
.replace(
'kaitaiIde.commitDate = "";',
`kaitaiIde.commitDate = "${formatCommitDate(commitDate)}";`
)
);
}

main().catch(err => console.error(err));
64 changes: 0 additions & 64 deletions build.py

This file was deleted.

39 changes: 39 additions & 0 deletions genKaitaiFsFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { readdirSync, statSync, writeFileSync, mkdirSync } = require("fs");
const { join } = require("path");

function recursiveFind(dir, pattern, results = []) {
const files = readdirSync(dir, "utf-8")
.map(fn => join(dir, fn))
.map(fn => ({ stat: statSync(fn), fn }));
results.push.apply(
results,
files
.filter(s => s.stat.isFile())
.map(s => s.fn)
.filter(fn => pattern.test(fn))
);
files
.filter(s => s.stat.isDirectory())
.forEach(s => recursiveFind(s.fn, pattern, results));
return results;
}

function generate(outDir) {
const files =
recursiveFind("formats/", /\.ksy$/)
.concat(recursiveFind("samples/", /.+/))
.map(path => path.replace(/\\/g,'/'));
files.sort();

const js = `var kaitaiFsFiles = ${JSON.stringify(files, null, 4)};`;

mkdirSync(outDir + "js", { recursive: true });
writeFileSync(outDir + "js/kaitaiFsFiles.js", js);
}

function main() {
const outDir = process.argv.length > 2 ? process.argv[2] + "/" : "";
generate(outDir);
}

main();
30 changes: 0 additions & 30 deletions genKaitaiFsFiles.py

This file was deleted.

Loading