@slim-lang/spm 0.0.0-stage → 1.0.1

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.
@@ -0,0 +1,186 @@
1
+ import path from "node:path"
2
+ import { formatItalic, getPackagePath, isPackageExists } from "../helpers.js"
3
+ import { existsSync } from "node:fs"
4
+ import { readFile } from "node:fs/promises"
5
+
6
+ export function validateGitHub(value) {
7
+ const regex = /^[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\/[a-zA-Z0-9._-]+$/;
8
+
9
+ if (regex.test(value)) {
10
+ return {
11
+ success: true,
12
+ value
13
+ };
14
+ }
15
+
16
+ try {
17
+ const url = new URL(value);
18
+
19
+ if (url.hostname === "github.com") {
20
+ const parts = url.pathname.split("/").filter(Boolean);
21
+
22
+ if (parts.length >= 2) {
23
+ const github = `${parts[0]}/${parts[1]}`;
24
+
25
+ return {
26
+ success: false,
27
+ value: github,
28
+ msg: `Use "${github}" instead of "${value}"`
29
+ };
30
+ }
31
+ }
32
+ } catch {}
33
+
34
+ return {
35
+ success: false,
36
+ msg: `Invalid GitHub repository. Expected "user/repo".`
37
+ };
38
+ }
39
+
40
+ export async function getSPM(pkgName) {
41
+ if (isPackageExists(pkgName)) {
42
+ const pkgPath = getPackagePath(pkgName)
43
+ const dotSpmPath = path.join(pkgPath, ".spm")
44
+
45
+ if (existsSync(dotSpmPath)) {
46
+ try {
47
+ const data = await readFile(dotSpmPath, 'utf8');
48
+ return {
49
+ success: true,
50
+ content: parseSPM(data)
51
+ }
52
+ } catch (err) {
53
+ return {
54
+ success: false,
55
+ content: String(err)
56
+ }
57
+ }
58
+ }
59
+ else {
60
+ return {
61
+ success: false,
62
+ content: ".spm file doest not exists inside the package"
63
+ }
64
+ }
65
+ }
66
+ else return {
67
+ success: false,
68
+ content: "Package does not exists"
69
+ }
70
+ }
71
+
72
+ const UNSAFE_KEYS = new Set(["__proto__", "constructor", "prototype"])
73
+
74
+ export function parseSPM(content) {
75
+ const result = {};
76
+ const required = ["name", "version"]
77
+ const allowedRoot = new Set(["name", "version", "description"]);
78
+
79
+ const githubSectionAllowed = new Set(["repo", "organization"]);
80
+
81
+ let currentSection = null;
82
+
83
+ for (const line of content.split(/\r?\n/)) {
84
+ const trimmed = line.trim();
85
+
86
+ if (!trimmed || trimmed.startsWith("#")) {
87
+ continue;
88
+ }
89
+
90
+ if (trimmed.startsWith("@")) {
91
+ const section = trimmed.slice(1).trim();
92
+
93
+ if (!section) {
94
+ throw new Error("empty section name");
95
+ }
96
+
97
+ currentSection = section.split("/").map(part => part.trim());
98
+
99
+ let target = result;
100
+
101
+ for (const part of currentSection) {
102
+ if (!/^[a-zA-Z_][\w-]*$/.test(part) || UNSAFE_KEYS.has(part)) {
103
+ throw new Error(`invalid section name "${section}"`);
104
+ }
105
+ if (!Object.hasOwn(target, part)) {
106
+ target[part] = {};
107
+ }
108
+ else if (typeof target[part] !== "object") {
109
+ throw new Error(`section "${section}" conflicts with property "${part}"`);
110
+ }
111
+
112
+ target = target[part];
113
+ }
114
+
115
+ continue;
116
+ }
117
+
118
+ const match = trimmed.match(/^([a-zA-Z_][\w-]*)\s*=\s*"([^"]*)"$/);
119
+
120
+ if (!match) {
121
+ throw new Error(`invalid line: ${trimmed}`);
122
+ }
123
+
124
+ const [, key, value] = match;
125
+
126
+ if (!currentSection) {
127
+ if (!allowedRoot.has(key)) {
128
+ throw new Error(`unknown property "${key}"`);
129
+ }
130
+
131
+ if (Object.hasOwn(result, key)) {
132
+ throw new Error(`duplicate property "${key}"`);
133
+ }
134
+
135
+ result[key] = value;
136
+ continue;
137
+ }
138
+
139
+ const sectionName = currentSection.join("/");
140
+
141
+ if (sectionName === "github") {
142
+ if (!githubSectionAllowed.has(key)) {
143
+ throw new Error(
144
+ `unknown property "${key}" in @${sectionName} section`
145
+ );
146
+ }
147
+
148
+ if (key == "repo") {
149
+ const repoTest = validateGitHub(value)
150
+ if (!repoTest.success) {
151
+ throw new Error(`@${sectionName}/${key}: ${repoTest.msg}`);
152
+ }
153
+ }
154
+
155
+ if (key === "organization" && value !== "true" && value !== "false") {
156
+ throw new Error(`@${sectionName}/${key} must be "true" or "false"`);
157
+ }
158
+ }
159
+
160
+ let target = result;
161
+
162
+ for (const part of currentSection) {
163
+ target = target[part];
164
+ }
165
+
166
+ if (UNSAFE_KEYS.has(key)) {
167
+ throw new Error(`invalid property name "${key}"`);
168
+ }
169
+
170
+ if (Object.hasOwn(target, key)) {
171
+ throw new Error(`duplicate property "${key}"`);
172
+ }
173
+
174
+ target[key] = (sectionName === "github" && key === "organization")
175
+ ? value === "true"
176
+ : value;
177
+ }
178
+
179
+ for (const key of required) {
180
+ if (!result[key]) {
181
+ throw new Error(`missing required property "${key}"`);
182
+ }
183
+ }
184
+
185
+ return result;
186
+ }