-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add populate option when create api project
Signed-off-by: Brian Nguyen <[email protected]>
- Loading branch information
Showing
4 changed files
with
73 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.env | ||
.vscode | ||
node_modules | ||
mydir | ||
myadmin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { mkdir, writeFile } from "fs/promises"; | ||
import { mkdir, writeFile, readFile } from "fs/promises"; | ||
import { parse, stringify } from "envfile"; | ||
import simpleGit from "simple-git"; | ||
import wget from "../utils/wget.js"; | ||
|
@@ -9,6 +9,20 @@ import Logger from "../utils/logger.js"; | |
|
||
const reactionRepoRoot = "https://raw.githubusercontent.com/reactioncommerce/reaction/trunk"; | ||
|
||
/** | ||
* @summary create the git instance | ||
* @param {String} projectName - The name of the directory to create | ||
* @returns {Object} - the git instance | ||
*/ | ||
function getGitInstance(projectName) { | ||
const gitOptions = { | ||
baseDir: `${process.cwd()}/${projectName}`, | ||
binary: "git", | ||
maxConcurrentProcesses: 6 | ||
}; | ||
return simpleGit(gitOptions); | ||
} | ||
|
||
/** | ||
* @summary create project directory | ||
* @param {String} projectName - The name of the directory to create | ||
|
@@ -68,21 +82,28 @@ async function getFileFromCore(fileName) { | |
/** | ||
* @summary update dotenv file to point to local mongo | ||
* @param {String} envData - file extracted from the reaction repo | ||
* @param {Object} options - Any options for project creation | ||
* @returns {String} updated env file | ||
*/ | ||
function updateEnv(envData) { | ||
function updateEnv(envData, options = {}) { | ||
const env = parse(envData); | ||
env.MONGO_URL = "mongodb://localhost:27017/reaction"; | ||
|
||
if (options.populate) { | ||
env.LOAD_SAMPLE_DATA = true; | ||
} | ||
|
||
const updatedEnv = stringify(env); | ||
return updatedEnv; | ||
} | ||
|
||
/** | ||
* @summary get files directory from core repo | ||
* @param {String} projectName - The name of the project we are creating | ||
* @param {Object} options - Any options for project creation | ||
* @returns {Promise<Boolean>} True if success | ||
*/ | ||
async function getFilesFromCore(projectName) { | ||
async function getFilesFromCore(projectName, options) { | ||
// get files directly from repo so it's always up-to-date | ||
const packageJson = await getFileFromCore("package.json"); | ||
const updatedPackageJson = await updatePackageJson(packageJson, projectName); | ||
|
@@ -104,7 +125,7 @@ async function getFilesFromCore(projectName) { | |
await writeFile(`${projectName}/.nvmrc`, nvmrc); | ||
|
||
const dotenv = await getFileFromCore(".env.example"); | ||
const updatedDotEnv = updateEnv(dotenv); | ||
const updatedDotEnv = updateEnv(dotenv, options); | ||
await writeFile(`${projectName}/.env`, updatedDotEnv); | ||
return true; | ||
} | ||
|
@@ -116,19 +137,40 @@ async function getFilesFromCore(projectName) { | |
* @returns {Promise<Boolean>} true if success | ||
*/ | ||
async function gitInitDirectory(projectName) { | ||
const gitOptions = { | ||
baseDir: `${process.cwd()}/${projectName}`, | ||
binary: "git", | ||
maxConcurrentProcesses: 6 | ||
}; | ||
const git = simpleGit(gitOptions); | ||
const git = getGitInstance(projectName); | ||
try { | ||
await git.init(); | ||
} catch (error) { | ||
Logger.error(error); | ||
} | ||
} | ||
|
||
/** | ||
* @summary add the sample data plugin to project | ||
* @param {String} projectName name of the project to create | ||
* @param {String} pluginName The plugin name | ||
* @returns {Promise<boolean>} true for success | ||
*/ | ||
async function addSampleDataPlugin(projectName) { | ||
const git = getGitInstance(projectName); | ||
try { | ||
await git.clone("[email protected]:reactioncommerce/api-plugin-sample-data.git", "custom-packages/api-plugin-sample-data"); | ||
|
||
const pluginJsonPath = `${projectName}/plugins.json`; | ||
const plugins = JSON.parse(await readFile(pluginJsonPath)); | ||
plugins.sampleData = "./custom-packages/api-plugin-sample-data/index.js"; | ||
|
||
await writeFile(pluginJsonPath, JSON.stringify(plugins, null, "\t")); | ||
|
||
Logger.info("Added the sample data plugin successfully."); | ||
return true; | ||
} catch (error) { | ||
Logger.error(error); | ||
Logger.warn("Can't add the sample data plugin by error. Please add it manual."); | ||
return false; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @summary clones projects locally from repo | ||
|
@@ -148,10 +190,14 @@ export default async function createProjectApi(projectName, options) { | |
await getFilesFromRepo("/templates/api-project/", projectName); | ||
|
||
// copy files directly from core that we want to be current | ||
await getFilesFromCore(projectName); | ||
await getFilesFromCore(projectName, options); | ||
|
||
// git init the new project | ||
await gitInitDirectory(projectName); | ||
|
||
if (options.populate) { | ||
await addSampleDataPlugin(projectName); | ||
} | ||
|
||
Logger.success("Project creation complete. Change to your directory and run `npm install`"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters