Code scraps: linking node_modules during build

Code scraps: linking node_modules during build

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]


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

    try:
        yield
    finally:
        shutil.rmtree(node_modules)


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:
                stack.enter_context(create_symlink(path))

            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.