@@ -10,10 +10,10 @@ import {
1010 TARO_CONFIG_FOLDER
1111} from '@tarojs/helper'
1212import { isArray } from '@tarojs/shared'
13+ import axios from 'axios'
1314import * as inquirer from 'inquirer'
1415import * as ora from 'ora'
1516import * as path from 'path'
16- import * as request from 'request'
1717import * as semver from 'semver'
1818
1919import { clearConsole , getPkgVersion , getRootPath } from '../util'
@@ -38,6 +38,7 @@ export interface IProjectConf {
3838 sourceRoot ?: string
3939 env ?: string
4040 autoInstall ?: boolean
41+ hideDefaultTemplate ?: boolean
4142 framework : FrameworkType
4243 compiler ?: CompilerType
4344}
@@ -58,9 +59,9 @@ export default class Project extends Creator {
5859
5960 constructor ( options : IProjectConfOptions ) {
6061 super ( options . sourceRoot )
61- const unSupportedVer = semver . lt ( process . version , 'v7.6 .0' )
62+ const unSupportedVer = semver . lt ( process . version , 'v18.0 .0' )
6263 if ( unSupportedVer ) {
63- throw new Error ( 'Node.js 版本过低,推荐升级 Node.js 至 v8 .0.0+' )
64+ throw new Error ( 'Node.js 版本过低,推荐升级 Node.js 至 v18 .0.0+' )
6465 }
6566 this . rootPath = this . _rootPath
6667
@@ -341,16 +342,17 @@ export default class Project extends Creator {
341342 }
342343
343344 askTemplate : AskMethods = function ( conf , prompts , list = [ ] ) {
344- const choices = [
345- {
345+ const choices = list . map ( item => ( {
346+ name : item . desc ? `${ item . name } (${ item . desc } )` : item . name ,
347+ value : item . value || item . name
348+ } ) )
349+
350+ if ( ! conf . hideDefaultTemplate ) {
351+ choices . unshift ( {
346352 name : '默认模板' ,
347353 value : 'default'
348- } ,
349- ...list . map ( item => ( {
350- name : item . desc ? `${ item . name } (${ item . desc } )` : item . name ,
351- value : item . name
352- } ) )
353- ]
354+ } )
355+ }
354356
355357 if ( ( typeof conf . template as 'string' | undefined ) !== 'string' ) {
356358 prompts . push ( {
@@ -393,7 +395,8 @@ export default class Project extends Creator {
393395 }
394396
395397 async fetchTemplates ( answers : IProjectConf ) : Promise < ITemplates [ ] > {
396- const { templateSource, framework } = answers
398+ const { templateSource, framework, compiler } = answers
399+ this . conf . framework = this . conf . framework || framework || ''
397400 this . conf . templateSource = this . conf . templateSource || templateSource
398401
399402 // 使用默认模版
@@ -407,17 +410,30 @@ export default class Project extends Creator {
407410 const isClone = / g i t e e / . test ( this . conf . templateSource ) || this . conf . clone
408411 const templateChoices = await fetchTemplate ( this . conf . templateSource , this . templatePath ( '' ) , isClone )
409412
413+ const filterFramework = ( _framework ) => {
414+ const current = this . conf . framework ?. toLowerCase ( )
415+
416+ if ( typeof _framework === 'string' && _framework ) {
417+ return current === _framework . toLowerCase ( )
418+ } else if ( isArray ( _framework ) ) {
419+ return _framework ?. map ( name => name . toLowerCase ( ) ) . includes ( current )
420+ } else {
421+ return true
422+ }
423+ }
424+
425+ const filterCompiler = ( _compiler ) => {
426+ if ( _compiler && isArray ( _compiler ) ) {
427+ return _compiler ?. includes ( compiler )
428+ }
429+ return true
430+ }
431+
410432 // 根据用户选择的框架筛选模板
411433 const newTemplateChoices : ITemplates [ ] = templateChoices
412434 . filter ( templateChoice => {
413- const { platforms } = templateChoice
414- if ( typeof platforms === 'string' && platforms ) {
415- return framework === templateChoice . platforms
416- } else if ( isArray ( platforms ) ) {
417- return templateChoice . platforms ?. includes ( framework )
418- } else {
419- return true
420- }
435+ const { platforms, compiler } = templateChoice
436+ return filterFramework ( platforms ) && filterCompiler ( compiler )
421437 } )
422438
423439 return newTemplateChoices
@@ -451,27 +467,23 @@ export default class Project extends Creator {
451467 }
452468}
453469
454- function getOpenSourceTemplates ( platform ) {
470+ function getOpenSourceTemplates ( platform : string ) {
455471 return new Promise ( ( resolve , reject ) => {
456472 const spinner = ora ( { text : '正在拉取开源模板列表...' , discardStdin : false } ) . start ( )
457- request . get ( 'https://gitee.com/NervJS/awesome-taro/raw/next/index.json' , ( error , _response , body ) => {
458- if ( error ) {
473+ axios . get ( 'https://gitee.com/NervJS/awesome-taro/raw/next/index.json' )
474+ . then ( response => {
475+ spinner . succeed ( `${ chalk . grey ( '拉取开源模板列表成功!' ) } ` )
476+ const collection = response . data
477+ switch ( platform . toLowerCase ( ) ) {
478+ case 'react' :
479+ return resolve ( collection . react )
480+ default :
481+ return resolve ( [ NONE_AVAILABLE_TEMPLATE ] )
482+ }
483+ } )
484+ . catch ( _error => {
459485 spinner . fail ( chalk . red ( '拉取开源模板列表失败!' ) )
460486 return reject ( new Error ( ) )
461- }
462-
463- spinner . succeed ( `${ chalk . grey ( '拉取开源模板列表成功!' ) } ` )
464-
465- const collection = JSON . parse ( body )
466-
467- switch ( platform ) {
468- case 'react' :
469- return resolve ( collection . react )
470- case 'vue' :
471- return resolve ( collection . vue )
472- default :
473- return resolve ( [ NONE_AVAILABLE_TEMPLATE ] )
474- }
475- } )
487+ } )
476488 } )
477489}
0 commit comments