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

feat: modernise and update #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
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.

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

Large diffs are not rendered by default.

Binary file modified dist/anitomyscript.wasm
Binary file not shown.
101 changes: 33 additions & 68 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 @@ -27,8 +22,17 @@ function build(cb) {
'-I', path.resolve('./include'),
'-s', 'EXPORT_NAME="anitomyscript"',
'-s', 'WASM=1',
'-s', 'ENVIRONMENT=web,node',
'-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',
'-s', 'ALLOW_MEMORY_GROWTH=1',
'--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 +43,35 @@ 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);

const emccExec = process.platform === 'win32' ? 'emcc.bat' : 'emcc';
console.log(`Starting ${isRelease ? 'release' : 'debug'} build with emcc args`, spawnArgs)
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
Submodule include updated 65 files
+17 −0 .clang-format
+9 −0 .editorconfig
+4 −0 .gitignore
+22 −0 CMakeLists.txt
+47 −0 CMakePresets.json
+68 −99 README.md
+0 −92 anitomy/anitomy.cpp
+0 −35 anitomy/anitomy.h
+0 −145 anitomy/element.cpp
+0 −93 anitomy/element.h
+0 −199 anitomy/keyword.cpp
+0 −59 anitomy/keyword.h
+0 −27 anitomy/options.h
+0 −346 anitomy/parser.cpp
+0 −98 anitomy/parser.h
+0 −254 anitomy/parser_helper.cpp
+0 −520 anitomy/parser_number.cpp
+0 −133 anitomy/string.cpp
+0 −36 anitomy/string.h
+0 −97 anitomy/token.cpp
+0 −70 anitomy/token.h
+0 −266 anitomy/tokenizer.cpp
+0 −43 anitomy/tokenizer.h
+7 −0 include/CMakeLists.txt
+24 −0 include/anitomy.hpp
+43 −0 include/anitomy/detail/bracket.hpp
+85 −0 include/anitomy/detail/cli.hpp
+39 −0 include/anitomy/detail/cli/print.hpp
+25 −0 include/anitomy/detail/container.hpp
+48 −0 include/anitomy/detail/delimiter.hpp
+63 −0 include/anitomy/detail/element.hpp
+118 −0 include/anitomy/detail/format.hpp
+26 −0 include/anitomy/detail/json.hpp
+216 −0 include/anitomy/detail/json/parser.hpp
+142 −0 include/anitomy/detail/json/serializer.hpp
+102 −0 include/anitomy/detail/json/value.hpp
+345 −0 include/anitomy/detail/keyword.hpp
+112 −0 include/anitomy/detail/parser.hpp
+337 −0 include/anitomy/detail/parser/episode.hpp
+60 −0 include/anitomy/detail/parser/episode_title.hpp
+38 −0 include/anitomy/detail/parser/file_checksum.hpp
+37 −0 include/anitomy/detail/parser/file_extension.hpp
+80 −0 include/anitomy/detail/parser/keywords.hpp
+74 −0 include/anitomy/detail/parser/release_group.hpp
+102 −0 include/anitomy/detail/parser/season.hpp
+77 −0 include/anitomy/detail/parser/title.hpp
+44 −0 include/anitomy/detail/parser/video_resolution.hpp
+37 −0 include/anitomy/detail/parser/volume.hpp
+49 −0 include/anitomy/detail/parser/year.hpp
+65 −0 include/anitomy/detail/token.hpp
+170 −0 include/anitomy/detail/tokenizer.hpp
+52 −0 include/anitomy/detail/unicode.hpp
+28 −0 include/anitomy/detail/unicode/base.hpp
+29 −0 include/anitomy/detail/unicode/utf32.hpp
+150 −0 include/anitomy/detail/unicode/utf8.hpp
+96 −0 include/anitomy/detail/util.hpp
+36 −0 include/anitomy/element.hpp
+15 −0 include/anitomy/format.hpp
+17 −0 include/anitomy/options.hpp
+25 −0 include/anitomy/version.hpp
+25 −0 src/CMakeLists.txt
+142 −0 src/main.cpp
+25 −0 test/CMakeLists.txt
+2,688 −1,948 test/data.json
+493 −0 test/main.cpp
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module "anitomyscript" {
declare module 'anitomyscript' {
export interface AnitomyResult {
file_name: string;
anime_season?: string;
Expand Down Expand Up @@ -29,8 +29,8 @@ declare module "anitomyscript" {
}

function parse(
input: string | string[]
): Promise<AnitomyResult[] | AnitomyResult>;
input: string[]
): Promise<AnitomyResult[]>;

export default parse;
export default parse
}
75 changes: 30 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
'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) {
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));
}
async function parse (file) {
const vector = mapArray(file)
const result = mapVector(anitomyModule.parseMultiple(vector))
vector.delete()
return result.map((each) => elements(each))
}

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 +47,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.7",
"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