Mark Vasilkov
Mark Vasilkov


Mark Vasilkov

Code scraps: linking node_modules during build

Code scraps: linking node_modules during build

Mark Vasilkov's photo
Mark Vasilkov
·Jan 16, 2023·

1 min read

Before version 0.1, natlib had examples in the main repo, and they were built together with the library. (This is no longer the case, as the examples and tests moved to a separate repo.)

To produce a node_modules folder as if natlib was installed as a dependency, the following Python script was used:

#!/usr/bin/env python3

from contextlib import contextmanager, ExitStack
from pathlib import Path
import shutil

from but.external.typescript import typescript_call

OUR_ROOT = Path(__file__).resolve().parents[1]

def create_symlink(path: Path):
    node_modules = path / 'node_modules'
    (node_modules / 'natlib').symlink_to(OUR_ROOT / 'out', target_is_directory=True)


def build():
    examples = OUR_ROOT / 'examples'

    projects = (
        ('..', False),
        ('couch2048', True),
        ('notfound', True),

    for path_str, need_symlink in projects:
        path = examples / path_str

        with ExitStack() as stack:
            if need_symlink:

            typescript_call(['--project', path])

Here, OUR_ROOT / 'out' is where TypeScript puts the resulting JS files.

This way the code that runs inside the context manager block has its node_modules folder set up correctly.