#!/usr/bin/env node /** * Patches tree-sitter-dart to use NAPI bindings compatible with tree-sitter 0.22+ * * tree-sitter-dart v1.0.0 ships with NAN-style bindings that are incompatible * with tree-sitter 0.22+ which expects NAPI-style bindings with type-tagged * externals. This script rewrites the binding files and rebuilds. */ const { writeFileSync, existsSync } = require('fs'); const { join } = require('path'); const { execSync } = require('child_process'); const DART_DIR = join(__dirname, '..', 'node_modules', 'tree-sitter-dart'); if (!existsSync(DART_DIR)) { // tree-sitter-dart not installed, skip process.exit(0); } // Check if already patched (look for NAPI-style binding) const bindingPath = join(DART_DIR, 'bindings', 'node', 'binding.cc'); const { readFileSync } = require('fs'); try { const existing = readFileSync(bindingPath, 'utf8'); if (existing.includes('napi.h')) { // Already patched, check if build exists const buildPath = join(DART_DIR, 'build', 'Release', 'tree_sitter_dart_binding.node'); if (existsSync(buildPath)) { console.log('tree-sitter-dart: already patched and built.'); process.exit(0); } // Patched but not built, fall through to rebuild } } catch { // Can't read, continue with patch } console.log('Patching tree-sitter-dart for NAPI compatibility...'); // Write NAPI-compatible binding.cc const bindingCC = `#include typedef struct TSLanguage TSLanguage; extern "C" TSLanguage *tree_sitter_dart(); // "tree-sitter", "language" hashed with BLAKE2 const napi_type_tag LANGUAGE_TYPE_TAG = { 0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16 }; Napi::Object Init(Napi::Env env, Napi::Object exports) { exports["name"] = Napi::String::New(env, "dart"); auto language = Napi::External::New(env, tree_sitter_dart()); language.TypeTag(&LANGUAGE_TYPE_TAG); exports["language"] = language; return exports; } NODE_API_MODULE(tree_sitter_dart_binding, Init) `; writeFileSync(bindingPath, bindingCC); // Write NAPI-compatible binding.gyp const bindingGyp = `{ "targets": [ { "target_name": "tree_sitter_dart_binding", "dependencies": [ "