tangled
alpha
login
or
join now
maxine.puppykitty.racing
/
typelex
forked from
danabra.mov/typelex
0
fork
atom
An experimental TypeSpec syntax for Lexicon
0
fork
atom
overview
issues
pulls
pipelines
fix
danabra.mov
5 months ago
17a20b86
c80e1d7f
+50
-47
1 changed file
expand all
collapse all
unified
split
packages
cli
src
commands
init.ts
+50
-47
packages/cli/src/commands/init.ts
···
1
1
import { resolve, relative } from "path";
2
2
-
import { mkdir, writeFile, readFile, access } from "fs/promises";
2
2
+
import { mkdir, writeFile, readFile, access, stat } from "fs/promises";
3
3
import { spawn } from "child_process";
4
4
import { createInterface } from "readline";
5
5
···
30
30
});
31
31
32
32
return new Promise((resolve) => {
33
33
-
rl.question("Namespace (e.g., com.example.*): ", (answer) => {
33
33
+
rl.question("\x1b[36mYour app's namespace (e.g., com.example.*): \x1b[0m", (answer) => {
34
34
rl.close();
35
35
resolve(answer.trim());
36
36
});
···
47
47
const externalsTspPath = resolve(typelexDir, "externals.tsp");
48
48
49
49
console.log("Initializing typelex project...\n");
50
50
-
console.log("Installing dependencies...\n");
50
50
+
console.log("Installing dependencies...");
51
51
52
52
-
// Detect package manager and lexicons directory by walking up directory tree
52
52
+
// Detect package manager: walk up from cwd
53
53
let packageManager = "npm";
54
54
-
let lexiconsDir: string | null = null;
55
55
-
let currentDir = cwd;
56
56
-
const root = resolve("/");
57
57
-
58
58
-
// First, move up one directory (we only want lexicons from parent directories)
59
59
-
const parentDir = resolve(currentDir, "..");
60
60
-
if (parentDir !== currentDir) {
61
61
-
currentDir = parentDir;
62
62
-
63
63
-
while (currentDir !== root) {
64
64
-
// Check for package manager lockfiles
65
65
-
if (packageManager === "npm") {
66
66
-
try {
67
67
-
await access(resolve(currentDir, "pnpm-lock.yaml"));
68
68
-
packageManager = "pnpm";
69
69
-
} catch {
70
70
-
try {
71
71
-
await access(resolve(currentDir, "yarn.lock"));
72
72
-
packageManager = "yarn";
73
73
-
} catch {
74
74
-
// Continue
75
75
-
}
76
76
-
}
77
77
-
}
78
78
-
79
79
-
// Check for lexicons directory (only in parent directories)
80
80
-
if (!lexiconsDir) {
81
81
-
try {
82
82
-
const lexiconsPath = resolve(currentDir, "lexicons");
83
83
-
await access(lexiconsPath);
84
84
-
// Calculate relative path from cwd to this lexicons directory
85
85
-
const relativePath = relative(cwd, lexiconsPath);
86
86
-
lexiconsDir = relativePath;
87
87
-
} catch {
88
88
-
// Continue
89
89
-
}
90
90
-
}
91
91
-
92
92
-
// Move up one directory
93
93
-
const nextParent = resolve(currentDir, "..");
94
94
-
if (nextParent === currentDir) break; // Reached root
95
95
-
currentDir = nextParent;
54
54
+
let dir = cwd;
55
55
+
while (dir !== resolve(dir, "..") && packageManager === "npm") {
56
56
+
try {
57
57
+
await access(resolve(dir, "pnpm-lock.yaml"));
58
58
+
packageManager = "pnpm";
59
59
+
break;
60
60
+
} catch {
61
61
+
// Not found
62
62
+
}
63
63
+
try {
64
64
+
await access(resolve(dir, "yarn.lock"));
65
65
+
packageManager = "yarn";
66
66
+
break;
67
67
+
} catch {
68
68
+
// Not found
96
69
}
70
70
+
dir = resolve(dir, "..");
97
71
}
98
72
99
73
// Install dependencies and only proceed if successful
···
136
110
// Remove the .* suffix for use in template
137
111
const namespacePrefix = namespace.slice(0, -2);
138
112
113
113
+
// Detect lexicons directory: check cwd first, then walk up parents
114
114
+
let lexiconsDir: string | null = null;
115
115
+
let hasLocalLexicons = false;
116
116
+
117
117
+
// Check current directory for lexicons/ (will use default, no --out flag needed)
118
118
+
try {
119
119
+
const localPath = resolve(cwd, "lexicons");
120
120
+
if ((await stat(localPath)).isDirectory()) {
121
121
+
hasLocalLexicons = true;
122
122
+
}
123
123
+
} catch {
124
124
+
// Not found in current directory, check parent directories
125
125
+
let dir = resolve(cwd, "..");
126
126
+
while (dir !== resolve(dir, "..")) {
127
127
+
try {
128
128
+
const lexPath = resolve(dir, "lexicons");
129
129
+
if ((await stat(lexPath)).isDirectory()) {
130
130
+
lexiconsDir = relative(cwd, lexPath);
131
131
+
break;
132
132
+
}
133
133
+
} catch {
134
134
+
// Not found, continue up
135
135
+
}
136
136
+
dir = resolve(dir, "..");
137
137
+
}
138
138
+
}
139
139
+
139
140
// Create typelex directory
140
141
await mkdir(typelexDir, { recursive: true });
141
142
···
173
174
packageJson.scripts["build:typelex"] = `typelex compile ${namespace}${outFlag}`;
174
175
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n", "utf-8");
175
176
console.log("✓ Added build:typelex script to package.json");
176
176
-
if (lexiconsDir) {
177
177
+
if (hasLocalLexicons) {
178
178
+
console.log(` Using existing lexicons directory: ./lexicons`);
179
179
+
} else if (lexiconsDir) {
177
180
console.log(` Using existing lexicons directory: ${lexiconsDir}`);
178
181
}
179
182
} else {