Jostraca code generation, made repeatable

Skip files when copying

Keep editor backups, caches and named paths out of a copied tree.

Rendered from docs/how-to/skip-files-when-copying.md in the generator repository — where a correction belongs, and where the test suite runs every example on this page or states why it does not.

Two mechanisms, and they do different jobs. cmp.Copy.ignore matches names anywhere in the tree; exclude matches paths relative to the copied source root.

The source holds the file worth copying, tpl/src/app.js:

export const app = 1

an editor backup beside it, tpl/src/app.js~:

an editor backup

something internal, tpl/src/notes.md:

internal notes

and a cache directory, tpl/src/.cache/blob.txt:

cached
import { Jostraca, Project, Copy } from 'jostraca'

const jostraca = Jostraca({
  cmp: { Copy: { ignore: [/^\.cache$/] } },
})

await jostraca.generate({ folder: './out' }, () => {
  Project({}, () => {
    Copy({ from: './tpl/src', exclude: ['notes.md'] })
  })
})
app.js

Three rules were in play:

  • app.js~ went without being asked. Names ending ~ or -jostraca-off are always skipped, whatever you configure. That rule cannot be turned off, and it applies to directories too.
  • .cache/ was pruned by cmp.Copy.ignore. The list holds regular expressions matched against the bare entry name, so naming a directory removes its whole subtree.
  • notes.md was dropped by exclude. That one is matched against the path relative to the copied source root, however deep in the output tree the Copy sits.

Two traps in exclude. String entries are compared exactly, so './a.txt' does not match a.txt. And a boolean is accepted and does nothing at all: exclude: true copies everything.

cmp.Copy.ignore merges over the built-in default index by index, so a list of your own replaces the default’s first entry. That costs nothing in practice, because the ~ rule is hard-coded as well.

See also#