Skip to content

Commit b10baa9

Browse files
committed
[TASK] add git clone and addDotEnvFile fn-s, use it with rh init cmd
1 parent b185bb8 commit b10baa9

5 files changed

Lines changed: 83 additions & 7 deletions

File tree

lib/cmd/init.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/// <reference path="../types/types.js" />
22
import { checkIsOnline, checkSshConnection } from '../utils/check.js'
33
import { checkGithubTokenEnv, checkGithubToken, getGithubOrgs } from '../github/auth.js'
4-
import { githubRepoFromTemplatePrompts, localRepoFromTemplatePrompts, initType } from './init/prompts.js'
4+
import { githubRepoFromTemplatePrompts, localRepoFromTemplatePrompts, initType, confirmClone } from './init/prompts.js'
55
import { createGithubRepoWithTemplate, updateGithubRepoSettings, updateGithubBranchProtection } from '../github/repo.js'
66
import { addRepositorySecrets } from '../github/secrets.js'
77
import { updateAndCommit } from '../github/commit.js'
88
import { confirmNextSteps } from '../utils/prompts.js'
9-
import { gitCloneDepth1, gitInit, gitAddAll, gitCommit } from '../git/git.js'
10-
import { parseJson, removePath, writeJson } from '../utils/fs.js'
9+
import { gitCloneDepth1, gitInit, gitAddAll, gitCommit, gitClone } from '../git/git.js'
10+
import { parseJson, removePath, writeJson, addDotEnvFile } from '../utils/fs.js'
1111
import chalk from 'chalk'
1212

