Initial commit: backend, storefront, vendor-panel added
This commit is contained in:
46
vendor-panel/scripts/generate-currencies.js
Normal file
46
vendor-panel/scripts/generate-currencies.js
Normal file
@@ -0,0 +1,46 @@
|
||||
async function generateCurrencies() {
|
||||
const { currencies } = await import(
|
||||
"@medusajs/medusa/dist/utils/currencies.js"
|
||||
)
|
||||
const fs = await import("fs")
|
||||
const path = await import("path")
|
||||
|
||||
const record = Object.entries(currencies).reduce((acc, [key, values]) => {
|
||||
const code = values.code
|
||||
const symbol_native = values.symbol_native
|
||||
const name = values.name
|
||||
const decimal_digits = values.decimal_digits
|
||||
|
||||
acc[key] = {
|
||||
code,
|
||||
name,
|
||||
symbol_native,
|
||||
decimal_digits,
|
||||
}
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
|
||||
const json = JSON.stringify(record, null, 2)
|
||||
|
||||
const dest = path.join(__dirname, "../src/lib/currencies.ts")
|
||||
const destDir = path.dirname(dest)
|
||||
|
||||
const fileContent = `/** This file is auto-generated. Do not modify it manually. */\ntype CurrencyInfo = { code: string; name: string; symbol_native: string; decimal_digits: number }\n\nexport const currencies: Record<string, CurrencyInfo> = ${json}`
|
||||
|
||||
if (!fs.existsSync(destDir)) {
|
||||
fs.mkdirSync(destDir, { recursive: true })
|
||||
}
|
||||
|
||||
fs.writeFileSync(dest, fileContent)
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
console.log("Generating currency info")
|
||||
try {
|
||||
await generateCurrencies()
|
||||
console.log("Currency info generated")
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
})()
|
||||
38
vendor-panel/scripts/generate-types.js
Normal file
38
vendor-panel/scripts/generate-types.js
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* We can't use the `tsc` command to generate types for the project because it
|
||||
* will generate types for each file in the project, which isn't needed. We only
|
||||
* need a single file that exports the App component.
|
||||
*/
|
||||
async function generateTypes() {
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
|
||||
const distDir = path.resolve(__dirname, "../dist")
|
||||
const filePath = path.join(distDir, "index.d.ts")
|
||||
|
||||
const fileContent = `
|
||||
import * as react_jsx_runtime from "react/jsx-runtime"
|
||||
|
||||
declare const App: () => react_jsx_runtime.JSX.Element
|
||||
|
||||
export default App
|
||||
`
|
||||
|
||||
// Ensure the dist directory exists
|
||||
if (!fs.existsSync(distDir)) {
|
||||
fs.mkdirSync(distDir)
|
||||
}
|
||||
|
||||
// Write the content to the index.d.ts file
|
||||
fs.writeFileSync(filePath, fileContent.trim(), "utf8")
|
||||
|
||||
console.log(`File created at ${filePath}`)
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
try {
|
||||
await generateTypes()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
})()
|
||||
64
vendor-panel/scripts/i18n/generate-schema.js
Normal file
64
vendor-panel/scripts/i18n/generate-schema.js
Normal file
@@ -0,0 +1,64 @@
|
||||
const fs = require("fs/promises")
|
||||
const path = require("path")
|
||||
const prettier = require("prettier")
|
||||
|
||||
const translationsDir = path.join(__dirname, "../../src/i18n/translations")
|
||||
const enPath = path.join(translationsDir, "en.json")
|
||||
const schemaPath = path.join(translationsDir, "$schema.json")
|
||||
|
||||
function generateSchemaFromObject(obj) {
|
||||
if (typeof obj !== "object" || obj === null) {
|
||||
return { type: typeof obj }
|
||||
}
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return {
|
||||
type: "array",
|
||||
items: generateSchemaFromObject(obj[0] || "string"),
|
||||
}
|
||||
}
|
||||
|
||||
const properties = {}
|
||||
const required = []
|
||||
|
||||
Object.entries(obj).forEach(([key, value]) => {
|
||||
properties[key] = generateSchemaFromObject(value)
|
||||
required.push(key)
|
||||
})
|
||||
|
||||
return {
|
||||
type: "object",
|
||||
properties,
|
||||
required,
|
||||
additionalProperties: false,
|
||||
}
|
||||
}
|
||||
|
||||
async function outputSchema() {
|
||||
const enContent = await fs.readFile(enPath, "utf-8")
|
||||
const enJson = JSON.parse(enContent)
|
||||
|
||||
const schema = {
|
||||
$schema: "http://json-schema.org/draft-07/schema#",
|
||||
...generateSchemaFromObject(enJson),
|
||||
}
|
||||
|
||||
const formattedSchema = await prettier.format(
|
||||
JSON.stringify(schema, null, 2),
|
||||
{
|
||||
parser: "json",
|
||||
}
|
||||
)
|
||||
|
||||
await fs
|
||||
.writeFile(schemaPath, formattedSchema)
|
||||
.then(() => {
|
||||
console.log("Schema generated successfully at:", schemaPath)
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error generating schema:", error.message)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
|
||||
outputSchema()
|
||||
47
vendor-panel/scripts/i18n/validate-translation.js
Normal file
47
vendor-panel/scripts/i18n/validate-translation.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const Ajv = require("ajv")
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
const schema = require("../../src/i18n/translations/$schema.json")
|
||||
|
||||
const ajv = new Ajv({ allErrors: true })
|
||||
const validate = ajv.compile(schema)
|
||||
|
||||
// Get file name from command line arguments
|
||||
const fileName = process.argv[2]
|
||||
|
||||
if (!fileName) {
|
||||
console.error("Please provide a file name (e.g., en.json) as an argument.")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const filePath = path.join(__dirname, "../../src/i18n/translations", fileName)
|
||||
|
||||
try {
|
||||
const translations = JSON.parse(fs.readFileSync(filePath, "utf-8"))
|
||||
|
||||
if (!validate(translations)) {
|
||||
console.error(`\nValidation failed for ${fileName}:`)
|
||||
validate.errors?.forEach((error) => {
|
||||
if (error.keyword === "required") {
|
||||
const missingKeys = error.params.missingProperty
|
||||
console.error(
|
||||
` Missing required key: "${missingKeys}" at ${error.instancePath}`
|
||||
)
|
||||
} else if (error.keyword === "additionalProperties") {
|
||||
const extraKey = error.params.additionalProperty
|
||||
console.error(
|
||||
` Unexpected key: "${extraKey}" at ${error.instancePath}`
|
||||
)
|
||||
} else {
|
||||
console.error(` Error: ${error.message} at ${error.instancePath}`)
|
||||
}
|
||||
})
|
||||
process.exit(1)
|
||||
} else {
|
||||
console.log(`${fileName} matches the schema.`)
|
||||
process.exit(0)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error reading or parsing file: ${error.message}`)
|
||||
process.exit(1)
|
||||
}
|
||||
Reference in New Issue
Block a user