Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/github-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Create GitHub release

on:
push:
tags:
- 'v*'
workflow_dispatch:

permissions:
contents: write

jobs:
release:
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
- name: Create GitHub release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ github.ref_name }}
name: Release ${{ github.ref_name }}
generate_release_notes: true
49 changes: 49 additions & 0 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Publish packages to npm

on:
push:
tags:
- 'v*'
workflow_dispatch:

permissions:
contents: read

jobs:
publish:
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 9

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
registry-url: https://registry.npmjs.org

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Lint code
run: pnpm lint

- name: Build package
run: pnpm build

- name: Run tests
run: pnpm test

- name: Publish package to npm
run: pnpm publish --no-git-checks --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mlightcad/mtext-parser",
"version": "1.3.2",
"version": "1.3.3",
"description": "AutoCAD MText parser written in TypeScript",
"type": "module",
"main": "dist/parser.cjs.js",
Expand All @@ -17,12 +17,13 @@
"build": "vite build",
"build:example": "vite build --config vite.config.ts --mode example",
"build:types": "tsc --emitDeclarationOnly",
"test": "jest",
"example": "npm run build && npm run build:example && node dist/node/example.cjs.js",
"format": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json}\"",
"format:check": "prettier --check \"src/**/*.{ts,tsx,js,jsx,json}\"",
"lint": "eslint ./src --ext .ts",
"lint:fix": "eslint ./src --ext .ts --fix"
"lint:fix": "eslint ./src --ext .ts --fix",
"release": "node tools/release.mjs",
"test": "jest"
},
"keywords": [
"autocad",
Expand Down
8 changes: 4 additions & 4 deletions src/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import {
describe('Utility Functions', () => {
describe('rgb2int', () => {
it('converts RGB tuple to integer', () => {
expect(rgb2int([255, 0, 0])).toBe(0x0000ff);
expect(rgb2int([255, 0, 0])).toBe(0xff0000);
expect(rgb2int([0, 255, 0])).toBe(0x00ff00);
expect(rgb2int([0, 0, 255])).toBe(0xff0000);
expect(rgb2int([0, 0, 255])).toBe(0x0000ff);
});
});

describe('int2rgb', () => {
it('converts integer to RGB tuple', () => {
expect(int2rgb(0x0000ff)).toEqual([255, 0, 0]);
expect(int2rgb(0xff0000)).toEqual([255, 0, 0]);
expect(int2rgb(0x00ff00)).toEqual([0, 255, 0]);
expect(int2rgb(0xff0000)).toEqual([0, 0, 255]);
expect(int2rgb(0x0000ff)).toEqual([0, 0, 255]);
});
});

Expand Down
6 changes: 3 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
/** Tab token with no data */
TABULATOR = 5,
/** New paragraph token with no data */
NEW_PARAGRAPH = 6,

Check warning on line 18 in src/parser.ts

View workflow job for this annotation

GitHub Actions / test

'NEW_PARAGRAPH' is defined but never used. Allowed unused vars must match /^_|^[A-Z][a-zA-Z]*$/u
/** New column token with no data */
NEW_COLUMN = 7,

Check warning on line 20 in src/parser.ts

View workflow job for this annotation

GitHub Actions / test

'NEW_COLUMN' is defined but never used. Allowed unused vars must match /^_|^[A-Z][a-zA-Z]*$/u
/** Wrap at dimension line token with no data */
WRAP_AT_DIMLINE = 8,

Check warning on line 22 in src/parser.ts

View workflow job for this annotation

GitHub Actions / test

'WRAP_AT_DIMLINE' is defined but never used. Allowed unused vars must match /^_|^[A-Z][a-zA-Z]*$/u
/** Properties changed token with string data (full command) */
PROPERTIES_CHANGED = 9,

Check warning on line 24 in src/parser.ts

View workflow job for this annotation

GitHub Actions / test

'PROPERTIES_CHANGED' is defined but never used. Allowed unused vars must match /^_|^[A-Z][a-zA-Z]*$/u
}

/**
Expand Down Expand Up @@ -145,7 +145,7 @@
/** Overline stroke */
OVERLINE = 2,
/** Strike-through stroke */
STRIKE_THROUGH = 4,

Check warning on line 148 in src/parser.ts

View workflow job for this annotation

GitHub Actions / test

'STRIKE_THROUGH' is defined but never used. Allowed unused vars must match /^_|^[A-Z][a-zA-Z]*$/u
}

/**
Expand Down Expand Up @@ -214,7 +214,7 @@
*/
export function rgb2int(rgb: RGB): number {
const [r, g, b] = rgb;
return (b << 16) | (g << 8) | r;
return (r << 16) | (g << 8) | b;
}

/**
Expand All @@ -223,9 +223,9 @@
* @returns RGB color tuple
*/
export function int2rgb(value: number): RGB {
const r = value & 0xff;
const r = (value >> 16) & 0xff;
const g = (value >> 8) & 0xff;
const b = (value >> 16) & 0xff;
const b = value & 0xff;
return [r, g, b];
}

Expand Down Expand Up @@ -491,7 +491,7 @@
return null;
} else {
const code = this.scanner.tail.match(new RegExp(`^[0-9A-Fa-f]{${length}}`))?.[0];
return code || null;

Check warning on line 494 in src/parser.ts

View workflow job for this annotation

GitHub Actions / test

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
}
}

