Skip to content

Commit 2b56e2c

Browse files
committed
feat: Add support for the --config flag and passing extra flags to the deno process
1 parent 86961ca commit 2b56e2c

File tree

4 files changed

+145
-6
lines changed

4 files changed

+145
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
22
dist/
33
docs/
4-
tmp/
4+
tmp/
5+
src/test/deno.lock

src/DenoWorker.spec.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,129 @@ describe('DenoWorker', () => {
749749
});
750750
});
751751

752+
describe('denoConfig', async () => {
753+
afterEach(() => {
754+
jest.clearAllMocks();
755+
});
756+
757+
it('should not include --config by default', async () => {
758+
const spawnSpy = jest.spyOn(child_process, 'spawn');
759+
760+
worker = new DenoWorker(echoScript);
761+
762+
let resolve: any;
763+
let promise = new Promise((res, rej) => {
764+
resolve = res;
765+
});
766+
worker.onmessage = (e) => {
767+
resolve();
768+
};
769+
770+
worker.postMessage({
771+
type: 'echo',
772+
message: 'Hello',
773+
});
774+
775+
await promise;
776+
777+
const call = spawnSpy.mock.calls[0];
778+
const [_deno, args] = call;
779+
expect(args).not.toContain('--config');
780+
781+
// should pass --no-config when denoConfig is omitted
782+
expect(args).toContain('--no-config');
783+
});
784+
785+
it('should not set --config by when denoConfig is empty', async () => {
786+
const spawnSpy = jest.spyOn(child_process, 'spawn');
787+
788+
worker = new DenoWorker(echoScript, { denoConfig: '' });
789+
790+
let resolve: any;
791+
let promise = new Promise((res, rej) => {
792+
resolve = res;
793+
});
794+
worker.onmessage = (e) => {
795+
resolve();
796+
};
797+
798+
worker.postMessage({
799+
type: 'echo',
800+
message: 'Hello',
801+
});
802+
803+
await promise;
804+
805+
const call = spawnSpy.mock.calls[0];
806+
const [_deno, args] = call;
807+
expect(args).not.toContain('--config');
808+
809+
// should pass --no-config when denoConfig is empty
810+
expect(args).toContain('--no-config');
811+
});
812+
813+
it('should set --config when denoConfig is nonempty', async () => {
814+
const spawnSpy = jest.spyOn(child_process, 'spawn');
815+
const configPath = path.resolve('./src/test/deno.json');
816+
worker = new DenoWorker(echoScript, {
817+
denoConfig: configPath,
818+
});
819+
820+
let resolve: any;
821+
let promise = new Promise((res, rej) => {
822+
resolve = res;
823+
});
824+
worker.onmessage = (e) => {
825+
resolve();
826+
};
827+
828+
worker.postMessage({
829+
type: 'echo',
830+
message: 'Hello',
831+
});
832+
833+
await promise;
834+
835+
const call = spawnSpy.mock.calls[0];
836+
const [_deno, args] = call;
837+
expect(args).toContain(`--config=${configPath}`);
838+
});
839+
});
840+
841+
describe('denoExtraFlags', async () => {
842+
afterEach(() => {
843+
jest.clearAllMocks();
844+
});
845+
846+
it('should include the given extra flags', async () => {
847+
const spawnSpy = jest.spyOn(child_process, 'spawn');
848+
849+
worker = new DenoWorker(echoScript, {
850+
denoExtraFlags: ['--unstable', '--inspect'],
851+
});
852+
853+
let resolve: any;
854+
let promise = new Promise((res, rej) => {
855+
resolve = res;
856+
});
857+
worker.onmessage = (e) => {
858+
resolve();
859+
};
860+
861+
worker.postMessage({
862+
type: 'echo',
863+
message: 'Hello',
864+
});
865+
866+
await promise;
867+
868+
const call = spawnSpy.mock.calls[0];
869+
const [_deno, args] = call;
870+
expect(args).toContain('--unstable');
871+
expect(args).toContain('--inspect');
872+
});
873+
});
874+
752875
describe('denoV8Flags', async () => {
753876
afterEach(() => {
754877
jest.clearAllMocks();

src/DenoWorker.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,22 @@ export interface DenoWorkerOptions {
160160
denoNoCheck: boolean;
161161

162162
/**
163-
* Whether to prevent Deno from loading a config file.
164-
* Defaults to true.
163+
* The path to the config file that Deno should use.
165164
*/
166-
denoNoConfig: boolean;
165+
denoConfig: string;
167166

168167
/**
169168
* Whether to prevent Deno from loading packages from npm.
170169
* Defaults to true.
171170
*/
172171
denoNoNPM: boolean;
173172

173+
/**
174+
* Extra flags that should be passed to Deno.
175+
* This may be useful for passing flags that are not supported by this library.
176+
*/
177+
denoExtraFlags: string[];
178+
174179
/**
175180
* Allow Deno to make requests to hosts with certificate
176181
* errors.
@@ -305,9 +310,10 @@ export class DenoWorker {
305310
denoLockFilePath: '',
306311
denoCachedOnly: false,
307312
denoNoCheck: false,
308-
denoNoConfig: true,
313+
denoConfig: undefined,
309314
denoNoNPM: true,
310315
unsafelyIgnoreCertificateErrors: false,
316+
denoExtraFlags: [],
311317
spawnOptions: {},
312318
},
313319
options || {}
@@ -419,7 +425,11 @@ export class DenoWorker {
419425
}
420426
addOption(runArgs, '--cached-only', this._options.denoCachedOnly);
421427
addOption(runArgs, '--no-check', this._options.denoNoCheck);
422-
addOption(runArgs, '--no-config', this._options.denoNoConfig);
428+
if (!this._options.denoConfig) {
429+
addOption(runArgs, '--no-config', true);
430+
} else {
431+
addOption(runArgs, '--config', [this._options.denoConfig]);
432+
}
423433
addOption(runArgs, '--no-npm', this._options.denoNoNPM);
424434
addOption(
425435
runArgs,
@@ -501,6 +511,10 @@ export class DenoWorker {
501511
}
502512
}
503513

514+
if (this._options.denoExtraFlags.length > 0) {
515+
runArgs.push(...this._options.denoExtraFlags);
516+
}
517+
504518
this._process = spawn(
505519
this._options.denoExecutable,
506520
[

src/test/deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)