diff --git a/packages/medusa/src/commands/develop.js b/packages/medusa/src/commands/develop.js index e573ee3a6b..096e989b0f 100644 --- a/packages/medusa/src/commands/develop.js +++ b/packages/medusa/src/commands/develop.js @@ -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 }) { diff --git a/packages/medusa/src/commands/migrate.js b/packages/medusa/src/commands/migrate.js index dc815def86..23d9791db0 100644 --- a/packages/medusa/src/commands/migrate.js +++ b/packages/medusa/src/commands/migrate.js @@ -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, diff --git a/packages/medusa/src/commands/seed.js b/packages/medusa/src/commands/seed.js new file mode 100644 index 0000000000..46adabe806 --- /dev/null +++ b/packages/medusa/src/commands/seed.js @@ -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 diff --git a/packages/medusa/src/commands/start.js b/packages/medusa/src/commands/start.js index 6d66f33613..1d4a95a04e 100644 --- a/packages/medusa/src/commands/start.js +++ b/packages/medusa/src/commands/start.js @@ -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" diff --git a/packages/medusa/src/commands/user.js b/packages/medusa/src/commands/user.js index 0c7a6ee18c..90ffe790fc 100644 --- a/packages/medusa/src/commands/user.js +++ b/packages/medusa/src/commands/user.js @@ -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() } diff --git a/packages/medusa/src/commands/utils/get-migrations.js b/packages/medusa/src/commands/utils/get-migrations.js new file mode 100644 index 0000000000..72c79e103d --- /dev/null +++ b/packages/medusa/src/commands/utils/get-migrations.js @@ -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 +}