Skip to content

Commit ab3d644

Browse files
committed
wip: working build
1 parent 813b3ef commit ab3d644

File tree

12 files changed

+86
-150
lines changed

12 files changed

+86
-150
lines changed

jest.config.js renamed to jest.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import path from 'node:path';
22
import url from 'node:url';
3+
import os from 'node:os';
4+
import process from 'node:process';
35
import tsconfigJSON from './tsconfig.json' assert { type: "json" };
46

57
const projectPath = path.dirname(url.fileURLToPath(import.meta.url));

npmDepsHash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sha256-YnXZgZFFew+nEbpxAnslWboXViBWXyRmtJ6xwLDEosg=
1+
sha256-gyMIa7XqMhUXivusY7zsZixlx/187Jr5gWDW9AizDjs=

package-lock.json

Lines changed: 4 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"./*": "./dist/*"
7373
},
7474
"imports": {
75-
"#*": "./dist/*"
75+
"#*": "./build/*"
7676
},
7777
"bin": {
7878
"polykey": "dist/polykey.js",
@@ -133,8 +133,7 @@
133133
"@matrixai/exec-darwin-universal": "*",
134134
"@matrixai/exec-linux-x64": "*",
135135
"fd-lock": "*",
136-
"sodium-native": "*",
137-
"threads": "*"
136+
"sodium-native": "*"
138137
},
139138
"devDependencies": {
140139
"@matrixai/errors": "^2.1.3",

scripts/build.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ async function main(argv = process.argv) {
8282
// External source map for debugging
8383
sourcemap: true,
8484
// Minify and keep the original names
85-
minify: true,
85+
minify: false,
8686
keepNames: true,
87+
// Supporting ESM
88+
format: 'esm',
89+
inject: [path.join(projectPath, './shims/require-shim.mjs')],
8790
};
8891
console.error('Running esbuild:');
8992
console.error(esbuildOptions);

shims/require-shim.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* This shim replaces the `require` in the global scope.
3+
* ESM does not provide a global `require` that is synchronous and can be used dynamically.
4+
* Some libraries that support ESM still use `require` which is causing problems in esbuild.
5+
*/
6+
import { createRequire } from 'node:module';
7+
// eslint-disable-next-line no-global-assign
8+
require = createRequire(import.meta.url);

src/CommandPolykey.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { FileSystem } from 'polykey/types.js';
2-
import commander from 'commander';
2+
import type { OptionValues } from 'commander';
3+
import { Command } from 'commander';
34
import Logger, {
45
StreamHandler,
56
formatting,
@@ -18,7 +19,7 @@ const logger = new Logger('polykey', undefined, [new StreamHandler()]);
1819
/**
1920
* Base class for all commands
2021
*/
21-
class CommandPolykey extends commander.Command {
22+
class CommandPolykey extends Command {
2223
protected logger: Logger = logger;
2324
protected fs: FileSystem;
2425
protected exitHandlers: binUtils.ExitHandlers;
@@ -51,7 +52,7 @@ class CommandPolykey extends commander.Command {
5152
/**
5253
* Overrides opts to return all options set in the command hierarchy
5354
*/
54-
public opts<T extends commander.OptionValues>(): T {
55+
public opts<T extends OptionValues>(): T {
5556
const opts = super.opts<T>();
5657
if (this.parent != null) {
5758
// Override the current options with parent options

src/polykey.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
import type { PolykeyAgent } from 'polykey';
88
import fs from 'fs';
99
import process from 'process';
10+
import { fileURLToPath } from 'node:url';
1011
/**
1112
* Hack for wiping out the threads signal handlers
1213
* See: https://github.com/andywer/threads.js/issues/388
@@ -15,7 +16,6 @@ import process from 'process';
1516
* in the entire codebase for this hack to work
1617
* If the worker manager is used, it must be stopped gracefully with the PolykeyAgent
1718
*/
18-
import 'threads';
1919
import url from 'node:url';
2020
process.removeAllListeners('SIGINT');
2121
process.removeAllListeners('SIGTERM');
@@ -25,7 +25,8 @@ process.removeAllListeners('SIGTERM');
2525
* This can be referred to globally.
2626
* For ESM, change to using `import.meta.url`.
2727
*/
28-
globalThis.PK_MAIN_EXECUTABLE = __filename;
28+
const filename = fileURLToPath(import.meta.url);
29+
globalThis.PK_MAIN_EXECUTABLE = filename;
2930

3031
async function polykeyAgentMain(): Promise<number> {
3132
const {
@@ -146,7 +147,7 @@ async function polykeyAgentMain(): Promise<number> {
146147
}
147148

148149
async function polykeyMain(argv: Array<string>): Promise<number> {
149-
const { default: commander } = await import('commander');
150+
const { CommanderError } = await import('commander');
150151
const { default: ErrorPolykey } = await import('polykey/ErrorPolykey.js');
151152
const { default: CommandBootstrap } = await import('./bootstrap/index.js');
152153
const { default: CommandAgent } = await import('./agent/index.js');
@@ -194,7 +195,7 @@ async function polykeyMain(argv: Array<string>): Promise<number> {
194195
process.exitCode = 0;
195196
} catch (e) {
196197
const errFormat = rootCommand.opts().format === 'json' ? 'json' : 'error';
197-
if (e instanceof commander.CommanderError) {
198+
if (e instanceof CommanderError) {
198199
// Commander writes help and error messages on stderr automatically
199200
if (
200201
e.code === 'commander.help' ||

src/secrets/CommandEnv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type PolykeyClient from 'polykey/PolykeyClient.js';
22
import type { ParsedSecretPathValue } from '../types.js';
33
import path from 'path';
44
import os from 'os';
5-
import commander from 'commander';
5+
import { InvalidArgumentError } from 'commander';
66
import * as utils from 'polykey/utils/index.js';
77
import CommandPolykey from '../CommandPolykey.js';
88
import * as binProcessors from '../utils/processors.js';
@@ -86,7 +86,7 @@ class CommandEnv extends CommandPolykey {
8686
if (envVariables.length === 0) {
8787
this.addHelpText('before', 'You must provide at least 1 secret path');
8888
this.outputHelp();
89-
throw new commander.InvalidArgumentError(
89+
throw new InvalidArgumentError(
9090
'You must provide at least 1 secret path',
9191
);
9292
}

0 commit comments

Comments
 (0)