Skip to content

Commit

Permalink
feat: git
Browse files Browse the repository at this point in the history
- fixes on the process
- removed out of main inputs, now after license
  • Loading branch information
Bugs5382 committed Apr 12, 2024
1 parent 48f61ad commit 097f6b6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 39 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -12,7 +12,7 @@
"template/"
],
"engines": {
"node": ">=20.0.0"
"node": ">=20.11.0"
},
"scripts": {
"clean": "rm -rf coverage docs dist",
Expand Down Expand Up @@ -72,7 +72,7 @@
"@types/yargs": "^17.0.32",
"npm-package-json-lint": "^7.1.0",
"precommit": "^1.2.2",
"semantic-release": "^23.0.0",
"semantic-release": "^23.0.8",
"snazzy": "^9.0.0",
"ts-node": "^10.9.2",
"ts-standard": "^12.0.2",
Expand Down
96 changes: 62 additions & 34 deletions src/index.ts
Expand Up @@ -28,7 +28,7 @@ export const main = async (): Promise<void> => {
// set var
const npmName = !isProd() ? undefined : await getProjectName(_.kebabCase(defaultProjectName))

const { npm, gitLocation, repoOwner, repoName, repoPrivateLocation, website, type, node, vite, email, description, license, keywords, port } = await inquirer.prompt([{
const { npm, gitLocation, website, type, node, vite, email, description, license, keywords, port } = await inquirer.prompt([{
default: defaultProjectName,
name: 'npm',
message: 'Your Project NPM name?',
Expand All @@ -45,30 +45,6 @@ export const main = async (): Promise<void> => {
message: 'Where are we storing this code today?',
type: 'list',
filter (val: string) { return val.toLowerCase() }
}, {
type: 'input',
name: 'repoOwner',
message: 'Repository Owner (e.g. https://github.com/{OWNER}/{PROJECT NAME}):',
default: 'Bugs5382', // this is me!
when: (answers) => answers.gitLocation === 'github'
}, {
type: 'input',
name: 'repoName',
message: 'Repository Project Name (e.g. https://github.com/{OWNER}/{PROJECT NAME}):',
validate: (result) => {
if (result === '') {
return 'Error: Please enter a repo name. If the repo does not exist, it will be created for you.'
} else {
return true
}
},
when: (answers) => answers.gitLocation === 'github',
filter (val: string) { return val.toLowerCase() }
}, {
type: 'input',
name: 'repoPrivateLocation',
message: 'Full URL of Git Repo:',
when: (answers) => answers.gitLocation !== 'github' && answers.gitLocation !== 'skip-git'
}, {
name: 'website',
message: 'Homepage of Author:',
Expand Down Expand Up @@ -158,17 +134,69 @@ export const main = async (): Promise<void> => {
let gitIssues: string | undefined
let gitReadme: string | undefined
let gitUrl: string | undefined
let gitOwner: string | undefined
if (gitLocation !== 'skip-git') {
await git.init(cwd, 'initial')
if (gitLocation === 'github' && typeof repoOwner !== 'undefined' && typeof repoName !== 'undefined') {
await git.addRemote(cwd, repoOwner, repoName)
}
switch (gitLocation) {
case 'github':

let { repoOwner, repoName } = await inquirer.prompt([{
type: 'input',
name: 'repoOwner',
message: 'Repository Owner (e.g. https://github.com/[OWNER]):',
default: 'Bugs5382', // this is me!
}, {
type: 'input',
name: 'repoName',
message: 'Repository Project Name (e.g. https://github.com/OWNER/[PROJECT NAME]):',
validate: (result) => {
if (result === '') {
return 'Error: Please enter a repo name. If the repo does not exist, it will be created for you.'
} else {
return true
}
},
filter (val: string) { return val.toLowerCase() }
}])

await git.addRemoteGitHUb(cwd, repoOwner, repoName)

gitUrl = `https://github.com/${repoOwner as string}/${repoName as string}.git`
gitOwner = repoOwner

break
case 'private-repo':

let { repoUrl, repoProject, repoNamePrivate } = await inquirer.prompt([ {
type: 'input',
name: 'repoUrl',
message: 'Full URL of Git Repo (e.g. https://REPOURL) Do not include the trailing /:'
}, {
type: 'input',
name: 'repoProject',
message: 'Project "Folder (e.g. https://REPOURL/[PROJECT]) Do not include the trailing /:'
}, {
type: 'input',
name: 'repoNamePrivate',
message: 'Repository Repo Name (e.g. https://REPOURL/PROJECT/[REPONAME].git}):',
validate: (result) => {
if (result === '') {
return 'Error: Please enter a repo name. If the repo does not exist, it will be potentially created for you.'
} else {
return true
}
},
filter (val: string) { return val.toLowerCase() }
}])

await git.addRemotePrivate(cwd, repoUrl, repoProject, repoNamePrivate )

gitUrl = `${repoUrl as string}/${repoProject as string}/${repoNamePrivate as string}.git`
gitOwner = repoProject

if (gitLocation === 'github') {
gitUrl = `https://github.com/${repoOwner as string}/${repoName as string}`
gitIssues = `${gitUrl}/issues`
gitReadme = `${gitUrl}#readme`
}
gitIssues = `${gitUrl}/issues`
gitReadme = `${gitUrl}#readme`
}

// Generate Licence
Expand All @@ -186,7 +214,7 @@ export const main = async (): Promise<void> => {
description,
gitIssues,
gitReadme,
gitUrl: typeof repoPrivateLocation !== 'undefined' ? repoPrivateLocation : gitUrl,
gitUrl,
license,
keywords
}, {
Expand All @@ -205,7 +233,7 @@ export const main = async (): Promise<void> => {
}, {
npm: typeof npmName !== 'undefined' ? npmName : npm,
author: DEFAULT_NPM.author.name,
repoOwner,
repoOwner: gitOwner,
description,
homepage: website,
license
Expand Down
23 changes: 22 additions & 1 deletion src/modules/git.ts
Expand Up @@ -39,7 +39,7 @@ export async function init (folder: string, step: string): Promise<void> {
* @param repoOwner
* @param repoName
*/
export async function addRemote (folder: string, repoOwner: string, repoName: string): Promise<void> {
export async function addRemoteGitHUb (folder: string, repoOwner: string, repoName: string): Promise<void> {
try {
// If this succeeds, there's already a remote for `origin`.
await execFile('git', ['config', 'remote.origin.url'], { cwd: folder })
Expand All @@ -52,6 +52,27 @@ export async function addRemote (folder: string, repoOwner: string, repoName: st
}
}

/**
* Add Git Remote for Other Repo Location
* @since 1.5.0
* @param folder
* @param repoUrl
* @param repoProject
* @param repoName
*/
export async function addRemotePrivate (folder: string, repoUrl: string, repoProject: string, repoName: string): Promise<void> {
try {
// If this succeeds, there's already a remote for `origin`.
await execFile('git', ['config', 'remote.origin.url'], { cwd: folder })
} catch {
await execFile(
'git',
['remote', 'add', 'origin', `${repoUrl}/${repoProject}/${repoName}.git`],
{ cwd: folder }
)
}
}

export async function getUserName (): Promise<string | undefined> {
try {
const { stdout } = await execFile('git', ['config', 'user.name'])
Expand Down
4 changes: 2 additions & 2 deletions src/modules/npm.ts
Expand Up @@ -119,12 +119,12 @@ export const generatePackageJson = (params: GeneratePackageJsonParams, input: Ge
version: '0.0.0-development',
description: params.description,
engines: {
node: '>=20.0.0'
node: '>=20.11.0'
},
...finalPackage,
repository: {
type: 'git',
url: `git+${params.gitUrl as string}.git`
url: typeof params.gitUrl === 'undefined' ? '' : `git+${params.gitUrl as string}`
},
keywords: params.keywords,
author: params.author,
Expand Down

0 comments on commit 097f6b6

Please sign in to comment.