diff --git a/.config/prettierignore b/.config/prettierignore index 67d30674..4c2cb3ed 100644 --- a/.config/prettierignore +++ b/.config/prettierignore @@ -1,11 +1,11 @@ .config -../.github -../.vscode -../docs -../es6 -../js-legacy -../node_modules -../screenshots -../LICENSE -../package-lock.json -../types/generated +.github +.vscode +docs +es6 +js-legacy +node_modules +screenshots +LICENSE +package-lock.json +types diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index d6a3dc3a..b5b295fd 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: - command: [format-check, test-es6, test-legacy] + command: [format-check, test-es6, build-test-types, test-legacy] steps: - uses: actions/checkout@v3 @@ -25,5 +25,4 @@ jobs: node-version: 18.x cache: 'npm' - run: npm ci - # - run: npm run build --if-present - run: npm run ${{ matrix.command }} diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 302a3b63..a59f7112 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -15,8 +15,9 @@ jobs: - uses: actions/setup-node@v3 with: node-version: 18.x - - run: npm ci && npm run gen:dts:check + - run: npm ci - run: npm test + - run: npm build-test-types publish-npm: needs: build @@ -28,7 +29,8 @@ jobs: node-version: 18.x registry-url: https://registry.npmjs.org/ - run: npm ci - - run: npm run gen:dts && npm run build + - run: npm run build + - run: npm run build-types - run: npm publish env: NODE_AUTH_TOKEN: ${{secrets.npm_token}} diff --git a/package.json b/package.json index c90885a6..fe23495f 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,12 @@ "version": "0.8.7", "description": "jsMind is a pure javascript library for mindmap, it base on html5 canvas. jsMind was released under BSD license, you can embed it in any project, if only you observe the license.", "main": "es6/jsmind.js", - "types": "types/generated/types-index.d.ts", + "types": "types/generated/index.d.ts", "exports": { ".": { "import": "./es6/jsmind.js", "require": "./es6/jsmind.js", - "types": "./types/generated/types-index.d.ts" + "types": "./types/generated/index.d.ts" }, "./draggable-node": { "import": "./es6/jsmind.draggable-node.js", @@ -36,16 +36,14 @@ "scripts": { "server": "http-server", "build": "rollup -c .config/rollup.config.js", - "test": "NODE_OPTIONS=--experimental-vm-modules jest", - "test-legacy": "jest tests/legacy", + "build-types": "npx -p typescript tsc -p types/tsconfig.declaration.json", + "test": "NODE_OPTIONS=--experimental-vm-modules jest tests/unit", "test-es6": "NODE_OPTIONS=--experimental-vm-modules jest tests/unit", - "gen:dts": "tsc -p tsconfig.decls.json", - "gen:dts:check": "npm run gen:dts && test -d types/generated && ls types/generated >/dev/null", + "test-types": "NODE_OPTIONS=--experimental-vm-modules jest tests/types", + "build-test-types": "npm run build-types && npm run test-types", + "test-legacy": "jest tests/legacy", "format": "prettier --config .config/prettierrc.json --ignore-path .config/prettierignore --write .", - "format-check": "prettier --config .config/prettierrc.json --ignore-path .config/prettierignore --check .", - "pretest": "npm run gen:dts", - "prepare": "npm run gen:dts", - "prepublishOnly": "npm run build && npm run gen:dts" + "format-check": "prettier --config .config/prettierrc.json --ignore-path .config/prettierignore --check ." }, "repository": { "type": "git", diff --git a/src/types-index.ts b/src/index.ts similarity index 100% rename from src/types-index.ts rename to src/index.ts diff --git a/tests/fixtures/typescript-test.ts b/tests/types/fixtures/typescript-test.ts similarity index 97% rename from tests/fixtures/typescript-test.ts rename to tests/types/fixtures/typescript-test.ts index d7dbb1fe..9d968c84 100644 --- a/tests/fixtures/typescript-test.ts +++ b/tests/types/fixtures/typescript-test.ts @@ -12,6 +12,7 @@ import jsMind, { NodeTreeFormat, NodeTreeData, } from 'jsmind'; +import { EventData } from 'types/generated/jsmind'; // Note: in real usage, plugins should be imported to register themselves // import 'jsmind/draggable-node'; // import 'jsmind/screenshot'; @@ -79,7 +80,7 @@ const fullOptions: JsMindOptions = { step: 0.2, mask_key: 4096, }, - custom_node_render: (jm, element, node) => { + custom_node_render: (jm: jsMind, element: HTMLElement, node: Node) => { element.innerHTML = `${node.topic}`; }, expander_style: 'number', @@ -229,7 +230,7 @@ jm.resize(); // Event listener with stricter data shape -jm.add_event_listener((type, data) => { +jm.add_event_listener((type: number, data: EventData) => { // data: { evt?: string; data?: unknown[]; node?: string } console.log(`Event ${type} triggered with data:`, data); }); diff --git a/tests/unit/typescript.types.test.js b/tests/types/jsmind.types.test.js similarity index 95% rename from tests/unit/typescript.types.test.js rename to tests/types/jsmind.types.test.js index 6d0ceedf..f5ea7d02 100644 --- a/tests/unit/typescript.types.test.js +++ b/tests/types/jsmind.types.test.js @@ -22,7 +22,7 @@ function formatHost() { } describe('TypeScript typings validation', () => { - test('tests/fixtures/typescript-test.ts should pass type-check (no diagnostics)', () => { + test('tests/types/fixtures/typescript-test.ts should pass type-check (no diagnostics)', () => { const projectRoot = process.cwd(); const tsconfigPath = path.join(projectRoot, 'tsconfig.json'); diff --git a/tests/types/package.json b/tests/types/package.json new file mode 100644 index 00000000..47200257 --- /dev/null +++ b/tests/types/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/tsconfig.json b/tsconfig.json index afc73871..c1932cd6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,11 +13,11 @@ "noEmit": true, "baseUrl": ".", "paths": { - "jsmind": ["types/generated/types-index.d.ts"], + "jsmind": ["types/generated/index.d.ts"], "jsmind/draggable-node": ["types/generated/plugins/jsmind.draggable-node.d.ts"], "jsmind/screenshot": ["types/generated/plugins/jsmind.screenshot.d.ts"] } }, - "include": ["types/**/*.d.ts", "tests/fixtures/typescript-test.ts"], + "include": ["types/**/*.d.ts", "tests/types/fixtures/typescript-test.ts"], "exclude": ["node_modules", "dist"] } diff --git a/tsconfig.decls.json b/types/tsconfig.declaration.json similarity index 64% rename from tsconfig.decls.json rename to types/tsconfig.declaration.json index 4b4ef31d..31ba2b9a 100644 --- a/tsconfig.decls.json +++ b/types/tsconfig.declaration.json @@ -4,17 +4,16 @@ "module": "ESNext", "moduleResolution": "node", "strict": false, - "skipLibCheck": true, "esModuleInterop": true, + "skipLibCheck": true, "declaration": true, "emitDeclarationOnly": true, "stripInternal": true, "allowJs": true, "checkJs": false, - "outDir": "types/generated", - "rootDir": "src", + "rootDirs": ["../src"], + "outDir": "./generated", "baseUrl": "." }, - "include": ["src/**/*.js", "src/**/*.ts"], - "exclude": ["node_modules", "dist", "tests", "example", "es6", "js-legacy", "js-legacy/**/*.js"] + "include" : ["../src/**/*.ts", "../src/**/*.js"] }