1313
/**
@@ -85,6 +85,18 @@ async function initGithubRepoBasedOnTemplate (data) {
8585
label: `${data.prompts?.githubRepoFromTmpl?.newRepoLabel.charAt(0).toUpperCase()}${data.prompts?.githubRepoFromTmpl?.newRepoLabel.slice(1)}`
8686
}
8787
)
88+
if (await confirmClone()) {
89+
if (data.git?.isRepo) {
90+
console.warn('Better to clone the new repository to a simple folder rather than a git repository')
91+
console.log('You can do it manually with the following command:')
92+
console.log(chalk.yellow(`git clone git@github.com:${data?.prompts?.githubRepoFromTmpl?.newRepoOwner}/${data.prompts?.githubRepoFromTmpl?.newRepoName}.git`))
93+
process.exit(1)
94+
}
95+
if (data.prompts?.githubRepoFromTmpl?.newRepoName && data.prompts?.githubRepoFromTmpl?.newRepoOwner && data.prompts?.githubRepoFromTmpl.projectName) {
96+
await gitClone(data.prompts.githubRepoFromTmpl.newRepoOwner, data.prompts.githubRepoFromTmpl.newRepoName)
97+
await addDotEnvFile(data.cwd.path, data.prompts.githubRepoFromTmpl.newRepoName, data.prompts.githubRepoFromTmpl.projectName)
98+
}
99+
}
88100
}
89101

90102
/**
@@ -127,6 +139,9 @@ async function initLocalRepoBasedOnTemplate (data) {
127139
await writeJson(`${data?.cwd.path}/${data.prompts?.localRepoFromTmpl?.newRepoName}/theme/theme.json`, themeJson)
128140
await gitAddAll({ cwd: `${data?.cwd.path}/${data.prompts?.localRepoFromTmpl?.newRepoName}` })
129141
await gitCommit('[TASK] update package.json and theme.json with new project info', { cwd: `${data?.cwd.path}/${data.prompts?.localRepoFromTmpl?.newRepoName}` })
142+
if (data.prompts?.localRepoFromTmpl?.newRepoName && data.prompts?.localRepoFromTmpl.projectName) {
143+
await addDotEnvFile(data.cwd.path, data.prompts.localRepoFromTmpl.newRepoName, data.prompts.localRepoFromTmpl.projectName)
144+
}
130145
}
131146
}
132147

lib/cmd/init/prompts.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ async function confirmRepoPrivacy () {
120120
return answer
121121
}
122122

123+
/**
124+
* #### confirm to clone just created repo
125+
* @async
126+
* @private
127+
* @returns {Promise<boolean>} - is private
128+
*/
129+
async function confirmClone () {
130+
const answer = await confirm({
131+
message: 'Clone the repository?'
132+
})
133+
return answer
134+
}
135+
123136
/**
124137
* #### confirm if to add secrets to the repo
125138
* @async
@@ -188,6 +201,7 @@ async function githubRepoFromTemplatePrompts (data) {
188201
}
189202
data.prompts = {
190203
githubRepoFromTmpl: {
204+
projectName,
191205
newRepoOwner: repoOwner,
192206
newRepoName: `${projectName}-${childTheme}-${new Date().getFullYear()}`,
193207
newRepoLabel: `${projectName} theme`,
@@ -210,6 +224,7 @@ async function localRepoFromTemplatePrompts (data) {
210224
const projectName = await getProjectName()
211225
data.prompts = {
212226
localRepoFromTmpl: {
227+
projectName,
213228
newRepoName: `${projectName}-${childTheme}-${new Date().getFullYear()}`,
214229
newRepoLabel: `${projectName} theme`,
215230
templateRepoOwner: 'Resultify',
@@ -218,4 +233,4 @@ async function localRepoFromTemplatePrompts (data) {
218233
}
219234
}
220235

221-
export { githubRepoFromTemplatePrompts, localRepoFromTemplatePrompts, initType }
236+
export { githubRepoFromTemplatePrompts, localRepoFromTemplatePrompts, initType, confirmClone }

lib/git/git.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,14 @@ async function saveGitlog (data) {
589589
}
590590

591591
/**
592-
* #### git clone repo
592+
* #### git clone repo depth 1
593593
* @async
594594
* @memberof GIT
595595
* @param {string} folder - folder name
596596
* @returns undefined
597597
*/
598598
async function gitCloneDepth1 (folder) {
599-
const spinner = ora('Clone repo').start()
599+
const spinner = ora('Cloning the repository').start()
600600
try {
601601
await $`git clone --depth 1 git@github.com:Resultify/nimbly-lite-child.git ${folder}`
602602
spinner.succeed()
@@ -606,6 +606,25 @@ async function gitCloneDepth1 (folder) {
606606
}
607607
}
608608

609+
/**
610+
* #### git clone repo
611+
* @async
612+
* @memberof GIT
613+
* @param {string} owner - owner
614+
* @param {string} repo - repo
615+
* @returns undefined
616+
*/
617+
async function gitClone (owner, repo) {
618+
const spinner = ora('Cloning the repository').start()
619+
try {
620+
await $`git clone git@github.com:${owner}/${repo}.git`
621+
spinner.succeed()
622+
} catch (error) {
623+
spinner.fail()
624+
console.error(error)
625+
}
626+
}
627+
609628
/**
610629
* #### git init
611630
* @async
@@ -641,6 +660,7 @@ export {
641660
gitPushTag,
642661
saveGitlog,
643662
gitCloneDepth1,
663+
gitClone,
644664
gitInit,
645665
gitAddAll
646666
}

lib/types/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
/**
8282
* #### github repository from template prompt data
8383
* @typedef {Object} GITHUB_REPO_FROM_TEMPLATE_PROMPT_DATA
84+
* @property {string} projectName - project name
8485
* @property {string} newRepoOwner - new repo owner
8586
* @property {string} newRepoName - new repo name
8687
* @property {string} newRepoLabel - new repo label
@@ -96,6 +97,7 @@
9697
/**
9798
* #### local repository from template prompt data
9899
* @typedef {Object} LOCAL_REPO_FROM_TEMPLATE_PROMPT_DATA
100+
* @property {string} projectName - project name
99101
* @property {string} newRepoName - new repo name
100102
* @property {string} newRepoLabel - new repo label
101103
* @property {string} templateRepoOwner - template repo owner

lib/utils/fs.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,28 @@ async function removePath (path) {
8383
}
8484
}
8585

86-
export { parseCliPackageJson, parseJson, isFileDirExists, writeJson, removePath }
86+
/**
87+
* #### Create .env file
88+
* @async
89+
* @param {string} cwd - current working directory
90+
* @param {string} destination - destination path
91+
* @param {string} portalname - portal name
92+
* @returns undefined
93+
*/
94+
async function addDotEnvFile (cwd, destination, portalname) {
95+
if (cwd !== '') {
96+
cwd = `${cwd}/`
97+
}
98+
if (destination !== '') {
99+
destination = `${destination}/`
100+
}
101+
if (!await isFileDirExists(`${cwd}${destination}.env`)) {
102+
try {
103+
await fsPromises.appendFile(`${cwd}${destination}.env`, `hub_${portalname}=personal-access-key-for-${portalname}`)
104+
} catch (error) {
105+
console.error(error)
106+
}
107+
}
108+
}
109+
110+
export { parseCliPackageJson, parseJson, isFileDirExists, writeJson, removePath, addDotEnvFile }

0 commit comments

Comments
 (0)