import { TemplateRepository } from 'creato' ; import fetch from 'node-fetch' ; import fs from 'fs' ; import path from 'path' ; import prompts from 'prompts' ; import { IStarter } from './algolia' ; const EMMA_CLI_URL = 'https://emma-cli.herokuapp.com/api/' ; param starter export async function getStarterTemplateRepo ( starter ) { try { const res = await fetch ( `${ EMMA_CLI_URL }/starters/${ starter . objectID }` ) . then ( res => res . json ( ) ) ; return [ null , { uri : `https://github.com/${ res . owner }/${ res . repo }` , branch : res . ref , path : res . path , } , ] ; } catch ( err ) { return [ err . message ] ; } } param provided export async function getDistDirectory ( cwd , provided ? ) { if ( provided ) { const conflicts = checkFolderForConflicts ( provided ) ; if ( conflicts . length > 0 ) { return [ `Selected directory is not empty.` ] ; } return [ null , provided ] ; } else { const destination = await prompts ( { type : 'text' , name : 'path' , message : 'Where should I load the starter?' , initial : 'my-app' , validate : dist => { const conflicts = checkFolderForConflicts ( path . resolve ( cwd , dist ) ) ; if ( conflicts . length > 0 ) { return `Selected directory is not empty.` ; } return true ; } , } ) ; return [ null , destination . path ] ; } } param cwd function checkFolderForConflicts ( cwd ) { const exists = fs . existsSync ( cwd ) ; if ( ! exists ) return [ ] ; const validFiles = [ '.DS_Store' , '.git' , '.gitattributes' , '.gitignore' , '.gitlab-ci.yml' , '.hg' , '.hgcheck' , '.hgignore' , '.idea' , '.npmignore' , '.travis.yml' , 'LICENSE' , 'Thumbs.db' , 'docs' , 'mkdocs.yml' , 'npm-debug.log' , 'yarn-debug.log' , 'yarn-error.log' , ] ; const conflicts = fs . readdirSync ( cwd ) . filter ( file => ! validFiles . includes ( file ) ) . filter ( file => ! /\.iml$/ . test ( file ) ) ; return conflicts ; }