-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuseMoustaches.ts
More file actions
67 lines (59 loc) · 1.92 KB
/
useMoustaches.ts
File metadata and controls
67 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Package, Installation } from "../types.ts"
import SemVer from "../utils/semver.ts"
import useConfig from "./useConfig.ts"
import useCellar from "./useCellar.ts"
function tokenizePackage(pkg: Package) {
return [{ from: "prefix", to: useCellar().keg(pkg).string }]
}
function tokenizeVersion(version: SemVer, prefix = 'version') {
const rv = [
{ from: prefix, to: `${version}` },
{ from: `${prefix}.major`, to: `${version.major}` },
{ from: `${prefix}.minor`, to: `${version.minor}` },
{ from: `${prefix}.patch`, to: `${version.patch}` },
{ from: `${prefix}.marketing`, to: `${version.major}.${version.minor}` },
{ from: `${prefix}.build`, to: version.build.join('+') },
{ from: `${prefix}.raw`, to: version.raw },
]
if ('tag' in version) {
rv.push({from: `${prefix}.tag`, to: (version as unknown as {tag: string}).tag})
}
return rv
}
function apply(input: string, map: { from: string, to: string }[]) {
return map.reduce((acc, {from, to}) =>
acc.replace(new RegExp(`(^\\$)?{{\\s*${from}\\s*}}`, "g"), to),
input)
}
export default function() {
const config = useConfig()
const base = {
apply,
tokenize: {
version: tokenizeVersion,
pkg: tokenizePackage
}
}
const deps = (deps: Installation[]) => {
const map: {from: string, to: string}[] = []
for (const dep of deps ?? []) {
map.push({ from: `deps.${dep.pkg.project}.prefix`, to: dep.path.string })
map.push(...base.tokenize.version(dep.pkg.version, `deps.${dep.pkg.project}.version`))
}
return map
}
const pkgx = () => [{ from: "pkgx.prefix", to: config.prefix.string }]
const all = (pkg: Package, deps_: Installation[]) => [
...deps(deps_),
...tokenizePackage(pkg),
...pkgx(),
...base.tokenize.version(pkg.version),
]
return {
apply: base.apply,
tokenize: {
...base.tokenize,
deps, pkgx, all
}
}
}