11import * as TYPES from '../types/types.js' // eslint-disable-line
2- import { checkIsOnline } from '../utils/check.js'
2+ import { checkIsOnline , checkSshConnection } from '../utils/check.js'
33import { checkGithubTokenEnv , checkGithubToken , getGithubOrgs } from '../github/auth.js'
4- import { githubRepoFromTemplatePrompts , initType } from './init/prompts.js'
4+ import { githubRepoFromTemplatePrompts , localRepoFromTemplatePrompts , initType } from './init/prompts.js'
55import { createGithubRepoWithTemplate , updateGithubRepoSettings , updateGithubBranchProtection } from '../github/repo.js'
66import { addRepositorySecrets } from '../github/secrets.js'
77import { updateAndCommit } from '../github/commit.js'
88import { confirmNextSteps } from '../utils/prompts.js'
9+ import { gitCloneDepth1 , gitInit , gitAddAll , gitCommit } from '../git/git.js'
10+ import { parseJson , removePath , writeJson } from '../utils/fs.js'
911import chalk from 'chalk'
1012
1113/**
1214 * @ignore
1315 * @typedef {TYPES.LOCALDATA } LOCALDATA {@link LOCALDATA }
1416 */
1517
18+ /**
19+ * #### Create a new GitHub repo based on a GitHub hosted template
20+ * @param {LOCALDATA } data - local data
21+ * @private
22+ * @async
23+ * @returns undefined
24+ */
25+ async function initGithubRepoBasedOnTemplate ( data ) {
26+ checkGithubTokenEnv ( )
27+ await checkIsOnline ( )
28+ await checkGithubToken ( data )
29+ await getGithubOrgs ( data )
30+ await githubRepoFromTemplatePrompts ( data )
31+ await confirmNextSteps (
32+ `You are about to create a new repository with the following settings:
33+ - New repo owner: ${ chalk . blue ( data . prompts ?. githubRepoFromTmpl ?. newRepoOwner ) }
34+ - New repo name: ${ chalk . green ( data . prompts ?. githubRepoFromTmpl ?. newRepoName ) }
35+ - Template owner: ${ data . prompts ?. githubRepoFromTmpl ?. templateRepoOwner }
36+ - Template name: ${ data . prompts ?. githubRepoFromTmpl ?. templateRepoName }
37+ - Private repo: ${ chalk . bold . yellow ( data . prompts ?. githubRepoFromTmpl ?. isPrivate ) }
38+ - Add secrets: ${ chalk . bold . yellow ( data . prompts ?. githubRepoFromTmpl ?. isSecrets ) } `
39+ )
40+ await createGithubRepoWithTemplate ( data )
41+ await updateGithubRepoSettings ( data , {
42+ owner : data . prompts ?. githubRepoFromTmpl ?. newRepoOwner || '' ,
43+ repo : data . prompts ?. githubRepoFromTmpl ?. newRepoName || '' ,
44+ has_issues : true ,
45+ has_projects : false ,
46+ has_wiki : false ,
47+ allow_rebase_merge : false ,
48+ allow_squash_merge : true ,
49+ allow_merge_commit : false ,
50+ delete_branch_on_merge : true ,
51+ allow_update_branch : true ,
52+ squash_merge_commit_title : 'PR_TITLE'
53+ } )
54+ await updateGithubBranchProtection ( data , {
55+ owner : data . prompts ?. githubRepoFromTmpl ?. newRepoOwner || '' ,
56+ repo : data . prompts ?. githubRepoFromTmpl ?. newRepoName || '' ,
57+ branch : 'master' ,
58+ required_status_checks : {
59+ strict : true ,
60+ contexts : [
61+ 'test' ,
62+ 'check-commit-message' ,
63+ 'validate-theme'
64+ ]
65+ } ,
66+ enforce_admins : false ,
67+ required_pull_request_reviews : {
68+ dismiss_stale_reviews : true ,
69+ required_approving_review_count : 1 ,
70+ require_last_push_approval : true
71+ } ,
72+ restrictions : null ,
73+ required_linear_history : true ,
74+ required_conversation_resolution : true
75+ } )
76+ if ( data . prompts ?. githubRepoFromTmpl ?. isSecrets ) {
77+ await addRepositorySecrets ( data )
78+ }
79+ await updateAndCommit (
80+ data . prompts ?. githubRepoFromTmpl ?. newRepoOwner || '' ,
81+ data . prompts ?. githubRepoFromTmpl ?. newRepoName || '' ,
82+ [ 'package.json' , 'theme/theme.json' ] ,
83+ {
84+ name : data . prompts ?. githubRepoFromTmpl ?. newRepoName ,
85+ label : `${ data . prompts ?. githubRepoFromTmpl ?. newRepoLabel . charAt ( 0 ) . toUpperCase ( ) } ${ data . prompts ?. githubRepoFromTmpl ?. newRepoLabel . slice ( 1 ) } `
86+ }
87+ )
88+ }
89+
90+ /**
91+ * #### Create a new local repo based on a GitHub hosted template
92+ * @param {LOCALDATA } data - local data
93+ * @private
94+ * @async
95+ * @returns undefined
96+ */
97+ async function initLocalRepoBasedOnTemplate ( data ) {
98+ await checkIsOnline ( )
99+ await checkSshConnection ( 'git@github.com' )
100+ if ( data . git ?. isRepo ) {
101+ console . error ( `${ chalk . bold . red ( 'Error:' ) } This script requires to be run in a simple folder, not a git repository\n` )
102+ process . exit ( 1 )
103+ }
104+ await localRepoFromTemplatePrompts ( data )
105+ await confirmNextSteps (
106+ `You are about to create a new repository with the following settings:
107+ - New repo name: ${ chalk . green ( data . prompts ?. localRepoFromTmpl ?. newRepoName ) }
108+ - Template owner: ${ data . prompts ?. localRepoFromTmpl ?. templateRepoOwner }
109+ - Template name: ${ data . prompts ?. localRepoFromTmpl ?. templateRepoName } `
110+ )
111+ if ( data . prompts ?. localRepoFromTmpl ?. newRepoName ) {
112+ await gitCloneDepth1 ( data . prompts ?. localRepoFromTmpl ?. newRepoName )
113+ await removePath ( `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } /.git` )
114+ await gitInit ( { cwd : `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } ` } )
115+ await gitAddAll ( { cwd : `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } ` } )
116+ await gitCommit ( 'Initial commit' , { cwd : `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } ` } )
117+ const packageJson = await parseJson ( `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } /package.json` )
118+ const themeJson = await parseJson ( `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } /theme/theme.json` )
119+ packageJson . name = data . prompts ?. localRepoFromTmpl ?. newRepoName
120+ themeJson . name = data . prompts ?. localRepoFromTmpl ?. newRepoName
121+ themeJson . label = `${ data . prompts ?. localRepoFromTmpl ?. newRepoLabel . charAt ( 0 ) . toUpperCase ( ) } ${ data . prompts ?. localRepoFromTmpl ?. newRepoLabel . slice ( 1 ) } `
122+ await writeJson ( `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } /package.json` , packageJson )
123+ await writeJson ( `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } /theme/theme.json` , themeJson )
124+ await gitAddAll ( { cwd : `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } ` } )
125+ await gitCommit ( '[TASK] update package.json and theme.json with new project info' , { cwd : `${ data ?. cwd . path } /${ data . prompts ?. localRepoFromTmpl ?. newRepoName } ` } )
126+ }
127+ }
128+
16129/**
17130 * #### Show info about the current project
18131 * @param {LOCALDATA } data - local data
@@ -22,68 +135,9 @@ import chalk from 'chalk'
22135async function init ( data ) {
23136 const type = await initType ( )
24137 if ( type === 'github-repo-based-on-template' ) {
25- checkGithubTokenEnv ( )
26- await checkIsOnline ( )
27- await checkGithubToken ( data )
28- await getGithubOrgs ( data )
29- await githubRepoFromTemplatePrompts ( data )
30- await confirmNextSteps (
31- `You are about to create a new repository with the following settings:
32- - New repo owner: ${ chalk . blue ( data . prompts ?. repoFromTmpl ?. newRepoOwner ) }
33- - New repo name: ${ chalk . green ( data . prompts ?. repoFromTmpl ?. newRepoName ) }
34- - Template owner: ${ data . prompts ?. repoFromTmpl ?. templateRepoOwner }
35- - Template name: ${ data . prompts ?. repoFromTmpl ?. templateRepoName }
36- - Private repo: ${ chalk . bold . yellow ( data . prompts ?. repoFromTmpl ?. isPrivate ) }
37- - Add secrets: ${ chalk . bold . yellow ( data . prompts ?. repoFromTmpl ?. isSecrets ) } `
38- )
39- await createGithubRepoWithTemplate ( data )
40- await updateGithubRepoSettings ( data , {
41- owner : data . prompts ?. repoFromTmpl ?. newRepoOwner || '' ,
42- repo : data . prompts ?. repoFromTmpl ?. newRepoName || '' ,
43- has_issues : true ,
44- has_projects : false ,
45- has_wiki : false ,
46- allow_rebase_merge : false ,
47- allow_squash_merge : true ,
48- allow_merge_commit : false ,
49- delete_branch_on_merge : true ,
50- allow_update_branch : true ,
51- squash_merge_commit_title : 'PR_TITLE'
52- } )
53- await updateGithubBranchProtection ( data , {
54- owner : data . prompts ?. repoFromTmpl ?. newRepoOwner || '' ,
55- repo : data . prompts ?. repoFromTmpl ?. newRepoName || '' ,
56- branch : 'master' ,
57- required_status_checks : {
58- strict : true ,
59- contexts : [
60- 'test' ,
61- 'check-commit-message' ,
62- 'validate-theme'
63- ]
64- } ,
65- enforce_admins : false ,
66- required_pull_request_reviews : {
67- dismiss_stale_reviews : true ,
68- required_approving_review_count : 1 ,
69- require_last_push_approval : true
70- } ,
71- restrictions : null ,
72- required_linear_history : true ,
73- required_conversation_resolution : true
74- } )
75- if ( data . prompts ?. repoFromTmpl ?. isSecrets ) {
76- await addRepositorySecrets ( data )
77- }
78- await updateAndCommit (
79- data . prompts ?. repoFromTmpl ?. newRepoOwner || '' ,
80- data . prompts ?. repoFromTmpl ?. newRepoName || '' ,
81- [ 'package.json' , 'theme/theme.json' ] ,
82- {
83- name : data . prompts ?. repoFromTmpl ?. newRepoName ,
84- label : `${ data . prompts ?. repoFromTmpl ?. newRepoLabel . charAt ( 0 ) . toUpperCase ( ) } ${ data . prompts ?. repoFromTmpl ?. newRepoLabel . slice ( 1 ) } `
85- }
86- )
138+ await initGithubRepoBasedOnTemplate ( data )
139+ } else if ( type === 'local-repo-based-on-template' ) {
140+ await initLocalRepoBasedOnTemplate ( data )
87141 }
88142}
89143
0 commit comments