Refactor code formatting for consistency in scaffold and update port registry scripts
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const TEMPLATE_ROOT = path.resolve(
|
||||
path.dirname(fileURLToPath(import.meta.url)),
|
||||
'..',
|
||||
'templates',
|
||||
'discord-oauth-vue3-vite',
|
||||
'src',
|
||||
'server',
|
||||
'discord-oauth',
|
||||
"..",
|
||||
"templates",
|
||||
"discord-oauth-vue3-vite",
|
||||
"src",
|
||||
"server",
|
||||
"discord-oauth",
|
||||
);
|
||||
|
||||
function usage() {
|
||||
@@ -32,52 +32,52 @@ Optional:
|
||||
|
||||
function parseArgs(argv) {
|
||||
const options = {
|
||||
mode: 'dry-run',
|
||||
frontendOrigin: 'http://localhost:5173',
|
||||
allowlistDiscordIds: '',
|
||||
scopes: 'identify,email',
|
||||
mode: "dry-run",
|
||||
frontendOrigin: "http://localhost:5173",
|
||||
allowlistDiscordIds: "",
|
||||
scopes: "identify,email",
|
||||
force: false,
|
||||
};
|
||||
|
||||
for (let index = 0; index < argv.length; index += 1) {
|
||||
const arg = argv[index];
|
||||
|
||||
if (arg === '--help') {
|
||||
if (arg === "--help") {
|
||||
usage();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (arg === '--project-root') {
|
||||
if (arg === "--project-root") {
|
||||
options.projectRoot = argv[index + 1];
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === '--mode') {
|
||||
if (arg === "--mode") {
|
||||
options.mode = argv[index + 1];
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === '--frontend-origin') {
|
||||
if (arg === "--frontend-origin") {
|
||||
options.frontendOrigin = argv[index + 1];
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === '--allowlist-discord-ids') {
|
||||
if (arg === "--allowlist-discord-ids") {
|
||||
options.allowlistDiscordIds = argv[index + 1];
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === '--scopes') {
|
||||
if (arg === "--scopes") {
|
||||
options.scopes = argv[index + 1];
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === '--force') {
|
||||
if (arg === "--force") {
|
||||
options.force = true;
|
||||
continue;
|
||||
}
|
||||
@@ -86,24 +86,29 @@ function parseArgs(argv) {
|
||||
}
|
||||
|
||||
if (!options.projectRoot) {
|
||||
throw new Error('--project-root is required.');
|
||||
throw new Error("--project-root is required.");
|
||||
}
|
||||
|
||||
if (!['dry-run', 'apply'].includes(options.mode)) {
|
||||
throw new Error('--mode must be dry-run or apply.');
|
||||
if (!["dry-run", "apply"].includes(options.mode)) {
|
||||
throw new Error("--mode must be dry-run or apply.");
|
||||
}
|
||||
|
||||
options.projectRoot = path.resolve(options.projectRoot);
|
||||
options.targetRoot = path.join(options.projectRoot, 'src', 'server', 'discord-oauth');
|
||||
options.targetRoot = path.join(
|
||||
options.projectRoot,
|
||||
"src",
|
||||
"server",
|
||||
"discord-oauth",
|
||||
);
|
||||
options.allowlistDiscordIdsJson = JSON.stringify(
|
||||
options.allowlistDiscordIds
|
||||
.split(',')
|
||||
.split(",")
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean),
|
||||
);
|
||||
options.scopesJson = JSON.stringify(
|
||||
options.scopes
|
||||
.split(',')
|
||||
.split(",")
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean),
|
||||
);
|
||||
@@ -111,7 +116,7 @@ function parseArgs(argv) {
|
||||
}
|
||||
|
||||
function ensureDir(dirPath, mode) {
|
||||
if (mode === 'dry-run') {
|
||||
if (mode === "dry-run") {
|
||||
return;
|
||||
}
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
@@ -123,9 +128,12 @@ function isTextFile(filePath) {
|
||||
|
||||
function replacePlaceholders(content, options) {
|
||||
return content
|
||||
.replaceAll('__FRONTEND_ORIGIN__', options.frontendOrigin)
|
||||
.replaceAll('__ALLOWLIST_DISCORD_IDS_JSON__', options.allowlistDiscordIdsJson)
|
||||
.replaceAll('__SCOPES_JSON__', options.scopesJson);
|
||||
.replaceAll("__FRONTEND_ORIGIN__", options.frontendOrigin)
|
||||
.replaceAll(
|
||||
"__ALLOWLIST_DISCORD_IDS_JSON__",
|
||||
options.allowlistDiscordIdsJson,
|
||||
)
|
||||
.replaceAll("__SCOPES_JSON__", options.scopesJson);
|
||||
}
|
||||
|
||||
function walkFiles(rootDir, callback) {
|
||||
@@ -154,16 +162,16 @@ function scaffold(options) {
|
||||
const exists = fs.existsSync(targetPath);
|
||||
|
||||
if (exists && !options.force) {
|
||||
results.push({ action: 'skipped', filePath: targetPath });
|
||||
results.push({ action: "skipped", filePath: targetPath });
|
||||
return;
|
||||
}
|
||||
|
||||
const action = exists ? 'updated' : 'created';
|
||||
if (options.mode === 'apply') {
|
||||
const action = exists ? "updated" : "created";
|
||||
if (options.mode === "apply") {
|
||||
ensureDir(path.dirname(targetPath), options.mode);
|
||||
const rawContent = fs.readFileSync(sourcePath);
|
||||
const content = isTextFile(sourcePath)
|
||||
? replacePlaceholders(rawContent.toString('utf8'), options)
|
||||
? replacePlaceholders(rawContent.toString("utf8"), options)
|
||||
: rawContent;
|
||||
fs.writeFileSync(targetPath, content);
|
||||
}
|
||||
@@ -177,17 +185,21 @@ function scaffold(options) {
|
||||
function main() {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
|
||||
console.log(`Target bundle: ${path.relative(options.projectRoot, options.targetRoot)}`);
|
||||
console.log(
|
||||
`Target bundle: ${path.relative(options.projectRoot, options.targetRoot)}`,
|
||||
);
|
||||
console.log(`Mode: ${options.mode}`);
|
||||
|
||||
const results = scaffold(options);
|
||||
for (const result of results) {
|
||||
console.log(`${result.action.toUpperCase()}: ${path.relative(options.projectRoot, result.filePath)}`);
|
||||
console.log(
|
||||
`${result.action.toUpperCase()}: ${path.relative(options.projectRoot, result.filePath)}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (options.mode === 'dry-run') {
|
||||
console.log('Dry-run only. Re-run with --mode apply to write files.');
|
||||
if (options.mode === "dry-run") {
|
||||
console.log("Dry-run only. Re-run with --mode apply to write files.");
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user