|
| 1 | +import * as TYPES from '../../types/types.js' // eslint-disable-line |
| 2 | +import select from '@inquirer/select' |
| 3 | +import input from '@inquirer/input' |
| 4 | + |
| 5 | +/** |
| 6 | +* @ignore |
| 7 | +* @typedef {TYPES.LOCALDATA} LOCALDATA {@link LOCALDATA} |
| 8 | +*/ |
| 9 | + |
| 10 | +/** |
| 11 | + * #### select what to initialize |
| 12 | + * @async |
| 13 | + * @returns {Promise<string>} - init type |
| 14 | + */ |
| 15 | +async function initType () { |
| 16 | + const answer = await select({ |
| 17 | + message: 'Initailize:', |
| 18 | + choices: [ |
| 19 | + { |
| 20 | + name: 'GitHub repo based on template', |
| 21 | + value: 'github-repo-based-on-template', |
| 22 | + description: 'Create a new GitHub repo based on a GitHub template repo' |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'Local repo based on template', |
| 26 | + value: 'local-repo-based-on-template', |
| 27 | + description: 'Create a new local repo based on a GitHub template repo', |
| 28 | + disabled: true |
| 29 | + } |
| 30 | + ] |
| 31 | + }) |
| 32 | + return answer |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * #### select new repo owner |
| 37 | + * @async |
| 38 | + * @private |
| 39 | + * @param {LOCALDATA} data - data object |
| 40 | + * @returns {Promise<string>} - new repo owner |
| 41 | + */ |
| 42 | +async function selectRepoOwner (data) { |
| 43 | + if (data.github && data.github.orgs && data.github.login) { |
| 44 | + const owners = data.github.orgs.map((/** @type {string} */ org) => { |
| 45 | + return { |
| 46 | + name: org, |
| 47 | + value: org, |
| 48 | + description: 'Repository owner to create the new repository' |
| 49 | + } |
| 50 | + }) |
| 51 | + owners.push({ |
| 52 | + name: data.github.login, |
| 53 | + value: data.github.login, |
| 54 | + description: 'Repository owner to create the new repository' |
| 55 | + }) |
| 56 | + const answer = await select({ |
| 57 | + message: 'Select repo owner:', |
| 58 | + choices: owners |
| 59 | + }) |
| 60 | + return answer |
| 61 | + } else { |
| 62 | + throw new Error('No GitHub user/orgs found') |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * #### get project name |
| 68 | + * @async |
| 69 | + * @private |
| 70 | + * @returns {Promise<string>} - project name |
| 71 | + */ |
| 72 | +async function getProjectName () { |
| 73 | + const getProjectName = await input({ |
| 74 | + message: 'New project name:', |
| 75 | + validate: (input) => { |
| 76 | + if (input.length < 1) { |
| 77 | + return 'Please enter a project name' |
| 78 | + } |
| 79 | + if (!/^[a-zA-Z0-9-]*$/.test(input)) { |
| 80 | + return 'Please enter a valid project name in one word or with hyphens' |
| 81 | + } |
| 82 | + return true |
| 83 | + } |
| 84 | + }) |
| 85 | + const projectName = getProjectName.toLowerCase().trim() |
| 86 | + return projectName |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * #### Select child theme |
| 91 | + * @async |
| 92 | + * @private |
| 93 | + * @returns {Promise<string>} - child theme |
| 94 | + */ |
| 95 | +async function selectRepoTemplate () { |
| 96 | + const answer = await select({ |
| 97 | + message: 'Choose template:', |
| 98 | + choices: [ |
| 99 | + { |
| 100 | + name: 'Nimbly lite child', |
| 101 | + value: 'nimbly-lite-child', |
| 102 | + description: 'Nimbly Light child theme template' |
| 103 | + }, |
| 104 | + { |
| 105 | + name: 'Nimbly pro child', |
| 106 | + value: 'nimbly-pro-child', |
| 107 | + description: 'Nimbly Pro child theme template', |
| 108 | + disabled: true |
| 109 | + } |
| 110 | + ] |
| 111 | + }) |
| 112 | + return answer |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * #### collect data for the new child theme |
| 117 | + * @param {LOCALDATA} data - env variables |
| 118 | + * @returns undefined |
| 119 | + */ |
| 120 | +async function githubRepoFromTemplatePrompts (data) { |
| 121 | + const childTheme = await selectRepoTemplate() |
| 122 | + const projectName = await getProjectName() |
| 123 | + const repoOwner = await selectRepoOwner(data) |
| 124 | + data.prompts = { |
| 125 | + repoFromTmpl: { |
| 126 | + newRepoOwner: repoOwner, |
| 127 | + newRepoName: `${projectName}-${childTheme}-${new Date().getFullYear()}`, |
| 128 | + newRepoLabel: `${projectName} theme`, |
| 129 | + templateRepoOwner: 'Resultify', |
| 130 | + templateRepoName: childTheme |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +export { githubRepoFromTemplatePrompts, initType } |
0 commit comments