This commit is contained in:
Sebastian Rindom
2021-06-02 13:44:25 +02:00
parent b20d5c7150
commit 42d9a487ff
6 changed files with 192 additions and 135 deletions

View File

@@ -1,10 +1,6 @@
import { spawn, execSync } from "child_process"
import mongoose from "mongoose"
import chokidar from "chokidar"
import express from "express"
import path from "path"
import loaders from "../loaders"
import Logger from "../loaders/logger"
export default async function({ port, directory }) {

View File

@@ -1,96 +1,9 @@
import { createConnection, MigrationExecutor } from "typeorm"
import { spawn, execSync } from "child_process"
import chokidar from "chokidar"
import path from "path"
import fs from "fs"
import { createConnection } from "typeorm"
import _ from "lodash"
import { getConfigFile, createRequireFromPath } from "medusa-core-utils"
import { sync as existsSync } from "fs-exists-cached"
import loaders from "../loaders"
import Logger from "../loaders/logger"
function createFileContentHash(path, files) {
return path + files
}
// TODO: Create unique id for each plugin
function createPluginId(name) {
return name
}
/**
* Finds the correct path for the plugin. If it is a local plugin it will be
* found in the plugins folder. Otherwise we will look for the plugin in the
* installed npm packages.
* @param {string} pluginName - the name of the plugin to find. Should match
* the name of the folder where the plugin is contained.
* @return {object} the plugin details
*/
function resolvePlugin(pluginName) {
// Only find plugins when we're not given an absolute path
if (!existsSync(pluginName)) {
// Find the plugin in the local plugins folder
const resolvedPath = path.resolve(`./plugins/${pluginName}`)
if (existsSync(resolvedPath)) {
if (existsSync(`${resolvedPath}/package.json`)) {
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
const name = packageJSON.name || pluginName
//warnOnIncompatiblePeerDependency(name, packageJSON)
return {
resolve: resolvedPath,
name,
id: createPluginId(name),
options: {},
version:
packageJSON.version || createFileContentHash(resolvedPath, `**`),
}
} else {
// Make package.json a requirement for local plugins too
throw new Error(`Plugin ${pluginName} requires a package.json file`)
}
}
}
const rootDir = path.resolve(".")
/**
* Here we have an absolute path to an internal plugin, or a name of a module
* which should be located in node_modules.
*/
try {
const requireSource =
rootDir !== null
? createRequireFromPath(`${rootDir}/:internal:`)
: require
// If the path is absolute, resolve the directory of the internal plugin,
// otherwise resolve the directory containing the package.json
const resolvedPath = path.dirname(
requireSource.resolve(`${pluginName}/package.json`)
)
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
// warnOnIncompatiblePeerDependency(packageJSON.name, packageJSON)
return {
resolve: resolvedPath,
id: createPluginId(packageJSON.name),
name: packageJSON.name,
version: packageJSON.version,
}
} catch (err) {
throw new Error(
`Unable to find plugin "${pluginName}". Perhaps you need to install its package?`
)
}
}
import getMigrations from "./utils/get-migrations"
const t = async function({ port, directory }) {
const args = process.argv
@@ -98,39 +11,7 @@ const t = async function({ port, directory }) {
args.shift()
args.shift()
const { configModule } = getConfigFile(directory, `medusa-config`)
const { plugins } = configModule
const resolved = plugins.map(plugin => {
if (_.isString(plugin)) {
return resolvePlugin(plugin)
}
const details = resolvePlugin(plugin.resolve)
details.options = plugin.options
return details
})
resolved.push({
resolve: `${directory}/dist`,
name: `project-plugin`,
id: createPluginId(`project-plugin`),
options: {},
version: createFileContentHash(process.cwd(), `**`),
})
const migrationDirs = []
const coreMigrations = path.resolve(__dirname, "../migrations")
migrationDirs.push(`${coreMigrations}/*.js`)
for (const p of resolved) {
const exists = existsSync(`${p.resolve}/migrations`)
if (exists) {
migrationDirs.push(`${p.resolve}/migrations/*.js`)
}
}
const migrationDirs = getMigrations(directory)
const connection = await createConnection({
type: configModule.projectConfig.database_type,

View File

@@ -0,0 +1,63 @@
import { createConnection } from "typeorm"
const t = async function({ port, directory }) {
const args = process.argv
args.shift()
args.shift()
args.shift()
const { configModule } = getConfigFile(directory, `medusa-config`)
const { plugins } = configModule
const resolved = plugins.map(plugin => {
if (_.isString(plugin)) {
return resolvePlugin(plugin)
}
const details = resolvePlugin(plugin.resolve)
details.options = plugin.options
return details
})
resolved.push({
resolve: `${directory}/dist`,
name: `project-plugin`,
id: createPluginId(`project-plugin`),
options: {},
version: createFileContentHash(process.cwd(), `**`),
})
const migrationDirs = []
const coreMigrations = path.resolve(__dirname, "../migrations")
migrationDirs.push(`${coreMigrations}/*.js`)
for (const p of resolved) {
const exists = existsSync(`${p.resolve}/migrations`)
if (exists) {
migrationDirs.push(`${p.resolve}/migrations/*.js`)
}
}
const connection = await createConnection({
type: configModule.projectConfig.database_type,
url: configModule.projectConfig.database_url,
extra: configModule.projectConfig.database_extra || {},
migrations: migrationDirs,
logging: true,
})
if (args[0] === "run") {
await connection.runMigrations()
await connection.close()
Logger.info("Migrations completed.")
process.exit()
} else if (args[0] === "show") {
const unapplied = await connection.showMigrations()
await connection.close()
process.exit(unapplied ? 1 : 0)
}
}
export default t

View File

@@ -1,10 +1,7 @@
import "core-js/stable"
import "regenerator-runtime/runtime"
import mongoose from "mongoose"
import chokidar from "chokidar"
import express from "express"
import cwdResolve from "resolve-cwd"
import loaders from "../loaders"
import Logger from "../loaders/logger"

View File

@@ -1,23 +1,19 @@
import "core-js/stable"
import "regenerator-runtime/runtime"
import mongoose from "mongoose"
import chokidar from "chokidar"
import express from "express"
import cwdResolve from "resolve-cwd"
import loaders from "../loaders"
import Logger from "../loaders/logger"
export default async function({ directory, id, email, password }) {
const app = express()
const { container, dbConnection } = await loaders({
const { container } = await loaders({
directory,
expressApp: app,
})
const userService = container.resolve("userService")
const user = await userService.create({ id, email }, password)
await userService.create({ id, email }, password)
process.exit()
}

View File

@@ -0,0 +1,124 @@
import path from "path"
import fs from "fs"
import { sync as existsSync } from "fs-exists-cached"
import { getConfigFile, createRequireFromPath } from "medusa-core-utils"
function createFileContentHash(path, files) {
return path + files
}
// TODO: Create unique id for each plugin
function createPluginId(name) {
return name
}
/**
* Finds the correct path for the plugin. If it is a local plugin it will be
* found in the plugins folder. Otherwise we will look for the plugin in the
* installed npm packages.
* @param {string} pluginName - the name of the plugin to find. Should match
* the name of the folder where the plugin is contained.
* @return {object} the plugin details
*/
function resolvePlugin(pluginName) {
// Only find plugins when we're not given an absolute path
if (!existsSync(pluginName)) {
// Find the plugin in the local plugins folder
const resolvedPath = path.resolve(`./plugins/${pluginName}`)
if (existsSync(resolvedPath)) {
if (existsSync(`${resolvedPath}/package.json`)) {
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
const name = packageJSON.name || pluginName
//warnOnIncompatiblePeerDependency(name, packageJSON)
return {
resolve: resolvedPath,
name,
id: createPluginId(name),
options: {},
version:
packageJSON.version || createFileContentHash(resolvedPath, `**`),
}
} else {
// Make package.json a requirement for local plugins too
throw new Error(`Plugin ${pluginName} requires a package.json file`)
}
}
}
const rootDir = path.resolve(".")
/**
* Here we have an absolute path to an internal plugin, or a name of a module
* which should be located in node_modules.
*/
try {
const requireSource =
rootDir !== null
? createRequireFromPath(`${rootDir}/:internal:`)
: require
// If the path is absolute, resolve the directory of the internal plugin,
// otherwise resolve the directory containing the package.json
const resolvedPath = path.dirname(
requireSource.resolve(`${pluginName}/package.json`)
)
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
// warnOnIncompatiblePeerDependency(packageJSON.name, packageJSON)
return {
resolve: resolvedPath,
id: createPluginId(packageJSON.name),
name: packageJSON.name,
version: packageJSON.version,
}
} catch (err) {
throw new Error(
`Unable to find plugin "${pluginName}". Perhaps you need to install its package?`
)
}
}
export default directory => {
const { configModule } = getConfigFile(directory, `medusa-config`)
const { plugins } = configModule
const resolved = plugins.map(plugin => {
if (_.isString(plugin)) {
return resolvePlugin(plugin)
}
const details = resolvePlugin(plugin.resolve)
details.options = plugin.options
return details
})
resolved.push({
resolve: `${directory}/dist`,
name: `project-plugin`,
id: createPluginId(`project-plugin`),
options: {},
version: createFileContentHash(process.cwd(), `**`),
})
const migrationDirs = []
const coreMigrations = path.resolve(__dirname, "../migrations")
migrationDirs.push(`${coreMigrations}/*.js`)
for (const p of resolved) {
const exists = existsSync(`${p.resolve}/migrations`)
if (exists) {
migrationDirs.push(`${p.resolve}/migrations/*.js`)
}
}
return migrationDirs
}