@stim-cli/core 1.0.0-rc.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 +21 -0
- package/README.md +14 -0
- package/dist/index.d.mts +56 -0
- package/dist/index.mjs +219 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Janic Duplessis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# @stim-cli/core
|
|
2
|
+
|
|
3
|
+
The primitives [`stim-cli`](https://www.npmjs.com/package/stim-cli),
|
|
4
|
+
[`@stim-cli/metro`](https://www.npmjs.com/package/@stim-cli/metro) and
|
|
5
|
+
[`@stim-cli/expo-build-cache`](https://www.npmjs.com/package/@stim-cli/expo-build-cache)
|
|
6
|
+
must agree on, implemented once: where the stim-cli config dir and the two
|
|
7
|
+
shared caches live (env override > machine config > default), how a build
|
|
8
|
+
cache key is derived, and how a cache registers itself for `stim gc`.
|
|
9
|
+
Metro cache overrides name a parent directory; the sanitized app name is always
|
|
10
|
+
appended beneath it, just as it is beneath the default root.
|
|
11
|
+
|
|
12
|
+
This is an internal dependency of those packages, not a user-facing API --
|
|
13
|
+
install one of them instead. It is ESM-only and has no dependencies. The
|
|
14
|
+
`module-sync` export lets supported Node versions load it through `require()`.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//#region index.d.ts
|
|
2
|
+
interface DirLockOptions {
|
|
3
|
+
staleMs?: number;
|
|
4
|
+
waitMs?: number;
|
|
5
|
+
pollMs?: number;
|
|
6
|
+
ensureParent?: () => void;
|
|
7
|
+
}
|
|
8
|
+
declare function withDirLock<T>(lockPath: string, fn: () => T, { staleMs, waitMs, pollMs, ensureParent }?: DirLockOptions): T;
|
|
9
|
+
declare function configDir(): string;
|
|
10
|
+
declare function workspaceSlug(projectRoot: string): string;
|
|
11
|
+
declare function workspaceId(projectRoot: string): string;
|
|
12
|
+
declare function workspaceName(projectRoot: string): string;
|
|
13
|
+
declare function workspaceStateDir(projectRoot: string): string;
|
|
14
|
+
declare function workspaceLogDir(projectRoot: string): string;
|
|
15
|
+
declare function cachePathSetting(key: "buildCache" | "metroCache"): string | null;
|
|
16
|
+
declare function cacheNameSegment(name: string | null | undefined): string;
|
|
17
|
+
declare function buildCacheRoot(): string;
|
|
18
|
+
declare function metroCacheRoot(name?: string | null): string;
|
|
19
|
+
declare const STORE_ROOT_TAG = "stimCliStoreRoot";
|
|
20
|
+
declare function tagSharedStore<T extends object>(store: T, root: string): T;
|
|
21
|
+
declare function sharedStoreRoot(store: unknown): string | null;
|
|
22
|
+
interface BuildRunOptions {
|
|
23
|
+
variant?: string;
|
|
24
|
+
configuration?: string;
|
|
25
|
+
buildConfiguration?: string;
|
|
26
|
+
isSimulator?: boolean;
|
|
27
|
+
device?: string | boolean | null;
|
|
28
|
+
}
|
|
29
|
+
declare function buildCacheKey(platform: string, fingerprintHash: string, options?: unknown): string;
|
|
30
|
+
interface RegisterOptions {
|
|
31
|
+
dir: string;
|
|
32
|
+
name: string;
|
|
33
|
+
prune: string;
|
|
34
|
+
note: string;
|
|
35
|
+
entriesDepth?: number;
|
|
36
|
+
layout?: string;
|
|
37
|
+
replaces?: CacheRegistrationMatch[];
|
|
38
|
+
}
|
|
39
|
+
interface CacheRegistrationMatch {
|
|
40
|
+
dir: string;
|
|
41
|
+
name?: string;
|
|
42
|
+
prune?: string;
|
|
43
|
+
entriesDepth?: number;
|
|
44
|
+
layout?: string | null;
|
|
45
|
+
}
|
|
46
|
+
declare const METRO_NAMED_CACHE_LAYOUT = "metro-named-v1";
|
|
47
|
+
interface CacheManifest {
|
|
48
|
+
version: number;
|
|
49
|
+
caches: Array<Record<string, unknown>>;
|
|
50
|
+
}
|
|
51
|
+
declare function cacheManifestLockPath(file: string): string;
|
|
52
|
+
declare function readCacheManifest(file: string): CacheManifest;
|
|
53
|
+
declare function updateCacheManifest(file: string, mutate: (caches: Array<Record<string, unknown>>) => Array<Record<string, unknown>>): CacheManifest;
|
|
54
|
+
declare function registerCache({ dir, name, prune, note, entriesDepth, layout, replaces }: RegisterOptions): void;
|
|
55
|
+
//#endregion
|
|
56
|
+
export { BuildRunOptions, CacheManifest, CacheRegistrationMatch, DirLockOptions, METRO_NAMED_CACHE_LAYOUT, RegisterOptions, STORE_ROOT_TAG, buildCacheKey, buildCacheRoot, cacheManifestLockPath, cacheNameSegment, cachePathSetting, configDir, metroCacheRoot, readCacheManifest, registerCache, sharedStoreRoot, tagSharedStore, updateCacheManifest, withDirLock, workspaceId, workspaceLogDir, workspaceName, workspaceSlug, workspaceStateDir };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
//#region index.ts
|
|
6
|
+
const DEFAULT_LOCK_STALE_MS = 1e4;
|
|
7
|
+
const DEFAULT_LOCK_WAIT_MS = 12e3;
|
|
8
|
+
const DEFAULT_LOCK_POLL_MS = 25;
|
|
9
|
+
const lockDepths = /* @__PURE__ */ new Map();
|
|
10
|
+
function sleepSync(ms) {
|
|
11
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
12
|
+
}
|
|
13
|
+
function acquireDirLock(lockPath, { staleMs, waitMs, pollMs, ensureParent }) {
|
|
14
|
+
ensureParent?.();
|
|
15
|
+
const deadline = Date.now() + waitMs;
|
|
16
|
+
for (;;) {
|
|
17
|
+
try {
|
|
18
|
+
fs.mkdirSync(lockPath);
|
|
19
|
+
return;
|
|
20
|
+
} catch (error) {
|
|
21
|
+
if (error?.code !== "EEXIST") throw error;
|
|
22
|
+
}
|
|
23
|
+
let ageMs = null;
|
|
24
|
+
try {
|
|
25
|
+
ageMs = Date.now() - fs.statSync(lockPath).mtimeMs;
|
|
26
|
+
} catch {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (ageMs > staleMs) {
|
|
30
|
+
try {
|
|
31
|
+
fs.rmSync(lockPath, {
|
|
32
|
+
recursive: true,
|
|
33
|
+
force: true
|
|
34
|
+
});
|
|
35
|
+
} catch {}
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (Date.now() >= deadline) {
|
|
39
|
+
const error = /* @__PURE__ */ new Error(`Timed out waiting for the lock at ${lockPath}. Another stim-cli process is holding it; if none is running, remove that directory.`);
|
|
40
|
+
error.code = "STIM_CLI_LOCK_TIMEOUT";
|
|
41
|
+
error.lockPath = lockPath;
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
sleepSync(pollMs);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function releaseDirLock(lockPath) {
|
|
48
|
+
try {
|
|
49
|
+
fs.rmSync(lockPath, {
|
|
50
|
+
recursive: true,
|
|
51
|
+
force: true
|
|
52
|
+
});
|
|
53
|
+
} catch {}
|
|
54
|
+
}
|
|
55
|
+
function withDirLock(lockPath, fn, { staleMs = DEFAULT_LOCK_STALE_MS, waitMs = DEFAULT_LOCK_WAIT_MS, pollMs = DEFAULT_LOCK_POLL_MS, ensureParent } = {}) {
|
|
56
|
+
const depth = lockDepths.get(lockPath) || 0;
|
|
57
|
+
if (depth > 0) {
|
|
58
|
+
lockDepths.set(lockPath, depth + 1);
|
|
59
|
+
try {
|
|
60
|
+
return fn();
|
|
61
|
+
} finally {
|
|
62
|
+
lockDepths.set(lockPath, (lockDepths.get(lockPath) ?? 1) - 1);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
acquireDirLock(lockPath, {
|
|
66
|
+
staleMs,
|
|
67
|
+
waitMs,
|
|
68
|
+
pollMs,
|
|
69
|
+
ensureParent
|
|
70
|
+
});
|
|
71
|
+
lockDepths.set(lockPath, 1);
|
|
72
|
+
try {
|
|
73
|
+
return fn();
|
|
74
|
+
} finally {
|
|
75
|
+
lockDepths.set(lockPath, 0);
|
|
76
|
+
releaseDirLock(lockPath);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function configDir() {
|
|
80
|
+
return process.env.STIM_CLI_HOME || path.join(os.homedir(), ".stim-cli");
|
|
81
|
+
}
|
|
82
|
+
function workspaceSlug(projectRoot) {
|
|
83
|
+
return path.basename(path.resolve(projectRoot)).normalize("NFKD").replace(/[^A-Za-z0-9]+/g, "-").replace(/^-+|-+$/g, "").toLowerCase().slice(0, 48) || "workspace";
|
|
84
|
+
}
|
|
85
|
+
function workspaceId(projectRoot) {
|
|
86
|
+
return createHash("sha256").update(path.resolve(projectRoot)).digest("hex").slice(0, 16);
|
|
87
|
+
}
|
|
88
|
+
function workspaceName(projectRoot) {
|
|
89
|
+
return `${workspaceSlug(projectRoot)}--${workspaceId(projectRoot)}`;
|
|
90
|
+
}
|
|
91
|
+
function workspaceStateDir(projectRoot) {
|
|
92
|
+
return path.join(configDir(), "workspaces", workspaceName(projectRoot));
|
|
93
|
+
}
|
|
94
|
+
function workspaceLogDir(projectRoot) {
|
|
95
|
+
return path.join(workspaceStateDir(projectRoot), "logs");
|
|
96
|
+
}
|
|
97
|
+
function cachePathSetting(key) {
|
|
98
|
+
try {
|
|
99
|
+
const value = JSON.parse(fs.readFileSync(path.join(configDir(), "config.json"), "utf-8"))?.caches?.[key];
|
|
100
|
+
return typeof value === "string" && value.startsWith("/") ? value : null;
|
|
101
|
+
} catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function cacheNameSegment(name) {
|
|
106
|
+
return String(name).replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^\.+/, "") || "app";
|
|
107
|
+
}
|
|
108
|
+
function buildCacheRoot() {
|
|
109
|
+
return process.env.STIM_CLI_BUILD_CACHE || cachePathSetting("buildCache") || path.join(configDir(), "build-cache");
|
|
110
|
+
}
|
|
111
|
+
function metroCacheRoot(name) {
|
|
112
|
+
const root = process.env.STIM_CLI_METRO_CACHE || cachePathSetting("metroCache") || path.join(configDir(), "metro-cache");
|
|
113
|
+
return name === void 0 || name === null || name === "" ? root : path.join(root, cacheNameSegment(name));
|
|
114
|
+
}
|
|
115
|
+
const STORE_ROOT_TAG = "stimCliStoreRoot";
|
|
116
|
+
function tagSharedStore(store, root) {
|
|
117
|
+
try {
|
|
118
|
+
Object.defineProperty(store, STORE_ROOT_TAG, {
|
|
119
|
+
value: root,
|
|
120
|
+
enumerable: false,
|
|
121
|
+
configurable: true
|
|
122
|
+
});
|
|
123
|
+
} catch {}
|
|
124
|
+
return store;
|
|
125
|
+
}
|
|
126
|
+
function sharedStoreRoot(store) {
|
|
127
|
+
if (store === null || typeof store !== "object") return null;
|
|
128
|
+
const tagged = store[STORE_ROOT_TAG];
|
|
129
|
+
if (typeof tagged === "string") return tagged;
|
|
130
|
+
const legacy = store._root;
|
|
131
|
+
return typeof legacy === "string" ? legacy : null;
|
|
132
|
+
}
|
|
133
|
+
const SIMULATOR_UDID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
134
|
+
const EMULATOR_SERIAL = /^emulator-\d+$/;
|
|
135
|
+
function slug(value) {
|
|
136
|
+
return String(value).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
137
|
+
}
|
|
138
|
+
function buildVariant(platform, options) {
|
|
139
|
+
const raw = platform === "android" ? options.variant : options.configuration ?? options.buildConfiguration;
|
|
140
|
+
return (typeof raw === "string" ? slug(raw) : "") || "debug";
|
|
141
|
+
}
|
|
142
|
+
function buildTarget(options) {
|
|
143
|
+
if (typeof options.isSimulator === "boolean") return options.isSimulator ? "sim" : "device";
|
|
144
|
+
const device = options.device;
|
|
145
|
+
if (device === void 0 || device === null || device === false) return "sim";
|
|
146
|
+
if (typeof device !== "string") return "prompted";
|
|
147
|
+
const name = device.trim();
|
|
148
|
+
if (name === "" || name === "generic") return "sim";
|
|
149
|
+
if (SIMULATOR_UDID.test(name) || EMULATOR_SERIAL.test(name)) return "sim";
|
|
150
|
+
return `on-${slug(name)}`;
|
|
151
|
+
}
|
|
152
|
+
function buildCacheKey(platform, fingerprintHash, options = {}) {
|
|
153
|
+
const opts = options && typeof options === "object" ? options : {};
|
|
154
|
+
return `${fingerprintHash}-${buildVariant(platform, opts)}-${buildTarget(opts)}`;
|
|
155
|
+
}
|
|
156
|
+
const METRO_NAMED_CACHE_LAYOUT = "metro-named-v1";
|
|
157
|
+
function cacheManifestLockPath(file) {
|
|
158
|
+
return `${file}.lock`;
|
|
159
|
+
}
|
|
160
|
+
function readCacheManifest(file) {
|
|
161
|
+
try {
|
|
162
|
+
const parsed = JSON.parse(fs.readFileSync(file, "utf-8"));
|
|
163
|
+
return {
|
|
164
|
+
version: 1,
|
|
165
|
+
caches: Array.isArray(parsed?.caches) ? parsed.caches : []
|
|
166
|
+
};
|
|
167
|
+
} catch {
|
|
168
|
+
return {
|
|
169
|
+
version: 1,
|
|
170
|
+
caches: []
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function updateCacheManifest(file, mutate) {
|
|
175
|
+
const parent = path.dirname(file);
|
|
176
|
+
return withDirLock(cacheManifestLockPath(file), () => {
|
|
177
|
+
const next = {
|
|
178
|
+
version: 1,
|
|
179
|
+
caches: mutate(readCacheManifest(file).caches)
|
|
180
|
+
};
|
|
181
|
+
const temporary = path.join(parent, `.${path.basename(file)}.${process.pid}.tmp`);
|
|
182
|
+
try {
|
|
183
|
+
fs.writeFileSync(temporary, JSON.stringify(next, null, 2));
|
|
184
|
+
fs.renameSync(temporary, file);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
fs.rmSync(temporary, { force: true });
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
return next;
|
|
190
|
+
}, { ensureParent: () => fs.mkdirSync(parent, { recursive: true }) });
|
|
191
|
+
}
|
|
192
|
+
function registerCache({ dir, name, prune, note, entriesDepth, layout, replaces = [] }) {
|
|
193
|
+
try {
|
|
194
|
+
updateCacheManifest(path.join(configDir(), "caches.json"), (caches) => {
|
|
195
|
+
const others = caches.filter((cache) => cache.dir !== dir && !replaces.some((match) => matchesCache(cache, match)));
|
|
196
|
+
const record = {
|
|
197
|
+
dir,
|
|
198
|
+
name,
|
|
199
|
+
prune,
|
|
200
|
+
note,
|
|
201
|
+
registeredBy: process.cwd()
|
|
202
|
+
};
|
|
203
|
+
if (entriesDepth) record.entriesDepth = entriesDepth;
|
|
204
|
+
if (layout) record.layout = layout;
|
|
205
|
+
others.push(record);
|
|
206
|
+
return others;
|
|
207
|
+
});
|
|
208
|
+
} catch {}
|
|
209
|
+
}
|
|
210
|
+
function matchesCache(cache, match) {
|
|
211
|
+
if (cache.dir !== match.dir) return false;
|
|
212
|
+
if (match.name !== void 0 && cache.name !== match.name) return false;
|
|
213
|
+
if (match.prune !== void 0 && cache.prune !== match.prune) return false;
|
|
214
|
+
if (match.entriesDepth !== void 0 && cache.entriesDepth !== match.entriesDepth) return false;
|
|
215
|
+
if (match.layout === null) return !Object.hasOwn(cache, "layout");
|
|
216
|
+
return match.layout === void 0 || cache.layout === match.layout;
|
|
217
|
+
}
|
|
218
|
+
//#endregion
|
|
219
|
+
export { METRO_NAMED_CACHE_LAYOUT, STORE_ROOT_TAG, buildCacheKey, buildCacheRoot, cacheManifestLockPath, cacheNameSegment, cachePathSetting, configDir, metroCacheRoot, readCacheManifest, registerCache, sharedStoreRoot, tagSharedStore, updateCacheManifest, withDirLock, workspaceId, workspaceLogDir, workspaceName, workspaceSlug, workspaceStateDir };
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stim-cli/core",
|
|
3
|
+
"version": "1.0.0-rc.0",
|
|
4
|
+
"description": "Shared primitives for stim-cli and its cache packages: config-dir and cache-root resolution, the build cache key, and cache self-registration. Not a user-facing API.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/appandflow/stim-cli.git",
|
|
9
|
+
"directory": "packages/core"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "dist/index.mjs",
|
|
18
|
+
"types": "dist/index.d.mts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.mts",
|
|
22
|
+
"module-sync": "./dist/index.mjs",
|
|
23
|
+
"default": "./dist/index.mjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": "^20.19.4 || >=22.12.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsdown"
|
|
31
|
+
}
|
|
32
|
+
}
|