create-objectstack 6.5.1 → 6.5.2
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/index.js
CHANGED
|
@@ -46,6 +46,26 @@ var TEMPLATES = {
|
|
|
46
46
|
function toTitleCase(str) {
|
|
47
47
|
return str.replace(/[-_]/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
|
|
48
48
|
}
|
|
49
|
+
function sanitizeNamespace(name) {
|
|
50
|
+
let s = name.replace(/^@[^/]+\//, "");
|
|
51
|
+
s = s.toLowerCase().replace(/[^a-z0-9]+/g, "_");
|
|
52
|
+
s = s.replace(/^_+|_+$/g, "");
|
|
53
|
+
if (!s) s = "app";
|
|
54
|
+
if (/^[0-9]/.test(s)) s = "a" + s;
|
|
55
|
+
if (s.length < 2) s = (s + "_app").slice(0, 20);
|
|
56
|
+
if (s.length > 20) s = s.slice(0, 20).replace(/_+$/, "");
|
|
57
|
+
if (["base", "system", "sys"].includes(s)) s = (s + "_app").slice(0, 20);
|
|
58
|
+
return s;
|
|
59
|
+
}
|
|
60
|
+
function readCliVersion() {
|
|
61
|
+
try {
|
|
62
|
+
const pkgPath = path.resolve(__dirname, "..", "package.json");
|
|
63
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
64
|
+
return String(pkg.version || "0.0.0");
|
|
65
|
+
} catch {
|
|
66
|
+
return "0.0.0";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
49
69
|
function printHeader(title) {
|
|
50
70
|
console.log(chalk.bold(`
|
|
51
71
|
\u25C6 ${title}`));
|
|
@@ -147,8 +167,30 @@ async function loadRemote(pkgName, targetDir) {
|
|
|
147
167
|
await rm(tmp, { recursive: true, force: true });
|
|
148
168
|
}
|
|
149
169
|
}
|
|
150
|
-
function
|
|
170
|
+
function walkAndRewriteTs(dir, fn) {
|
|
171
|
+
if (!fs.existsSync(dir)) return;
|
|
172
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
173
|
+
const full = path.join(dir, entry.name);
|
|
174
|
+
if (entry.isDirectory()) {
|
|
175
|
+
walkAndRewriteTs(full, fn);
|
|
176
|
+
} else if (entry.isFile() && entry.name.endsWith(".ts")) {
|
|
177
|
+
const before = fs.readFileSync(full, "utf8");
|
|
178
|
+
const after = fn(before);
|
|
179
|
+
if (after !== before) fs.writeFileSync(full, after);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function rewriteProjectIdentity(targetDir, projectName, namespace) {
|
|
151
184
|
const title = toTitleCase(projectName);
|
|
185
|
+
let templateNamespace;
|
|
186
|
+
const manifestPathPre = path.join(targetDir, "objectstack.manifest.json");
|
|
187
|
+
if (fs.existsSync(manifestPathPre)) {
|
|
188
|
+
try {
|
|
189
|
+
const m = JSON.parse(fs.readFileSync(manifestPathPre, "utf8"));
|
|
190
|
+
if (typeof m.namespace === "string") templateNamespace = m.namespace;
|
|
191
|
+
} catch {
|
|
192
|
+
}
|
|
193
|
+
}
|
|
152
194
|
const pkgPath = path.join(targetDir, "package.json");
|
|
153
195
|
if (fs.existsSync(pkgPath)) {
|
|
154
196
|
try {
|
|
@@ -164,6 +206,7 @@ function rewriteProjectIdentity(targetDir, projectName) {
|
|
|
164
206
|
const m = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
165
207
|
m.name = projectName;
|
|
166
208
|
m.displayName = title;
|
|
209
|
+
if ("namespace" in m) m.namespace = namespace;
|
|
167
210
|
fs.writeFileSync(manifestPath, JSON.stringify(m, null, 2) + "\n");
|
|
168
211
|
} catch {
|
|
169
212
|
}
|
|
@@ -172,9 +215,23 @@ function rewriteProjectIdentity(targetDir, projectName) {
|
|
|
172
215
|
if (fs.existsSync(configPath)) {
|
|
173
216
|
let cfg = fs.readFileSync(configPath, "utf8");
|
|
174
217
|
cfg = cfg.replace(/(\bid:\s*)(['"`])[^'"`]*\2/, `$1$2${projectName}$2`);
|
|
218
|
+
cfg = cfg.replace(/(\bnamespace:\s*)(['"`])[^'"`]*\2/, `$1$2${namespace}$2`);
|
|
175
219
|
cfg = cfg.replace(/(\bname:\s*)(['"`])[^'"`]*\2/, `$1$2${title}$2`);
|
|
176
220
|
fs.writeFileSync(configPath, cfg);
|
|
177
221
|
}
|
|
222
|
+
if (namespace !== templateNamespace && templateNamespace) {
|
|
223
|
+
const prefixRe = new RegExp(
|
|
224
|
+
`(\\bname:\\s*)(['"\`])${templateNamespace}_([a-z0-9_]+)\\2`,
|
|
225
|
+
"g"
|
|
226
|
+
);
|
|
227
|
+
walkAndRewriteTs(
|
|
228
|
+
path.join(targetDir, "src"),
|
|
229
|
+
(src) => src.replace(
|
|
230
|
+
prefixRe,
|
|
231
|
+
(_m, prefix, q, rest) => `${prefix}${q}${namespace}_${rest}${q}`
|
|
232
|
+
)
|
|
233
|
+
);
|
|
234
|
+
}
|
|
178
235
|
const readmePath = path.join(targetDir, "README.md");
|
|
179
236
|
if (fs.existsSync(readmePath)) {
|
|
180
237
|
let md = fs.readFileSync(readmePath, "utf8");
|
|
@@ -182,7 +239,7 @@ function rewriteProjectIdentity(targetDir, projectName) {
|
|
|
182
239
|
fs.writeFileSync(readmePath, md);
|
|
183
240
|
}
|
|
184
241
|
}
|
|
185
|
-
var program = new Command().name("create-objectstack").description("Create a new ObjectStack environment").version(
|
|
242
|
+
var program = new Command().name("create-objectstack").description("Create a new ObjectStack environment").version(readCliVersion()).argument("[name]", "Environment name (defaults to current directory name)").option(
|
|
186
243
|
"-t, --template <template>",
|
|
187
244
|
`Template: ${Object.keys(TEMPLATES).join(", ")}`,
|
|
188
245
|
"blank"
|
|
@@ -200,9 +257,11 @@ var program = new Command().name("create-objectstack").description("Create a new
|
|
|
200
257
|
}
|
|
201
258
|
const cwd = process.cwd();
|
|
202
259
|
const projectName = name || path.basename(cwd);
|
|
260
|
+
const namespace = sanitizeNamespace(projectName);
|
|
203
261
|
const targetDir = name ? path.resolve(cwd, name) : cwd;
|
|
204
262
|
const isCurrentDir = targetDir === cwd;
|
|
205
263
|
printKV("Environment", projectName);
|
|
264
|
+
printKV("Namespace", namespace);
|
|
206
265
|
printKV("Template", `${options.template} \u2014 ${template.description}`);
|
|
207
266
|
printKV("Directory", targetDir);
|
|
208
267
|
console.log("");
|
|
@@ -221,7 +280,7 @@ var program = new Command().name("create-objectstack").description("Create a new
|
|
|
221
280
|
} else {
|
|
222
281
|
createdFiles = await loadRemote(template.source.pkg, targetDir);
|
|
223
282
|
}
|
|
224
|
-
rewriteProjectIdentity(targetDir, projectName);
|
|
283
|
+
rewriteProjectIdentity(targetDir, projectName, namespace);
|
|
225
284
|
console.log(chalk.bold(" Created files:"));
|
|
226
285
|
for (const f of createdFiles.slice(0, 20)) {
|
|
227
286
|
console.log(chalk.green(` + ${f}`));
|
|
@@ -280,3 +339,6 @@ var program = new Command().name("create-objectstack").description("Create a new
|
|
|
280
339
|
}
|
|
281
340
|
});
|
|
282
341
|
program.parse();
|
|
342
|
+
export {
|
|
343
|
+
sanitizeNamespace
|
|
344
|
+
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://schemas.objectstack.dev/template-manifest.json",
|
|
3
3
|
"name": "blank",
|
|
4
|
+
"namespace": "blank",
|
|
4
5
|
"specVersion": "^6.0.0",
|
|
5
6
|
"displayName": "Blank Starter",
|
|
6
7
|
"description": "Minimal ObjectStack environment with a single object — a clean slate for building.",
|