@nimiplatform/app-tools 0.1.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 +9 -0
- package/README.md +44 -0
- package/bin/nimi-app.mjs +112 -0
- package/lib/app-doctor-update.mjs +472 -0
- package/lib/app-scaffold.mjs +959 -0
- package/lib/index.mjs +93 -0
- package/package.json +25 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @nimiplatform/nimi Licensing Map
|
|
2
|
+
|
|
3
|
+
This repository is multi-licensed by component.
|
|
4
|
+
|
|
5
|
+
- `runtime/**`, `sdk/**`, `proto/**`: Apache-2.0
|
|
6
|
+
- `apps/desktop/**`, `apps/web/**`, `apps/_libs/**`, `nimi-mods/**`: MIT
|
|
7
|
+
- `docs/**`: CC-BY-4.0
|
|
8
|
+
|
|
9
|
+
Canonical texts are in `licenses/`.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @nimiplatform/app-tools
|
|
2
|
+
|
|
3
|
+
Public app-authoring CLI for Nimi App projects.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm dlx @nimiplatform/app-tools nimi-app create --profile standalone
|
|
7
|
+
pnpm dlx @nimiplatform/app-tools nimi-app doctor --dir path/to/app
|
|
8
|
+
pnpm dlx @nimiplatform/app-tools nimi-app update --dir path/to/app
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`nimi-app create` emits a publishable Tauri app-authoring scaffold. The
|
|
12
|
+
generated project is designed to install its own dependencies with
|
|
13
|
+
`pnpm install`, run local checks, produce a pre-submission package artifact,
|
|
14
|
+
and remain directly usable without hand-editing scaffold-managed glue.
|
|
15
|
+
|
|
16
|
+
`nimi-app doctor` verifies scaffold lock state, managed glue, package-owned
|
|
17
|
+
projections, dependency alignment, and forbidden shortcut patterns in a source
|
|
18
|
+
checkout. `nimi-app update` refreshes scaffold-managed files while preserving
|
|
19
|
+
app-owned product code.
|
|
20
|
+
|
|
21
|
+
The CLI does not create public admission truth, permission grants, registry
|
|
22
|
+
visibility, release descriptors, or installed-app update truth. Platform review
|
|
23
|
+
owns those outcomes.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm add -D @nimiplatform/app-tools
|
|
29
|
+
pnpm add @nimiplatform/sdk @nimiplatform/kit
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Commands
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
nimi-app create [--dir path] [--profile standalone|workspace-app] [--app-id id] [--title title] [--package-name name] [--author author]
|
|
36
|
+
nimi-app doctor [--dir path] [--json]
|
|
37
|
+
nimi-app update [--dir path] [--json]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Inside this monorepo, use the repo-local binary:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
node ../../app-tools/bin/nimi-app.mjs create --profile standalone
|
|
44
|
+
```
|
package/bin/nimi-app.mjs
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
import { createApp, doctorAppScaffold, updateAppScaffold } from '../lib/index.mjs';
|
|
5
|
+
|
|
6
|
+
function parseArgs(argv) {
|
|
7
|
+
const [command = '', ...rest] = argv;
|
|
8
|
+
let dir = '';
|
|
9
|
+
let profile = '';
|
|
10
|
+
let appId = '';
|
|
11
|
+
let title = '';
|
|
12
|
+
let packageName = '';
|
|
13
|
+
let author = '';
|
|
14
|
+
let json = false;
|
|
15
|
+
for (let index = 0; index < rest.length; index += 1) {
|
|
16
|
+
if (rest[index] === '--dir') {
|
|
17
|
+
dir = String(rest[index + 1] || '').trim();
|
|
18
|
+
index += 1;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (rest[index] === '--profile') {
|
|
22
|
+
profile = String(rest[index + 1] || '').trim();
|
|
23
|
+
index += 1;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (rest[index] === '--app-id') {
|
|
27
|
+
appId = String(rest[index + 1] || '').trim();
|
|
28
|
+
index += 1;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (rest[index] === '--title') {
|
|
32
|
+
title = String(rest[index + 1] || '').trim();
|
|
33
|
+
index += 1;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (rest[index] === '--package-name') {
|
|
37
|
+
packageName = String(rest[index + 1] || '').trim();
|
|
38
|
+
index += 1;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (rest[index] === '--author') {
|
|
42
|
+
author = String(rest[index + 1] || '').trim();
|
|
43
|
+
index += 1;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (rest[index] === '--json') {
|
|
47
|
+
json = true;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
throw new Error(`Unknown option: ${rest[index]}`);
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
command: String(command || '').trim(),
|
|
54
|
+
dir,
|
|
55
|
+
profile,
|
|
56
|
+
appId,
|
|
57
|
+
title,
|
|
58
|
+
packageName,
|
|
59
|
+
author,
|
|
60
|
+
json,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function printUsage() {
|
|
65
|
+
process.stdout.write(
|
|
66
|
+
[
|
|
67
|
+
'Usage:',
|
|
68
|
+
' nimi-app create [--dir path] [--profile standalone|workspace-app] [--app-id id] [--title title] [--package-name name] [--author author]',
|
|
69
|
+
' nimi-app doctor [--dir path] [--json]',
|
|
70
|
+
' nimi-app update [--dir path] [--json]',
|
|
71
|
+
'',
|
|
72
|
+
].join('\n'),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const { command, dir, profile, appId, title, packageName, author, json } = parseArgs(process.argv.slice(2));
|
|
78
|
+
if (!command || command === '--help' || command === '-h') {
|
|
79
|
+
printUsage();
|
|
80
|
+
process.exit(0);
|
|
81
|
+
}
|
|
82
|
+
switch (command) {
|
|
83
|
+
case 'create':
|
|
84
|
+
createApp(process.cwd(), {
|
|
85
|
+
dir,
|
|
86
|
+
profile,
|
|
87
|
+
appId,
|
|
88
|
+
title,
|
|
89
|
+
packageName,
|
|
90
|
+
author,
|
|
91
|
+
});
|
|
92
|
+
break;
|
|
93
|
+
case 'doctor':
|
|
94
|
+
doctorAppScaffold(process.cwd(), {
|
|
95
|
+
dir,
|
|
96
|
+
json,
|
|
97
|
+
});
|
|
98
|
+
break;
|
|
99
|
+
case 'update':
|
|
100
|
+
updateAppScaffold(process.cwd(), {
|
|
101
|
+
dir,
|
|
102
|
+
json,
|
|
103
|
+
});
|
|
104
|
+
break;
|
|
105
|
+
default:
|
|
106
|
+
throw new Error(`Unknown command: ${command}`);
|
|
107
|
+
}
|
|
108
|
+
} catch (error) {
|
|
109
|
+
const message = error instanceof Error ? error.message : String(error || 'unknown error');
|
|
110
|
+
process.stderr.write(`[nimi-app] failed: ${message}\n`);
|
|
111
|
+
process.exit(1);
|
|
112
|
+
}
|
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
buildAppScaffoldSnapshot,
|
|
5
|
+
hashScaffoldContent,
|
|
6
|
+
SCAFFOLD_LOCK_PATH,
|
|
7
|
+
SCAFFOLD_VERSION,
|
|
8
|
+
SUPPORTED_APP_SCAFFOLD_PROFILES,
|
|
9
|
+
} from './app-scaffold.mjs';
|
|
10
|
+
|
|
11
|
+
const SCAN_EXCLUDED_DIRS = new Set(['.git', 'node_modules', 'dist', 'target', '.turbo', '.vite']);
|
|
12
|
+
const TEXT_EXTENSIONS = new Set([
|
|
13
|
+
'',
|
|
14
|
+
'.cjs',
|
|
15
|
+
'.css',
|
|
16
|
+
'.html',
|
|
17
|
+
'.js',
|
|
18
|
+
'.json',
|
|
19
|
+
'.md',
|
|
20
|
+
'.mjs',
|
|
21
|
+
'.rs',
|
|
22
|
+
'.toml',
|
|
23
|
+
'.ts',
|
|
24
|
+
'.tsx',
|
|
25
|
+
'.txt',
|
|
26
|
+
'.yaml',
|
|
27
|
+
'.yml',
|
|
28
|
+
]);
|
|
29
|
+
const PROVIDER_MODEL_HARDCODING_PROVIDERS = Object.freeze([
|
|
30
|
+
'openai',
|
|
31
|
+
'anthropic',
|
|
32
|
+
'gemini',
|
|
33
|
+
'mistral',
|
|
34
|
+
'llama',
|
|
35
|
+
'deepseek',
|
|
36
|
+
'qwen',
|
|
37
|
+
'cohere',
|
|
38
|
+
'groq',
|
|
39
|
+
'xai',
|
|
40
|
+
'grok',
|
|
41
|
+
'perplexity',
|
|
42
|
+
'ollama',
|
|
43
|
+
]);
|
|
44
|
+
const PROVIDER_MODEL_HARDCODING_MODEL_FAMILIES = Object.freeze([
|
|
45
|
+
String.raw`claude(?:-[a-z0-9][a-z0-9._-]*)+`,
|
|
46
|
+
String.raw`gemini(?:[/-][a-z0-9][a-z0-9._-]*)+`,
|
|
47
|
+
String.raw`gpt-(?:[a-z0-9][a-z0-9._-]*)?`,
|
|
48
|
+
String.raw`mistral(?:-[a-z0-9][a-z0-9._-]*)*`,
|
|
49
|
+
String.raw`llama(?:[-.][a-z0-9][a-z0-9._-]*|[0-9][a-z0-9._-]*)?`,
|
|
50
|
+
String.raw`deepseek(?:-[a-z0-9][a-z0-9._-]*)*`,
|
|
51
|
+
String.raw`qwen(?:[-.][a-z0-9][a-z0-9._-]*|[0-9][a-z0-9._-]*)?`,
|
|
52
|
+
String.raw`cohere(?:-[a-z0-9][a-z0-9._-]*)*`,
|
|
53
|
+
String.raw`groq(?:-[a-z0-9][a-z0-9._-]*)*`,
|
|
54
|
+
String.raw`xai(?:-[a-z0-9][a-z0-9._-]*)*`,
|
|
55
|
+
String.raw`grok(?:-[a-z0-9][a-z0-9._-]*)*`,
|
|
56
|
+
String.raw`perplexity(?:-[a-z0-9][a-z0-9._-]*)*`,
|
|
57
|
+
String.raw`ollama(?:-[a-z0-9][a-z0-9._-]*)*`,
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
function resolveTargetDir(cwd, options = {}) {
|
|
61
|
+
return path.resolve(cwd, String(options.dir || '').trim() || '.');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readJsonFile(filePath) {
|
|
65
|
+
try {
|
|
66
|
+
return JSON.parse(readFileSync(filePath, 'utf8'));
|
|
67
|
+
} catch (error) {
|
|
68
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
69
|
+
throw new Error(`Invalid scaffold lock: ${filePath}: ${message}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function readLock(targetDir) {
|
|
74
|
+
const lockPath = path.join(targetDir, SCAFFOLD_LOCK_PATH);
|
|
75
|
+
if (!existsSync(lockPath)) {
|
|
76
|
+
throw new Error(`Missing scaffold lock: ${SCAFFOLD_LOCK_PATH}`);
|
|
77
|
+
}
|
|
78
|
+
return readJsonFile(lockPath);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function assertSupportedLock(lock) {
|
|
82
|
+
if (lock?.scaffoldVersion !== SCAFFOLD_VERSION) {
|
|
83
|
+
throw new Error(`Unsupported scaffold version: ${String(lock?.scaffoldVersion || 'missing')}`);
|
|
84
|
+
}
|
|
85
|
+
if (!SUPPORTED_APP_SCAFFOLD_PROFILES.includes(lock?.profile)) {
|
|
86
|
+
throw new Error(`Unsupported scaffold profile: ${String(lock?.profile || 'missing')}`);
|
|
87
|
+
}
|
|
88
|
+
if (!lock.appId || !lock.appTitle) {
|
|
89
|
+
throw new Error('Scaffold lock missing app identity');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function sortedObject(value) {
|
|
94
|
+
if (Array.isArray(value)) {
|
|
95
|
+
return value.map(sortedObject);
|
|
96
|
+
}
|
|
97
|
+
if (!value || typeof value !== 'object') {
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
return Object.fromEntries(
|
|
101
|
+
Object.keys(value)
|
|
102
|
+
.sort((left, right) => left.localeCompare(right))
|
|
103
|
+
.map((key) => [key, sortedObject(value[key])]),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function stableStringify(value) {
|
|
108
|
+
return JSON.stringify(sortedObject(value));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function assertSameJson(actual, expected, label) {
|
|
112
|
+
if (stableStringify(actual) !== stableStringify(expected)) {
|
|
113
|
+
throw new Error(`${label} does not match current scaffold generator`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function expectedSnapshotFromLock(lock, versions) {
|
|
118
|
+
assertSupportedLock(lock);
|
|
119
|
+
return buildAppScaffoldSnapshot({
|
|
120
|
+
profile: lock.profile,
|
|
121
|
+
versions,
|
|
122
|
+
appId: lock.appId,
|
|
123
|
+
appTitle: lock.appTitle,
|
|
124
|
+
packageName: lock.packageName,
|
|
125
|
+
author: lock.packageAuthor || '',
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function readContentForHash(filePath) {
|
|
130
|
+
return readFileSync(filePath);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function ensureLockMatchesCurrentGenerator(lock, snapshot) {
|
|
134
|
+
assertSameJson(lock.appIdentity, snapshot.lock.appIdentity, 'App identity');
|
|
135
|
+
assertSameJson(lock.packageName, snapshot.lock.packageName, 'Package name');
|
|
136
|
+
assertSameJson(lock.packageAuthor, snapshot.lock.packageAuthor, 'Package author');
|
|
137
|
+
assertSameJson(lock.cargoPackageName, snapshot.lock.cargoPackageName, 'Cargo package name');
|
|
138
|
+
assertSameJson(lock.tauriIdentifier, snapshot.lock.tauriIdentifier, 'Tauri identifier');
|
|
139
|
+
assertSameJson(lock.managedFileTaxonomy, snapshot.lock.managedFileTaxonomy, 'Managed file taxonomy');
|
|
140
|
+
assertSameJson(lock.dependencyMatrix, snapshot.lock.dependencyMatrix, 'Dependency matrix');
|
|
141
|
+
assertSameJson(lock.managedFileHashes, snapshot.lock.managedFileHashes, 'Managed file hashes');
|
|
142
|
+
assertSameJson(lock.appOwnedInitialHashes, snapshot.lock.appOwnedInitialHashes, 'App-owned taxonomy hashes');
|
|
143
|
+
assertSameJson(lock.semantics, snapshot.lock.semantics, 'Scaffold semantics');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function collectTextFiles(rootDir) {
|
|
147
|
+
const results = [];
|
|
148
|
+
const walk = (currentDir) => {
|
|
149
|
+
for (const entry of readdirSync(currentDir, { withFileTypes: true })) {
|
|
150
|
+
if (SCAN_EXCLUDED_DIRS.has(entry.name)) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
154
|
+
if (entry.isDirectory()) {
|
|
155
|
+
walk(fullPath);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (!entry.isFile()) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const stat = statSync(fullPath);
|
|
162
|
+
if (stat.size > 1024 * 1024) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (!TEXT_EXTENSIONS.has(path.extname(entry.name))) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
results.push(fullPath);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
walk(rootDir);
|
|
172
|
+
return results;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function escapeRegExp(value) {
|
|
176
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function buildProviderModelHardcodingPattern() {
|
|
180
|
+
const providerAlternation = PROVIDER_MODEL_HARDCODING_PROVIDERS.map(escapeRegExp).join('|');
|
|
181
|
+
const modelAlternation = PROVIDER_MODEL_HARDCODING_MODEL_FAMILIES.join('|');
|
|
182
|
+
const providerOrModelAlternation = `(?:${providerAlternation}|${modelAlternation})`;
|
|
183
|
+
const quote = `['"\`]`;
|
|
184
|
+
const tokenStart = String.raw`(?:^|[^A-Za-z0-9_@./-])`;
|
|
185
|
+
const tokenEnd = String.raw`(?=$|[^A-Za-z0-9_@./-])`;
|
|
186
|
+
const keyedLiteral = String.raw`\b(?:provider|providerId|providerName|model|modelId|modelName|defaultModel|selectedModel)\b\s*[:=]\s*${quote}${providerOrModelAlternation}${quote}`;
|
|
187
|
+
const slashQualifiedLiteral = `${tokenStart}(?:${providerAlternation})/(?:${modelAlternation}|[a-z0-9][a-z0-9._-]*)${tokenEnd}`;
|
|
188
|
+
const bareProviderOrModelLiteral = `${tokenStart}${providerOrModelAlternation}${tokenEnd}`;
|
|
189
|
+
return new RegExp(`(?:${keyedLiteral}|${slashQualifiedLiteral}|${bareProviderOrModelLiteral})`, 'i');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function buildForbiddenPatterns() {
|
|
193
|
+
const runtimeBridgeName = ['runtime', 'bridge', 'plugin'].join('_');
|
|
194
|
+
const retiredFlag = ['--', 'template'].join('');
|
|
195
|
+
const modelTestImport = ['kit', 'features', 'model-test'].join('/');
|
|
196
|
+
const retiredProfiles = ['basic', ['vercel', 'ai'].join('-')];
|
|
197
|
+
const retiredBuilders = [
|
|
198
|
+
['build', 'Basic', 'App', 'Template'].join(''),
|
|
199
|
+
['build', 'VercelAI', 'App', 'Template'].join(''),
|
|
200
|
+
].join('|');
|
|
201
|
+
return [
|
|
202
|
+
['generic platform auth helper in scaffold', /createPlatformClient\s*\(/],
|
|
203
|
+
['first-party helper in third-party scaffold', /createLocalFirstPartyRuntimePlatformClient/],
|
|
204
|
+
['Realm login bypass endpoint', /\/api\/auth\/login/],
|
|
205
|
+
['Realm refresh bypass endpoint', /\/api\/auth\/refresh/],
|
|
206
|
+
['app-owned session store', /sessionStore/],
|
|
207
|
+
['app-owned refresh token provider', /refreshTokenProvider/],
|
|
208
|
+
['raw bearer token custody wording', /raw JWT/i],
|
|
209
|
+
['model-test import', new RegExp(modelTestImport.replace('/', '\\/'))],
|
|
210
|
+
['stale Runtime shell API', new RegExp(runtimeBridgeName)],
|
|
211
|
+
['retired template flag', new RegExp(retiredFlag.replace('-', '\\-'))],
|
|
212
|
+
['retired template builder', new RegExp(retiredBuilders)],
|
|
213
|
+
['retired profile switch branch', new RegExp(`case\\s+['"](?:${retiredProfiles.join('|')})['"]`)],
|
|
214
|
+
['retired submitted profile', new RegExp(`(?:profile|template):\\s*(?:${retiredProfiles.join('|')})\\b`)],
|
|
215
|
+
['retired vercel profile remnant', new RegExp(retiredProfiles[1])],
|
|
216
|
+
['provider/model hardcoding', buildProviderModelHardcodingPattern()],
|
|
217
|
+
['local audit wording promoted to admission', /local[\s-]+audit[\s-]+as[\s-]+admission/i],
|
|
218
|
+
['manifest wording promoted to grant', /manifest[\s-]+as[\s-]+grant/i],
|
|
219
|
+
['installed-app update claim', /installed[\s-]+app[\s-]+update[\s-]+truth:\s*(?!not-generated)/i],
|
|
220
|
+
];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function scanForbiddenPatterns(targetDir) {
|
|
224
|
+
const findings = [];
|
|
225
|
+
const patterns = buildForbiddenPatterns();
|
|
226
|
+
for (const filePath of collectTextFiles(targetDir)) {
|
|
227
|
+
const relativePath = path.relative(targetDir, filePath).split(path.sep).join('/');
|
|
228
|
+
const text = readFileSync(filePath, 'utf8');
|
|
229
|
+
for (const [label, pattern] of patterns) {
|
|
230
|
+
if (pattern.test(text)) {
|
|
231
|
+
findings.push(`${relativePath}: ${label}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return findings;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function readYamlScalar(text, key) {
|
|
239
|
+
const match = text.match(new RegExp(`^${key}:\\s*(.+?)\\s*$`, 'm'));
|
|
240
|
+
return match ? match[1].trim() : '';
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function assertRequiredSupportFiles(targetDir, snapshot) {
|
|
244
|
+
const missing = [];
|
|
245
|
+
for (const file of snapshot.files) {
|
|
246
|
+
if (!existsSync(path.join(targetDir, file.path))) {
|
|
247
|
+
missing.push(file.path);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (missing.length > 0) {
|
|
251
|
+
throw new Error(`Missing required scaffold support files: ${missing.join(', ')}`);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const tauriConfig = JSON.parse(readFileSync(path.join(targetDir, 'src-tauri/tauri.conf.json'), 'utf8'));
|
|
255
|
+
const icons = tauriConfig?.bundle?.icon;
|
|
256
|
+
if (!Array.isArray(icons) || icons.join(',') !== 'icons/icon.png') {
|
|
257
|
+
throw new Error('Tauri icon support drift: bundle.icon must be ["icons/icon.png"]');
|
|
258
|
+
}
|
|
259
|
+
const icon = readFileSync(path.join(targetDir, 'src-tauri/icons/icon.png'));
|
|
260
|
+
const pngSignature = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
|
261
|
+
if (icon.length !== 68 || pngSignature.some((value, index) => icon[index] !== value)) {
|
|
262
|
+
throw new Error('Tauri icon support drift: src-tauri/icons/icon.png is not the scaffold PNG');
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function assertSemanticMarkers(targetDir) {
|
|
267
|
+
const manifest = readFileSync(path.join(targetDir, 'nimi.app.yaml'), 'utf8');
|
|
268
|
+
if (!manifest.includes('manifest_role: submitted-input')) {
|
|
269
|
+
throw new Error('Submitted manifest marker missing');
|
|
270
|
+
}
|
|
271
|
+
if (/admitted|descriptor_role:\s*release|grant(ed)?_permissions/i.test(manifest)) {
|
|
272
|
+
throw new Error('Submitted manifest contains admission or grant wording');
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const buildProfile = readFileSync(path.join(targetDir, '.nimi/config/build-profile.yaml'), 'utf8');
|
|
276
|
+
for (const marker of [
|
|
277
|
+
'build_profile_ref:',
|
|
278
|
+
'toolchain_version:',
|
|
279
|
+
'build_command:',
|
|
280
|
+
'output_path:',
|
|
281
|
+
'lockfile_path:',
|
|
282
|
+
'lockfile_policy:',
|
|
283
|
+
'ci_install_command:',
|
|
284
|
+
'profile_role: developer-workflow-input',
|
|
285
|
+
]) {
|
|
286
|
+
if (!buildProfile.includes(marker)) {
|
|
287
|
+
throw new Error(`Build profile marker missing: ${marker}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (/checksum-pinned|ordinary-user install|direct npm install|direct npx|direct clone/i.test(buildProfile)) {
|
|
291
|
+
throw new Error('Build profile contains stale install or admission wording');
|
|
292
|
+
}
|
|
293
|
+
const ciWorkflow = readFileSync(path.join(targetDir, '.github/workflows/ci.yml'), 'utf8');
|
|
294
|
+
const lockfilePath = readYamlScalar(buildProfile, 'lockfile_path');
|
|
295
|
+
const lockfilePolicy = readYamlScalar(buildProfile, 'lockfile_policy');
|
|
296
|
+
const ciInstallCommand = readYamlScalar(buildProfile, 'ci_install_command');
|
|
297
|
+
if (lockfilePath !== 'pnpm-lock.yaml') {
|
|
298
|
+
throw new Error(`Build profile lockfile path must be pnpm-lock.yaml: ${lockfilePath}`);
|
|
299
|
+
}
|
|
300
|
+
if (lockfilePolicy !== 'author-install-generates-lockfile') {
|
|
301
|
+
throw new Error(`Build profile lockfile policy mismatch: ${lockfilePolicy}`);
|
|
302
|
+
}
|
|
303
|
+
if (!ciWorkflow.includes(`- run: ${ciInstallCommand}`)) {
|
|
304
|
+
throw new Error('CI install command does not match build profile');
|
|
305
|
+
}
|
|
306
|
+
const lockfileExists = existsSync(path.join(targetDir, lockfilePath));
|
|
307
|
+
if (!lockfileExists && /--frozen-lockfile/.test(ciWorkflow)) {
|
|
308
|
+
throw new Error(`CI uses frozen lockfile install but lockfile is missing: ${lockfilePath}`);
|
|
309
|
+
}
|
|
310
|
+
if (!lockfileExists && /^\s*cache:\s*pnpm\s*$/m.test(ciWorkflow)) {
|
|
311
|
+
throw new Error(`CI enables pnpm cache before lockfile exists: ${lockfilePath}`);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const appIdentity = readFileSync(path.join(targetDir, '.nimi/config/app-identity.yaml'), 'utf8');
|
|
315
|
+
for (const marker of [
|
|
316
|
+
'app_id:',
|
|
317
|
+
'npm_package_name:',
|
|
318
|
+
'cargo_package_name:',
|
|
319
|
+
'tauri_identifier:',
|
|
320
|
+
'identity_role: scaffold-generated-authoring-input',
|
|
321
|
+
]) {
|
|
322
|
+
if (!appIdentity.includes(marker)) {
|
|
323
|
+
throw new Error(`App identity marker missing: ${marker}`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const boundary = readFileSync(path.join(targetDir, '.nimi/contracts/scaffold-boundary.yaml'), 'utf8');
|
|
328
|
+
for (const marker of [
|
|
329
|
+
'public_admission_truth: not-generated',
|
|
330
|
+
'local_audit_role: pre-submission-self-check',
|
|
331
|
+
'permission_declarations: transparency-input-only',
|
|
332
|
+
]) {
|
|
333
|
+
if (!boundary.includes(marker)) {
|
|
334
|
+
throw new Error(`Scaffold boundary marker missing: ${marker}`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const agents = readFileSync(path.join(targetDir, 'AGENTS.md'), 'utf8');
|
|
339
|
+
for (const marker of [
|
|
340
|
+
'host-local scaffold truth',
|
|
341
|
+
'scaffold-managed files',
|
|
342
|
+
'app-owned area',
|
|
343
|
+
'pre-submission self-checks only',
|
|
344
|
+
]) {
|
|
345
|
+
if (!agents.includes(marker)) {
|
|
346
|
+
throw new Error(`AGENTS.md boundary text missing: ${marker}`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function assertManagedFilesCurrent(targetDir, lock) {
|
|
352
|
+
const drift = [];
|
|
353
|
+
for (const [relativePath, entry] of Object.entries(lock.managedFileHashes || {})) {
|
|
354
|
+
const absolutePath = path.join(targetDir, relativePath);
|
|
355
|
+
if (!existsSync(absolutePath)) {
|
|
356
|
+
drift.push(`${relativePath}: missing`);
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
const currentHash = hashScaffoldContent(readContentForHash(absolutePath));
|
|
360
|
+
if (currentHash !== entry.sha256) {
|
|
361
|
+
drift.push(`${relativePath}: sha256 drift`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (drift.length > 0) {
|
|
365
|
+
throw new Error(`Managed scaffold drift detected: ${drift.join('; ')}`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function validateDoctorState(targetDir, versions) {
|
|
370
|
+
const lock = readLock(targetDir);
|
|
371
|
+
const snapshot = expectedSnapshotFromLock(lock, versions);
|
|
372
|
+
ensureLockMatchesCurrentGenerator(lock, snapshot);
|
|
373
|
+
assertRequiredSupportFiles(targetDir, snapshot);
|
|
374
|
+
assertSemanticMarkers(targetDir);
|
|
375
|
+
assertManagedFilesCurrent(targetDir, lock);
|
|
376
|
+
const forbiddenFindings = scanForbiddenPatterns(targetDir);
|
|
377
|
+
if (forbiddenFindings.length > 0) {
|
|
378
|
+
throw new Error(`Forbidden scaffold remnants detected: ${forbiddenFindings.join('; ')}`);
|
|
379
|
+
}
|
|
380
|
+
return {
|
|
381
|
+
targetDir,
|
|
382
|
+
lock,
|
|
383
|
+
snapshot,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export function doctorApp(cwd, options = {}, versions) {
|
|
388
|
+
const targetDir = resolveTargetDir(cwd, options);
|
|
389
|
+
const result = validateDoctorState(targetDir, versions);
|
|
390
|
+
const payload = {
|
|
391
|
+
ok: true,
|
|
392
|
+
command: 'doctor',
|
|
393
|
+
dir: targetDir,
|
|
394
|
+
scaffoldVersion: result.lock.scaffoldVersion,
|
|
395
|
+
profile: result.lock.profile,
|
|
396
|
+
appId: result.lock.appId,
|
|
397
|
+
checkedManagedFiles: Object.keys(result.lock.managedFileHashes || {}).length,
|
|
398
|
+
};
|
|
399
|
+
if (options.json) {
|
|
400
|
+
process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
|
|
401
|
+
} else {
|
|
402
|
+
process.stdout.write(`[nimi-app] doctor passed for ${targetDir}\n`);
|
|
403
|
+
}
|
|
404
|
+
return payload;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function collectExpectedClasses(snapshot) {
|
|
408
|
+
const entries = new Map();
|
|
409
|
+
for (const [relativePath, entry] of Object.entries(snapshot.lock.managedFileHashes)) {
|
|
410
|
+
entries.set(relativePath, entry.class);
|
|
411
|
+
}
|
|
412
|
+
for (const [relativePath, entry] of Object.entries(snapshot.lock.appOwnedInitialHashes)) {
|
|
413
|
+
entries.set(relativePath, entry.class);
|
|
414
|
+
}
|
|
415
|
+
return entries;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function assertNoClassificationConflict(lock, snapshot) {
|
|
419
|
+
const currentClasses = collectExpectedClasses(snapshot);
|
|
420
|
+
for (const [relativePath, entry] of Object.entries(lock.managedFileHashes || {})) {
|
|
421
|
+
if (currentClasses.get(relativePath) !== entry.class) {
|
|
422
|
+
throw new Error(`Scaffold classification conflict: ${relativePath}`);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
for (const [relativePath, entry] of Object.entries(lock.appOwnedInitialHashes || {})) {
|
|
426
|
+
if (currentClasses.get(relativePath) !== entry.class) {
|
|
427
|
+
throw new Error(`Scaffold classification conflict: ${relativePath}`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function writeScaffoldFile(targetDir, file) {
|
|
433
|
+
const targetPath = path.join(targetDir, file.path);
|
|
434
|
+
mkdirSync(path.dirname(targetPath), { recursive: true });
|
|
435
|
+
writeFileSync(targetPath, file.content);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export function updateApp(cwd, options = {}, versions) {
|
|
439
|
+
const targetDir = resolveTargetDir(cwd, options);
|
|
440
|
+
const lock = readLock(targetDir);
|
|
441
|
+
const snapshot = expectedSnapshotFromLock(lock, versions);
|
|
442
|
+
assertNoClassificationConflict(lock, snapshot);
|
|
443
|
+
|
|
444
|
+
for (const file of snapshot.filesWithoutLock) {
|
|
445
|
+
const managedEntry = snapshot.lock.managedFileHashes[file.path];
|
|
446
|
+
if (!managedEntry) {
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
writeScaffoldFile(targetDir, file);
|
|
450
|
+
}
|
|
451
|
+
writeScaffoldFile(targetDir, {
|
|
452
|
+
path: SCAFFOLD_LOCK_PATH,
|
|
453
|
+
content: `${JSON.stringify(snapshot.lock, null, 2)}\n`,
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
validateDoctorState(targetDir, versions);
|
|
457
|
+
const payload = {
|
|
458
|
+
ok: true,
|
|
459
|
+
command: 'update',
|
|
460
|
+
dir: targetDir,
|
|
461
|
+
scaffoldVersion: snapshot.lock.scaffoldVersion,
|
|
462
|
+
profile: snapshot.lock.profile,
|
|
463
|
+
appId: snapshot.lock.appId,
|
|
464
|
+
refreshedManagedFiles: Object.keys(snapshot.lock.managedFileHashes).length,
|
|
465
|
+
};
|
|
466
|
+
if (options.json) {
|
|
467
|
+
process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
|
|
468
|
+
} else {
|
|
469
|
+
process.stdout.write(`[nimi-app] update refreshed managed scaffold files at ${targetDir}\n`);
|
|
470
|
+
}
|
|
471
|
+
return payload;
|
|
472
|
+
}
|