Skip to content

Commit

Permalink
ESM support
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Oct 29, 2023
1 parent 5bc5efa commit 265d7ae
Show file tree
Hide file tree
Showing 112 changed files with 781 additions and 743 deletions.
46 changes: 26 additions & 20 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

const child_process = require('child_process');
const crypto = require('crypto');
const fs = require('fs').promises;
const os = require('os');
const path = require('path');
import * as child_process from 'child_process';
import * as crypto from 'crypto';
import * as fs from 'fs/promises';
import * as os from 'os';
import * as path from 'path';
import * as url from 'url';

const args = process.argv.slice(2);

Expand All @@ -16,8 +17,13 @@ const read = (match) => {

let configuration = null;

const dirname = (...args) => {
const dir = path.dirname(url.fileURLToPath(import.meta.url));
return path.join(dir, ...args);
};

const load = async () => {
const file = path.join(__dirname, 'package.json');
const file = dirname('package.json');
const content = await fs.readFile(file, 'utf-8');
configuration = JSON.parse(content);
};
Expand Down Expand Up @@ -48,7 +54,7 @@ const access = async (path) => {
};

const rm = async (...args) => {
const dir = path.join(__dirname, ...args);
const dir = dirname(...args);
const exists = await access(dir);
if (exists) {
writeLine('rm ' + path.join(...args));
Expand All @@ -58,7 +64,7 @@ const rm = async (...args) => {
};

const mkdir = async (...args) => {
const dir = path.join(__dirname, ...args);
const dir = dirname(...args);
const exists = await access(dir);
if (!exists) {
writeLine('mkdir ' + path.join(...args));
Expand All @@ -84,13 +90,13 @@ const unlink = async (dir, filter) => {

const exec = async (command, encoding) => {
if (encoding) {
return child_process.execSync(command, { cwd: __dirname, encoding: encoding });
return child_process.execSync(command, { cwd: dirname(), encoding: encoding });
}
child_process.execSync(command, { cwd: __dirname, stdio: [ 0,1,2 ] });
child_process.execSync(command, { cwd: dirname(), stdio: [ 0,1,2 ] });
return '';
/*
return new Promise((resolve, reject) => {
const child = child_process.exec(command, { cwd: __dirname }, (error, stdout, stderr) => {
const child = child_process.exec(command, { cwd: dirname() }, (error, stdout, stderr) => {
if (error) {
stderr = '\n' + stderr ;
if (error.message && error.message.endsWith(stderr)) {
Expand Down Expand Up @@ -201,7 +207,7 @@ const pullrequest = async (organization, repository, body) => {
};

const install = async () => {
const node_modules = path.join(__dirname, 'node_modules');
const node_modules = dirname('node_modules');
const exists = await access(node_modules);
if (!exists) {
await exec('npm install');
Expand Down Expand Up @@ -234,13 +240,13 @@ const build = async (target) => {
await rm('dist', 'web');
await mkdir('dist', 'web');
writeLine('cp source/dir dist/dir');
const source_dir = path.join(__dirname, 'source');
const dist_dir = path.join(__dirname, 'dist', 'web');
const source_dir = dirname('source');
const dist_dir = dirname('dist', 'web');
const extensions = new Set([ 'html', 'css', 'js', 'json', 'ico', 'png' ]);
await copy(source_dir, dist_dir, (file) => extensions.has(file.split('.').pop()));
await rm('dist', 'web', 'app.js');
await rm('dist', 'web', 'electron.js');
const contentFile = path.join(__dirname, 'dist', 'web', 'index.html');
const contentFile = dirname('dist', 'web', 'index.html');
let content = await fs.readFile(contentFile, 'utf-8');
content = content.replace(/(<meta\s*name="version"\s*content=")(.*)(">)/m, (match, p1, p2, p3) => {
return p1 + configuration.version + p3;
Expand Down Expand Up @@ -294,8 +300,8 @@ const publish = async (target) => {
const url = 'https://x-access-token:' + GITHUB_TOKEN + '@github.com/' + GITHUB_USER + '/netron.git';
await exec('git clone --depth=1 ' + url + ' --branch gh-pages ./dist/gh-pages 2>&1 > /dev/null');
writeLine('cp dist/web dist/gh-pages');
const source_dir = path.join(__dirname, 'dist', 'web');
const target_dir = path.join(__dirname, 'dist', 'gh-pages');
const source_dir = dirname('dist', 'web');
const target_dir = dirname('dist', 'gh-pages');
await unlink(target_dir, (file) => file !== '.git');
await copy(source_dir, target_dir);
await exec('git -C dist/gh-pages add --all');
Expand Down Expand Up @@ -380,7 +386,7 @@ const publish = async (target) => {
const paths = [ 'dist', 'winget-pkgs', 'manifests', publisher[0].toLowerCase(), publisher.replace(' ', ''), product, version ];
await mkdir(...paths);
writeLine('update manifest');
const manifestFile = path.join(__dirname, ...paths, identifier);
const manifestFile = dirname(...paths, identifier);
await fs.writeFile(manifestFile + '.yaml', [
'# yaml-language-server: $schema=https://aka.ms/winget-manifest.version.1.2.0.schema.json',
'PackageIdentifier: ' + identifier,
Expand Down Expand Up @@ -478,7 +484,7 @@ const publish = async (target) => {
const lint = async () => {
await install();
writeLine('eslint');
await exec('npx eslint *.js source/*.js test/*.js publish/*.js tools/*.js');
await exec('npx eslint *.*js source/*.*js test/*.*js publish/*.*js tools/*.js');
writeLine('pylint');
await exec('python -m pip install --upgrade --quiet pylint');
await exec('python -m pylint -sn --recursive=y source test publish tools *.py');
Expand Down Expand Up @@ -562,7 +568,7 @@ const analyze = async () => {

const version = async () => {
await pull();
const file = path.join(__dirname, 'package.json');
const file = dirname('package.json');
let content = await fs.readFile(file, 'utf-8');
content = content.replace(/(\s*"version":\s")(\d\.\d\.\d)(",)/m, (match, p1, p2, p3) => {
const version = Array.from((parseInt(p2.split('.').join(''), 10) + 1).toString()).join('.');
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"description": "Visualizer for neural network, deep learning, and machine learning models",
"license": "MIT",
"repository": "lutzroeder/netron",
"type": "module",
"main": "source/app.js",
"scripts": {
"start": "node package.js start",
Expand All @@ -31,14 +32,14 @@
"electron-updater": "6.1.4"
},
"devDependencies": {
"electron": "27.1.0",
"electron": "28.0.0-beta.7",
"electron-builder": "24.6.4",
"@electron/notarize": "2.2.0",
"eslint": "8.53.0"
},
"eslintConfig": {
"env": {
"es2017": true,
"es2020": true,
"browser": true,
"node": true
},
Expand Down Expand Up @@ -79,6 +80,7 @@
"no-unreachable-loop": "error",
"no-unused-private-class-members": "error",
"no-use-before-define": "error",
"no-var": "error",
"object-curly-spacing": [ "error", "always" ],
"prefer-const": [ "error", { "destructuring": "all" } ],
"require-atomic-updates": "error",
Expand Down Expand Up @@ -154,7 +156,7 @@
{ "ext": "uff", "name": "UFF Model" },
{ "ext": "xmodel", "name": "Vitis AI Model" }
],
"afterSign": "./publish/notarize.js",
"afterSign": "./publish/notarize.cjs",
"publish": [
{ "provider": "github", "releaseType": "release" }
],
Expand Down
2 changes: 1 addition & 1 deletion package.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _build():
shutil.rmtree(dist_pypi_dir, ignore_errors=True)
shutil.copytree(source_dir, os.path.join(dist_pypi_dir, 'netron'))
shutil.copyfile(os.path.join(publish_dir, 'setup.py'), os.path.join(dist_pypi_dir, 'setup.py'))
os.remove(os.path.join(dist_pypi_dir, 'netron', 'electron.js'))
os.remove(os.path.join(dist_pypi_dir, 'netron', 'electron.mjs'))
os.remove(os.path.join(dist_pypi_dir, 'netron', 'app.js'))

def _install():
Expand Down
2 changes: 1 addition & 1 deletion publish/notarize.js → publish/notarize.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ exports.default = function (context) {
}
}
return null;
};
};
6 changes: 2 additions & 4 deletions source/acuity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

var acuity = {};
const acuity = {};

acuity.ModelFactory = class {

Expand Down Expand Up @@ -556,6 +556,4 @@ acuity.Error = class extends Error {
}
};

if (typeof module !== 'undefined' && typeof module.exports === 'object') {
module.exports.ModelFactory = acuity.ModelFactory;
}
export const ModelFactory = acuity.ModelFactory;
34 changes: 19 additions & 15 deletions source/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

const electron = require('electron');
const updater = require('electron-updater');
const fs = require('fs');
const os = require('os');
const path = require('path');
const process = require('process');
const url = require('url');
const base = require('./base');
import * as electron from 'electron';
import * as updater from 'electron-updater';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as process from 'process';
import * as url from 'url';
import * as base from './base.js';

var app = {};
const app = {};

app.Application = class {

Expand All @@ -21,8 +21,9 @@ app.Application = class {
}

start() {
const packageFile = path.join(path.dirname(__dirname), 'package.json');
const packageContent = fs.readFileSync(packageFile, 'utf-8');
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
const packageFile = path.join(path.dirname(dirname), 'package.json');
const packageContent = fs.readFileSync(packageFile, 'utf-8');
this._package = JSON.parse(packageContent);

electron.app.setAppUserModelId('com.lutzroeder.netron');
Expand Down Expand Up @@ -128,7 +129,8 @@ app.Application = class {
let open = false;
if (argv.length > 1) {
for (const arg of argv.slice(1)) {
if (!arg.startsWith('-') && arg !== path.dirname(__dirname)) {
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
if (!arg.startsWith('-') && arg !== path.dirname(dirname)) {
const extension = path.extname(arg).toLowerCase();
if (extension !== '' && extension !== '.js' && fs.existsSync(arg)) {
const stat = fs.statSync(arg);
Expand Down Expand Up @@ -634,18 +636,19 @@ app.View = class {
this._path = null;
this._properties = new Map();
this._dispatch = [];
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
const size = electron.screen.getPrimaryDisplay().workAreaSize;
const options = {
show: false,
title: electron.app.name,
backgroundColor: electron.nativeTheme.shouldUseDarkColors ? '#1d1d1d' : '#e6e6e6',
icon: electron.nativeImage.createFromPath(path.join(__dirname, 'icon.png')),
icon: electron.nativeImage.createFromPath(path.join(dirname, 'icon.png')),
minWidth: 600,
minHeight: 600,
width: size.width > 1024 ? 1024 : size.width,
height: size.height > 768 ? 768 : size.height,
webPreferences: {
preload: path.join(__dirname, 'electron.js'),
preload: path.join(dirname, 'electron.mjs'),
nodeIntegration: true
}
};
Expand Down Expand Up @@ -716,7 +719,8 @@ app.View = class {
}

_loadURL() {
const pathname = path.join(__dirname, 'index.html');
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
const pathname = path.join(dirname, 'index.html');
let content = fs.readFileSync(pathname, 'utf-8');
content = content.replace(/<\s*script[^>]*>[\s\S]*?(<\s*\/script[^>]*>|$)/ig, '');
const data = 'data:text/html;charset=utf-8,' + encodeURIComponent(content);
Expand Down
5 changes: 4 additions & 1 deletion source/armnn-schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
var $root = flatbuffers.get('armnn');

import * as flatbuffers from './flatbuffers.js';

const $root = flatbuffers.get('armnn');

$root.armnnSerializer = $root.armnnSerializer || {};

Expand Down
9 changes: 4 additions & 5 deletions source/armnn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

var armnn = {};
var flatbuffers = require('./flatbuffers');
import * as flatbuffers from './flatbuffers.js';

const armnn = {};

armnn.ModelFactory = class {

Expand Down Expand Up @@ -318,6 +319,4 @@ armnn.Error = class extends Error {
}
};

if (typeof module !== 'undefined' && typeof module.exports === 'object') {
module.exports.ModelFactory = armnn.ModelFactory;
}
export const ModelFactory = armnn.ModelFactory;
10 changes: 5 additions & 5 deletions source/barracuda.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

// Experimental

var barracuda = {};
var base = require('./base');
import * as base from './base.js';

const barracuda = {};

barracuda.ModelFactory = class {

Expand Down Expand Up @@ -532,6 +533,5 @@ barracuda.Error = class extends Error {
}
};

if (typeof module !== 'undefined' && typeof module.exports === 'object') {
module.exports.ModelFactory = barracuda.ModelFactory;
}
export const ModelFactory = barracuda.ModelFactory;

20 changes: 9 additions & 11 deletions source/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

var base = {};
const base = {};

base.Int64 = class Int64 {

Expand Down Expand Up @@ -1108,13 +1108,11 @@ if (typeof window !== 'undefined' && typeof window.Long != 'undefined') {
window.Uint64 = base.Uint64;
}

if (typeof module !== 'undefined' && typeof module.exports === 'object') {
module.exports.Int64 = base.Int64;
module.exports.Uint64 = base.Uint64;
module.exports.Complex64 = base.Complex64;
module.exports.Complex128 = base.Complex128;
module.exports.BinaryStream = base.BinaryStream;
module.exports.BinaryReader = base.BinaryReader;
module.exports.Telemetry = base.Telemetry;
module.exports.Metadata = base.Metadata;
}
export const Int64 = base.Int64;
export const Uint64 = base.Uint64;
export const Complex64 = base.Complex64;
export const Complex128 = base.Complex128;
export const BinaryStream = base.BinaryStream;
export const BinaryReader = base.BinaryReader;
export const Telemetry = base.Telemetry;
export const Metadata = base.Metadata;
5 changes: 4 additions & 1 deletion source/bigdl-proto.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
var $root = protobuf.get('bigdl');

import * as protobuf from './protobuf.js';

const $root = protobuf.get('bigdl');

$root.com = {};

Expand Down
10 changes: 5 additions & 5 deletions source/bigdl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

// Experimental

var bigdl = {};
var protobuf = require('./protobuf');
import * as protobuf from './protobuf.js';

const bigdl = {};

bigdl.ModelFactory = class {

Expand Down Expand Up @@ -408,6 +409,5 @@ bigdl.Error = class extends Error {
}
};

if (typeof module !== 'undefined' && typeof module.exports === 'object') {
module.exports.ModelFactory = bigdl.ModelFactory;
}
export const ModelFactory = bigdl.ModelFactory;

Loading

0 comments on commit 265d7ae

Please sign in to comment.