Skip to content

Commit

Permalink
feat: modernise and update
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Oct 23, 2022
1 parent 46b2df9 commit 5f7c6e0
Show file tree
Hide file tree
Showing 14 changed files with 6,401 additions and 1,781 deletions.
19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

93 changes: 0 additions & 93 deletions build/anitomyscript.js

This file was deleted.

Binary file removed build/anitomyscript.wasm
Binary file not shown.
1,460 changes: 0 additions & 1,460 deletions dist/anitomyscript.bundle.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/anitomyscript.bundle.min.js

This file was deleted.

83 changes: 83 additions & 0 deletions dist/anitomyscript.js

Large diffs are not rendered by default.

Binary file modified dist/anitomyscript.wasm
Binary file not shown.
98 changes: 31 additions & 67 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
'use strict';
import gulp from 'gulp'
import { spawn } from 'child_process'
import path from 'path'
import fs from 'fs'

const gulp = require('gulp');
const spawn = require('child_process').spawn;
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const browserify = require('browserify');

function build(cb) {
const emscriptenPath = process.env.EMSCRIPTEN || process.env.EMSCRIPTEN_ROOT;
console.log(process.env)
function build (cb) {
const emscriptenPath = process.env.EMSCRIPTEN || process.env.EMSCRIPTEN_ROOT

if (!emscriptenPath || !fs.existsSync(path.resolve(emscriptenPath))) {
return cb('Unable to find emscripten root. Use the env variable EMSCRIPTEN or EMSCRIPTEN_ROOT to set it manually.');
return cb('Unable to find emscripten root. Use the env variable EMSCRIPTEN or EMSCRIPTEN_ROOT to set it manually.')
}

const out = path.resolve('./', 'build', 'anitomyscript.js');
const isRelease = process.env.NODE_ENV === 'prod';
const out = path.resolve('./', 'dist', 'anitomyscript.js')
const isRelease = process.env.NODE_ENV === 'prod'

let spawnArgs = [
'--memory-init-file', '0',
Expand All @@ -29,6 +24,13 @@ function build(cb) {
'-s', 'WASM=1',
'-s', 'FILESYSTEM=0',
'-s', 'MODULARIZE=1',
'-s', 'EXPORT_ES6=1',
'-s', 'AUTO_JS_LIBRARIES=0',
'-s', 'AUTO_NATIVE_LIBRARIES=0',
'-s', 'HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS=0',
'-s', 'USE_SDL=0',
'-s', 'INITIAL_MEMORY=5308416',
'--no-heap-copy',
path.resolve('./include/anitomy/anitomy.cpp'),
path.resolve('./include/anitomy/element.cpp'),
path.resolve('./include/anitomy/keyword.cpp'),
Expand All @@ -39,74 +41,36 @@ function build(cb) {
path.resolve('./include/anitomy/token.cpp'),
path.resolve('./include/anitomy/tokenizer.cpp'),
path.resolve('./src/anitomyscript.cpp'),
'-o', out,
];
'-o', out
]

if (isRelease) {
spawnArgs = spawnArgs.concat([
'-O3',
'-s', 'USE_CLOSURE_COMPILER=1',
'-s', 'IGNORE_CLOSURE_COMPILER_ERRORS=1',
'--closure', '1',
'--llvm-lto', '3',
'--llvm-lto', '3'
])
}

console.log(`Starting ${isRelease ? 'release' : 'debug'} build with emcc args`, spawnArgs);
console.log(`Starting ${isRelease ? 'release' : 'debug'} build with emcc args`, spawnArgs)

const emccExec = process.platform === 'win32' ? 'emcc.bat' : 'emcc';
const emccExec = process.platform === 'win32' ? 'emcc.bat' : 'emcc'
const s = spawn(path.join(emscriptenPath, emccExec), spawnArgs, {
cwd: './build',
});
cwd: './dist'
})

s.stdout.on('data', (data) => {
console.log(data.toString());
});
console.log(data.toString())
})

s.stderr.on('data', (data) => {
console.log(data.toString());
});
console.log(data.toString())
})

s.on('error', (e) => cb(e))
s.on('close', () => cb());
}

function copyWasm() {
return gulp.src('./build/anitomyscript.wasm').pipe(gulp.dest('./dist'));
}

function clearBuild(cb) {
const buildPath = path.resolve('./build');
fs.readdirSync(buildPath).forEach((file) => {
const fileWithPath = path.resolve(buildPath, file);
if (file !== '.gitkeep') fse.removeSync(fileWithPath);
});
cb();
}

function _babel() {
return {
presets: ['@babel/preset-env'],
plugins: [
['@babel/plugin-transform-runtime', { helpers: false }]
],
only: ['index.js'],
};
}

function browser() {
return browserify('./index.js', { standalone: 'anitomyscript' })
.transform('babelify', _babel())
.bundle()
.pipe(fs.createWriteStream('./dist/anitomyscript.bundle.js'));
}

function browserMin() {
return browserify('./index.js', { standalone: 'anitomyscript' })
.transform('babelify', _babel())
.plugin('tinyify')
.bundle()
.pipe(fs.createWriteStream('./dist/anitomyscript.bundle.min.js'));
s.on('close', () => cb())
}

