import chalk from 'chalk' ; import path from 'path' ; import fs from 'fs-extra' ; import { generate } from '../../utils/initUtils' ; import { homedir } from 'os' ; import download from 'download-git-repo' ; import { IOptions , IPlugin , IPicGo } from '../../types' ; import rm from 'rimraf' ; const run = ( ctx , options ) : void => { if ( options . offline ) { if ( fs . existsSync ( options . template ) ) { generate ( ctx , options ) . catch ( ( e ) => { ctx . log . error ( e ) ; } ) ; } else { ctx . log . error ( `Local template ${ options . template } not found` ) ; } } else { options . template = ! options . hasSlash ? 'PicGo/picgo-template-' + options . template : options . template ; downloadAndGenerate ( ctx , options ) ; } } ; param { PicGo } ctx param { IOptions } options const downloadAndGenerate = ( ctx , options ) : void => { if ( fs . existsSync ( options . tmp ) ) { rm . sync ( options . tmp ) ; } ctx . log . info ( 'Template files are downloading...' ) ; download ( options . template , options . tmp , { clone : options . clone } , ( err ) => { if ( err ) { return ctx . log . error ( err ) ; } ctx . log . success ( 'Template files are downloaded!' ) ; generate ( ctx , options ) . catch ( ( e ) => { ctx . log . error ( e ) ; } ) ; } ) ; } ; const init = { handle : async ( ctx ) => { const cmd = ctx . cmd ; cmd . program . command ( 'init' ) . arguments ( '<template> [project]' ) . option ( '--clone' , 'use git clone' ) . option ( '--offline' , 'use cached template' ) . description ( 'create picgo plugin\'s development templates' ) . action ( ( template , project , program ) => { ( async ( ) => { try { const hasSlash = template . includes ( '/' ) ; const inPlace = ! project || project === '.' ; const dest = path . resolve ( project || '.' ) ; const clone = program . clone || false ; const offline = program . offline || false ; const tmp = path . join ( homedir ( ) , '.picgo/templates' , template . replace ( /[/:]/g , '-' ) ) ; if ( program . offline ) { template = tmp ; } const options = { template , project , hasSlash , inPlace , dest , clone , tmp , offline } ; if ( inPlace || fs . existsSync ( dest ) ) { await ctx . cmd . inquirer . prompt ( [ { type : 'confirm' , message : inPlace ? 'Generate project in current directory?' : 'Target directory exists. Continue?' , name : 'ok' } ] ) . then ( ( answer ) => { if ( answer . ok ) { run ( ctx , options ) ; } } ) ; } else { run ( ctx , options ) ; } } catch ( e ) { ctx . log . error ( e ) ; if ( process . argv . includes ( '--debug' ) ) { throw e ; } } } ) ( ) . catch ( ( e ) => { ctx . log . error ( e ) ; } ) ; } ) . on ( '--help' , ( ) => { console . log ( ) ; console . log ( 'Examples:' ) ; console . log ( ) ; console . log ( chalk . gray ( '  # create a new project with an official template' ) ) ; console . log ( '  $ picgo init plugin my-project' ) ; console . log ( ) ; console . log ( chalk . gray ( '  # create a new project straight from a github template' ) ) ; console . log ( '  $ picgo init username/repo my-project' ) ; console . log ( ) ; } ) ; } } ; export default init ;