Expand Down
107 changes: 107 additions & 0 deletions tools/release.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env node
import { execSync } from 'node:child_process';

function run(cmd) {
return execSync(cmd, {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
}).trim();
}

function fail(msg) {
console.error(`❌ ${msg}`);
process.exit(1);
}

/**
* Parse vX.Y.Z
*/
function parseVersion(tag) {
const m = tag.match(/^v(\d+)\.(\d+)\.(\d+)$/);
if (!m) return null;
return {
major: Number(m[1]),
minor: Number(m[2]),
patch: Number(m[3]),
};
}

/**
* Find latest vX.Y.Z tag (sorted by semver, not by time)
*/
function findLatestTag() {
const output = run('git tag --list "v*.*.*"');
if (!output) return null;

const tags = output
.split('\n')
.map(t => ({ tag: t, v: parseVersion(t) }))
.filter(t => t.v !== null);

if (tags.length === 0) return null;

tags.sort((a, b) => {
if (a.v.major !== b.v.major) return b.v.major - a.v.major;
if (a.v.minor !== b.v.minor) return b.v.minor - a.v.minor;
return b.v.patch - a.v.patch;
});

return tags[0].tag;
}

/**
* Main
*/
let version = process.argv[2];

if (!version) {
const latestTag = findLatestTag();
if (!latestTag) {
fail('No existing vX.Y.Z tag found, please specify a version explicitly.');
}

const v = parseVersion(latestTag);
version = `${v.major}.${v.minor}.${v.patch + 1}`;

console.log(`ℹ️ No version provided, auto-incrementing patch: ${latestTag} → v${version}`);
}

if (!/^\d+\.\d+\.\d+$/.test(version)) {
fail(`Invalid version "${version}". Expected format: X.Y.Z`);
}

const tag = `v${version}`;

/**
* Safety checks
*/
try {
run(`git rev-parse --verify refs/tags/${tag}`);
fail(`Tag ${tag} already exists.`);
} catch {
// tag does not exist → OK
}

try {
const branch = run('git branch --show-current');
if (branch !== 'main') {
fail(`Current branch is "${branch}". Please release from "main".`);
}
} catch {
// non-fatal
}

const status = run('git status --porcelain');
if (status) {
fail('Working tree is not clean. Please commit or stash changes.');
}

/**
* Create & push annotated tag
*/
console.log(`🚀 Creating tag ${tag}`);

run(`git tag -a ${tag} -m "release: release ${tag}"`);
run(`git push origin ${tag}`);

console.log(`✅ Release tag ${tag} pushed successfully`);