Skip to content

Commit 8552b6d

Browse files
committed
feat: Refactor structure and move to esm
Refactor structure to feature-based. Implement as ESM. Extend typescript support with custom types for class field mapping. BREAKING CHANGE: This is the next version. Old configuration will work probably. The basic restructuring argued for a major version.
1 parent 113bf5e commit 8552b6d

18 files changed

Lines changed: 1346 additions & 1061 deletions

bin/parse-server-schema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#!/usr/bin/env node
22

3-
require("../dist/cli");
3+
import "../dist/cli.js";
4+

src/cli.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// @ts-check
2-
require("dotenv").config();
1+
import 'dotenv/config';
32

4-
// @ts-ignore
5-
import pkg from "../package.json";
3+
import pkg from "../package.json" with { type: "json" };
64

75
import { Command } from "commander";
8-
import { del, down, loadConfig, typescript, up } from "./index";
6+
import { loadConfig, printConfig } from './features/config/index.js';
7+
import { del, down, typescript, up } from './features/schema/index.js';
98

109
main().catch((error) => {
1110
console.error("Error: " + error.message);
@@ -17,7 +16,17 @@ async function main() {
1716

1817
program.version(pkg.version);
1918

20-
program.option("--configPath <path>", "Path to .js(on) config file");
19+
program
20+
.option("--configPath <path>", "Path to .js(on) config file");
21+
22+
program
23+
.command("config")
24+
.description("Returns the current configuration")
25+
.action(async () => {
26+
const cfg = await loadConfig(program.opts().configPath);
27+
28+
printConfig(cfg);
29+
});
2130

2231
program
2332
.command("down <schemaPath>")
@@ -84,7 +93,8 @@ async function main() {
8493
.option("--no-class", "Don't create and register custom Parse.Object")
8594
.option("--no-sdk", "Don't use Parse JS SDK, just TS without dependencies")
8695
.option("--global-sdk", "Use a global Parse JS SDK", false)
87-
.option("--is_esm", "Use ES module imports in generated files.", false)
96+
.option("--is-esm", "Use ES module imports in generated files.", false)
97+
.option("--custom-class-field-types-config <path>", "Path to .json config file for custom class field types")
8898
.action(async (typescriptPath, options) => {
8999
const cfg = await loadConfig(program.opts().configPath);
90100

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { ConfigInterface } from "../index.js";
2+
3+
/**
4+
* Prints the current configuration.
5+
* @param cfg - Configuration object
6+
*/
7+
export function printConfig(cfg: ConfigInterface) {
8+
console.log("Current configuration:");
9+
console.log(
10+
"Location: " +
11+
(process.env.PARSE_SERVER_SCHEMA_CONFIG_PATH ||
12+
"./config/parse-server.config.json (default)")
13+
);
14+
console.log(" Server URL: " + cfg.publicServerURL);
15+
console.log(" App ID: " + cfg.appId);
16+
console.log(" Master Key: " + (cfg.masterKey ? "********" : "(not set)"));
17+
console.log(" Custom Class Field Types: ");
18+
}

src/features/config/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { printConfig } from "./helper/printConfig.js";
2+
export { loadConfig } from "./services/loadConfig.js";
3+
export { loadCustomClassFieldConfig } from "./services/loadCustomClassFieldConfig.js";
4+
export { type ConfigInterface } from "./types/ConfigInterface.js";
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import "dotenv/config";
2+
import fs from "fs";
3+
import path from "path";
4+
import type { ConfigInterface } from "../index.js";
5+
6+
/**
7+
* Load the Parse Server configuration.
8+
* @param configPath The path to the config file.
9+
* @param options Options for loading the config.
10+
* @returns The loaded config.
11+
*/
12+
export async function loadConfig(
13+
configPath?: string,
14+
options?: { operation: string }
15+
): Promise<ConfigInterface> {
16+
const {
17+
PARSE_SERVER_APPLICATION_ID,
18+
PARSE_SERVER_MASTER_KEY,
19+
PARSE_PUBLIC_SERVER_URL,
20+
PARSE_SERVER_URL,
21+
PARSE_SERVER_DOWN_SCHEMA_SERVER_URL,
22+
PARSE_SERVER_DOWN_SCHEMA_APPID,
23+
PARSE_SERVER_DOWN_SCHEMA_MASTERKEY,
24+
PARSE_SERVER_UP_SCHEMA_SERVER_URL,
25+
PARSE_SERVER_UP_SCHEMA_APPID,
26+
PARSE_SERVER_UP_SCHEMA_MASTERKEY,
27+
} = process.env;
28+
29+
//Set url to the fetch schema server url if it is set and options.operation is "down"
30+
if (
31+
options?.operation === "down" &&
32+
PARSE_SERVER_DOWN_SCHEMA_SERVER_URL &&
33+
PARSE_SERVER_DOWN_SCHEMA_APPID &&
34+
PARSE_SERVER_DOWN_SCHEMA_MASTERKEY
35+
) {
36+
console.log(
37+
"[@openinc/parse-server-schema] Using config from process.env with PARSE_SERVER_DOWN_SCHEMA_SERVER_URL: " +
38+
PARSE_SERVER_DOWN_SCHEMA_SERVER_URL +
39+
" with APPID: " +
40+
PARSE_SERVER_DOWN_SCHEMA_APPID +
41+
" and MASTERKEY: " +
42+
PARSE_SERVER_DOWN_SCHEMA_MASTERKEY
43+
);
44+
45+
return {
46+
publicServerURL: PARSE_SERVER_DOWN_SCHEMA_SERVER_URL,
47+
appId: PARSE_SERVER_DOWN_SCHEMA_APPID,
48+
masterKey: PARSE_SERVER_DOWN_SCHEMA_MASTERKEY,
49+
};
50+
}
51+
52+
//Set url to the push schema server url if it is set and options.operation is "up"
53+
if (
54+
options?.operation === "up" &&
55+
PARSE_SERVER_UP_SCHEMA_SERVER_URL &&
56+
PARSE_SERVER_UP_SCHEMA_APPID &&
57+
PARSE_SERVER_UP_SCHEMA_MASTERKEY
58+
) {
59+
console.log(
60+
"[@openinc/parse-server-schema] Using config from process.env with PARSE_SERVER_UP_SCHEMA_SERVER_URL: " +
61+
PARSE_SERVER_UP_SCHEMA_SERVER_URL +
62+
" with APPID: " +
63+
PARSE_SERVER_UP_SCHEMA_APPID +
64+
" and MASTERKEY: " +
65+
PARSE_SERVER_UP_SCHEMA_MASTERKEY
66+
);
67+
68+
return {
69+
publicServerURL: PARSE_SERVER_UP_SCHEMA_SERVER_URL,
70+
appId: PARSE_SERVER_UP_SCHEMA_APPID,
71+
masterKey: PARSE_SERVER_UP_SCHEMA_MASTERKEY,
72+
};
73+
}
74+
75+
//Use default env variables
76+
const url = PARSE_SERVER_URL || PARSE_PUBLIC_SERVER_URL;
77+
if (PARSE_SERVER_APPLICATION_ID && PARSE_SERVER_MASTER_KEY && url) {
78+
console.log(
79+
"[@openinc/parse-server-schema] Using config from process.env with PARSE_SERVER_URL: " +
80+
url +
81+
" with APPID: " +
82+
PARSE_SERVER_APPLICATION_ID +
83+
" and MASTERKEY: " +
84+
PARSE_SERVER_MASTER_KEY
85+
);
86+
87+
return {
88+
publicServerURL: url,
89+
appId: PARSE_SERVER_APPLICATION_ID,
90+
masterKey: PARSE_SERVER_MASTER_KEY,
91+
};
92+
}
93+
94+
configPath = path.resolve(configPath || "config/parse-server.config.json");
95+
96+
if (!fs.existsSync(configPath)) {
97+
console.error(
98+
`[@openinc/parse-server-schema] No config at '${configPath}'`
99+
);
100+
process.exit(1);
101+
}
102+
103+
let config;
104+
105+
if (configPath.endsWith(".js")) {
106+
config = require(configPath);
107+
}
108+
109+
if (configPath.endsWith(".json")) {
110+
config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
111+
}
112+
113+
if (!config) {
114+
console.error(`[@openinc/parse-server-schema] Invalid config file type`);
115+
process.exit(1);
116+
}
117+
118+
if (!config.publicServerURL) {
119+
console.error(
120+
`[@openinc/parse-server-schema] Invalid config: Missing key 'publicServerURL'.`
121+
);
122+
process.exit(1);
123+
}
124+
125+
if (!config.appId) {
126+
console.error(
127+
`[@openinc/parse-server-schema] Invalid config: Missing key 'appId'.`
128+
);
129+
process.exit(1);
130+
}
131+
132+
if (!config.masterKey) {
133+
console.error(
134+
`[@openinc/parse-server-schema] Invalid config: Missing key 'masterKey'.`
135+
);
136+
process.exit(1);
137+
}
138+
139+
return config;
140+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import type { CustomClassFieldType } from "../../schema/index.js";
4+
5+
/**
6+
* Loads the custom class field configuration from a .json file.
7+
* @param configPath Path to .json config file
8+
* @returns An array of custom class field types or undefined if not found.
9+
*/
10+
export function loadCustomClassFieldConfig(
11+
configPath: string
12+
): CustomClassFieldType[] {
13+
const resolvedPath = path.resolve(configPath);
14+
if (fs.existsSync(resolvedPath)) {
15+
const rawData = fs.readFileSync(resolvedPath, "utf-8");
16+
return JSON.parse(rawData);
17+
}
18+
return [];
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Configuration interface for Parse Server Schema tool.
3+
*/
4+
export interface ConfigInterface {
5+
publicServerURL: string;
6+
appId: string;
7+
masterKey: string;
8+
}

0 commit comments

Comments
 (0)