@astryxdesign/cli 0.7.14-canary.g63bbda1 → 0.7.14-canary.g9e7e532
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/api/component/_adapter.d.mts +15 -0
- package/api/component/_adapter.mjs +76 -0
- package/api/component/component.mjs +8 -1
- package/api/theme-edit/_adapter.d.mts +28 -2
- package/api/theme-edit/_adapter.mjs +319 -12
- package/api/theme-edit/_check.mjs +254 -29
- package/api/theme-edit/_color.d.mts +10 -2
- package/api/theme-edit/_color.mjs +68 -7
- package/api/theme-edit/_compiler.mjs +8 -2
- package/api/theme-edit/_context.mjs +15 -0
- package/api/theme-edit/_edit.mjs +164 -26
- package/api/theme-edit/_palette.d.mts +16 -0
- package/api/theme-edit/_palette.mjs +61 -2
- package/api/theme-edit/_preview.mjs +5 -2
- package/api/theme-edit/_settings.d.mts +77 -6
- package/api/theme-edit/_settings.mjs +528 -50
- package/api/theme-edit/_tables.mjs +1 -1
- package/api/theme-edit/theme-edit.type.d.mts +12 -12
- package/api/theme-edit/theme-edit.type.mjs +11 -11
- package/api/theme-edit/themeEditNew.doc.mjs +6 -1
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +2 -1
- package/api/theme-edit/themeEditPreviewList.doc.mjs +2 -1
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +2 -1
- package/api/theme-edit/themeEditSet.doc.mjs +9 -3
- package/api/theme-edit/themeEditShow.doc.mjs +6 -1
- package/api/theme-edit/themeEditUndo.doc.mjs +7 -2
- package/clients/cli/commands/experimental/theme.mjs +57 -7
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
- package/foundation/response/error-codes.d.mts +2 -1
- package/foundation/response/error-codes.doc.mjs +6 -0
- package/foundation/response/error-codes.mjs +5 -0
- package/package.json +6 -6
|
@@ -276,6 +276,21 @@ export function linkComponentResponse<R extends {
|
|
|
276
276
|
package?: string;
|
|
277
277
|
data?: any;
|
|
278
278
|
}>(response: R, cwd: string): Promise<R>;
|
|
279
|
+
/**
|
|
280
|
+
* When `error` is the unknown-component error for a name that is an example
|
|
281
|
+
* block, returns the same error with the commands that show the block in its
|
|
282
|
+
* message: `astryx template <Block>`, and `astryx component <Parent> --blocks`
|
|
283
|
+
* for a component whose `--blocks` listing holds the block. The code and the
|
|
284
|
+
* matcher's suggestions stay. Returns `error` unchanged otherwise.
|
|
285
|
+
* @param {unknown} error
|
|
286
|
+
* @param {string} dirName
|
|
287
|
+
* @param {{coreDir: string, cwd: string}} context
|
|
288
|
+
* @returns {Promise<unknown>}
|
|
289
|
+
*/
|
|
290
|
+
export function withExampleBlockPointer(error: unknown, dirName: string, { coreDir, cwd }: {
|
|
291
|
+
coreDir: string;
|
|
292
|
+
cwd: string;
|
|
293
|
+
}): Promise<unknown>;
|
|
279
294
|
/**
|
|
280
295
|
* Internal ambiguity marker. Single-component callers still receive the same
|
|
281
296
|
* AstryxError code, message, and suggestions; batch callers can additionally
|
|
@@ -49,6 +49,10 @@ import {
|
|
|
49
49
|
synonymMatches,
|
|
50
50
|
} from '../../foundation/text/suggest-order.mjs';
|
|
51
51
|
import {AstryxError, coreDirOrThrow} from '../error.mjs';
|
|
52
|
+
import {
|
|
53
|
+
discoverAll,
|
|
54
|
+
findRelatedBlocks,
|
|
55
|
+
} from '../../foundation/discovery/template-adapter.mjs';
|
|
52
56
|
import {levenshteinDistance} from '../../foundation/text/levenshtein.mjs';
|
|
53
57
|
import {
|
|
54
58
|
componentLinkProvider,
|
|
@@ -827,3 +831,75 @@ export async function linkComponentResponse(response, cwd) {
|
|
|
827
831
|
}
|
|
828
832
|
return {...response, data: {...response.data, components}};
|
|
829
833
|
}
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* When `error` is the unknown-component error for a name that is an example
|
|
837
|
+
* block, returns the same error with the commands that show the block in its
|
|
838
|
+
* message: `astryx template <Block>`, and `astryx component <Parent> --blocks`
|
|
839
|
+
* for a component whose `--blocks` listing holds the block. The code and the
|
|
840
|
+
* matcher's suggestions stay. Returns `error` unchanged otherwise.
|
|
841
|
+
* @param {unknown} error
|
|
842
|
+
* @param {string} dirName
|
|
843
|
+
* @param {{coreDir: string, cwd: string}} context
|
|
844
|
+
* @returns {Promise<unknown>}
|
|
845
|
+
*/
|
|
846
|
+
export async function withExampleBlockPointer(error, dirName, {coreDir, cwd}) {
|
|
847
|
+
if (
|
|
848
|
+
!(error instanceof AstryxError) ||
|
|
849
|
+
error.code !== ERROR_CODES.ERR_UNKNOWN_COMPONENT
|
|
850
|
+
) {
|
|
851
|
+
return error;
|
|
852
|
+
}
|
|
853
|
+
// A block doc that fails to load must not replace the unknown-component
|
|
854
|
+
// error and its suggestions: the pointer is extra help, so a failed lookup
|
|
855
|
+
// keeps the original error.
|
|
856
|
+
try {
|
|
857
|
+
const typed = dirName.toLowerCase();
|
|
858
|
+
const block = (await discoverAll(cwd)).find(
|
|
859
|
+
template =>
|
|
860
|
+
template.type === 'block' && template.dirName.toLowerCase() === typed,
|
|
861
|
+
);
|
|
862
|
+
if (block == null) {
|
|
863
|
+
return error;
|
|
864
|
+
}
|
|
865
|
+
const parent = await listingParent(block, coreDir, cwd);
|
|
866
|
+
const listing = parent
|
|
867
|
+
? `, and astryx component ${parent} --blocks lists it`
|
|
868
|
+
: '';
|
|
869
|
+
return new AstryxError(
|
|
870
|
+
`${error.message}. It is an example block: astryx template ${block.dirName} prints its source${listing}.`,
|
|
871
|
+
error.suggestions,
|
|
872
|
+
ERROR_CODES.ERR_UNKNOWN_COMPONENT,
|
|
873
|
+
);
|
|
874
|
+
} catch {
|
|
875
|
+
return error;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* A component whose `component <Name> --blocks` listing holds `block`: one of
|
|
881
|
+
* the components the block uses, its own folder's component first, that
|
|
882
|
+
* resolves as a component and whose related blocks include the block.
|
|
883
|
+
* @param {{dirName: string, category?: string, componentsUsed?: string[]}} block
|
|
884
|
+
* @param {string} coreDir
|
|
885
|
+
* @param {string} cwd
|
|
886
|
+
* @returns {Promise<string | null>}
|
|
887
|
+
*/
|
|
888
|
+
async function listingParent(block, coreDir, cwd) {
|
|
889
|
+
const used = block.componentsUsed ?? [];
|
|
890
|
+
const folder = path.basename(block.category ?? '');
|
|
891
|
+
const candidates = [
|
|
892
|
+
...used.filter(name => name === folder),
|
|
893
|
+
...used.filter(name => name !== folder),
|
|
894
|
+
];
|
|
895
|
+
for (const name of candidates) {
|
|
896
|
+
if (findComponentReadme(coreDir, name) == null) {
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
const related = await findRelatedBlocks(name, cwd);
|
|
900
|
+
if (related.some(entry => entry.dirName === block.dirName)) {
|
|
901
|
+
return name;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
return null;
|
|
905
|
+
}
|
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
legacyExternalKeeps,
|
|
32
32
|
resolveCoreSourcePath,
|
|
33
33
|
resolveUnscopedDoc,
|
|
34
|
+
withExampleBlockPointer,
|
|
34
35
|
loadComponentDoc,
|
|
35
36
|
scopeSubComponent,
|
|
36
37
|
ComponentAmbiguityError,
|
|
@@ -623,7 +624,13 @@ async function componentRead(name, options = {}) {
|
|
|
623
624
|
});
|
|
624
625
|
}
|
|
625
626
|
|
|
626
|
-
const resolved = await resolveUnscopedDoc(dirName, {
|
|
627
|
+
const resolved = await resolveUnscopedDoc(dirName, {
|
|
628
|
+
coreDir,
|
|
629
|
+
cwd,
|
|
630
|
+
name,
|
|
631
|
+
}).catch(async error => {
|
|
632
|
+
throw await withExampleBlockPointer(error, dirName, {coreDir, cwd});
|
|
633
|
+
});
|
|
627
634
|
const docs = await loadComponentDoc(resolved.readmePath, docOpts);
|
|
628
635
|
|
|
629
636
|
// ── Blocks mode ──────────────────────────────────────────────
|
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
// @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
|
|
2
2
|
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* The project a command acts on: the Astryx project at or above `cwd`, found
|
|
6
|
+
* as `upgrade` finds it (the nearest package that declares an Astryx package
|
|
7
|
+
* in any dependency list; the search stops at a repository root and never
|
|
8
|
+
* counts the home folder or above), else `cwd` itself. A package nearer
|
|
9
|
+
* than that project that does not use Astryx is refused with the folder to
|
|
10
|
+
* run from, and a nearest package.json that does not parse is never walked
|
|
11
|
+
* past.
|
|
12
|
+
* @param {string} cwd
|
|
13
|
+
*/
|
|
14
|
+
export function projectRoot(cwd: string): string;
|
|
15
|
+
export const themeEditPreviewApp: typeof preview.themeEditPreviewApp;
|
|
16
|
+
/** @type {typeof edit.themeEditNew} */
|
|
17
|
+
export const themeEditNew: typeof edit.themeEditNew;
|
|
18
|
+
/** @type {typeof edit.themeEditSet} */
|
|
19
|
+
export const themeEditSet: typeof edit.themeEditSet;
|
|
20
|
+
/** @type {typeof edit.themeEditUndo} */
|
|
21
|
+
export const themeEditUndo: typeof edit.themeEditUndo;
|
|
22
|
+
/** @type {typeof edit.themeEditShow} */
|
|
23
|
+
export const themeEditShow: typeof edit.themeEditShow;
|
|
24
|
+
/** @type {typeof preview.themeEditPreviewAdd} */
|
|
25
|
+
export const themeEditPreviewAdd: typeof preview.themeEditPreviewAdd;
|
|
26
|
+
/** @type {typeof preview.themeEditPreviewList} */
|
|
27
|
+
export const themeEditPreviewList: typeof preview.themeEditPreviewList;
|
|
28
|
+
/** @type {typeof preview.themeEditPreviewRemove} */
|
|
29
|
+
export const themeEditPreviewRemove: typeof preview.themeEditPreviewRemove;
|
|
30
|
+
import * as preview from './_preview-app.mjs';
|
|
31
|
+
import * as edit from './_edit.mjs';
|
|
6
32
|
export { themeEditHow, parsePoints } from "./_how.mjs";
|
|
@@ -31,16 +31,323 @@
|
|
|
31
31
|
* @position api/theme-edit — adapter for api/theme-edit/theme-edit.mjs
|
|
32
32
|
*/
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
} from '
|
|
34
|
+
import {createHash} from 'node:crypto';
|
|
35
|
+
import * as fs from 'node:fs';
|
|
36
|
+
import * as os from 'node:os';
|
|
37
|
+
import * as path from 'node:path';
|
|
38
|
+
import * as edit from './_edit.mjs';
|
|
39
|
+
import * as preview from './_preview-app.mjs';
|
|
40
|
+
import {AstryxError} from '../error.mjs';
|
|
41
|
+
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
42
|
+
import {
|
|
43
|
+
findAstryxProject,
|
|
44
|
+
findPackageRoot,
|
|
45
|
+
} from '../../foundation/fs/paths.mjs';
|
|
46
|
+
import {tryLock} from '../../foundation/release/cache.mjs';
|
|
47
|
+
import {q} from './_settings.mjs';
|
|
48
|
+
|
|
46
49
|
export {themeEditHow, parsePoints} from './_how.mjs';
|
|
50
|
+
export const {themeEditPreviewApp} = preview;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The project a command acts on: the Astryx project at or above `cwd`, found
|
|
54
|
+
* as `upgrade` finds it (the nearest package that declares an Astryx package
|
|
55
|
+
* in any dependency list; the search stops at a repository root and never
|
|
56
|
+
* counts the home folder or above), else `cwd` itself. A package nearer
|
|
57
|
+
* than that project that does not use Astryx is refused with the folder to
|
|
58
|
+
* run from, and a nearest package.json that does not parse is never walked
|
|
59
|
+
* past.
|
|
60
|
+
* @param {string} cwd
|
|
61
|
+
*/
|
|
62
|
+
export function projectRoot(cwd) {
|
|
63
|
+
const start = path.resolve(cwd);
|
|
64
|
+
const project = findAstryxProject(start);
|
|
65
|
+
const pkg = findPackageRoot(start);
|
|
66
|
+
const inReach = (/** @type {string} */ dir) =>
|
|
67
|
+
project ? !path.relative(project, dir).startsWith('..') : true;
|
|
68
|
+
if (pkg && inReach(pkg) && pkg !== project) {
|
|
69
|
+
const file = path.join(pkg, 'package.json');
|
|
70
|
+
/** @type {any} */
|
|
71
|
+
let json;
|
|
72
|
+
try {
|
|
73
|
+
json = JSON.parse(fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, ''));
|
|
74
|
+
} catch {
|
|
75
|
+
throw new AstryxError(
|
|
76
|
+
`${path.relative(start, file) || 'package.json'} is not valid JSON, so the app it belongs to is unknown. Fix it, then run the command again. Nothing was written.`,
|
|
77
|
+
[],
|
|
78
|
+
ERROR_CODES.ERR_PROJECT_REQUIRED,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
// A package.json that only sets the module type (no name, no
|
|
82
|
+
// dependencies) is a folder setting, not another package.
|
|
83
|
+
const trivial =
|
|
84
|
+
json?.name == null &&
|
|
85
|
+
!['dependencies', 'devDependencies', 'peerDependencies'].some(
|
|
86
|
+
k => json?.[k] != null,
|
|
87
|
+
);
|
|
88
|
+
if (project && !trivial)
|
|
89
|
+
throw new AstryxError(
|
|
90
|
+
`${path.relative(start, file) || 'package.json'} is a package that does not use Astryx; the app that does is ${path.relative(start, project) || '.'}. Run the command from there, so the theme lands in that app. Nothing was written.`,
|
|
91
|
+
[
|
|
92
|
+
{
|
|
93
|
+
name: `cd ${q(path.relative(start, project))}`,
|
|
94
|
+
reason: 'do this first',
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
ERROR_CODES.ERR_PROJECT_REQUIRED,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return project ?? start;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The node_modules folder Core resolves from for `root` (its own, or one
|
|
105
|
+
* above it, as Node finds packages), or null when Core is not installed.
|
|
106
|
+
* @param {string} root
|
|
107
|
+
* @returns {string | null}
|
|
108
|
+
*/
|
|
109
|
+
function coreModules(root) {
|
|
110
|
+
for (let d = root; ; d = path.dirname(d)) {
|
|
111
|
+
const modules = path.join(d, 'node_modules');
|
|
112
|
+
if (
|
|
113
|
+
fs.existsSync(path.join(modules, '@astryxdesign', 'core', 'package.json'))
|
|
114
|
+
)
|
|
115
|
+
return modules;
|
|
116
|
+
if (path.dirname(d) === d) return null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The locks this process holds now. */
|
|
121
|
+
const HELD = new Set();
|
|
122
|
+
|
|
123
|
+
/** @param {number} ms */
|
|
124
|
+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Where a project's locks are: the `.cache` of the node_modules its Core
|
|
128
|
+
* is installed in (the project's own, or a workspace root's), shared by
|
|
129
|
+
* every shell and user that can change the project, outside the source
|
|
130
|
+
* tree. Null when Core is not installed for it (nothing can be written
|
|
131
|
+
* without Core, so there is nothing to keep apart). A folder that
|
|
132
|
+
* cannot be written is refused, naming it.
|
|
133
|
+
* @param {string} root
|
|
134
|
+
* @returns {string | null}
|
|
135
|
+
*/
|
|
136
|
+
function lockDir(root) {
|
|
137
|
+
const modules = coreModules(root);
|
|
138
|
+
if (!modules) return null;
|
|
139
|
+
const dir = path.join(modules, '.cache', 'astryx-theme', 'locks');
|
|
140
|
+
try {
|
|
141
|
+
fs.mkdirSync(dir, {recursive: true});
|
|
142
|
+
fs.accessSync(dir, fs.constants.W_OK);
|
|
143
|
+
return dir;
|
|
144
|
+
} catch (e) {
|
|
145
|
+
throw new AstryxError(
|
|
146
|
+
`Could not write ${path.relative(root, dir)} (${/** @type {any} */ (e).code ?? String(e)}), where the lock that keeps two commands on one theme apart is kept. Fix its permission, then run the command again. Nothing was changed.`,
|
|
147
|
+
[],
|
|
148
|
+
ERROR_CODES.ERR_WRITE_FAILED,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Run `fn` holding a lock named `name` for the project at `root`, so two
|
|
155
|
+
* commands on one theme (or one preview) run one after the other: each one
|
|
156
|
+
* reads what the other wrote. The lock is a folder in the project's
|
|
157
|
+
* node_modules/.cache; a lock left by a command that is no longer running
|
|
158
|
+
* is taken over.
|
|
159
|
+
* @template T
|
|
160
|
+
* @param {string} root
|
|
161
|
+
* @param {string} name
|
|
162
|
+
* @param {() => Promise<T>} fn
|
|
163
|
+
* @returns {Promise<T>}
|
|
164
|
+
*/
|
|
165
|
+
async function withLock(root, name, fn) {
|
|
166
|
+
const dir = lockDir(root);
|
|
167
|
+
if (!dir) return fn();
|
|
168
|
+
const key = createHash('sha256').update(root).digest('hex').slice(0, 16);
|
|
169
|
+
const lock = path.join(dir, `${key}-${name}.lock`);
|
|
170
|
+
const ownerFile = path.join(lock, 'owner.json');
|
|
171
|
+
/** @returns {any} */
|
|
172
|
+
const owner = () => {
|
|
173
|
+
try {
|
|
174
|
+
return JSON.parse(fs.readFileSync(ownerFile, 'utf8'));
|
|
175
|
+
} catch {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
// A lock naming this very process on this computer, that this process
|
|
180
|
+
// does not hold, is left from a process that had this pid before (a
|
|
181
|
+
// container restarted).
|
|
182
|
+
const held = owner();
|
|
183
|
+
if (
|
|
184
|
+
held?.pid === process.pid &&
|
|
185
|
+
held?.host === os.hostname() &&
|
|
186
|
+
!HELD.has(lock)
|
|
187
|
+
)
|
|
188
|
+
fs.rmSync(lock, {recursive: true, force: true});
|
|
189
|
+
try {
|
|
190
|
+
// The release cache's lock: made with mkdir (one taker wins), and a
|
|
191
|
+
// holder that stopped is taken over by a rename (one taker wins).
|
|
192
|
+
for (let waited = 0; !tryLock(lock); waited += 50) {
|
|
193
|
+
if (waited > 30_000) {
|
|
194
|
+
const o = owner();
|
|
195
|
+
throw new AstryxError(
|
|
196
|
+
`Another command is changing ${name.replace(/^theme-/, '')} and did not finish within 30 seconds${o ? ` (process ${o.pid} on ${o.host}, for ${Math.round((Date.now() - Number(o.at)) / 1000)} s)` : ''}. Nothing was changed; run this command again. If no astryx command is running, remove the lock ${lock}.`,
|
|
197
|
+
[
|
|
198
|
+
{
|
|
199
|
+
name: `rm -r ${q(lock)}`,
|
|
200
|
+
reason: 'only when no astryx command is running',
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
ERROR_CODES.ERR_THEME_BUSY,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
await sleep(50);
|
|
207
|
+
}
|
|
208
|
+
} catch (e) {
|
|
209
|
+
if (e instanceof AstryxError) throw e;
|
|
210
|
+
throw new AstryxError(
|
|
211
|
+
`Could not take the lock ${lock} (${/** @type {any} */ (e).code ?? String(e)}). Nothing was changed; fix the folder, then run the command again.`,
|
|
212
|
+
[],
|
|
213
|
+
ERROR_CODES.ERR_WRITE_FAILED,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
HELD.add(lock);
|
|
217
|
+
const mine = fs.existsSync(ownerFile)
|
|
218
|
+
? fs.readFileSync(ownerFile, 'utf8')
|
|
219
|
+
: null;
|
|
220
|
+
// Only this command's own lock is removed, never one another took over.
|
|
221
|
+
const release = () => {
|
|
222
|
+
try {
|
|
223
|
+
const now = fs.existsSync(ownerFile)
|
|
224
|
+
? fs.readFileSync(ownerFile, 'utf8')
|
|
225
|
+
: null;
|
|
226
|
+
if (now === mine) fs.rmSync(lock, {recursive: true, force: true});
|
|
227
|
+
} catch {
|
|
228
|
+
// Gone already.
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
// Stopped by a signal, the lock still goes.
|
|
232
|
+
const onSignal = (/** @type {NodeJS.Signals} */ sig) => {
|
|
233
|
+
release();
|
|
234
|
+
process.kill(process.pid, sig);
|
|
235
|
+
};
|
|
236
|
+
process.once('exit', release);
|
|
237
|
+
process.once('SIGINT', onSignal);
|
|
238
|
+
process.once('SIGTERM', onSignal);
|
|
239
|
+
try {
|
|
240
|
+
return await fn();
|
|
241
|
+
} catch (e) {
|
|
242
|
+
// A folder that cannot be written (no permission, a full or read-only
|
|
243
|
+
// disk) is a documented refusal, not a crash.
|
|
244
|
+
const code = /** @type {any} */ (e)?.code;
|
|
245
|
+
if (['EACCES', 'EPERM', 'ENOSPC', 'EROFS'].includes(code))
|
|
246
|
+
throw new AstryxError(
|
|
247
|
+
`Could not write ${path.relative(root, /** @type {any} */ (e).path ?? '') || 'the theme files'}: ${code === 'ENOSPC' ? 'the disk is full' : code === 'EROFS' ? 'the disk is read-only' : 'no permission'}. Free the space or fix the permission, then run the command again; a refused change is undone.`,
|
|
248
|
+
[],
|
|
249
|
+
ERROR_CODES.ERR_WRITE_FAILED,
|
|
250
|
+
);
|
|
251
|
+
throw e;
|
|
252
|
+
} finally {
|
|
253
|
+
release();
|
|
254
|
+
HELD.delete(lock);
|
|
255
|
+
process.removeListener('exit', release);
|
|
256
|
+
process.removeListener('SIGINT', onSignal);
|
|
257
|
+
process.removeListener('SIGTERM', onSignal);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Options with `cwd` moved to the project root, and a `--path` given from a
|
|
263
|
+
* subfolder kept pointing where it was given.
|
|
264
|
+
* @template {{cwd?: string, path?: string}} O
|
|
265
|
+
* @param {O} options
|
|
266
|
+
* @returns {O & {cwd: string}}
|
|
267
|
+
*/
|
|
268
|
+
function atRoot(options) {
|
|
269
|
+
const from = options.cwd ?? process.cwd();
|
|
270
|
+
const cwd = projectRoot(from);
|
|
271
|
+
return {
|
|
272
|
+
...options,
|
|
273
|
+
cwd,
|
|
274
|
+
...(options.path != null && options.path !== ''
|
|
275
|
+
? {path: path.relative(cwd, path.resolve(from, options.path)) || '.'}
|
|
276
|
+
: {}),
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** @param {any} name */
|
|
281
|
+
const lockName = name =>
|
|
282
|
+
`theme-${String(name ?? '')
|
|
283
|
+
.replace(/[^a-z0-9-]/gi, '_')
|
|
284
|
+
.slice(0, 80)}`;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* A result whose theme paths read from the project root, made absolute when
|
|
288
|
+
* the command ran in a folder below that root, so a path in the reply
|
|
289
|
+
* points at the file wherever the person is.
|
|
290
|
+
* @template T
|
|
291
|
+
* @param {T} result
|
|
292
|
+
* @param {string} root
|
|
293
|
+
* @param {string} from
|
|
294
|
+
* @returns {T}
|
|
295
|
+
*/
|
|
296
|
+
function absolute(result, root, from) {
|
|
297
|
+
if (path.resolve(from) === root) return result;
|
|
298
|
+
/** @param {any} v @returns {any} */
|
|
299
|
+
const map = v =>
|
|
300
|
+
typeof v === 'string'
|
|
301
|
+
? /^themes\//.test(v)
|
|
302
|
+
? path.join(root, v)
|
|
303
|
+
: v
|
|
304
|
+
: Array.isArray(v)
|
|
305
|
+
? v.map(map)
|
|
306
|
+
: v && typeof v === 'object'
|
|
307
|
+
? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, map(x)]))
|
|
308
|
+
: v;
|
|
309
|
+
return map(result);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Run an editor command at the project root, holding `lock` when one is
|
|
314
|
+
* named, with paths in the reply that work from where it ran.
|
|
315
|
+
* @template {{cwd?: string, path?: string}} O
|
|
316
|
+
* @template T
|
|
317
|
+
* @param {O} options
|
|
318
|
+
* @param {string | null} lock
|
|
319
|
+
* @param {(o: O & {cwd: string}) => Promise<T>} fn
|
|
320
|
+
* @returns {Promise<T>}
|
|
321
|
+
*/
|
|
322
|
+
async function inProject(options, lock, fn) {
|
|
323
|
+
const from = options.cwd ?? process.cwd();
|
|
324
|
+
const o = atRoot(options);
|
|
325
|
+
const r = lock ? await withLock(o.cwd, lock, () => fn(o)) : await fn(o);
|
|
326
|
+
return absolute(r, o.cwd, from);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** @type {typeof edit.themeEditNew} */
|
|
330
|
+
export const themeEditNew = (name, options = {}) =>
|
|
331
|
+
inProject(options, lockName(name), o => edit.themeEditNew(name, o));
|
|
332
|
+
/** @type {typeof edit.themeEditSet} */
|
|
333
|
+
export const themeEditSet = (theme, thing, value, options = {}) =>
|
|
334
|
+
inProject(options, lockName(theme), o =>
|
|
335
|
+
edit.themeEditSet(theme, thing, value, o),
|
|
336
|
+
);
|
|
337
|
+
/** @type {typeof edit.themeEditUndo} */
|
|
338
|
+
export const themeEditUndo = (theme, options = {}) =>
|
|
339
|
+
inProject(options, lockName(theme), o => edit.themeEditUndo(theme, o));
|
|
340
|
+
/** @type {typeof edit.themeEditShow} */
|
|
341
|
+
export const themeEditShow = (theme, options = {}) =>
|
|
342
|
+
inProject(options, lockName(theme), o => edit.themeEditShow(theme, o));
|
|
343
|
+
/** @type {typeof preview.themeEditPreviewAdd} */
|
|
344
|
+
export const themeEditPreviewAdd = (description, options = {}) =>
|
|
345
|
+
inProject(options, 'preview', o =>
|
|
346
|
+
preview.themeEditPreviewAdd(description, o),
|
|
347
|
+
);
|
|
348
|
+
/** @type {typeof preview.themeEditPreviewList} */
|
|
349
|
+
export const themeEditPreviewList = (options = {}) =>
|
|
350
|
+
inProject(options, null, async o => preview.themeEditPreviewList(o));
|
|
351
|
+
/** @type {typeof preview.themeEditPreviewRemove} */
|
|
352
|
+
export const themeEditPreviewRemove = (page, options = {}) =>
|
|
353
|
+
inProject(options, 'preview', o => preview.themeEditPreviewRemove(page, o));
|