obsidian-plugin-config 1.4.4 ā 1.4.6
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/.injection-info.json +5 -1
- package/.vscode/tasks.json +116 -116
- package/bin/obsidian-inject.js +1 -1
- package/package.json +3 -1
- package/scripts/acp.ts +64 -56
- package/scripts/build-npm.ts +152 -135
- package/scripts/esbuild.config.ts +221 -196
- package/scripts/inject-core.ts +613 -619
- package/scripts/inject-path.ts +144 -130
- package/scripts/inject-prompt.ts +85 -78
- package/scripts/sync-template-deps.ts +44 -44
- package/scripts/update-version-config.ts +117 -98
- package/scripts/utils.ts +121 -118
- package/templates/.vscode/settings.json +9 -9
- package/templates/.vscode/tasks.json +53 -53
- package/templates/package.json +3 -1
- package/templates/scripts/acp.ts +64 -56
- package/templates/scripts/esbuild.config.ts +268 -232
- package/templates/scripts/release.ts +78 -79
- package/templates/scripts/update-version.ts +129 -106
- package/templates/scripts/utils.ts +129 -126
- package/tsconfig.json +30 -41
- package/versions.json +3 -1
package/scripts/inject-core.ts
CHANGED
|
@@ -1,731 +1,725 @@
|
|
|
1
1
|
#!/usr/bin/env tsx
|
|
2
2
|
|
|
3
|
-
import fs from
|
|
4
|
-
import path from
|
|
5
|
-
import { execSync } from
|
|
6
|
-
import { fileURLToPath } from
|
|
7
|
-
import { isValidPath, gitExec } from
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { execSync } from 'child_process';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
import { isValidPath, gitExec } from './utils.js';
|
|
8
8
|
|
|
9
9
|
export interface InjectionPlan {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
targetPath: string;
|
|
11
|
+
isObsidianPlugin: boolean;
|
|
12
|
+
hasPackageJson: boolean;
|
|
13
|
+
hasManifest: boolean;
|
|
14
|
+
hasScriptsFolder: boolean;
|
|
15
|
+
currentDependencies: string[];
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Analyze the target plugin directory
|
|
20
20
|
*/
|
|
21
21
|
export async function analyzePlugin(pluginPath: string): Promise<InjectionPlan> {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
22
|
+
const packageJsonPath = path.join(pluginPath, 'package.json');
|
|
23
|
+
const manifestPath = path.join(pluginPath, 'manifest.json');
|
|
24
|
+
const scriptsPath = path.join(pluginPath, 'scripts');
|
|
25
|
+
|
|
26
|
+
const plan: InjectionPlan = {
|
|
27
|
+
targetPath: pluginPath,
|
|
28
|
+
isObsidianPlugin: false,
|
|
29
|
+
hasPackageJson: await isValidPath(packageJsonPath),
|
|
30
|
+
hasManifest: await isValidPath(manifestPath),
|
|
31
|
+
hasScriptsFolder: await isValidPath(scriptsPath),
|
|
32
|
+
currentDependencies: []
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if (plan.hasManifest) {
|
|
36
|
+
try {
|
|
37
|
+
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
|
38
|
+
plan.isObsidianPlugin = !!(manifest.id && manifest.name && manifest.version);
|
|
39
|
+
} catch {
|
|
40
|
+
console.warn('Warning: Could not parse manifest.json');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (plan.hasPackageJson) {
|
|
45
|
+
try {
|
|
46
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
47
|
+
plan.currentDependencies = [
|
|
48
|
+
...Object.keys(packageJson.dependencies || {}),
|
|
49
|
+
...Object.keys(packageJson.devDependencies || {})
|
|
50
|
+
];
|
|
51
|
+
} catch {
|
|
52
|
+
console.warn('Warning: Could not parse package.json');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return plan;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
60
|
* Find plugin-config root directory (handles NPM global installs)
|
|
61
61
|
*/
|
|
62
62
|
export function findPluginConfigRoot(): string {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
63
|
+
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
64
|
+
const npmPackageRoot = path.resolve(scriptDir, '..');
|
|
65
|
+
const npmPackageJson = path.join(npmPackageRoot, 'package.json');
|
|
66
|
+
|
|
67
|
+
if (fs.existsSync(npmPackageJson)) {
|
|
68
|
+
try {
|
|
69
|
+
const packageContent = JSON.parse(fs.readFileSync(npmPackageJson, 'utf8'));
|
|
70
|
+
if (packageContent.name === 'obsidian-plugin-config') {
|
|
71
|
+
return npmPackageRoot;
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
// Ignore parsing errors
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return process.cwd();
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
82
|
* Copy file content from local plugin-config directory
|
|
83
83
|
*/
|
|
84
84
|
export function copyFromLocal(filePath: string): string {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
85
|
+
const configRoot = findPluginConfigRoot();
|
|
86
|
+
const sourcePath = path.join(configRoot, filePath);
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
return fs.readFileSync(sourcePath, 'utf8');
|
|
90
|
+
} catch (error) {
|
|
91
|
+
throw new Error(`Failed to copy ${filePath}: ${error}`);
|
|
92
|
+
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
96
|
* Check if plugin-config repo is clean and commit if needed
|
|
97
97
|
*/
|
|
98
98
|
export async function ensurePluginConfigClean(): Promise<void> {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
"git rev-parse --abbrev-ref HEAD",
|
|
148
|
-
{ encoding: "utf8" }
|
|
149
|
-
).trim();
|
|
150
|
-
gitExec(
|
|
151
|
-
`git push --set-upstream origin ${branch}`
|
|
152
|
-
);
|
|
153
|
-
console.log(
|
|
154
|
-
`ā
New branch pushed with upstream`
|
|
155
|
-
);
|
|
156
|
-
} catch {
|
|
157
|
-
console.log(
|
|
158
|
-
`ā ļø Committed locally, push failed`
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
} else {
|
|
163
|
-
console.log(`ā
Plugin-config repo is clean`);
|
|
164
|
-
}
|
|
165
|
-
} finally {
|
|
166
|
-
process.chdir(originalCwd);
|
|
167
|
-
}
|
|
99
|
+
const configRoot = findPluginConfigRoot();
|
|
100
|
+
const gitDir = path.join(configRoot, '.git');
|
|
101
|
+
|
|
102
|
+
// Skip git check if not a git repo
|
|
103
|
+
// (e.g. NPM global install)
|
|
104
|
+
if (!fs.existsSync(gitDir)) {
|
|
105
|
+
console.log(`ā
Plugin-config repo is clean` + ` (NPM install, no git check)`);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const originalCwd = process.cwd();
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
process.chdir(configRoot);
|
|
113
|
+
const gitStatus = execSync('git status --porcelain', { encoding: 'utf8' }).trim();
|
|
114
|
+
|
|
115
|
+
if (gitStatus) {
|
|
116
|
+
console.log(`\nā ļø Plugin-config has uncommitted changes:`);
|
|
117
|
+
console.log(gitStatus);
|
|
118
|
+
console.log(`\nš§ Auto-committing changes...`);
|
|
119
|
+
|
|
120
|
+
const msg = 'š§ Update plugin-config templates';
|
|
121
|
+
gitExec('git add -A');
|
|
122
|
+
gitExec(`git commit -m "${msg}"`);
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
const branch = execSync('git rev-parse --abbrev-ref HEAD', {
|
|
126
|
+
encoding: 'utf8'
|
|
127
|
+
}).trim();
|
|
128
|
+
gitExec(`git push origin ${branch}`);
|
|
129
|
+
console.log(`ā
Changes committed and pushed`);
|
|
130
|
+
} catch {
|
|
131
|
+
try {
|
|
132
|
+
const branch = execSync('git rev-parse --abbrev-ref HEAD', {
|
|
133
|
+
encoding: 'utf8'
|
|
134
|
+
}).trim();
|
|
135
|
+
gitExec(`git push --set-upstream origin ${branch}`);
|
|
136
|
+
console.log(`ā
New branch pushed with upstream`);
|
|
137
|
+
} catch {
|
|
138
|
+
console.log(`ā ļø Committed locally, push failed`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
console.log(`ā
Plugin-config repo is clean`);
|
|
143
|
+
}
|
|
144
|
+
} finally {
|
|
145
|
+
process.chdir(originalCwd);
|
|
146
|
+
}
|
|
168
147
|
}
|
|
169
148
|
|
|
170
149
|
/**
|
|
171
150
|
* Display injection plan and ask for confirmation
|
|
172
151
|
*/
|
|
173
152
|
export async function showInjectionPlan(
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
153
|
+
plan: InjectionPlan,
|
|
154
|
+
autoConfirm: boolean = false,
|
|
155
|
+
useSass: boolean = false
|
|
177
156
|
): Promise<boolean> {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
157
|
+
const { askConfirmation, createReadlineInterface } = await import('./utils.js');
|
|
158
|
+
const rl = createReadlineInterface();
|
|
159
|
+
|
|
160
|
+
console.log(`\nšÆ Injection Plan for: ${plan.targetPath}`);
|
|
161
|
+
console.log(`š Target: ${path.basename(plan.targetPath)}`);
|
|
162
|
+
console.log(`š¦ Package.json: ${plan.hasPackageJson ? 'ā
' : 'ā'}`);
|
|
163
|
+
console.log(`š Manifest.json: ${plan.hasManifest ? 'ā
' : 'ā'}`);
|
|
164
|
+
console.log(
|
|
165
|
+
`š Scripts folder: ${plan.hasScriptsFolder ? 'ā
(will be updated)' : 'ā (will be created)'}`
|
|
166
|
+
);
|
|
167
|
+
console.log(`š Obsidian plugin: ${plan.isObsidianPlugin ? 'ā
' : 'ā'}`);
|
|
168
|
+
console.log(
|
|
169
|
+
`šØ SASS support: ${useSass ? 'ā
(esbuild-sass-plugin will be added)' : 'ā'}`
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
if (!plan.isObsidianPlugin) {
|
|
173
|
+
console.log(`\nā ļø Warning: This doesn't appear to be a valid Obsidian plugin`);
|
|
174
|
+
console.log(` Missing manifest.json or invalid structure`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
console.log(`\nš Will inject:`);
|
|
178
|
+
console.log(` ā
Local scripts (utils.ts, esbuild.config.ts, acp.ts, etc.)`);
|
|
179
|
+
console.log(` ā
Updated package.json scripts`);
|
|
180
|
+
console.log(` ā
Required dependencies`);
|
|
181
|
+
console.log(` š Analyze centralized imports (manual commenting may be needed)`);
|
|
182
|
+
|
|
183
|
+
if (autoConfirm) {
|
|
184
|
+
console.log(`\nā
Auto-confirming injection...`);
|
|
185
|
+
rl.close();
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const result = await askConfirmation(`\nProceed with injection?`, rl);
|
|
190
|
+
rl.close();
|
|
191
|
+
return result;
|
|
213
192
|
}
|
|
214
193
|
|
|
215
194
|
/**
|
|
216
195
|
* Clean old script files
|
|
217
196
|
*/
|
|
218
197
|
export async function cleanOldScripts(scriptsPath: string): Promise<void> {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
198
|
+
const scriptNames = [
|
|
199
|
+
'utils',
|
|
200
|
+
'esbuild.config',
|
|
201
|
+
'acp',
|
|
202
|
+
'update-version',
|
|
203
|
+
'release',
|
|
204
|
+
'help'
|
|
205
|
+
];
|
|
206
|
+
const extensions = ['.ts', '.mts', '.js', '.mjs'];
|
|
207
|
+
|
|
208
|
+
for (const scriptName of scriptNames) {
|
|
209
|
+
for (const ext of extensions) {
|
|
210
|
+
const scriptFile = path.join(scriptsPath, `${scriptName}${ext}`);
|
|
211
|
+
if (await isValidPath(scriptFile)) {
|
|
212
|
+
fs.unlinkSync(scriptFile);
|
|
213
|
+
console.log(
|
|
214
|
+
`šļø Removed existing ${scriptName}${ext} (will be replaced)`
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const obsoleteRootFiles = ['help-plugin.ts'];
|
|
221
|
+
for (const fileName of obsoleteRootFiles) {
|
|
222
|
+
const filePath = path.join(path.dirname(scriptsPath), fileName);
|
|
223
|
+
if (await isValidPath(filePath)) {
|
|
224
|
+
fs.unlinkSync(filePath);
|
|
225
|
+
console.log(`šļø Removed obsolete root file: ${fileName}`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const obsoleteFiles = ['start.mjs', 'start.js'];
|
|
230
|
+
for (const fileName of obsoleteFiles) {
|
|
231
|
+
const filePath = path.join(scriptsPath, fileName);
|
|
232
|
+
if (await isValidPath(filePath)) {
|
|
233
|
+
fs.unlinkSync(filePath);
|
|
234
|
+
console.log(`šļø Removed obsolete file: ${fileName}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
249
237
|
}
|
|
250
238
|
|
|
251
239
|
/**
|
|
252
240
|
* Clean old ESLint config files
|
|
253
241
|
*/
|
|
254
242
|
export async function cleanOldLintFiles(targetPath: string): Promise<void> {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
243
|
+
const oldLintFiles = ['.eslintrc', '.eslintrc.js', '.eslintrc.json', '.eslintignore'];
|
|
244
|
+
const conflictingLintFiles = [
|
|
245
|
+
'eslint.config.ts',
|
|
246
|
+
'eslint.config.cjs',
|
|
247
|
+
'eslint.config.js',
|
|
248
|
+
'eslint.config.mjs'
|
|
249
|
+
];
|
|
250
|
+
|
|
251
|
+
for (const fileName of oldLintFiles) {
|
|
252
|
+
const filePath = path.join(targetPath, fileName);
|
|
253
|
+
if (await isValidPath(filePath)) {
|
|
254
|
+
fs.unlinkSync(filePath);
|
|
255
|
+
console.log(
|
|
256
|
+
`šļø Removed old ESLint file: ${fileName} (replaced by eslint.config.ts)`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
for (const fileName of conflictingLintFiles) {
|
|
262
|
+
const filePath = path.join(targetPath, fileName);
|
|
263
|
+
if (await isValidPath(filePath)) {
|
|
264
|
+
fs.unlinkSync(filePath);
|
|
265
|
+
console.log(
|
|
266
|
+
`šļø Removed existing ESLint file: ${fileName} (will be replaced by injection)`
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
280
270
|
}
|
|
281
271
|
|
|
282
272
|
/**
|
|
283
273
|
* Inject scripts and config files
|
|
284
274
|
*/
|
|
285
275
|
export async function injectScripts(targetPath: string): Promise<void> {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
console.log(`\nš„ Copying GitHub workflows from local files...`);
|
|
424
|
-
|
|
425
|
-
for (const workflowFile of workflowFiles) {
|
|
426
|
-
try {
|
|
427
|
-
const content = copyFromLocal(workflowFile);
|
|
428
|
-
const relativePath = workflowFile.replace("templates/", "");
|
|
429
|
-
const targetFile = path.join(targetPath, relativePath);
|
|
430
|
-
const targetDir = path.dirname(targetFile);
|
|
431
|
-
|
|
432
|
-
if (!await isValidPath(targetDir)) {
|
|
433
|
-
fs.mkdirSync(targetDir, { recursive: true });
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
fs.writeFileSync(targetFile, content, "utf8");
|
|
437
|
-
console.log(` ā
${relativePath}`);
|
|
438
|
-
} catch (error) {
|
|
439
|
-
console.error(` ā Failed to inject ${workflowFile}: ${error}`);
|
|
440
|
-
}
|
|
441
|
-
}
|
|
276
|
+
const scriptsPath = path.join(targetPath, 'scripts');
|
|
277
|
+
|
|
278
|
+
if (!(await isValidPath(scriptsPath))) {
|
|
279
|
+
fs.mkdirSync(scriptsPath, { recursive: true });
|
|
280
|
+
console.log(`š Created scripts directory`);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
await cleanOldScripts(scriptsPath);
|
|
284
|
+
await cleanOldLintFiles(targetPath);
|
|
285
|
+
|
|
286
|
+
const scriptFiles = [
|
|
287
|
+
'templates/scripts/utils.ts',
|
|
288
|
+
'templates/scripts/esbuild.config.ts',
|
|
289
|
+
'templates/scripts/acp.ts',
|
|
290
|
+
'templates/scripts/update-version.ts',
|
|
291
|
+
'templates/scripts/release.ts',
|
|
292
|
+
'templates/scripts/help.ts'
|
|
293
|
+
];
|
|
294
|
+
|
|
295
|
+
// Files that need value-preserving merge instead
|
|
296
|
+
// of full overwrite (user fills in their paths)
|
|
297
|
+
const mergeEnvFile = new Set(['.env']);
|
|
298
|
+
|
|
299
|
+
// Files with .template suffix (NPM excludes dotfiles)
|
|
300
|
+
// Map: { source: targetName }
|
|
301
|
+
const configFileMap: Record<string, string> = {
|
|
302
|
+
'templates/tsconfig.json': 'tsconfig.json',
|
|
303
|
+
'templates/gitignore.template': '.gitignore',
|
|
304
|
+
'templates/eslint.config.mts': 'eslint.config.mts',
|
|
305
|
+
'templates/.editorconfig': '.editorconfig',
|
|
306
|
+
'templates/.prettierrc': '.prettierrc',
|
|
307
|
+
'templates/npmrc.template': '.npmrc',
|
|
308
|
+
'templates/env.template': '.env'
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const configVscodeMap: Record<string, string> = {
|
|
312
|
+
'templates/.vscode/settings.json': '.vscode/settings.json',
|
|
313
|
+
'templates/.vscode/tasks.json': '.vscode/tasks.json'
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const workflowFiles = [
|
|
317
|
+
'templates/.github/workflows/release.yml',
|
|
318
|
+
'templates/.github/workflows/release-body.md'
|
|
319
|
+
];
|
|
320
|
+
|
|
321
|
+
console.log(`\nš„ Copying scripts from local files...`);
|
|
322
|
+
|
|
323
|
+
for (const scriptFile of scriptFiles) {
|
|
324
|
+
try {
|
|
325
|
+
const content = copyFromLocal(scriptFile);
|
|
326
|
+
const fileName = path.basename(scriptFile);
|
|
327
|
+
const targetFile = path.join(scriptsPath, fileName);
|
|
328
|
+
fs.writeFileSync(targetFile, content, 'utf8');
|
|
329
|
+
console.log(` ā
${fileName}`);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
console.error(` ā Failed to inject ${scriptFile}: ${error}`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
console.log(`\nš„ Copying config files...`);
|
|
336
|
+
|
|
337
|
+
// Copy root config files
|
|
338
|
+
for (const [src, destName] of Object.entries(configFileMap)) {
|
|
339
|
+
try {
|
|
340
|
+
const targetFile = path.join(targetPath, destName);
|
|
341
|
+
const templateContent = copyFromLocal(src);
|
|
342
|
+
|
|
343
|
+
// For .env: merge existing values into the template
|
|
344
|
+
if (mergeEnvFile.has(destName) && fs.existsSync(targetFile)) {
|
|
345
|
+
const existing = fs.readFileSync(targetFile, 'utf8');
|
|
346
|
+
// Parse existing key=value pairs
|
|
347
|
+
const existingVals: Record<string, string> = {};
|
|
348
|
+
for (const line of existing.split(/\r?\n/)) {
|
|
349
|
+
const m = line.match(/^([^#=]+)=(.*)$/);
|
|
350
|
+
if (m) existingVals[m[1].trim()] = m[2].trim();
|
|
351
|
+
}
|
|
352
|
+
// Re-write template, substituting existing values
|
|
353
|
+
const merged = templateContent
|
|
354
|
+
.split(/\r?\n/)
|
|
355
|
+
.map((line) => {
|
|
356
|
+
const m = line.match(/^([^#=]+)=(.*)$/);
|
|
357
|
+
if (m) {
|
|
358
|
+
const key = m[1].trim();
|
|
359
|
+
const val = existingVals[key] ?? m[2].trim();
|
|
360
|
+
return `${key}=${val}`;
|
|
361
|
+
}
|
|
362
|
+
return line;
|
|
363
|
+
})
|
|
364
|
+
.join('\n');
|
|
365
|
+
fs.writeFileSync(targetFile, merged, 'utf8');
|
|
366
|
+
console.log(` ā
${destName} (values preserved)`);
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
fs.writeFileSync(targetFile, templateContent, 'utf8');
|
|
371
|
+
console.log(` ā
${destName}`);
|
|
372
|
+
} catch (error) {
|
|
373
|
+
console.error(` ā Failed to inject ${destName}: ${error}`);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Copy .vscode config files
|
|
378
|
+
for (const [src, destName] of Object.entries(configVscodeMap)) {
|
|
379
|
+
try {
|
|
380
|
+
const content = copyFromLocal(src);
|
|
381
|
+
const targetFile = path.join(targetPath, destName);
|
|
382
|
+
const targetDir = path.dirname(targetFile);
|
|
383
|
+
if (!(await isValidPath(targetDir))) {
|
|
384
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
385
|
+
}
|
|
386
|
+
fs.writeFileSync(targetFile, content, 'utf8');
|
|
387
|
+
console.log(` ā
${destName}`);
|
|
388
|
+
} catch (error) {
|
|
389
|
+
console.error(` ā Failed to inject ${destName}: ${error}`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
console.log(`\nš„ Copying GitHub workflows from local files...`);
|
|
394
|
+
|
|
395
|
+
for (const workflowFile of workflowFiles) {
|
|
396
|
+
try {
|
|
397
|
+
const content = copyFromLocal(workflowFile);
|
|
398
|
+
const relativePath = workflowFile.replace('templates/', '');
|
|
399
|
+
const targetFile = path.join(targetPath, relativePath);
|
|
400
|
+
const targetDir = path.dirname(targetFile);
|
|
401
|
+
|
|
402
|
+
if (!(await isValidPath(targetDir))) {
|
|
403
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
fs.writeFileSync(targetFile, content, 'utf8');
|
|
407
|
+
console.log(` ā
${relativePath}`);
|
|
408
|
+
} catch (error) {
|
|
409
|
+
console.error(` ā Failed to inject ${workflowFile}: ${error}`);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
442
412
|
}
|
|
443
413
|
|
|
444
414
|
/**
|
|
445
415
|
* Update package.json with autonomous configuration
|
|
446
416
|
*/
|
|
447
417
|
export async function updatePackageJson(
|
|
448
|
-
|
|
449
|
-
|
|
418
|
+
targetPath: string,
|
|
419
|
+
useSass: boolean = false
|
|
450
420
|
): Promise<void> {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
421
|
+
const packageJsonPath = path.join(targetPath, 'package.json');
|
|
422
|
+
|
|
423
|
+
if (!(await isValidPath(packageJsonPath))) {
|
|
424
|
+
console.log(`ā No package.json found, skipping package.json update`);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
try {
|
|
429
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
430
|
+
|
|
431
|
+
const configRoot = findPluginConfigRoot();
|
|
432
|
+
const templatePkg = JSON.parse(
|
|
433
|
+
fs.readFileSync(path.join(configRoot, 'templates/package.json'), 'utf8')
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
if (useSass) {
|
|
437
|
+
const sassPkg = JSON.parse(
|
|
438
|
+
fs.readFileSync(
|
|
439
|
+
path.join(configRoot, 'templates/package-sass.json'),
|
|
440
|
+
'utf8'
|
|
441
|
+
)
|
|
442
|
+
);
|
|
443
|
+
Object.assign(templatePkg.devDependencies, sassPkg.devDependencies);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const obsoleteScripts = ['version'];
|
|
447
|
+
for (const script of obsoleteScripts) {
|
|
448
|
+
if (packageJson.scripts?.[script]) {
|
|
449
|
+
console.log(` š§¹ Removing obsolete script: "${script}"`);
|
|
450
|
+
delete packageJson.scripts[script];
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
packageJson.scripts = {
|
|
455
|
+
...packageJson.scripts,
|
|
456
|
+
...templatePkg.scripts
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
if (!packageJson.devDependencies) packageJson.devDependencies = {};
|
|
460
|
+
|
|
461
|
+
const requiredDeps: Record<string, string> = templatePkg.devDependencies;
|
|
462
|
+
|
|
463
|
+
let addedDeps = 0;
|
|
464
|
+
let updatedDeps = 0;
|
|
465
|
+
for (const [dep, version] of Object.entries(requiredDeps)) {
|
|
466
|
+
if (!packageJson.devDependencies[dep]) {
|
|
467
|
+
packageJson.devDependencies[dep] = version as string;
|
|
468
|
+
addedDeps++;
|
|
469
|
+
} else if (packageJson.devDependencies[dep] !== version) {
|
|
470
|
+
packageJson.devDependencies[dep] = version as string;
|
|
471
|
+
updatedDeps++;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
if (!packageJson.engines) packageJson.engines = {};
|
|
476
|
+
packageJson.engines.npm = templatePkg.engines.npm;
|
|
477
|
+
packageJson.engines.yarn = templatePkg.engines.yarn;
|
|
478
|
+
packageJson.type = templatePkg.type;
|
|
479
|
+
|
|
480
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8');
|
|
481
|
+
console.log(
|
|
482
|
+
` ā
Updated package.json (${addedDeps} new, ${updatedDeps} updated dependencies)`
|
|
483
|
+
);
|
|
484
|
+
} catch (error) {
|
|
485
|
+
console.error(` ā Failed to update package.json: ${error}`);
|
|
486
|
+
}
|
|
512
487
|
}
|
|
513
488
|
|
|
514
489
|
/**
|
|
515
490
|
* Analyze centralized imports in source files (without modifying)
|
|
516
491
|
*/
|
|
517
492
|
export async function analyzeCentralizedImports(targetPath: string): Promise<void> {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
493
|
+
const srcPath = path.join(targetPath, 'src');
|
|
494
|
+
|
|
495
|
+
if (!(await isValidPath(srcPath))) {
|
|
496
|
+
console.log(` ā¹ļø No src directory found`);
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
console.log(`\nš Analyzing centralized imports...`);
|
|
501
|
+
|
|
502
|
+
try {
|
|
503
|
+
const findTsFiles = (dir: string): string[] => {
|
|
504
|
+
const files: string[] = [];
|
|
505
|
+
for (const item of fs.readdirSync(dir)) {
|
|
506
|
+
const fullPath = path.join(dir, item);
|
|
507
|
+
if (fs.statSync(fullPath).isDirectory()) {
|
|
508
|
+
files.push(...findTsFiles(fullPath));
|
|
509
|
+
} else if (item.endsWith('.ts') || item.endsWith('.tsx')) {
|
|
510
|
+
files.push(fullPath);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return files;
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
const tsFiles = findTsFiles(srcPath);
|
|
517
|
+
let filesWithImports = 0;
|
|
518
|
+
|
|
519
|
+
for (const filePath of tsFiles) {
|
|
520
|
+
try {
|
|
521
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
522
|
+
const importRegex =
|
|
523
|
+
/import\s+.*from\s+["']obsidian-plugin-config[^"']*["']/g;
|
|
524
|
+
if (importRegex.test(content)) {
|
|
525
|
+
filesWithImports++;
|
|
526
|
+
console.log(
|
|
527
|
+
` ā ļø ${path.relative(targetPath, filePath)} - contains centralized imports`
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
} catch (error) {
|
|
531
|
+
console.warn(
|
|
532
|
+
` ā ļø Could not analyze ${path.relative(targetPath, filePath)}: ${error}`
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (filesWithImports === 0) {
|
|
538
|
+
console.log(` ā
No centralized imports found`);
|
|
539
|
+
} else {
|
|
540
|
+
console.log(
|
|
541
|
+
` ā ļø Found ${filesWithImports} files with centralized imports`
|
|
542
|
+
);
|
|
543
|
+
console.log(
|
|
544
|
+
` š” You may need to manually comment these imports for the plugin to work`
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
} catch (error) {
|
|
548
|
+
console.error(` ā Failed to analyze imports: ${error}`);
|
|
549
|
+
}
|
|
566
550
|
}
|
|
567
551
|
|
|
568
552
|
/**
|
|
569
553
|
* Create required directories
|
|
570
554
|
*/
|
|
571
555
|
export async function createRequiredDirectories(targetPath: string): Promise<void> {
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
556
|
+
const directories = [path.join(targetPath, '.github', 'workflows')];
|
|
557
|
+
|
|
558
|
+
for (const dir of directories) {
|
|
559
|
+
if (!(await isValidPath(dir))) {
|
|
560
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
561
|
+
console.log(` š Created ${path.relative(targetPath, dir)}`);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
580
564
|
}
|
|
581
565
|
|
|
582
566
|
/**
|
|
583
567
|
* Create injection info file
|
|
584
568
|
*/
|
|
585
569
|
export async function createInjectionInfo(targetPath: string): Promise<void> {
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
570
|
+
const configRoot = findPluginConfigRoot();
|
|
571
|
+
const configPackageJsonPath = path.join(configRoot, 'package.json');
|
|
572
|
+
|
|
573
|
+
let injectorVersion = 'unknown';
|
|
574
|
+
try {
|
|
575
|
+
const configPackageJson = JSON.parse(
|
|
576
|
+
fs.readFileSync(configPackageJsonPath, 'utf8')
|
|
577
|
+
);
|
|
578
|
+
injectorVersion = configPackageJson.version || 'unknown';
|
|
579
|
+
} catch {
|
|
580
|
+
console.warn('Warning: Could not read injector version');
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
const injectionInfo = {
|
|
584
|
+
injectorVersion,
|
|
585
|
+
injectionDate: new Date().toISOString(),
|
|
586
|
+
injectorName: 'obsidian-plugin-config'
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const infoPath = path.join(targetPath, '.injection-info.json');
|
|
590
|
+
fs.writeFileSync(infoPath, JSON.stringify(injectionInfo, null, 2));
|
|
591
|
+
console.log(` ā
Created injection info file (.injection-info.json)`);
|
|
606
592
|
}
|
|
607
593
|
|
|
608
594
|
/**
|
|
609
595
|
* Read injection info from target plugin
|
|
610
596
|
*/
|
|
611
597
|
export function readInjectionInfo(targetPath: string): Record<string, string> | null {
|
|
612
|
-
|
|
598
|
+
const infoPath = path.join(targetPath, '.injection-info.json');
|
|
613
599
|
|
|
614
|
-
|
|
600
|
+
if (!fs.existsSync(infoPath)) return null;
|
|
615
601
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
602
|
+
try {
|
|
603
|
+
return JSON.parse(fs.readFileSync(infoPath, 'utf8'));
|
|
604
|
+
} catch {
|
|
605
|
+
console.warn('Warning: Could not parse .injection-info.json');
|
|
606
|
+
return null;
|
|
607
|
+
}
|
|
622
608
|
}
|
|
623
609
|
|
|
624
610
|
/**
|
|
625
611
|
* Clean NPM artifacts if package-lock.json is found
|
|
626
612
|
*/
|
|
627
613
|
export async function cleanNpmArtifactsIfNeeded(targetPath: string): Promise<void> {
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
614
|
+
const packageLockPath = path.join(targetPath, 'package-lock.json');
|
|
615
|
+
|
|
616
|
+
if (fs.existsSync(packageLockPath)) {
|
|
617
|
+
console.log(`\nš§¹ NPM installation detected, cleaning artifacts...`);
|
|
618
|
+
|
|
619
|
+
try {
|
|
620
|
+
fs.unlinkSync(packageLockPath);
|
|
621
|
+
console.log(` šļø Removed package-lock.json`);
|
|
622
|
+
|
|
623
|
+
const nodeModulesPath = path.join(targetPath, 'node_modules');
|
|
624
|
+
if (fs.existsSync(nodeModulesPath)) {
|
|
625
|
+
fs.rmSync(nodeModulesPath, { recursive: true, force: true });
|
|
626
|
+
console.log(
|
|
627
|
+
` šļø Removed node_modules (will be reinstalled with Yarn)`
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
console.log(` ā
NPM artifacts cleaned to avoid Yarn conflicts`);
|
|
632
|
+
} catch (error) {
|
|
633
|
+
console.error(` ā Failed to clean NPM artifacts: ${error}`);
|
|
634
|
+
console.log(
|
|
635
|
+
` š” You may need to manually remove package-lock.json and node_modules`
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
649
639
|
}
|
|
650
640
|
|
|
651
641
|
/**
|
|
652
642
|
* Check if tsx is installed locally and install it if needed
|
|
653
643
|
*/
|
|
654
644
|
export async function ensureTsxInstalled(targetPath: string): Promise<void> {
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
645
|
+
console.log(`\nš Checking tsx installation...`);
|
|
646
|
+
|
|
647
|
+
const packageJsonPath = path.join(targetPath, 'package.json');
|
|
648
|
+
|
|
649
|
+
try {
|
|
650
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
651
|
+
const devDependencies = packageJson.devDependencies || {};
|
|
652
|
+
const dependencies = packageJson.dependencies || {};
|
|
653
|
+
|
|
654
|
+
if (devDependencies.tsx || dependencies.tsx) {
|
|
655
|
+
console.log(` ā
tsx is already installed`);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
console.log(` ā ļø tsx not found, installing as dev dependency...`);
|
|
660
|
+
execSync('yarn add -D tsx', { cwd: targetPath, stdio: 'inherit' });
|
|
661
|
+
console.log(` ā
tsx installed successfully`);
|
|
662
|
+
} catch (error) {
|
|
663
|
+
console.error(` ā Failed to install tsx: ${error}`);
|
|
664
|
+
console.log(` š” You may need to install tsx manually: yarn add -D tsx`);
|
|
665
|
+
throw new Error('tsx installation failed');
|
|
666
|
+
}
|
|
677
667
|
}
|
|
678
668
|
|
|
679
669
|
/**
|
|
680
670
|
* Run yarn install in target directory
|
|
681
671
|
*/
|
|
682
672
|
export async function runYarnInstall(targetPath: string): Promise<void> {
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
673
|
+
console.log(`\nš¦ Installing dependencies...`);
|
|
674
|
+
|
|
675
|
+
try {
|
|
676
|
+
execSync('yarn install', { cwd: targetPath, stdio: 'inherit' });
|
|
677
|
+
console.log(` ā
Dependencies installed successfully`);
|
|
678
|
+
} catch (error) {
|
|
679
|
+
console.error(` ā Failed to install dependencies: ${error}`);
|
|
680
|
+
console.log(
|
|
681
|
+
` š” You may need to run 'yarn install' manually in the target directory`
|
|
682
|
+
);
|
|
683
|
+
}
|
|
692
684
|
}
|
|
693
685
|
|
|
694
686
|
/**
|
|
695
687
|
* Main injection orchestration function
|
|
696
688
|
*/
|
|
697
689
|
export async function performInjection(
|
|
698
|
-
|
|
699
|
-
|
|
690
|
+
targetPath: string,
|
|
691
|
+
useSass: boolean = false
|
|
700
692
|
): Promise<void> {
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
693
|
+
console.log(`\nš Starting injection process...`);
|
|
694
|
+
|
|
695
|
+
try {
|
|
696
|
+
await cleanNpmArtifactsIfNeeded(targetPath);
|
|
697
|
+
await ensureTsxInstalled(targetPath);
|
|
698
|
+
await injectScripts(targetPath);
|
|
699
|
+
|
|
700
|
+
console.log(`\nš¦ Updating package.json...`);
|
|
701
|
+
await updatePackageJson(targetPath, useSass);
|
|
702
|
+
|
|
703
|
+
await analyzeCentralizedImports(targetPath);
|
|
704
|
+
|
|
705
|
+
console.log(`\nš Creating required directories...`);
|
|
706
|
+
await createRequiredDirectories(targetPath);
|
|
707
|
+
|
|
708
|
+
await runYarnInstall(targetPath);
|
|
709
|
+
|
|
710
|
+
console.log(`\nš Creating injection info...`);
|
|
711
|
+
await createInjectionInfo(targetPath);
|
|
712
|
+
|
|
713
|
+
console.log(`\nā
Injection completed successfully!`);
|
|
714
|
+
console.log(`\nš Next steps:`);
|
|
715
|
+
console.log(` 1. cd ${targetPath}`);
|
|
716
|
+
console.log(` 2. yarn build # Test the build`);
|
|
717
|
+
console.log(` 3. yarn start # Test development mode`);
|
|
718
|
+
console.log(
|
|
719
|
+
` 4. yarn acp # Commit changes (or yarn bacp for build+commit)`
|
|
720
|
+
);
|
|
721
|
+
} catch (error) {
|
|
722
|
+
console.error(`\nā Injection failed: ${error}`);
|
|
723
|
+
throw error;
|
|
724
|
+
}
|
|
731
725
|
}
|