diff --git a/commands/clone-api-plugins.js b/commands/clone-api-plugins.js index 5f2dd01..ed5a91e 100644 --- a/commands/clone-api-plugins.js +++ b/commands/clone-api-plugins.js @@ -3,20 +3,19 @@ import fs from "fs"; import inquirer from "inquirer"; import isProjectOfType from "../utils/isProjectOfType.js"; import Logger from "../utils/logger.js"; -import wget from "../utils/wget.js"; +import getFileFromCore from "../utils/getFileFromCore.js"; /** * @summary Get and parse local plugins.json file * @returns {Object} - return the local plugins.json as object */ async function getRemotePluginsJson() { - const remotePackageJsonUrl = "https://raw.githubusercontent.com/reactioncommerce/reaction/trunk/plugins.json"; - try { - const pluginsJson = await wget(remotePackageJsonUrl); + const pluginsJson = await getFileFromCore("plugins.json"); + try { return JSON.parse(pluginsJson); } catch (error) { - Logger.error("Unable to get local plugins.json"); + Logger.error("Unable to parse remote plugins.json"); Logger.error(error); throw error; } diff --git a/commands/create-project-api.js b/commands/create-project-api.js index 60b5915..cb61628 100644 --- a/commands/create-project-api.js +++ b/commands/create-project-api.js @@ -1,14 +1,10 @@ import { mkdir, writeFile } from "fs/promises"; import { parse, stringify } from "envfile"; import simpleGit from "simple-git"; -import wget from "../utils/wget.js"; import getFilesFromRepo from "../utils/getFilesFromRepo.js"; import pathExists from "../utils/pathExists.js"; import Logger from "../utils/logger.js"; - - -const reactionAppRoot = "https://raw.githubusercontent.com/reactioncommerce/reaction/trunk/apps/reaction"; -const reactionRoot = "https://raw.githubusercontent.com/reactioncommerce/reaction/trunk/"; +import getFileFromCore, { reactionRoot } from "../utils/getFileFromCore.js"; /** * @summary create project directory @@ -29,8 +25,7 @@ async function makeProject(projectName) { async function getNodeMonVersionFromRoot() { const rootPackage = await getFileFromCore("package.json", reactionRoot); const packageData = JSON.parse(rootPackage); - const nodeMon = packageData.devDependencies.nodemon; - return nodeMon; + return packageData.devDependencies.nodemon; } @@ -64,22 +59,10 @@ async function updatePackageJson(packageJson, projectName) { delete packageData.bugs; packageData.main = "./index.js"; packageData.nodemonConfig.watch.push("custom-packages"); - const nodemon = await getNodeMonVersionFromRoot(); - packageData.devDependencies.nodemon = nodemon; + packageData.devDependencies.nodemon = await getNodeMonVersionFromRoot(); return JSON.stringify(packageData, null, 2); } -/** - * @summary get a single file via HTTP - * @param {String} fileName - The file to get - * @param {String} rootDir - path to root for file - * @returns {Promise} The contents of the file - */ -async function getFileFromCore(fileName, rootDir = reactionAppRoot) { - const contents = await wget(`${rootDir}/${fileName}`); - return contents; -} - /** * @summary update dotenv file to point to local mongo * @param {String} envData - file extracted from the reaction repo @@ -88,8 +71,7 @@ async function getFileFromCore(fileName, rootDir = reactionAppRoot) { function updateEnv(envData) { const env = parse(envData); env.MONGO_URL = "mongodb://localhost:27017/reaction"; - const updatedEnv = stringify(env); - return updatedEnv; + return stringify(env); } /** diff --git a/utils/getFileFromCore.js b/utils/getFileFromCore.js new file mode 100644 index 0000000..4805a5a --- /dev/null +++ b/utils/getFileFromCore.js @@ -0,0 +1,20 @@ +import wget from "./wget.js"; +import Logger from "./logger.js"; + +export const reactionAppRoot = "https://raw.githubusercontent.com/reactioncommerce/reaction/trunk/apps/reaction"; +export const reactionRoot = "https://raw.githubusercontent.com/reactioncommerce/reaction/trunk/"; + +/** + * @summary get a single file via HTTP + * @param {String} fileName - The file to get + * @param {String} rootDir - path to root for file + * @returns {Promise} The contents of the file + */ +export default async function getFileFromCore(fileName, rootDir = reactionAppRoot) { + try { + return wget(`${rootDir}/${fileName}`); + } catch (error) { + Logger.error(`Unable to get file from ${rootDir}/${fileName}`); + throw error; + } +}