@synchronized-studio/cmsassets-agent 0.3.7 → 0.3.8
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/dist/cli.js +118 -41
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -16,14 +16,14 @@ import "./chunk-QGM4M3NI.js";
|
|
|
16
16
|
|
|
17
17
|
// src/cli.ts
|
|
18
18
|
import { existsSync as existsSync5 } from "fs";
|
|
19
|
-
import { dirname as pathDirname, resolve as
|
|
19
|
+
import { dirname as pathDirname, resolve as resolve7 } from "path";
|
|
20
20
|
import { fileURLToPath } from "url";
|
|
21
21
|
import { defineCommand as defineCommand8, runMain } from "citty";
|
|
22
22
|
import { config as dotenvConfig } from "dotenv";
|
|
23
23
|
|
|
24
24
|
// src/commands/init.ts
|
|
25
25
|
import { createInterface } from "readline";
|
|
26
|
-
import { resolve } from "path";
|
|
26
|
+
import { resolve as resolve2 } from "path";
|
|
27
27
|
import { defineCommand } from "citty";
|
|
28
28
|
import consola3 from "consola";
|
|
29
29
|
import pc from "picocolors";
|
|
@@ -60,7 +60,7 @@ function injectNpmScript(projectRoot) {
|
|
|
60
60
|
|
|
61
61
|
// src/patcher/injectNuxtRuntimeConfig.ts
|
|
62
62
|
import { existsSync as existsSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
63
|
-
import { join as join2 } from "path";
|
|
63
|
+
import { join as join2, dirname, resolve } from "path";
|
|
64
64
|
import consola2 from "consola";
|
|
65
65
|
function injectNuxtRuntimeConfig(projectRoot) {
|
|
66
66
|
const candidates = ["nuxt.config.ts", "nuxt.config.js", "nuxt.config.mjs"];
|
|
@@ -69,59 +69,131 @@ function injectNuxtRuntimeConfig(projectRoot) {
|
|
|
69
69
|
return { added: false, reason: "No nuxt.config found" };
|
|
70
70
|
}
|
|
71
71
|
const absPath = join2(projectRoot, configFile);
|
|
72
|
-
|
|
72
|
+
const content = readFileSync2(absPath, "utf-8");
|
|
73
73
|
if (content.includes("cmsAssetsUrl")) {
|
|
74
74
|
return { added: false, reason: "cmsAssetsUrl already present in nuxt.config" };
|
|
75
75
|
}
|
|
76
|
+
const externalResult = tryInjectInExternalRuntimeConfig(absPath, content, projectRoot);
|
|
77
|
+
if (externalResult) return externalResult;
|
|
78
|
+
return injectInline(absPath, configFile, content);
|
|
79
|
+
}
|
|
80
|
+
var ENTRY = `cmsAssetsUrl: process.env.CMS_ASSETS_URL || ''`;
|
|
81
|
+
function tryInjectInExternalRuntimeConfig(nuxtConfigPath, content, projectRoot) {
|
|
82
|
+
const importRe = /import\s+runtimeConfig\s+from\s+["']([^"']+)["']/;
|
|
83
|
+
const importMatch = content.match(importRe);
|
|
84
|
+
if (!importMatch) return null;
|
|
85
|
+
const importPath = importMatch[1];
|
|
86
|
+
const configDir = dirname(nuxtConfigPath);
|
|
87
|
+
const extensions = ["", ".ts", ".js", ".mjs"];
|
|
88
|
+
let externalPath = null;
|
|
89
|
+
for (const ext of extensions) {
|
|
90
|
+
const candidate = resolve(configDir, importPath + ext);
|
|
91
|
+
if (existsSync2(candidate)) {
|
|
92
|
+
externalPath = candidate;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (!externalPath) {
|
|
97
|
+
return {
|
|
98
|
+
added: false,
|
|
99
|
+
reason: `runtimeConfig is imported from "${importPath}" but the file was not found`
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
let extContent = readFileSync2(externalPath, "utf-8");
|
|
103
|
+
if (extContent.includes("cmsAssetsUrl")) {
|
|
104
|
+
return { added: false, reason: "cmsAssetsUrl already present in external runtimeConfig file" };
|
|
105
|
+
}
|
|
106
|
+
const indentMatch = extContent.match(/^( +)\S/m);
|
|
107
|
+
const indent = indentMatch?.[1] ?? " ";
|
|
108
|
+
const i2 = indent.repeat(2);
|
|
109
|
+
const publicBlockRe = /(public\s*:\s*\{)(\s*)/;
|
|
110
|
+
if (publicBlockRe.test(extContent)) {
|
|
111
|
+
extContent = extContent.replace(publicBlockRe, `$1$2${i2}${ENTRY},
|
|
112
|
+
`);
|
|
113
|
+
writeFileSync2(externalPath, extContent, "utf-8");
|
|
114
|
+
const relPath = externalPath.replace(projectRoot + "/", "");
|
|
115
|
+
consola2.success(`Added cmsAssetsUrl to public block in ${relPath}`);
|
|
116
|
+
return { added: true, file: relPath };
|
|
117
|
+
}
|
|
118
|
+
const exportObjRe = /(export\s+default\s*\{)(\s*)/;
|
|
119
|
+
if (exportObjRe.test(extContent)) {
|
|
120
|
+
const block = `$1$2${indent}public: {
|
|
121
|
+
${i2}${ENTRY},
|
|
122
|
+
${indent}},
|
|
123
|
+
`;
|
|
124
|
+
extContent = extContent.replace(exportObjRe, block);
|
|
125
|
+
writeFileSync2(externalPath, extContent, "utf-8");
|
|
126
|
+
const relPath = externalPath.replace(projectRoot + "/", "");
|
|
127
|
+
consola2.success(`Added runtimeConfig.public.cmsAssetsUrl in ${relPath}`);
|
|
128
|
+
return { added: true, file: relPath };
|
|
129
|
+
}
|
|
130
|
+
const defineRe = /(defineNuxtConfig\s*\(\s*\{|(?:const|let)\s+\w+\s*=\s*\{)(\s*)/;
|
|
131
|
+
if (defineRe.test(extContent)) {
|
|
132
|
+
const block = `$1$2${indent}public: {
|
|
133
|
+
${i2}${ENTRY},
|
|
134
|
+
${indent}},
|
|
135
|
+
`;
|
|
136
|
+
extContent = extContent.replace(defineRe, block);
|
|
137
|
+
writeFileSync2(externalPath, extContent, "utf-8");
|
|
138
|
+
const relPath = externalPath.replace(projectRoot + "/", "");
|
|
139
|
+
consola2.success(`Added runtimeConfig.public.cmsAssetsUrl in ${relPath}`);
|
|
140
|
+
return { added: true, file: relPath };
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
added: false,
|
|
144
|
+
reason: `runtimeConfig is imported from "${importPath}" but could not find insertion point in that file`
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function injectInline(absPath, configFile, content) {
|
|
148
|
+
let modified = content;
|
|
76
149
|
const indentMatch = content.match(/^( +)\S/m);
|
|
77
150
|
const indent = indentMatch?.[1] ?? " ";
|
|
78
151
|
const i2 = indent.repeat(2);
|
|
79
152
|
const i3 = indent.repeat(3);
|
|
80
|
-
const entry = `cmsAssetsUrl: process.env.CMS_ASSETS_URL || ''`;
|
|
81
153
|
const publicBlockRe = /(runtimeConfig\s*:\s*\{[^}]*?public\s*:\s*\{)(\s*)/;
|
|
82
|
-
if (publicBlockRe.test(
|
|
83
|
-
|
|
154
|
+
if (publicBlockRe.test(modified)) {
|
|
155
|
+
modified = modified.replace(publicBlockRe, `$1$2${i3}${ENTRY},
|
|
84
156
|
`);
|
|
85
|
-
writeFileSync2(absPath,
|
|
157
|
+
writeFileSync2(absPath, modified, "utf-8");
|
|
86
158
|
consola2.success(`Added cmsAssetsUrl to runtimeConfig.public in ${configFile}`);
|
|
87
|
-
return { added: true };
|
|
159
|
+
return { added: true, file: configFile };
|
|
88
160
|
}
|
|
89
161
|
const runtimeConfigRe = /(runtimeConfig\s*:\s*\{)(\s*)/;
|
|
90
|
-
if (runtimeConfigRe.test(
|
|
162
|
+
if (runtimeConfigRe.test(modified)) {
|
|
91
163
|
const publicBlock = `$1$2${i2}public: {
|
|
92
|
-
${i3}${
|
|
164
|
+
${i3}${ENTRY},
|
|
93
165
|
${i2}},
|
|
94
166
|
`;
|
|
95
|
-
|
|
96
|
-
writeFileSync2(absPath,
|
|
167
|
+
modified = modified.replace(runtimeConfigRe, publicBlock);
|
|
168
|
+
writeFileSync2(absPath, modified, "utf-8");
|
|
97
169
|
consola2.success(`Added runtimeConfig.public.cmsAssetsUrl in ${configFile}`);
|
|
98
|
-
return { added: true };
|
|
170
|
+
return { added: true, file: configFile };
|
|
99
171
|
}
|
|
100
172
|
const defineConfigRe = /(defineNuxtConfig\s*\(\s*\{)(\s*)/;
|
|
101
|
-
if (defineConfigRe.test(
|
|
173
|
+
if (defineConfigRe.test(modified)) {
|
|
102
174
|
const block = `$1$2${indent}runtimeConfig: {
|
|
103
175
|
${i2}public: {
|
|
104
|
-
${i3}${
|
|
176
|
+
${i3}${ENTRY},
|
|
105
177
|
${i2}},
|
|
106
178
|
${indent}},
|
|
107
179
|
`;
|
|
108
|
-
|
|
109
|
-
writeFileSync2(absPath,
|
|
180
|
+
modified = modified.replace(defineConfigRe, block);
|
|
181
|
+
writeFileSync2(absPath, modified, "utf-8");
|
|
110
182
|
consola2.success(`Added runtimeConfig.public.cmsAssetsUrl in ${configFile}`);
|
|
111
|
-
return { added: true };
|
|
183
|
+
return { added: true, file: configFile };
|
|
112
184
|
}
|
|
113
185
|
const exportRe = /(export\s+default\s*\{)(\s*)/;
|
|
114
|
-
if (exportRe.test(
|
|
186
|
+
if (exportRe.test(modified)) {
|
|
115
187
|
const block = `$1$2${indent}runtimeConfig: {
|
|
116
188
|
${i2}public: {
|
|
117
|
-
${i3}${
|
|
189
|
+
${i3}${ENTRY},
|
|
118
190
|
${i2}},
|
|
119
191
|
${indent}},
|
|
120
192
|
`;
|
|
121
|
-
|
|
122
|
-
writeFileSync2(absPath,
|
|
193
|
+
modified = modified.replace(exportRe, block);
|
|
194
|
+
writeFileSync2(absPath, modified, "utf-8");
|
|
123
195
|
consola2.success(`Added runtimeConfig.public.cmsAssetsUrl in ${configFile}`);
|
|
124
|
-
return { added: true };
|
|
196
|
+
return { added: true, file: configFile };
|
|
125
197
|
}
|
|
126
198
|
return { added: false, reason: "Could not find insertion point in nuxt.config" };
|
|
127
199
|
}
|
|
@@ -129,10 +201,10 @@ ${indent}},
|
|
|
129
201
|
// src/commands/init.ts
|
|
130
202
|
function confirm(question) {
|
|
131
203
|
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
132
|
-
return new Promise((
|
|
204
|
+
return new Promise((resolve8) => {
|
|
133
205
|
rl.question(question, (answer) => {
|
|
134
206
|
rl.close();
|
|
135
|
-
|
|
207
|
+
resolve8(answer.trim().toLowerCase() === "y" || answer.trim().toLowerCase() === "yes");
|
|
136
208
|
});
|
|
137
209
|
});
|
|
138
210
|
}
|
|
@@ -232,7 +304,7 @@ var initCommand = defineCommand({
|
|
|
232
304
|
}
|
|
233
305
|
},
|
|
234
306
|
async run({ args }) {
|
|
235
|
-
const root =
|
|
307
|
+
const root = resolve2(args.dir);
|
|
236
308
|
consola3.log("");
|
|
237
309
|
consola3.log(pc.bold(" CMS Assets Agent \u2014 Init"));
|
|
238
310
|
consola3.log(pc.dim(" \u2500".repeat(25)));
|
|
@@ -388,9 +460,14 @@ var initCommand = defineCommand({
|
|
|
388
460
|
}
|
|
389
461
|
}
|
|
390
462
|
let runtimeConfigAdded = false;
|
|
463
|
+
let runtimeConfigFile = "nuxt.config";
|
|
391
464
|
if (scanResult.framework.name === "nuxt") {
|
|
392
465
|
const rcResult = injectNuxtRuntimeConfig(root);
|
|
393
466
|
runtimeConfigAdded = rcResult.added;
|
|
467
|
+
if (rcResult.file) runtimeConfigFile = rcResult.file;
|
|
468
|
+
if (!rcResult.added && rcResult.reason) {
|
|
469
|
+
consola3.warn(` ${pc.yellow("runtimeConfig:")} ${rcResult.reason}`);
|
|
470
|
+
}
|
|
394
471
|
}
|
|
395
472
|
let scriptAdded = false;
|
|
396
473
|
if (!args["no-script"]) {
|
|
@@ -418,7 +495,7 @@ var initCommand = defineCommand({
|
|
|
418
495
|
consola3.log(` ${pc.yellow("Skipped:")} ${skipped.length + manualReview.length} files`);
|
|
419
496
|
}
|
|
420
497
|
if (installed) consola3.log(` ${pc.green("Installed:")} @synchronized-studio/response-transformer`);
|
|
421
|
-
if (runtimeConfigAdded) consola3.log(` ${pc.green("Config:")} Added runtimeConfig.public.cmsAssetsUrl to
|
|
498
|
+
if (runtimeConfigAdded) consola3.log(` ${pc.green("Config:")} Added runtimeConfig.public.cmsAssetsUrl to ${runtimeConfigFile}`);
|
|
422
499
|
if (scriptAdded) consola3.log(` ${pc.green("Script:")} Added "cmsassets:transform" to package.json`);
|
|
423
500
|
consola3.log("");
|
|
424
501
|
for (const file of applied) {
|
|
@@ -596,7 +673,7 @@ var planCommand = defineCommand3({
|
|
|
596
673
|
|
|
597
674
|
// src/commands/apply.ts
|
|
598
675
|
import { existsSync as existsSync3 } from "fs";
|
|
599
|
-
import { join as join3, resolve as
|
|
676
|
+
import { join as join3, resolve as resolve3 } from "path";
|
|
600
677
|
import { defineCommand as defineCommand4 } from "citty";
|
|
601
678
|
import consola6 from "consola";
|
|
602
679
|
import pc4 from "picocolors";
|
|
@@ -706,7 +783,7 @@ var applyCommand = defineCommand4({
|
|
|
706
783
|
}
|
|
707
784
|
},
|
|
708
785
|
async run({ args }) {
|
|
709
|
-
const root =
|
|
786
|
+
const root = resolve3(args.dir);
|
|
710
787
|
if (gitOps.isGitRepo(root) && !args["allow-dirty"] && !gitOps.isClean(root)) {
|
|
711
788
|
consola6.error(
|
|
712
789
|
"Git worktree is dirty. Commit or stash changes first, or use --allow-dirty."
|
|
@@ -715,7 +792,7 @@ var applyCommand = defineCommand4({
|
|
|
715
792
|
}
|
|
716
793
|
let plan;
|
|
717
794
|
if (args.plan) {
|
|
718
|
-
const planPath =
|
|
795
|
+
const planPath = resolve3(args.plan);
|
|
719
796
|
const loaded = loadPlanFile(planPath);
|
|
720
797
|
if (!loaded) {
|
|
721
798
|
consola6.error(`Invalid plan file: ${planPath}`);
|
|
@@ -914,7 +991,7 @@ function generatePlan(root, args) {
|
|
|
914
991
|
}
|
|
915
992
|
|
|
916
993
|
// src/commands/verify.ts
|
|
917
|
-
import { resolve as
|
|
994
|
+
import { resolve as resolve4 } from "path";
|
|
918
995
|
import { defineCommand as defineCommand5 } from "citty";
|
|
919
996
|
import consola7 from "consola";
|
|
920
997
|
import pc5 from "picocolors";
|
|
@@ -936,7 +1013,7 @@ var verifyCommand = defineCommand5({
|
|
|
936
1013
|
}
|
|
937
1014
|
},
|
|
938
1015
|
async run({ args }) {
|
|
939
|
-
const root =
|
|
1016
|
+
const root = resolve4(args.dir);
|
|
940
1017
|
const scanResult = scan(root);
|
|
941
1018
|
consola7.log("");
|
|
942
1019
|
consola7.log(pc5.bold(` CMS Assets Agent \u2014 Verify (${args.profile})`));
|
|
@@ -967,7 +1044,7 @@ var verifyCommand = defineCommand5({
|
|
|
967
1044
|
|
|
968
1045
|
// src/commands/doctor.ts
|
|
969
1046
|
import { existsSync as existsSync4, readFileSync as readFileSync4 } from "fs";
|
|
970
|
-
import { resolve as
|
|
1047
|
+
import { resolve as resolve5, join as join4 } from "path";
|
|
971
1048
|
import { defineCommand as defineCommand6 } from "citty";
|
|
972
1049
|
import consola8 from "consola";
|
|
973
1050
|
import pc6 from "picocolors";
|
|
@@ -984,7 +1061,7 @@ var doctorCommand = defineCommand6({
|
|
|
984
1061
|
}
|
|
985
1062
|
},
|
|
986
1063
|
async run({ args }) {
|
|
987
|
-
const root =
|
|
1064
|
+
const root = resolve5(args.dir);
|
|
988
1065
|
const checks = [];
|
|
989
1066
|
checks.push({
|
|
990
1067
|
label: "package.json",
|
|
@@ -1077,7 +1154,7 @@ var doctorCommand = defineCommand6({
|
|
|
1077
1154
|
});
|
|
1078
1155
|
|
|
1079
1156
|
// src/commands/rollback.ts
|
|
1080
|
-
import { resolve as
|
|
1157
|
+
import { resolve as resolve6 } from "path";
|
|
1081
1158
|
import { defineCommand as defineCommand7 } from "citty";
|
|
1082
1159
|
import consola9 from "consola";
|
|
1083
1160
|
import pc7 from "picocolors";
|
|
@@ -1098,7 +1175,7 @@ var rollbackCommand = defineCommand7({
|
|
|
1098
1175
|
}
|
|
1099
1176
|
},
|
|
1100
1177
|
async run({ args }) {
|
|
1101
|
-
const root =
|
|
1178
|
+
const root = resolve6(args.dir);
|
|
1102
1179
|
if (!gitOps.isGitRepo(root)) {
|
|
1103
1180
|
consola9.error("Not a git repository. Cannot rollback.");
|
|
1104
1181
|
process.exit(1);
|
|
@@ -1154,9 +1231,9 @@ var main = defineCommand8({
|
|
|
1154
1231
|
var cwd = process.cwd();
|
|
1155
1232
|
var cliDir = typeof __dirname !== "undefined" ? __dirname : pathDirname(fileURLToPath(import.meta.url));
|
|
1156
1233
|
function findProjectRootEnv(startDir) {
|
|
1157
|
-
let dir =
|
|
1234
|
+
let dir = resolve7(startDir);
|
|
1158
1235
|
for (let i = 0; i < 6; i++) {
|
|
1159
|
-
const envPath =
|
|
1236
|
+
const envPath = resolve7(dir, ".env");
|
|
1160
1237
|
if (existsSync5(envPath)) return envPath;
|
|
1161
1238
|
const parent = pathDirname(dir);
|
|
1162
1239
|
if (parent === dir) break;
|
|
@@ -1166,8 +1243,8 @@ function findProjectRootEnv(startDir) {
|
|
|
1166
1243
|
}
|
|
1167
1244
|
var envPaths = [];
|
|
1168
1245
|
var explicitEnv = process.env.DOTENV_CONFIG_PATH || process.env.CMSASSETS_ENV_FILE;
|
|
1169
|
-
if (explicitEnv) envPaths.push(
|
|
1170
|
-
envPaths.push(
|
|
1246
|
+
if (explicitEnv) envPaths.push(resolve7(cwd, explicitEnv));
|
|
1247
|
+
envPaths.push(resolve7(cwd, ".env"));
|
|
1171
1248
|
var projectRootEnv = findProjectRootEnv(cliDir);
|
|
1172
1249
|
if (projectRootEnv && !envPaths.includes(projectRootEnv)) envPaths.push(projectRootEnv);
|
|
1173
1250
|
for (const p of envPaths) {
|
package/package.json
CHANGED