From 1b490a17eeeaeb332b9e182edd297c636415ecfe Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Fri, 27 Oct 2023 13:26:23 +0100 Subject: [PATCH] wip --- index.ts | 4 ++-- list.ts | 17 +++++++++++++---- package.json | 14 +++++++++----- tests/test.cjs | 11 ++++------- tests/test.mjs | 2 +- tests/test.ts | 4 ++-- tsconfig.cjs.json | 7 +++++++ tsconfig.esm.json | 7 +++++++ tsconfig.json | 5 ++--- types/global.d.ts | 7 +++++++ 10 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 tsconfig.cjs.json create mode 100644 tsconfig.esm.json create mode 100644 types/global.d.ts diff --git a/index.ts b/index.ts index 25c4de8..af20bce 100644 --- a/index.ts +++ b/index.ts @@ -316,7 +316,7 @@ export class Scenarios { } } -export const seenScenarios: Scenario[] = []; +global.scenarioTesterSeenScenarios = []; export class Scenario { constructor( @@ -324,7 +324,7 @@ export class Scenario { private callbackCreateProject: CallbackCreateProject, private mutators: CallbackMutateProject[] ) { - seenScenarios.push(this); + global.scenarioTesterSeenScenarios.push(this); } async prepare(outdir?: string): Promise { diff --git a/list.ts b/list.ts index bd3499b..208d93a 100644 --- a/list.ts +++ b/list.ts @@ -1,10 +1,10 @@ -import { Scenario, seenScenarios } from './index.js'; +import { Scenario } from './index.js'; import glob from 'glob'; import { resolve } from 'path'; import { format } from 'util'; -import { createRequire } from 'node:module'; -const require = createRequire(import.meta.url); +// import { createRequire } from 'node:module'; +// const require = createRequire(import.meta.url); const { sync: globSync } = glob; @@ -15,8 +15,17 @@ export interface ListParams { } export async function list(params: ListParams): Promise { + debugger if (params.require) { for (let r of params.require) { + let require; + if (typeof import.meta !== undefined) { + const { default: { createRequire } } = await(import('node:module')); + require = createRequire(require('url').pathToFileURL(__filename).toString()); + } else { + require = global.require; + } + await import(require.resolve(r, { paths: [process.cwd()]})); } } @@ -25,7 +34,7 @@ export async function list(params: ListParams): Promise { await import(resolve(file)); } } - return seenScenarios; + return global.scenarioTesterSeenScenarios; } export async function printList(params: ListParams) { diff --git a/package.json b/package.json index fd00723..0831e7f 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,11 @@ }, "type": "module", "scripts": { - "prepare": "tsc", + "prepare": "yarn build", + "build": "concurrently --no-color \"npm:build:*\" --names \"build:\"", + "build:esm": "tsc -p tsconfig.esm.json", + "build:cjs": "tsc -p tsconfig.cjs.json", + "build:types": "tsc -p tsconfig.types.json", "start": "tsc --watch", "test": "concurrently --no-color \"npm:test:*\" --names \"test:\"", "test:ts": "node --loader ts-node/esm node_modules/.bin/qunit --require ts-node/register tests/test.ts", @@ -61,12 +65,12 @@ }, "exports": { ".": { - "import": "./build/index.js" + "import": "./build/mjs/index.js" }, "./*": { - "types": "./build/*.d.ts", - "import": "./build/*.js", - "default": "./build/*.js" + "types": "./build/types/*.d.ts", + "import": "./build/mjs/*.js", + "default": "./build/cjs/*.js" } } } diff --git a/tests/test.cjs b/tests/test.cjs index 54ed450..efbf0c4 100644 --- a/tests/test.cjs +++ b/tests/test.cjs @@ -1,9 +1,6 @@ "use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const index_js_1 = require("./index.js"); + +const { Scenarios } = require('../'); const qunit_1 = __importDefault(require("qunit")); const child_process_1 = __importDefault(require("child_process")); function hello1(project) { @@ -18,7 +15,7 @@ function hello2(project) { resolveName: 'hello', }); } -const scenarios = index_js_1.Scenarios.fromDir('./fixtures/app').expand({ +const scenarios = Scenarios.fromDir('./fixtures/app').expand({ hello1, hello2, }); @@ -59,4 +56,4 @@ qunit_1.default.module('cli', () => { .split('\n'), ['hello1', 'hello2']); }); }); -//# sourceMappingURL=test.js.map \ No newline at end of file +//# sourceMappingURL=test.js.map diff --git a/tests/test.mjs b/tests/test.mjs index 7b690d9..b5cbd06 100644 --- a/tests/test.mjs +++ b/tests/test.mjs @@ -74,7 +74,7 @@ ok 1 project > createHello Qunit.module('cli', () => { Qunit.test('list', async (assert) => { - const result = await execa('npx', ['.', 'list', '--files', 'tests/test.mjs', '--matrix']) + const result = await execa('node', ['build/cli.js', 'list', '--files', 'tests/test.mjs', '--matrix']) const { stdout } = result; assert.deepEqual( diff --git a/tests/test.ts b/tests/test.ts index 729e81b..44ab847 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -1,5 +1,5 @@ import { Project } from 'fixturify-project'; -import { Scenarios } from '../index.js'; +import { Scenarios } from '../'; import type { PreparedApp } from '../index.js'; import Qunit from 'qunit'; import execa from 'execa'; @@ -79,7 +79,7 @@ Qunit.module('cli', () => { Qunit.test('list', async (assert) => { // I tried to test this using the ts file dirrectly but I couldn't get ts-node/require to work correctly // so I'm just testing against the compiled esm output - const result = await execa('npx', ['.', 'list', '--files', './build/tests/test.js', '--matrix']) + const result = await execa('node', ['build/cli.js', 'list', '--files', './build/tests/test.js', '--matrix']) const { stdout } = result; assert.deepEqual( diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..25fe1c6 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./build/cjs", + "module": "commonjs", + } +} diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..4e02861 --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./build/esm", + "module": "ES2020", + } +} diff --git a/tsconfig.json b/tsconfig.json index 535be30..9f973c8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,9 +4,7 @@ "experimentalSpecifierResolution": "node" }, "compilerOptions": { - "outDir": "./build/", "target": "ES2020", - "module": "ES2020", "moduleResolution": "node", "declaration": true, "esModuleInterop": true, @@ -17,7 +15,8 @@ "strict": true }, "include": [ - "*.ts" + "*.ts", + "types/*" ], "exclude": [ "node_modules/**/*", diff --git a/types/global.d.ts b/types/global.d.ts new file mode 100644 index 0000000..fc3fc45 --- /dev/null +++ b/types/global.d.ts @@ -0,0 +1,7 @@ +import { Scenario } from ".."; + + +declare global { + // eslint-disable-next-line no-var + var scenarioTesterSeenScenarios: Scenario[]; +}