@treelocator/init 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Krzysztof Wende
4
+ Copyright (c) 2023 Michael Musil (original LocatorJS)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/index.js ADDED
@@ -0,0 +1,269 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/index.ts
4
+ import fs from "fs";
5
+ import path from "path";
6
+ import { execSync } from "child_process";
7
+ import prompts from "prompts";
8
+ import pc from "picocolors";
9
+ function detectPackageManager() {
10
+ let dir = process.cwd();
11
+ const root = path.parse(dir).root;
12
+ while (dir !== root) {
13
+ if (fs.existsSync(path.join(dir, "bun.lockb"))) return "bun";
14
+ if (fs.existsSync(path.join(dir, "pnpm-lock.yaml"))) return "pnpm";
15
+ if (fs.existsSync(path.join(dir, "yarn.lock"))) return "yarn";
16
+ if (fs.existsSync(path.join(dir, "package-lock.json"))) return "npm";
17
+ dir = path.dirname(dir);
18
+ }
19
+ return "npm";
20
+ }
21
+ function detectProject() {
22
+ const pkgPath = path.join(process.cwd(), "package.json");
23
+ if (!fs.existsSync(pkgPath)) {
24
+ console.error(pc.red("No package.json found. Run this from your project root."));
25
+ process.exit(1);
26
+ }
27
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
28
+ const deps = { ...pkg.dependencies, ...pkg.devDependencies };
29
+ const info = {
30
+ packageManager: detectPackageManager(),
31
+ buildTool: "unknown",
32
+ framework: "unknown",
33
+ hasTypeScript: !!deps.typescript || fs.existsSync("tsconfig.json"),
34
+ configFile: null,
35
+ entryFile: null
36
+ };
37
+ if (deps.next) {
38
+ info.buildTool = "next";
39
+ info.configFile = fs.existsSync("next.config.ts") ? "next.config.ts" : fs.existsSync("next.config.mjs") ? "next.config.mjs" : "next.config.js";
40
+ } else if (deps.vite) {
41
+ info.buildTool = "vite";
42
+ info.configFile = fs.existsSync("vite.config.ts") ? "vite.config.ts" : "vite.config.js";
43
+ }
44
+ if (deps.react || deps["react-dom"]) {
45
+ info.framework = "react";
46
+ } else if (deps.vue) {
47
+ info.framework = "vue";
48
+ } else if (deps.svelte) {
49
+ info.framework = "svelte";
50
+ } else if (deps.preact) {
51
+ info.framework = "preact";
52
+ } else if (deps["solid-js"]) {
53
+ info.framework = "solid";
54
+ }
55
+ const entryPaths = [
56
+ "src/main.tsx",
57
+ "src/main.ts",
58
+ "src/main.jsx",
59
+ "src/main.js",
60
+ "src/index.tsx",
61
+ "src/index.ts",
62
+ "src/index.jsx",
63
+ "src/index.js",
64
+ "app/layout.tsx",
65
+ // Next.js app router
66
+ "app/layout.js",
67
+ "pages/_app.tsx",
68
+ // Next.js pages router
69
+ "pages/_app.js"
70
+ ];
71
+ for (const entry of entryPaths) {
72
+ if (fs.existsSync(entry)) {
73
+ info.entryFile = entry;
74
+ break;
75
+ }
76
+ }
77
+ return info;
78
+ }
79
+ function getInstallCommand(pm, packages) {
80
+ const pkgs = packages.join(" ");
81
+ switch (pm) {
82
+ case "bun":
83
+ return `bun add -D ${pkgs}`;
84
+ case "pnpm":
85
+ return `pnpm add -D ${pkgs}`;
86
+ case "yarn":
87
+ return `yarn add -D ${pkgs}`;
88
+ default:
89
+ return `npm install -D ${pkgs}`;
90
+ }
91
+ }
92
+ function updateViteConfig(configFile, framework) {
93
+ let content = fs.readFileSync(configFile, "utf-8");
94
+ if (content.includes("@locator/babel-jsx")) {
95
+ console.log(pc.yellow("TreeLocatorJS babel plugin already configured in vite.config"));
96
+ return;
97
+ }
98
+ if (framework === "react") {
99
+ const reactPluginRegex = /react\(\s*\)/;
100
+ const reactPluginWithOptionsRegex = /react\(\s*\{/;
101
+ if (reactPluginRegex.test(content)) {
102
+ content = content.replace(
103
+ reactPluginRegex,
104
+ `react({
105
+ babel: {
106
+ plugins: [
107
+ ["@locator/babel-jsx/dist", { env: "development" }],
108
+ ],
109
+ },
110
+ })`
111
+ );
112
+ } else if (reactPluginWithOptionsRegex.test(content)) {
113
+ content = content.replace(
114
+ /react\(\s*\{/,
115
+ `react({
116
+ babel: {
117
+ plugins: [
118
+ ["@locator/babel-jsx/dist", { env: "development" }],
119
+ ],
120
+ },`
121
+ );
122
+ }
123
+ }
124
+ fs.writeFileSync(configFile, content);
125
+ console.log(pc.green(`Updated ${configFile}`));
126
+ }
127
+ function updateNextConfig(configFile) {
128
+ let content = fs.readFileSync(configFile, "utf-8");
129
+ if (content.includes("@locator/webpack-loader")) {
130
+ console.log(pc.yellow("TreeLocatorJS webpack loader already configured in next.config"));
131
+ return;
132
+ }
133
+ const webpackConfig = `
134
+ webpack: (config) => {
135
+ config.module.rules.push({
136
+ test: /\\.tsx?$/,
137
+ use: [
138
+ { loader: "@locator/webpack-loader", options: { env: "development" } },
139
+ ],
140
+ });
141
+ return config;
142
+ },`;
143
+ if (content.includes("const nextConfig = {")) {
144
+ content = content.replace(
145
+ "const nextConfig = {",
146
+ `const nextConfig = {${webpackConfig}`
147
+ );
148
+ } else if (content.includes("module.exports = {")) {
149
+ content = content.replace(
150
+ "module.exports = {",
151
+ `module.exports = {${webpackConfig}`
152
+ );
153
+ } else if (content.includes("export default {")) {
154
+ content = content.replace(
155
+ "export default {",
156
+ `export default {${webpackConfig}`
157
+ );
158
+ }
159
+ fs.writeFileSync(configFile, content);
160
+ console.log(pc.green(`Updated ${configFile}`));
161
+ }
162
+ function addRuntimeImport(entryFile, isNextApp) {
163
+ let content = fs.readFileSync(entryFile, "utf-8");
164
+ if (content.includes("@treelocator/runtime")) {
165
+ console.log(pc.yellow("TreeLocatorJS runtime already imported"));
166
+ return;
167
+ }
168
+ const importLine = `import setupLocatorUI from "@treelocator/runtime";
169
+ `;
170
+ const setupCall = isNextApp ? "" : `
171
+ if (typeof window !== "undefined") {
172
+ setupLocatorUI();
173
+ }
174
+ `;
175
+ if (isNextApp) {
176
+ console.log(pc.yellow("\nFor Next.js App Router, create a client component:"));
177
+ console.log(pc.cyan(`
178
+ // app/LocatorProvider.tsx
179
+ "use client";
180
+ import { useEffect } from "react";
181
+ import setupLocatorUI from "@treelocator/runtime";
182
+
183
+ export function LocatorProvider({ children }: { children: React.ReactNode }) {
184
+ useEffect(() => {
185
+ if (process.env.NODE_ENV === "development") {
186
+ setupLocatorUI();
187
+ }
188
+ }, []);
189
+ return <>{children}</>;
190
+ }
191
+
192
+ // Then wrap your app in app/layout.tsx:
193
+ // <LocatorProvider>{children}</LocatorProvider>
194
+ `));
195
+ return;
196
+ }
197
+ content = importLine + content;
198
+ const lastImportIndex = content.lastIndexOf("import ");
199
+ if (lastImportIndex !== -1) {
200
+ const lineEnd = content.indexOf("\n", lastImportIndex);
201
+ content = content.slice(0, lineEnd + 1) + setupCall + content.slice(lineEnd + 1);
202
+ } else {
203
+ content = content + setupCall;
204
+ }
205
+ fs.writeFileSync(entryFile, content);
206
+ console.log(pc.green(`Updated ${entryFile}`));
207
+ }
208
+ async function main() {
209
+ console.log(pc.bold(pc.cyan("\n TreeLocatorJS Setup Wizard\n")));
210
+ const info = detectProject();
211
+ console.log(pc.dim("Detected:"));
212
+ console.log(pc.dim(` Package manager: ${info.packageManager}`));
213
+ console.log(pc.dim(` Build tool: ${info.buildTool}`));
214
+ console.log(pc.dim(` Framework: ${info.framework}`));
215
+ console.log(pc.dim(` TypeScript: ${info.hasTypeScript}`));
216
+ console.log();
217
+ if (info.buildTool === "unknown") {
218
+ console.log(pc.red("Could not detect build tool (Vite or Next.js)."));
219
+ console.log(pc.dim("TreeLocatorJS currently supports Vite and Next.js projects."));
220
+ process.exit(1);
221
+ }
222
+ if (info.framework === "unknown") {
223
+ console.log(pc.red("Could not detect framework."));
224
+ process.exit(1);
225
+ }
226
+ const { confirm } = await prompts({
227
+ type: "confirm",
228
+ name: "confirm",
229
+ message: "Install and configure TreeLocatorJS?",
230
+ initial: true
231
+ });
232
+ if (!confirm) {
233
+ console.log(pc.dim("Cancelled."));
234
+ process.exit(0);
235
+ }
236
+ const packages = ["@treelocator/runtime"];
237
+ if (info.buildTool === "vite") {
238
+ packages.push("@locator/babel-jsx");
239
+ } else if (info.buildTool === "next") {
240
+ packages.push("@locator/webpack-loader");
241
+ }
242
+ console.log(pc.dim(`
243
+ Installing ${packages.join(", ")}...`));
244
+ const installCmd = getInstallCommand(info.packageManager, packages);
245
+ try {
246
+ execSync(installCmd, { stdio: "inherit" });
247
+ } catch {
248
+ console.error(pc.red("Failed to install packages."));
249
+ process.exit(1);
250
+ }
251
+ if (info.configFile) {
252
+ console.log(pc.dim(`
253
+ Updating ${info.configFile}...`));
254
+ if (info.buildTool === "vite") {
255
+ updateViteConfig(info.configFile, info.framework);
256
+ } else if (info.buildTool === "next") {
257
+ updateNextConfig(info.configFile);
258
+ }
259
+ }
260
+ if (info.entryFile) {
261
+ console.log(pc.dim(`
262
+ Updating ${info.entryFile}...`));
263
+ const isNextApp = info.entryFile.startsWith("app/");
264
+ addRuntimeImport(info.entryFile, isNextApp);
265
+ }
266
+ console.log(pc.bold(pc.green("\nTreeLocatorJS installed successfully!")));
267
+ console.log(pc.dim("\nUsage: Hold Alt and click any component to copy its ancestry.\n"));
268
+ }
269
+ main().catch(console.error);
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@treelocator/init",
3
+ "version": "0.1.0",
4
+ "description": "Setup wizard for TreeLocatorJS - auto-configure component ancestry tracking",
5
+ "bin": {
6
+ "treelocatorjs": "./dist/index.js",
7
+ "locatorjs": "./dist/index.js",
8
+ "@treelocator/init": "./dist/index.js"
9
+ },
10
+ "main": "./dist/index.js",
11
+ "type": "module",
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup src/index.ts --format esm --dts",
20
+ "dev": "tsup src/index.ts --format esm --watch"
21
+ },
22
+ "dependencies": {
23
+ "picocolors": "^1.1.1",
24
+ "prompts": "^2.4.2"
25
+ },
26
+ "devDependencies": {
27
+ "@types/node": "^20.0.0",
28
+ "@types/prompts": "^2.4.9",
29
+ "tsup": "^8.0.0",
30
+ "typescript": "^5.0.0"
31
+ },
32
+ "engines": {
33
+ "node": ">=18.0.0"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/wende/treelocatorjs.git",
38
+ "directory": "packages/init"
39
+ },
40
+ "keywords": [
41
+ "treelocatorjs",
42
+ "locatorjs",
43
+ "devtools",
44
+ "react",
45
+ "vue",
46
+ "svelte",
47
+ "cli",
48
+ "component-ancestry"
49
+ ],
50
+ "license": "MIT",
51
+ "gitHead": "e7aff23b5a8461ba254e2f60e7bdb25081d7be50"
52
+ }