import fs from 'fs' ; import fsExtra from 'fs-extra' ; import { spawnSync } from 'child_process' ; import updateNotifier from 'update-notifier' ; import inquirer from 'inquirer' ; import chalk from 'chalk' ; import { CMDType , IRunParams } from './interface' ; import pkg from '../package.json' ; const prompts = [ { type : 'confirm' , name : 'git' , message : '是否初始化 git？' , default : false } , { type : 'list' , name : 'selectInstall' , message : '选择安装依赖方式' , default : 'yarn' , choices : [ { name : '使用 Yarn' , value : 'yarn' } , { name : '使用 Npm' , value : 'npm' } ] } ] ; param p export const isExistPath = ( p ) : boolean => fs . existsSync ( p ) ; param args param cmd param cwd export const run = ( { args , cmd = 'npm' , cwd = process . cwd ( ) , isStdio = true } ) : void => { spawnSync ( cmd , args , { cwd , stdio : isStdio ? 'inherit' : undefined , shell : true } ) ; } ; param name param template const downloadTemplate = ( name , template = 'https://github.com/tal-tech/create-electron-app-template.git' ) : void => { const isGitTemplate = /\.git/ . test ( template ) ; const currentPath = process . cwd ( ) ; let runParams = { cmd : 'git' , args : [ 'clone' , template , name ] } ; if ( ! isGitTemplate ) { let cmd = 'cp' ; let args = [ template , currentPath , '/e' , '/xd' , 'node_modules' ] ; if ( process . platform === 'win32' ) { cmd = 'robocopy' ; args = [ template , currentPath ] ; } runParams = { cmd , args } ; } console . log ( '\n下载模版...' ) ; run ( { ... runParams , isStdio : true } ) ; console . log ( '模板下载完成。\n' ) ; fsExtra . removeSync ( `${ currentPath }/${ name }/.git` ) ; run ( { cmd : 'rm' , args : [ '-rf' , '.git' ] , cwd : `${ currentPath }/${ name }` } ) ; } ; param cmd param path const installDependencie = ( cmd , path = process . cwd ( ) ) => { run ( { cmd , args : [ 'install' ] , cwd : path } ) ; } ; param path const initGit = ( path = process . cwd ( ) ) => { run ( { cmd : 'git' , args : [ 'init' ] , cwd : path , isStdio : false } ) ; run ( { cmd : 'git' , args : [ 'add' , '.' ] , cwd : path , isStdio : false } ) ; run ( { cmd : 'git' , args : [ 'commit' , '-m' , '"init commit"' ] , cwd : path , isStdio : false } ) ; } ; /**
 * 初始化完成
 */ const initComplate = ( ) => { console . log ( '\n✅ 项目初始化完成。\n' ) ; } ; /**
 * 初始化命令提示
 */ const initPrompt = ( name , template ) => { inquirer . prompt ( prompts ) . then ( ( { git , selectInstall } ) => { const fullPath = `${ process . cwd ( ) }/${ name }` ; downloadTemplate ( name , template ) ; installDependencie ( selectInstall , fullPath ) ; if ( git ) { initGit ( fullPath ) ; } initComplate ( ) ; } ) ; } ; /**
 * 检查 Node
 */ export const checkNodeVersion = ( ) : void => { const currentVersion = process . versions . node ; const major = Number ( currentVersion . split ( '.' ) [ 0 ] ) ; if ( major < 10 ) { console . log ( `你使用的 Node 版本：\n\n\t${ currentVersion }\n
${ chalk . red ( '创建 Electron 应用需要 Node 10 或者更高版本，请升级你的 Node 版本。' ) }` ) ; process . exit ( 1 ) ; } } ; /**
 * 检查更新
 */ export const checkUpdate = ( ) : void => { updateNotifier ( { pkg } ) . notify ( ) ; } ; param name param template export const createApp = ( name , template ) : void => { const fullPath = `${ process . cwd ( ) }/${ name }` ; if ( isExistPath ( fullPath ) ) { console . log ( `${ chalk . cyan ( name ) } 在当前目录已经存在。` ) ; return ; } console . clear ( ) ; process . stdout . write ( chalk . bold . cyan ( `${ pkg . name } v${ pkg . version }\n` ) , 'utf-8' ) ; initPrompt ( name , template ) ; } ; export { pkg } ;