gulp.task('build', build);
gulp.task('browser', gulp.series(gulp.parallel(browser, browserMin), copyWasm));
gulp.task('default', gulp.series(clearBuild, 'build', 'browser'));
gulp.task('default', build)
2 changes: 1 addition & 1 deletion include
23 changes: 23 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<input type="text" id="demo-input" name="demo-input" style="width: 91%">
<button id="demo-parse">Parse!</button>
<pre id="demo-preview">
// preview
</pre>
<script type="module">
import anitomyscript from './index.js'
const input = document.getElementById('demo-input')
const button = document.getElementById('demo-parse')
const preview = document.getElementById('demo-preview')
input.onkeypress = function (e) {
if (e.keyCode == 13) {
button.click()
}
}
button.onclick = function () {
anitomyscript(input.value).then(function (parsed) {
preview.innerText = JSON.stringify(parsed, null, 2)
}).catch(function (err) {
preview.innerText = err.message
})
}
</script>
75 changes: 32 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
'use strict';
import AnitomyNative from './dist/anitomyscript.js'
let anitomyModule

let AnitomyNative = require('./build/anitomyscript');
let anitomyModule = undefined;

module.exports = function (file) {
export default async function (file) {
if (!Array.isArray(file) && typeof file !== 'string') {
return Promise.reject(new Error('Input must be either an Array or a string'));
return new Error('Input must be either an Array or a string')
}

if (anitomyModule) {
return parse(file);
return parse(file)
}

return new Promise((resolve, reject) => {
try {
AnitomyNative().then((actualModule) => {
anitomyModule = actualModule;
parse(file).then(resolve).catch(reject);
});
} catch (err) {
reject(err);
}
});
anitomyModule = await AnitomyNative()
return parse(file)
}

async function parse(file) {
async function parse (file) {
if (Array.isArray(file)) {
const vector = mapArray(file);
const result = mapVector(anitomyModule.parseMultiple(vector));
vector.delete();
return result.map((each) => elements(each));
}
else {
return elements(anitomyModule.parseSingle(file));
const vector = mapArray(file)
const result = mapVector(anitomyModule.parseMultiple(vector))
vector.delete()
return result.map((each) => elements(each))
} else {
return elements(anitomyModule.parseSingle(file))
}
}

function elements(elements) {
function elements (elements) {
const returnObj = {
anime_season: elementEntry(elements, anitomyModule.ElementCategory.kElementAnimeSeason),
season_prefix: elementEntry(elements, anitomyModule.ElementCategory.kElementAnimeSeasonPrefix),
Expand All @@ -62,36 +51,36 @@ function elements(elements) {
video_term: elementEntry(elements, anitomyModule.ElementCategory.kElementVideoTerm),
volume_number: elementEntry(elements, anitomyModule.ElementCategory.kElementVolumeNumber),
volume_prefix: elementEntry(elements, anitomyModule.ElementCategory.kElementVolumePrefix),
unknown: elementEntry(elements, anitomyModule.ElementCategory.kElementUnknown),
};
elements.delete();
return returnObj;
unknown: elementEntry(elements, anitomyModule.ElementCategory.kElementUnknown)
}
elements.delete()
return returnObj
}

function elementEntry(elements, key) {
function elementEntry (elements, key) {
if (elements.count(key) > 1) {
return mapVector(elements.get_all(key));
return mapVector(elements.get_all(key))
} else {
return elements.get(key) || undefined;
return elements.get(key) || undefined
}
}

function mapArray(array) {
const vector = new anitomyModule.StringVector();
function mapArray (array) {
const vector = new anitomyModule.StringVector()
array.forEach((element, index) => {
if (typeof element !== 'string') {
throw new Error(`Element at index ${index} is not a string`);
throw new Error(`Element at index ${index} is not a string`)
}
vector.push_back(element)
});
return vector;
})
return vector
}

function mapVector(vector) {
const array = [];
function mapVector (vector) {
const array = []
for (let index = 0; index < vector.size(); index++) {
array.push(vector.get(index));
array.push(vector.get(index))
}
vector.delete();
return array;
vector.delete()
return array
}
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "anitomyscript",
"version": "2.0.4",
"version": "2.0.5",
"description": "Pure JavaScript bindings for Anitomy, ported with emscripten",
"main": "index.js",
"type": "module",
"scripts": {
"test": "mocha test/*.spec.js",
"build": "cross-env NODE_ENV=prod gulp",
"build:dev": "cross-env NODE_ENV=dev gulp"
},
"files": ["index.js", "dist/anitomyscript.js", "dist/anitomyscript.wasm"],
"types": "index.d.ts",
"repository": {
"type": "git",
Expand All @@ -20,26 +22,25 @@
"anime"
],
"author": "Thiago Oliveira <thiago_ogt@outlook.com>",
"contributors": [
{
"name" : "ThaUnknown",
"email" : "casistaken@gmail.com",
"url" : "https://github.com/ThaUnknown/"
}
],
"license": "BSD-2-Clause",
"bugs": {
"url": "https://github.com/skiptirengu/anitomyscript/issues"
},
"homepage": "https://github.com/skiptirengu/anitomyscript#readme",
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.6.3",
"@babel/runtime": "^7.6.3",
"babelify": "^10.0.0",
"browserify": "^16.5.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"cross-env": "^6.0.3",
"fs-extra": "^8.1.0",
"gulp": "^4.0.2",
"mocha": "^6.2.2",
"mocha-logger": "^1.0.6",
"tinyify": "^2.5.2"
},
"dependencies": {}
}
}
Loading

0 comments on commit 5f7c6e0

Please sign in to comment.