refactor: rewrite scripts in typescript
This commit is contained in:
parent
31ceb99603
commit
169d7ad57f
5 changed files with 70 additions and 53 deletions
|
|
@ -1,20 +1,22 @@
|
|||
import { readFileSync, writeFileSync } from 'fs';
|
||||
import { SourceMapGenerator, SourceMapConsumer } from 'source-map';
|
||||
import { SourceMapGenerator, SourceMapConsumer, RawSourceMap } from 'source-map';
|
||||
|
||||
async function fixSourceMap() {
|
||||
const distMap = JSON.parse(readFileSync('dist/tree-sitter.js.map', 'utf8'));
|
||||
const distMap = JSON.parse(readFileSync('dist/tree-sitter.js.map', 'utf8')) as RawSourceMap;
|
||||
const distJs = readFileSync('dist/tree-sitter.js', 'utf8').split('\n');
|
||||
const finalJs = readFileSync('tree-sitter.js', 'utf8').split('\n');
|
||||
|
||||
const lineMap = new Map();
|
||||
const lineMap = new Map<number, number>();
|
||||
|
||||
let currentFinalLine = 0;
|
||||
for (let distLine = 0; distLine < distJs.length; distLine++) {
|
||||
const line = distJs[distLine].trim();
|
||||
if (!line) continue;
|
||||
|
||||
for (let finalLine = 0; finalLine < finalJs.length; finalLine++) {
|
||||
for (let finalLine = currentFinalLine; finalLine < finalJs.length; finalLine++) {
|
||||
if (finalJs[finalLine].trim() === line) {
|
||||
lineMap.set(distLine + 1, finalLine + 1);
|
||||
currentFinalLine = finalLine;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +25,7 @@ async function fixSourceMap() {
|
|||
const consumer = await new SourceMapConsumer(distMap);
|
||||
const generator = new SourceMapGenerator({
|
||||
file: 'tree-sitter.js',
|
||||
sourceRoot: ''
|
||||
sourceRoot: '',
|
||||
});
|
||||
|
||||
consumer.eachMapping(mapping => {
|
||||
|
|
@ -32,15 +34,15 @@ async function fixSourceMap() {
|
|||
generator.addMapping({
|
||||
generated: {
|
||||
line: finalLine,
|
||||
column: mapping.generatedColumn
|
||||
column: mapping.generatedColumn,
|
||||
},
|
||||
original: {
|
||||
line: mapping.originalLine,
|
||||
column: mapping.originalColumn
|
||||
column: mapping.originalColumn,
|
||||
},
|
||||
// Fix the source path to be relative to binding_web
|
||||
source: `src/${mapping.source.split('/').pop()}`,
|
||||
name: mapping.name
|
||||
name: mapping.name,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
@ -50,7 +52,7 @@ async function fixSourceMap() {
|
|||
if (content) {
|
||||
generator.setSourceContent(
|
||||
`src/${source.split('/').pop()}`,
|
||||
content
|
||||
content,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const inputFiles = [
|
||||
'lib/binding.c',
|
||||
'binding.js',
|
||||
'wasm/exports.txt',
|
||||
'wasm/imports.js',
|
||||
'prefix.js',
|
||||
...list('../include/tree_sitter'),
|
||||
...list('../src'),
|
||||
];
|
||||
|
||||
const outputFiles = ['tree-sitter.js', 'tree-sitter.wasm'];
|
||||
|
||||
const outputMtime = Math.min(...outputFiles.map(mtime));
|
||||
|
||||
for (const inputFile of inputFiles) {
|
||||
if (mtime(inputFile) > outputMtime) {
|
||||
console.log(`File '${inputFile}' has changed. Re-run 'script/build-wasm'.`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function list(dir) {
|
||||
return fs
|
||||
.readdirSync(path.join(__dirname, dir), 'utf8')
|
||||
.filter((p) => !p.startsWith('.'))
|
||||
.map((p) => path.join(dir, p));
|
||||
}
|
||||
|
||||
function mtime(p) {
|
||||
return fs.statSync(path.join(__dirname, p)).mtime;
|
||||
}
|
||||
46
lib/binding_web/script/check-artifacts-fresh.ts
Executable file
46
lib/binding_web/script/check-artifacts-fresh.ts
Executable file
|
|
@ -0,0 +1,46 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const inputFiles = [
|
||||
'../lib/tree-sitter.c',
|
||||
'../src/constants.ts',
|
||||
'../src/index.ts',
|
||||
'../src/language.ts',
|
||||
'../src/lookahead_iterator.ts',
|
||||
'../src/marshal.ts',
|
||||
'../src/node.ts',
|
||||
'../src/parser.ts',
|
||||
'../src/query.ts',
|
||||
'../src/tree.ts',
|
||||
'../src/tree_cursor.ts',
|
||||
'../dist/tree-sitter.js',
|
||||
'../wasm/exports.txt',
|
||||
'../wasm/imports.js',
|
||||
'../wasm/prefix.js',
|
||||
...listFiles('../../include/tree_sitter'),
|
||||
...listFiles('../../src'),
|
||||
];
|
||||
|
||||
const outputFiles = ['../tree-sitter.js', '../tree-sitter.wasm'];
|
||||
const outputMtime = Math.min(...outputFiles.map(getMtime));
|
||||
|
||||
for (const inputFile of inputFiles) {
|
||||
if (getMtime(inputFile) > outputMtime) {
|
||||
console.log(`File '${inputFile}' has changed. Re-run 'npm run build:wasm'.`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function listFiles(dir: string): string[] {
|
||||
return fs
|
||||
.readdirSync(path.resolve(scriptDir, dir))
|
||||
.filter(p => !p.startsWith('.'))
|
||||
.map(p => path.join(dir, p));
|
||||
}
|
||||
|
||||
function getMtime(p: string): number {
|
||||
return fs.statSync(path.resolve(scriptDir, p)).mtime.getTime();
|
||||
}
|
||||
|
|
@ -1,25 +1,24 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const isDebug = process.env.npm_config_web_tree_sitter_debug === 'true';
|
||||
const isDebug = process.env.npm_config_debug === 'true';
|
||||
|
||||
if (isDebug) {
|
||||
// Copy debug versions to root
|
||||
fs.copyFileSync(
|
||||
path.join(__dirname, '../debug/tree-sitter.js'),
|
||||
path.join(__dirname, '../tree-sitter.js')
|
||||
path.join(__dirname, '../tree-sitter.js'),
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(__dirname, '../debug/tree-sitter.wasm'),
|
||||
path.join(__dirname, '../tree-sitter.wasm')
|
||||
path.join(__dirname, '../tree-sitter.wasm'),
|
||||
);
|
||||
// Copy sourcemaps too
|
||||
fs.copyFileSync(
|
||||
path.join(__dirname, '../debug/tree-sitter.js.map'),
|
||||
path.join(__dirname, '../tree-sitter.js.map')
|
||||
path.join(__dirname, '../tree-sitter.js.map'),
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(__dirname, '../debug/tree-sitter.wasm.map'),
|
||||
path.join(__dirname, '../tree-sitter.wasm.map')
|
||||
path.join(__dirname, '../tree-sitter.wasm.map'),
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue