@powerhousedao/ph-cli 0.33.2-dev.0 → 0.34.2-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/dist/package.json +51 -0
  2. package/dist/src/cli.d.ts +3 -0
  3. package/dist/src/cli.d.ts.map +1 -0
  4. package/dist/src/cli.js +10 -0
  5. package/dist/src/commands/connect.d.ts +6 -0
  6. package/dist/src/commands/connect.d.ts.map +1 -0
  7. package/dist/src/commands/connect.js +44 -0
  8. package/dist/src/commands/dev.d.ts +14 -0
  9. package/dist/src/commands/dev.d.ts.map +1 -0
  10. package/dist/src/commands/dev.js +88 -0
  11. package/dist/src/commands/generate.d.ts +21 -0
  12. package/dist/src/commands/generate.d.ts.map +1 -0
  13. package/dist/src/commands/generate.js +74 -0
  14. package/dist/src/commands/help.d.ts +3 -0
  15. package/dist/src/commands/help.d.ts.map +1 -0
  16. package/dist/src/commands/help.js +8 -0
  17. package/dist/src/commands/index.d.ts +14 -0
  18. package/dist/src/commands/index.d.ts.map +1 -0
  19. package/dist/src/commands/index.js +34 -0
  20. package/dist/src/commands/inspect.d.ts +10 -0
  21. package/dist/src/commands/inspect.d.ts.map +1 -0
  22. package/dist/src/commands/inspect.js +57 -0
  23. package/dist/src/commands/install.d.ts +15 -0
  24. package/dist/src/commands/install.d.ts.map +1 -0
  25. package/dist/src/commands/install.js +80 -0
  26. package/dist/src/commands/list.d.ts +9 -0
  27. package/dist/src/commands/list.d.ts.map +1 -0
  28. package/dist/src/commands/list.js +33 -0
  29. package/dist/src/commands/service.d.ts +5 -0
  30. package/dist/src/commands/service.d.ts.map +1 -0
  31. package/dist/src/commands/service.js +141 -0
  32. package/dist/src/commands/switchboard.d.ts +34 -0
  33. package/dist/src/commands/switchboard.d.ts.map +1 -0
  34. package/dist/src/commands/switchboard.js +82 -0
  35. package/dist/src/commands/uninstall.d.ts +15 -0
  36. package/dist/src/commands/uninstall.d.ts.map +1 -0
  37. package/dist/src/commands/uninstall.js +80 -0
  38. package/dist/src/index.d.ts +3 -0
  39. package/dist/src/index.d.ts.map +1 -0
  40. package/dist/src/index.js +2 -0
  41. package/dist/src/types.d.ts +2 -0
  42. package/dist/src/types.d.ts.map +1 -0
  43. package/dist/src/types.js +1 -0
  44. package/dist/src/utils.d.ts +47 -0
  45. package/dist/src/utils.d.ts.map +1 -0
  46. package/dist/src/utils.js +114 -0
  47. package/dist/tsconfig.tsbuildinfo +1 -0
  48. package/package.json +15 -15
  49. package/dist/chunk-3LA6D2BV.js +0 -109
  50. package/dist/chunk-4CHMELTF.js +0 -51
  51. package/dist/chunk-6VK3FH3G.js +0 -36
  52. package/dist/chunk-7FTFL36R.js +0 -52
  53. package/dist/chunk-ACKSBZXF.js +0 -85
  54. package/dist/chunk-D42NLWCH.js +0 -98
  55. package/dist/chunk-FEGQ7JEJ.js +0 -97
  56. package/dist/chunk-FFVIBNX6.js +0 -147
  57. package/dist/chunk-IFV5NKI4.js +0 -57
  58. package/dist/chunk-KPMNRIJU.js +0 -10
  59. package/dist/chunk-RRSG7FV6.js +0 -52
  60. package/dist/chunk-V725N3UP.js +0 -132
  61. package/dist/chunk-WUBXHLKE.js +0 -51
  62. package/dist/chunk-ZZ3KNXW6.js +0 -91
  63. package/dist/cli.js +0 -22
  64. package/dist/commands/connect.js +0 -8
  65. package/dist/commands/dev.js +0 -9
  66. package/dist/commands/generate.js +0 -8
  67. package/dist/commands/help.js +0 -6
  68. package/dist/commands/index.js +0 -68
  69. package/dist/commands/inspect.js +0 -9
  70. package/dist/commands/install.js +0 -11
  71. package/dist/commands/list.js +0 -9
  72. package/dist/commands/service.js +0 -8
  73. package/dist/commands/switchboard.js +0 -10
  74. package/dist/commands/uninstall.js +0 -11
  75. package/dist/index.js +0 -91
  76. package/dist/scripts/service-startup.sh +0 -7
  77. package/dist/scripts/service-unstartup.sh +0 -6
  78. package/dist/scripts/setup.sh +0 -24
  79. package/dist/types.js +0 -0
  80. package/dist/utils.js +0 -28
@@ -0,0 +1,141 @@
1
+ import { getConfig } from "@powerhousedao/config/powerhouse";
2
+ import { Argument } from "commander";
3
+ import { execSync } from "node:child_process";
4
+ import path from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import pm2 from "pm2";
7
+ const actions = ["start", "stop", "status", "list", "startup", "unstartup"];
8
+ const services = ["switchboard", "connect", "all"];
9
+ let switchboardPort = 8442;
10
+ let connectPort = 8443;
11
+ export const manageService = async (action, service) => {
12
+ try {
13
+ const config = getConfig();
14
+ if (config.reactor?.port) {
15
+ switchboardPort = config.reactor.port;
16
+ }
17
+ if (config.studio?.port) {
18
+ connectPort = config.studio.port;
19
+ }
20
+ pm2.connect((err) => {
21
+ switch (action) {
22
+ case "start":
23
+ startServices(service);
24
+ break;
25
+ case "stop":
26
+ stopServices(service);
27
+ break;
28
+ case "startup":
29
+ startupServices();
30
+ break;
31
+ case "unstartup":
32
+ unstartupServices();
33
+ break;
34
+ default:
35
+ statusServices();
36
+ break;
37
+ }
38
+ });
39
+ }
40
+ catch (error) {
41
+ console.error(error);
42
+ }
43
+ };
44
+ export function serviceCommand(program) {
45
+ program
46
+ .command("service")
47
+ .description("Manage services")
48
+ .addArgument(new Argument("action").choices(actions).default("list"))
49
+ .addArgument(new Argument("service").choices(services).argOptional().default("all"))
50
+ .action(manageService);
51
+ }
52
+ function startServices(service) {
53
+ if (service === "switchboard" || service === "all") {
54
+ const switchboardOptions = {
55
+ name: "switchboard",
56
+ script: "npx ph-cli switchboard",
57
+ };
58
+ console.log("Starting Switchboard...");
59
+ pm2.start(switchboardOptions, (err) => {
60
+ if (err) {
61
+ console.log(err.name);
62
+ // throw new Error(err.message);
63
+ }
64
+ dumpServices();
65
+ });
66
+ console.log("Switchboard started");
67
+ }
68
+ if (service === "connect" || service === "all") {
69
+ const connectOptions = {
70
+ name: "connect",
71
+ script: "npx ph-cli connect",
72
+ args: ["--port", connectPort.toString()],
73
+ };
74
+ console.log("Starting connect...");
75
+ pm2.start(connectOptions, (err) => {
76
+ if (err) {
77
+ throw new Error(err.message);
78
+ }
79
+ dumpServices();
80
+ });
81
+ console.log("Connect started");
82
+ }
83
+ }
84
+ function dumpServices() {
85
+ pm2.dump((err) => {
86
+ if (err) {
87
+ throw new Error(err.message);
88
+ }
89
+ statusServices();
90
+ });
91
+ }
92
+ function stopServices(service) {
93
+ if (service === "all" || service === "connect") {
94
+ pm2.stop("connect", (err) => {
95
+ if (err) {
96
+ throw new Error(err.message);
97
+ }
98
+ dumpServices();
99
+ });
100
+ }
101
+ if (service === "all" || service === "switchboard") {
102
+ pm2.stop("switchboard", (err) => {
103
+ if (err) {
104
+ throw new Error(err.message);
105
+ }
106
+ dumpServices();
107
+ });
108
+ }
109
+ }
110
+ function statusServices() {
111
+ pm2.list((err, list) => {
112
+ const formattedList = list.map((item) => {
113
+ return {
114
+ id: item.pm_id,
115
+ name: item.name,
116
+ pid: item.pid,
117
+ uptime: item.pm2_env?.pm_uptime,
118
+ restarts: item.pm2_env?.unstable_restarts,
119
+ status: item.pm2_env?.status,
120
+ mem: item.monit?.memory,
121
+ cpu: item.monit?.cpu,
122
+ };
123
+ });
124
+ console.table(formattedList);
125
+ process.exit(0);
126
+ });
127
+ }
128
+ function startupServices() {
129
+ const dirname = import.meta.dirname || path.dirname(fileURLToPath(import.meta.url));
130
+ const scriptPath = path.join(dirname, "..", "scripts", "service-startup.sh");
131
+ const result = execSync(`bash ${scriptPath}`).toString();
132
+ console.log(result);
133
+ process.exit(0);
134
+ }
135
+ function unstartupServices() {
136
+ const dirname = import.meta.dirname || path.dirname(fileURLToPath(import.meta.url));
137
+ const scriptPath = path.join(dirname, "..", "scripts", "service-unstartup.sh");
138
+ const result = execSync(`bash ${scriptPath}`).toString();
139
+ console.log(result);
140
+ process.exit(0);
141
+ }
@@ -0,0 +1,34 @@
1
+ import { LocalReactor, StartServerOptions } from "@powerhousedao/reactor-local";
2
+ import { Command } from "commander";
3
+ import { CommandActionType } from "../types.js";
4
+ export type SwitchboardOptions = StartServerOptions & {
5
+ configFile?: string;
6
+ generate?: boolean;
7
+ watch?: boolean;
8
+ dbPath?: string;
9
+ };
10
+ export declare const DefaultSwitchboardOptions: {
11
+ dev: boolean;
12
+ port: number;
13
+ storagePath: string;
14
+ dbPath: string;
15
+ drive: {
16
+ global: {
17
+ id: string;
18
+ name: string;
19
+ icon: string;
20
+ slug: string;
21
+ };
22
+ local: {
23
+ availableOffline: true;
24
+ listeners: never[];
25
+ sharingType: string;
26
+ triggers: never[];
27
+ };
28
+ };
29
+ };
30
+ export declare const switchboard: CommandActionType<[
31
+ SwitchboardOptions
32
+ ], Promise<LocalReactor>>;
33
+ export declare function reactorCommand(program: Command): void;
34
+ //# sourceMappingURL=switchboard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"switchboard.d.ts","sourceRoot":"","sources":["../../../src/commands/switchboard.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,YAAY,EAEZ,kBAAkB,EACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,MAAM,MAAM,kBAAkB,GAAG,kBAAkB,GAAG;IACpD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,yBAAyB;;;;;;;;;;;;;;;;;;;CAGrC,CAAC;AA0DF,eAAO,MAAM,WAAW,EAAE,iBAAiB,CACzC;IAAC,kBAAkB;CAAC,EACpB,OAAO,CAAC,YAAY,CAAC,CAGtB,CAAC;AAEF,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,QA0B9C"}
@@ -0,0 +1,82 @@
1
+ import { generateFromFile } from "@powerhousedao/codegen";
2
+ import { getConfig } from "@powerhousedao/config/powerhouse";
3
+ import { DefaultStartServerOptions, startServer, } from "@powerhousedao/reactor-local";
4
+ export const DefaultSwitchboardOptions = {
5
+ ...DefaultStartServerOptions,
6
+ dev: true,
7
+ };
8
+ async function startLocalSwitchboard(switchboardOptions) {
9
+ const baseConfig = getConfig(switchboardOptions.configFile);
10
+ const options = {
11
+ ...DefaultSwitchboardOptions,
12
+ ...switchboardOptions,
13
+ };
14
+ const { https } = baseConfig.reactor ?? { https: false };
15
+ const reactor = await startServer({
16
+ ...options,
17
+ https,
18
+ logLevel: baseConfig.logLevel,
19
+ });
20
+ if (options.generate) {
21
+ await addGenerateTransmitter(reactor, baseConfig);
22
+ }
23
+ return reactor;
24
+ }
25
+ async function addGenerateTransmitter(reactor, config) {
26
+ return reactor.addListener("powerhouse", {
27
+ onStrands: async function (strands) {
28
+ const documentPaths = new Set();
29
+ for (const strand of strands) {
30
+ documentPaths.add(reactor.getDocumentPath(strand.driveId, strand.documentId));
31
+ }
32
+ for (const path of documentPaths) {
33
+ await generateFromFile(path, config);
34
+ }
35
+ return Promise.resolve();
36
+ },
37
+ onDisconnect: () => Promise.resolve(),
38
+ }, {
39
+ filter: {
40
+ documentType: ["powerhouse/document-model"],
41
+ scope: ["global"],
42
+ branch: ["*"],
43
+ documentId: ["*"],
44
+ },
45
+ block: false,
46
+ listenerId: "reactor-local-document-model-generator",
47
+ label: "reactor-local-document-model-generator",
48
+ });
49
+ }
50
+ export const switchboard = (options) => {
51
+ return startLocalSwitchboard(options);
52
+ };
53
+ export function reactorCommand(program) {
54
+ program
55
+ .command("switchboard")
56
+ .alias("reactor")
57
+ .description("Starts local switchboard")
58
+ .option("--port <PORT>", "port to host the api", "4001")
59
+ .option("--config-file <configFile>", "Path to the powerhouse.config.js file", "./powerhouse.config.json")
60
+ .option("--generate", "generate code when document model is updated")
61
+ .option("--db-path <DB_PATH>", "path to the database")
62
+ .option("--https-key-file <HTTPS_KEY_FILE>", "path to the ssl key file")
63
+ .option("--https-cert-file <HTTPS_CERT_FILE>", "path to the ssl cert file")
64
+ .option("-w, --watch", "if the reactor should watch for local changes to document models and processors")
65
+ .option("--packages <packages...>", "list of packages to be loaded, if defined then packages on config file are ignored")
66
+ .action(async (...args) => {
67
+ await switchboard(...args);
68
+ });
69
+ }
70
+ if (process.argv.at(2) === "spawn") {
71
+ const optionsArg = process.argv.at(3);
72
+ const options = optionsArg
73
+ ? JSON.parse(optionsArg)
74
+ : {};
75
+ startLocalSwitchboard(options)
76
+ .then((switchboard) => {
77
+ process.send?.(`driveUrl:${switchboard.driveUrl}`);
78
+ })
79
+ .catch((e) => {
80
+ throw e;
81
+ });
82
+ }
@@ -0,0 +1,15 @@
1
+ import { Command } from "commander";
2
+ import { CommandActionType } from "../types.js";
3
+ import { PackageManager } from "../utils.js";
4
+ export declare function uninstallDependency(packageManager: PackageManager, dependencies: string[], projectPath: string, workspace?: boolean): void;
5
+ export declare const uninstall: CommandActionType<[
6
+ string[] | undefined,
7
+ {
8
+ debug?: boolean;
9
+ global?: boolean;
10
+ workspace?: boolean;
11
+ packageManager?: string;
12
+ }
13
+ ]>;
14
+ export declare function uninstallCommand(program: Command): void;
15
+ //# sourceMappingURL=uninstall.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uninstall.d.ts","sourceRoot":"","sources":["../../../src/commands/uninstall.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAGL,cAAc,EAIf,MAAM,aAAa,CAAC;AAErB,wBAAgB,mBAAmB,CACjC,cAAc,EAAE,cAAc,EAC9B,YAAY,EAAE,MAAM,EAAE,EACtB,WAAW,EAAE,MAAM,EACnB,SAAS,CAAC,EAAE,OAAO,QAuBpB;AAED,eAAO,MAAM,SAAS,EAAE,iBAAiB,CACvC;IACE,MAAM,EAAE,GAAG,SAAS;IACpB;QACE,KAAK,CAAC,EAAE,OAAO,CAAC;QAChB,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,SAAS,CAAC,EAAE,OAAO,CAAC;QACpB,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB;CACF,CAmEF,CAAC;AAEF,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,QAiBhD"}
@@ -0,0 +1,80 @@
1
+ import { execSync } from "node:child_process";
2
+ import fs from "node:fs";
3
+ import { getPackageManagerFromLockfile, getProjectInfo, packageManagers, SUPPORTED_PACKAGE_MANAGERS, updateConfigFile, } from "../utils.js";
4
+ export function uninstallDependency(packageManager, dependencies, projectPath, workspace) {
5
+ if (!fs.existsSync(projectPath)) {
6
+ throw new Error(`Project path not found: ${projectPath}`);
7
+ }
8
+ const manager = packageManagers[packageManager];
9
+ let uninstallCommand = manager.uninstallCommand.replace("{{dependency}}", dependencies.join(" "));
10
+ if (workspace) {
11
+ uninstallCommand += ` ${manager.workspaceOption}`;
12
+ }
13
+ const commandOptions = { cwd: projectPath };
14
+ execSync(uninstallCommand, {
15
+ stdio: "inherit",
16
+ ...commandOptions,
17
+ });
18
+ }
19
+ export const uninstall = (dependencies, options) => {
20
+ if (options.debug) {
21
+ console.log(">>> command arguments", { dependencies, options });
22
+ }
23
+ if (!dependencies || dependencies.length === 0) {
24
+ throw new Error("❌ Dependency name is required");
25
+ }
26
+ if (options.packageManager &&
27
+ !SUPPORTED_PACKAGE_MANAGERS.includes(options.packageManager)) {
28
+ throw new Error("❌ Unsupported package manager. Supported package managers: npm, yarn, pnpm, bun");
29
+ }
30
+ const projectInfo = getProjectInfo(options.debug);
31
+ if (options.debug) {
32
+ console.log("\n>>> projectInfo", projectInfo);
33
+ }
34
+ const isGlobal = options.global || projectInfo.isGlobal;
35
+ const packageManager = options.packageManager || getPackageManagerFromLockfile(projectInfo.path);
36
+ if (options.debug) {
37
+ console.log("\n>>> uninstallDependency arguments:");
38
+ console.log(">>> packageManager", packageManager);
39
+ console.log(">>> dependencies", dependencies);
40
+ console.log(">>> isGlobal", isGlobal);
41
+ console.log(">>> projectPath", projectInfo.path);
42
+ console.log(">>> workspace", options.workspace);
43
+ }
44
+ try {
45
+ console.log("Uninstalling dependencies 📦 ...");
46
+ uninstallDependency(packageManager, dependencies, projectInfo.path, options.workspace);
47
+ console.log("Dependency uninstalled successfully 🎉");
48
+ }
49
+ catch (error) {
50
+ console.error("❌ Failed to uninstall dependencies");
51
+ throw error;
52
+ }
53
+ if (options.debug) {
54
+ console.log("\n>>> updateConfigFile arguments:");
55
+ console.log(">>> dependencies", dependencies);
56
+ console.log(">>> projectPath", projectInfo.path);
57
+ console.log(">>> task", "uninstall");
58
+ }
59
+ try {
60
+ console.log("⚙️ Updating powerhouse config file...");
61
+ updateConfigFile(dependencies, projectInfo.path, "uninstall");
62
+ console.log("Config file updated successfully 🎉");
63
+ }
64
+ catch (error) {
65
+ console.error("❌ Failed to update config file");
66
+ throw error;
67
+ }
68
+ };
69
+ export function uninstallCommand(program) {
70
+ program
71
+ .command("uninstall")
72
+ .alias("remove")
73
+ .description("Uninstall a powerhouse dependency")
74
+ .argument("[dependencies...]", "Names of the dependencies to remove")
75
+ .option("-g, --global", "Remove the dependency globally")
76
+ .option("--debug", "Show additional logs")
77
+ .option("-w, --workspace", "Remove the dependency in the workspace (use this option for monorepos)")
78
+ .option("--package-manager <packageManager>", "force package manager to use")
79
+ .action(uninstall);
80
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./commands/index.js";
2
+ export * from "./utils.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./commands/index.js";
2
+ export * from "./utils.js";
@@ -0,0 +1,2 @@
1
+ export type CommandActionType<Args extends any[], Return = void> = (...args: Args) => Return | Promise<Return>;
2
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,CAAC,IAAI,SAAS,GAAG,EAAE,EAAE,MAAM,GAAG,IAAI,IAAI,CACjE,GAAG,IAAI,EAAE,IAAI,KACV,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,47 @@
1
+ export declare const POWERHOUSE_CONFIG_FILE = "powerhouse.config.json";
2
+ export declare const POWERHOUSE_GLOBAL_DIR: string;
3
+ export declare const SUPPORTED_PACKAGE_MANAGERS: string[];
4
+ export declare const packageManagers: {
5
+ bun: {
6
+ globalPathRegexp: RegExp;
7
+ installCommand: string;
8
+ uninstallCommand: string;
9
+ workspaceOption: string;
10
+ lockfile: string;
11
+ };
12
+ pnpm: {
13
+ globalPathRegexp: RegExp;
14
+ installCommand: string;
15
+ uninstallCommand: string;
16
+ workspaceOption: string;
17
+ lockfile: string;
18
+ };
19
+ yarn: {
20
+ globalPathRegexp: RegExp;
21
+ installCommand: string;
22
+ uninstallCommand: string;
23
+ workspaceOption: string;
24
+ lockfile: string;
25
+ };
26
+ npm: {
27
+ installCommand: string;
28
+ uninstallCommand: string;
29
+ workspaceOption: string;
30
+ lockfile: string;
31
+ };
32
+ };
33
+ type PathValidation = (dir: string) => boolean;
34
+ export type PackageManager = "npm" | "yarn" | "pnpm" | "bun";
35
+ export type ProjectInfo = {
36
+ isGlobal: boolean;
37
+ path: string;
38
+ };
39
+ export declare function defaultPathValidation(): boolean;
40
+ export declare function isPowerhouseProject(dir: string): boolean;
41
+ export declare function findNodeProjectRoot(dir: string, pathValidation?: PathValidation): string | null;
42
+ export declare function getProjectInfo(debug?: boolean): ProjectInfo;
43
+ export declare function getPackageManagerFromLockfile(dir: string): PackageManager;
44
+ export declare function getPackageManagerFromPath(dir: string): PackageManager;
45
+ export declare function updateConfigFile(dependencies: string[], projectPath: string, task?: "install" | "uninstall"): void;
46
+ export { getConfig } from "@powerhousedao/config/powerhouse";
47
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,sBAAsB,2BAA2B,CAAC;AAC/D,eAAO,MAAM,qBAAqB,QAA8B,CAAC;AACjE,eAAO,MAAM,0BAA0B,UAAiC,CAAC;AAEzE,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4B3B,CAAC;AAEF,KAAK,cAAc,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;AAE/C,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;AAE7D,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,wBAAgB,qBAAqB,YAEpC;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,WAI9C;AAED,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,MAAM,EACX,cAAc,GAAE,cAAsC,iBAevD;AAED,wBAAgB,cAAc,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,WAAW,CAoB3D;AAED,wBAAgB,6BAA6B,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CAUzE;AAED,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CAYrE;AAED,wBAAgB,gBAAgB,CAC9B,YAAY,EAAE,MAAM,EAAE,EACtB,WAAW,EAAE,MAAM,EACnB,IAAI,GAAE,SAAS,GAAG,WAAuB,QA+B1C;AAED,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC"}
@@ -0,0 +1,114 @@
1
+ import fs from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import path, { dirname } from "node:path";
4
+ export const POWERHOUSE_CONFIG_FILE = "powerhouse.config.json";
5
+ export const POWERHOUSE_GLOBAL_DIR = path.join(homedir(), ".ph");
6
+ export const SUPPORTED_PACKAGE_MANAGERS = ["npm", "yarn", "pnpm", "bun"];
7
+ export const packageManagers = {
8
+ bun: {
9
+ globalPathRegexp: /[\\/].bun[\\/]/,
10
+ installCommand: "bun add {{dependency}}",
11
+ uninstallCommand: "bun remove {{dependency}}",
12
+ workspaceOption: "",
13
+ lockfile: "bun.lock",
14
+ },
15
+ pnpm: {
16
+ globalPathRegexp: /[\\/]pnpm[\\/]/,
17
+ installCommand: "pnpm add {{dependency}}",
18
+ uninstallCommand: "pnpm remove {{dependency}}",
19
+ workspaceOption: "--workspace-root",
20
+ lockfile: "pnpm-lock.yaml",
21
+ },
22
+ yarn: {
23
+ globalPathRegexp: /[\\/]yarn[\\/]/,
24
+ installCommand: "yarn add {{dependency}}",
25
+ uninstallCommand: "yarn remove {{dependency}}",
26
+ workspaceOption: "-W",
27
+ lockfile: "yarn.lock",
28
+ },
29
+ npm: {
30
+ installCommand: "npm install {{dependency}}",
31
+ uninstallCommand: "npm uninstall {{dependency}}",
32
+ workspaceOption: "",
33
+ lockfile: "package-lock.json",
34
+ },
35
+ };
36
+ export function defaultPathValidation() {
37
+ return true;
38
+ }
39
+ export function isPowerhouseProject(dir) {
40
+ const powerhouseConfigPath = path.join(dir, POWERHOUSE_CONFIG_FILE);
41
+ return fs.existsSync(powerhouseConfigPath);
42
+ }
43
+ export function findNodeProjectRoot(dir, pathValidation = defaultPathValidation) {
44
+ const packageJsonPath = path.join(dir, "package.json");
45
+ if (fs.existsSync(packageJsonPath) && pathValidation(dir)) {
46
+ return dir;
47
+ }
48
+ const parentDir = dirname(dir);
49
+ if (parentDir === dir) {
50
+ return null;
51
+ }
52
+ return findNodeProjectRoot(parentDir, pathValidation);
53
+ }
54
+ export function getProjectInfo(debug) {
55
+ const currentPath = process.cwd();
56
+ if (debug) {
57
+ console.log(">>> currentPath", currentPath);
58
+ }
59
+ const projectPath = findNodeProjectRoot(currentPath, isPowerhouseProject);
60
+ if (!projectPath) {
61
+ return {
62
+ isGlobal: true,
63
+ path: POWERHOUSE_GLOBAL_DIR,
64
+ };
65
+ }
66
+ return {
67
+ isGlobal: false,
68
+ path: projectPath,
69
+ };
70
+ }
71
+ export function getPackageManagerFromLockfile(dir) {
72
+ if (fs.existsSync(path.join(dir, packageManagers.pnpm.lockfile))) {
73
+ return "pnpm";
74
+ }
75
+ else if (fs.existsSync(path.join(dir, packageManagers.yarn.lockfile))) {
76
+ return "yarn";
77
+ }
78
+ else if (fs.existsSync(path.join(dir, packageManagers.bun.lockfile))) {
79
+ return "bun";
80
+ }
81
+ return "npm";
82
+ }
83
+ export function getPackageManagerFromPath(dir) {
84
+ const lowerCasePath = dir.toLowerCase();
85
+ if (packageManagers.bun.globalPathRegexp.test(lowerCasePath)) {
86
+ return "bun";
87
+ }
88
+ else if (packageManagers.pnpm.globalPathRegexp.test(lowerCasePath)) {
89
+ return "pnpm";
90
+ }
91
+ else if (packageManagers.yarn.globalPathRegexp.test(lowerCasePath)) {
92
+ return "yarn";
93
+ }
94
+ return "npm";
95
+ }
96
+ export function updateConfigFile(dependencies, projectPath, task = "install") {
97
+ const configPath = path.join(projectPath, POWERHOUSE_CONFIG_FILE);
98
+ const isInstall = task === "install";
99
+ if (!fs.existsSync(configPath)) {
100
+ throw new Error(`powerhouse.config.json file not found. projectPath: ${projectPath}`);
101
+ }
102
+ const config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
103
+ const mappedPackages = dependencies.map((dep) => ({
104
+ packageName: dep,
105
+ }));
106
+ const updatedConfig = {
107
+ ...config,
108
+ packages: isInstall
109
+ ? [...(config.packages || []), ...mappedPackages]
110
+ : [...(config.packages || [])].filter((pkg) => !dependencies.includes(pkg.packageName)),
111
+ };
112
+ fs.writeFileSync(configPath, JSON.stringify(updatedConfig, null, 2));
113
+ }
114
+ export { getConfig } from "@powerhousedao/config/powerhouse";