@app-studio/cli 0.1.0 → 0.1.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.
- package/imports.mjs +59 -0
- package/install.mjs +94 -5
- package/package.json +2 -2
- package/registry.mjs +41 -4
package/imports.mjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { posix } from 'node:path';
|
|
2
|
+
|
|
3
|
+
const scriptExtension = /\.[jt]sx?$/i;
|
|
4
|
+
|
|
5
|
+
export async function localizeImports(file, source, entries) {
|
|
6
|
+
if (!entries.size || !scriptExtension.test(file)) return source;
|
|
7
|
+
|
|
8
|
+
const { parse } = await import('@babel/parser');
|
|
9
|
+
const plugins = [];
|
|
10
|
+
if (/\.tsx?$/i.test(file)) plugins.push('typescript');
|
|
11
|
+
if (/\.(?:jsx?|tsx)$/i.test(file)) plugins.push('jsx');
|
|
12
|
+
let program;
|
|
13
|
+
try {
|
|
14
|
+
({ program } = parse(source, {
|
|
15
|
+
plugins,
|
|
16
|
+
sourceType: 'unambiguous',
|
|
17
|
+
createImportExpressions: true,
|
|
18
|
+
attachComment: false,
|
|
19
|
+
}));
|
|
20
|
+
} catch (error) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
`Cannot rewrite registry imports in ${file}: ${error.message}`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const edits = [];
|
|
27
|
+
function visit(node) {
|
|
28
|
+
let specifier = node.source;
|
|
29
|
+
if (node.type === 'TSImportType') specifier = node.argument;
|
|
30
|
+
if (node.type === 'TSModuleDeclaration' && program.sourceType === 'module')
|
|
31
|
+
specifier = node.id;
|
|
32
|
+
if (specifier?.type === 'StringLiteral' && entries.has(specifier.value)) {
|
|
33
|
+
const entry = entries.get(specifier.value).replace(scriptExtension, '');
|
|
34
|
+
const relative = posix.relative(posix.dirname(file), entry);
|
|
35
|
+
edits.push({
|
|
36
|
+
start: specifier.start,
|
|
37
|
+
end: specifier.end,
|
|
38
|
+
value: JSON.stringify(
|
|
39
|
+
relative.startsWith('.') ? relative : './' + relative
|
|
40
|
+
),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
for (const value of Object.values(node)) {
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
for (const child of value) {
|
|
46
|
+
if (child?.type) visit(child);
|
|
47
|
+
}
|
|
48
|
+
} else if (value?.type) {
|
|
49
|
+
visit(value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
visit(program);
|
|
55
|
+
for (const { start, end, value } of edits.sort((a, b) => b.start - a.start)) {
|
|
56
|
+
source = source.slice(0, start) + value + source.slice(end);
|
|
57
|
+
}
|
|
58
|
+
return source;
|
|
59
|
+
}
|
package/install.mjs
CHANGED
|
@@ -13,8 +13,11 @@ import {
|
|
|
13
13
|
writeFile,
|
|
14
14
|
} from 'node:fs/promises';
|
|
15
15
|
import { basename, dirname, join } from 'node:path';
|
|
16
|
+
import semver from 'semver';
|
|
17
|
+
import { localizeImports } from './imports.mjs';
|
|
16
18
|
import {
|
|
17
19
|
confined,
|
|
20
|
+
dependencyParts,
|
|
18
21
|
digest,
|
|
19
22
|
fetchItem,
|
|
20
23
|
itemURL,
|
|
@@ -27,6 +30,11 @@ import {
|
|
|
27
30
|
const ledgerPath = '.app-studio/registry-lock.json';
|
|
28
31
|
const lockPath = '.app-studio/install.lock';
|
|
29
32
|
|
|
33
|
+
function registryDirectory(address) {
|
|
34
|
+
const namespace = digest(new URL('.', address).href).slice(0, 16);
|
|
35
|
+
return `components/app-studio/${namespace}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
30
38
|
async function engineVersion(root) {
|
|
31
39
|
try {
|
|
32
40
|
const require = createRequire(join(root, 'package.json'));
|
|
@@ -55,6 +63,58 @@ async function localFile(root, path) {
|
|
|
55
63
|
});
|
|
56
64
|
}
|
|
57
65
|
|
|
66
|
+
async function reportPeerVersions(root, dependencies, report) {
|
|
67
|
+
if (!dependencies.length) return;
|
|
68
|
+
const resolver = createRequire(join(root, 'package.json'));
|
|
69
|
+
const requirements = new Map();
|
|
70
|
+
for (const specifier of dependencies) {
|
|
71
|
+
const [, name, range = '*'] = dependencyParts(specifier);
|
|
72
|
+
if (!requirements.has(name)) requirements.set(name, new Set());
|
|
73
|
+
requirements.get(name).add(range);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for (const [name, ranges] of requirements) {
|
|
77
|
+
const installed = await installedPeerVersion(resolver, name);
|
|
78
|
+
const versions = [...ranges];
|
|
79
|
+
if (
|
|
80
|
+
installed &&
|
|
81
|
+
versions.every((range) => semver.satisfies(installed, range))
|
|
82
|
+
)
|
|
83
|
+
continue;
|
|
84
|
+
|
|
85
|
+
const required =
|
|
86
|
+
versions.length === 1
|
|
87
|
+
? JSON.stringify(versions[0] === '*' ? name : `${name}@${versions[0]}`)
|
|
88
|
+
: `${name} at a version that satisfies all of ${versions
|
|
89
|
+
.map((range) => JSON.stringify(range))
|
|
90
|
+
.join(', ')}`;
|
|
91
|
+
report(
|
|
92
|
+
installed
|
|
93
|
+
? `Incompatible dependency: ${name}@${installed}. Install ${required} in this project.`
|
|
94
|
+
: `Dependency version not found in this project: ${name}. Install ${required}.`
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function installedPeerVersion(resolver, name) {
|
|
100
|
+
try {
|
|
101
|
+
const manifest = await readJSON(resolver.resolve(`${name}/package.json`));
|
|
102
|
+
if (manifest.name === name && semver.valid(manifest.version))
|
|
103
|
+
return manifest.version;
|
|
104
|
+
} catch {
|
|
105
|
+
// Many packages keep package.json private in their exports map.
|
|
106
|
+
}
|
|
107
|
+
for (const directory of resolver.resolve.paths(name) ?? []) {
|
|
108
|
+
try {
|
|
109
|
+
const manifest = await readJSON(join(directory, name, 'package.json'));
|
|
110
|
+
if (manifest.name === name && semver.valid(manifest.version))
|
|
111
|
+
return manifest.version;
|
|
112
|
+
} catch {
|
|
113
|
+
// A diagnostic must not prevent the user from reviewing or copying source.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
58
118
|
async function plan(url, selected, engine, load = fetchItem) {
|
|
59
119
|
const sources = new Map();
|
|
60
120
|
const dependencies = new Set();
|
|
@@ -68,8 +128,14 @@ async function plan(url, selected, engine, load = fetchItem) {
|
|
|
68
128
|
const resolved = await resolveItems(url, platform, engine, read);
|
|
69
129
|
for (const [address, item] of resolved) {
|
|
70
130
|
const meta = item.meta.appStudio;
|
|
71
|
-
const
|
|
72
|
-
const
|
|
131
|
+
const directory = registryDirectory(address);
|
|
132
|
+
const imports = new Map(
|
|
133
|
+
Object.entries(meta.registryImports ?? {}).map(([specifier, ref]) => {
|
|
134
|
+
const dependencyURL = itemURL(ref, address);
|
|
135
|
+
const entry = resolved.get(dependencyURL).meta.appStudio.entry;
|
|
136
|
+
return [specifier, `${registryDirectory(dependencyURL)}/${entry}`];
|
|
137
|
+
})
|
|
138
|
+
);
|
|
73
139
|
for (const value of [
|
|
74
140
|
...(item.dependencies ?? []),
|
|
75
141
|
...(meta.dependencies?.[platform] ?? []),
|
|
@@ -79,19 +145,23 @@ async function plan(url, selected, engine, load = fetchItem) {
|
|
|
79
145
|
const files = {};
|
|
80
146
|
for (const file of item.files) {
|
|
81
147
|
const path = `${directory}/${file.path}`;
|
|
82
|
-
const
|
|
148
|
+
const content = await localizeImports(path, file.content, imports);
|
|
149
|
+
const hash = digest(content);
|
|
83
150
|
const key = path.toLowerCase();
|
|
84
151
|
const existing = sources.get(key);
|
|
85
152
|
if (existing && (existing.hash !== hash || existing.path !== path)) {
|
|
86
153
|
throw new Error(`Conflicting registry files: ${path}`);
|
|
87
154
|
}
|
|
88
|
-
sources.set(key, { path, hash, content
|
|
155
|
+
sources.set(key, { path, hash, content });
|
|
89
156
|
files[path] = hash;
|
|
90
157
|
}
|
|
91
158
|
items[address] = {
|
|
92
159
|
version: meta.version,
|
|
93
160
|
entry: `${directory}/${meta.entry}`,
|
|
94
161
|
files,
|
|
162
|
+
registryDependencies: (item.registryDependencies ?? []).map((ref) =>
|
|
163
|
+
itemURL(ref, address)
|
|
164
|
+
),
|
|
95
165
|
};
|
|
96
166
|
}
|
|
97
167
|
}
|
|
@@ -113,6 +183,21 @@ async function state(root) {
|
|
|
113
183
|
return value;
|
|
114
184
|
}
|
|
115
185
|
|
|
186
|
+
function installedFiles(items, addresses) {
|
|
187
|
+
const files = new Set();
|
|
188
|
+
const visited = new Set();
|
|
189
|
+
const pending = [...addresses];
|
|
190
|
+
while (pending.length) {
|
|
191
|
+
const address = pending.pop();
|
|
192
|
+
if (visited.has(address) || !items[address]) continue;
|
|
193
|
+
visited.add(address);
|
|
194
|
+
const item = items[address];
|
|
195
|
+
for (const path of Object.keys(item.files ?? {})) files.add(path);
|
|
196
|
+
pending.push(...(item.registryDependencies ?? []));
|
|
197
|
+
}
|
|
198
|
+
return files;
|
|
199
|
+
}
|
|
200
|
+
|
|
116
201
|
async function changes(root, plan, previous, installing) {
|
|
117
202
|
const tracked = new Set(
|
|
118
203
|
Object.values(previous.items).flatMap((item) =>
|
|
@@ -167,7 +252,10 @@ export async function runInstall({
|
|
|
167
252
|
const installed = previous.items[url];
|
|
168
253
|
if (!installed) throw new Error(`This item is not tracked: ${url}`);
|
|
169
254
|
report(`${installed.version} → ${proposal.items[url].version}`);
|
|
170
|
-
for (const path of
|
|
255
|
+
for (const path of installedFiles(
|
|
256
|
+
previous.items,
|
|
257
|
+
Object.keys(proposal.items)
|
|
258
|
+
)) {
|
|
171
259
|
if (!proposal.sources.has(path.toLowerCase()))
|
|
172
260
|
diff.push({ path, status: 'removed upstream' });
|
|
173
261
|
}
|
|
@@ -197,6 +285,7 @@ export async function runInstall({
|
|
|
197
285
|
'none beyond the platform and app-studio'
|
|
198
286
|
}`
|
|
199
287
|
);
|
|
288
|
+
await reportPeerVersions(root, proposal.dependencies, report);
|
|
200
289
|
for (const { path } of diff) report(`Add ${path}`);
|
|
201
290
|
if (dryRun) return;
|
|
202
291
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-studio/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": { "app-studio": "./cli.mjs" },
|
|
6
6
|
"files": ["*.mjs", "template"],
|
|
7
7
|
"engines": { "node": ">=20" },
|
|
8
|
-
"dependencies": { "semver": "^6.3.1" },
|
|
8
|
+
"dependencies": { "@babel/parser": "^7.29.7", "semver": "^6.3.1" },
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"publishConfig": { "access": "public" }
|
|
11
11
|
}
|
package/registry.mjs
CHANGED
|
@@ -61,6 +61,12 @@ const list = (value) =>
|
|
|
61
61
|
Array.isArray(value) && value.every((entry) => typeof entry === 'string');
|
|
62
62
|
const npmDependency =
|
|
63
63
|
/^(@[a-z0-9._-]+\/[a-z0-9._-]+|[a-z0-9][a-z0-9._-]*)(?:@([^@]+))?$/;
|
|
64
|
+
export function dependencyParts(value) {
|
|
65
|
+
const match = typeof value === 'string' ? value.match(npmDependency) : null;
|
|
66
|
+
return match && !/\/\.{1,2}$/.test(match[1]) ? match : null;
|
|
67
|
+
}
|
|
68
|
+
const moduleSpecifier =
|
|
69
|
+
/^(?:@[a-z0-9._-]*\/|#)?[a-z0-9][a-z0-9._-]*(?:\/[a-z0-9][a-z0-9._-]*)*$/i;
|
|
64
70
|
|
|
65
71
|
export function validateItem(item, { content = true } = {}) {
|
|
66
72
|
const meta = item?.meta?.appStudio;
|
|
@@ -110,8 +116,7 @@ export function validateItem(item, { content = true } = {}) {
|
|
|
110
116
|
...(item.dependencies ?? []),
|
|
111
117
|
...Object.values(meta.dependencies ?? {}).flat(),
|
|
112
118
|
]) {
|
|
113
|
-
const match =
|
|
114
|
-
typeof dependency === 'string' && dependency.match(npmDependency);
|
|
119
|
+
const match = dependencyParts(dependency);
|
|
115
120
|
if (!match || (match[2] && !semver.validRange(match[2]))) {
|
|
116
121
|
throw new Error(`Invalid npm dependency in ${item.name}: ${dependency}`);
|
|
117
122
|
}
|
|
@@ -121,6 +126,37 @@ export function validateItem(item, { content = true } = {}) {
|
|
|
121
126
|
throw new Error(`Invalid platform dependencies in ${item.name}`);
|
|
122
127
|
}
|
|
123
128
|
}
|
|
129
|
+
if ('engineByPlatform' in meta) {
|
|
130
|
+
const engines = meta.engineByPlatform;
|
|
131
|
+
if (
|
|
132
|
+
!engines ||
|
|
133
|
+
typeof engines !== 'object' ||
|
|
134
|
+
Array.isArray(engines) ||
|
|
135
|
+
Object.entries(engines).some(
|
|
136
|
+
([platform, range]) =>
|
|
137
|
+
!meta.platforms.includes(platform) ||
|
|
138
|
+
typeof range !== 'string' ||
|
|
139
|
+
!semver.validRange(range)
|
|
140
|
+
)
|
|
141
|
+
) {
|
|
142
|
+
throw new Error(`Invalid platform engine compatibility in ${item.name}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if ('registryImports' in meta) {
|
|
146
|
+
const imports = meta.registryImports;
|
|
147
|
+
if (
|
|
148
|
+
!imports ||
|
|
149
|
+
typeof imports !== 'object' ||
|
|
150
|
+
Array.isArray(imports) ||
|
|
151
|
+
Object.entries(imports).some(
|
|
152
|
+
([specifier, ref]) =>
|
|
153
|
+
!moduleSpecifier.test(specifier) ||
|
|
154
|
+
!(item.registryDependencies ?? []).includes(ref)
|
|
155
|
+
)
|
|
156
|
+
) {
|
|
157
|
+
throw new Error(`Invalid registry imports in ${item.name}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
124
160
|
const paths = new Set();
|
|
125
161
|
for (const file of item.files) {
|
|
126
162
|
const path = sourcePath(file?.path).toLowerCase();
|
|
@@ -195,9 +231,10 @@ export async function resolveItems(url, platform, engine, load = fetchItem) {
|
|
|
195
231
|
const meta = item.meta.appStudio;
|
|
196
232
|
if (!meta.platforms.includes(platform))
|
|
197
233
|
throw new Error(`${item.name} does not support ${platform}`);
|
|
198
|
-
|
|
234
|
+
const engineRange = meta.engineByPlatform?.[platform] ?? meta.engine;
|
|
235
|
+
if (!semver.satisfies(engine, engineRange, { includePrerelease: true })) {
|
|
199
236
|
throw new Error(
|
|
200
|
-
`${item.name} requires app-studio ${
|
|
237
|
+
`${item.name} requires app-studio ${engineRange} on ${platform}. Installed: ${engine}`
|
|
201
238
|
);
|
|
202
239
|
}
|
|
203
240
|
for (const ref of item.registryDependencies ?? []) {
|