-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·90 lines (83 loc) · 2.52 KB
/
index.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
import {
getPkgRegistry,
getPkgEntrypoint,
getPkgChildPkgs,
getPkgChildDependenciesEntrypoints,
getPkgLocalStorageInfo,
getPkgResolvedVersion,
getPkgImportName,
getSourceFiles,
updateSourceDependencies,
saveSourceFiles,
removePkgFiles,
getPkgLocalVersion,
} from "./src/package.js";
import {
addToWebDependencies,
removeFromWebDependencies,
getWebDependenciesPkgs,
} from "./src/package-json.js";
import {
getImportMap,
saveImportMap,
getImportMapDependencies,
removeImportMapDependencies,
} from "./src/import-map.js";
import { formatPkgCommand } from "./src/format.js";
import { asyncMap } from "./src/utils.js";
const install = (pkgs) =>
asyncMap(pkgs)
.thenMap(getPkgRegistry)
.thenMap(getPkgEntrypoint)
.thenMap(getPkgResolvedVersion)
.log(({ pkgName, pkgTarget, pkgVersion, pkgRegistry: { name } }) =>
console.log(
`Installing ${pkgName}@${pkgVersion}${
pkgTarget ? `/${pkgTarget}` : ""
} from ${name} registry.`
)
)
.thenMap(getPkgChildDependenciesEntrypoints)
.thenMap(getPkgLocalStorageInfo)
.thenFlatMap(getPkgChildPkgs)
.thenMap(getPkgImportName)
.thenMap(getPkgLocalStorageInfo)
.thenMap(getImportMap)
.thenMap(getSourceFiles)
.thenMap(updateSourceDependencies)
.thenMap(saveImportMap)
.thenMap(saveSourceFiles);
function cli(command, ...pkgs) {
const actions = {
[undefined]: () => install(getWebDependenciesPkgs()),
install: () => install(getWebDependenciesPkgs()),
remove: (pkgs) =>
asyncMap(pkgs)
.thenMap(getPkgLocalVersion)
.thenFilter(({ pkgName, pkgVersion }) => {
if (pkgVersion) return true;
else {
console.log(`[${pkgName}] no matched version`);
return false;
}
})
.thenMap(getPkgLocalStorageInfo)
.thenMap(getPkgImportName)
.thenFlatMap(getImportMapDependencies)
.thenMap(removeImportMapDependencies)
.thenMap(removePkgFiles)
.log(({ pkgFilesRemoved }) =>
pkgFilesRemoved.map((file) => console.log(`Removed ${file}`))
)
.thenMap(removeFromWebDependencies),
add: (pkgs) =>
install(pkgs)
.thenFilter(({ pkgParent }) => !pkgParent)
.thenMap(addToWebDependencies),
};
if (command in actions) actions[command](pkgs?.map(formatPkgCommand));
else console.error("Error in command. Supported: ", Object.keys(actions));
}
console.log(`Heritage Package Manager`);
cli(...process.argv.slice(2));