/**
 * Copyright (c) 2015-2017, Facebook, Inc.
 * Copyright (c) 2017, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */ import fs from 'fs-extra' ; import path from 'path' ; import spawn from 'cross-spawn' ; import chalk from 'chalk' ; function install ( useYarn , dependencies , verbose ) { return new Promise ( ( resolve , reject ) => { let command ; let args ; if ( useYarn ) { command = 'yarnpkg' ; args = [ 'add' , '--exact' ] ; Array . prototype . push . apply ( args , dependencies ) ; } else { command = 'npm' ; args = [ 'install' , '--save' , '--save-exact' ] . concat ( dependencies ) ; } if ( verbose ) { args . push ( '--verbose' ) ; } const child = spawn ( command , args , { stdio : 'inherit' } ) ; child . on ( 'close' , ( code ) => { if ( code !== 0 ) { reject ( { command : `${ command } ${ args . join ( ' ' ) }` , } ) ; return ; } resolve ( ) ; } ) ; } ) ; } const init = async ( appPath , appName , verbose , originalDirectory ) => { const ownPackageName = require ( path . join ( __dirname , '..' , 'package.json' ) ) . name ; const ownPath = path . join ( appPath , 'node_modules' , ownPackageName ) ; const appPackage = require ( path . join ( appPath , 'package.json' ) ) ; const useYarn = await fs . pathExists ( path . join ( appPath , 'yarn.lock' ) ) ; appPackage . dependencies = appPackage . dependencies || { } ; appPackage . devDependencies = appPackage . devDependencies || { } ; appPackage . scripts = { start : 'tux-scripts start' , build : 'tux-scripts build' , serve : 'tux-scripts serve' , } ; await fs . writeFile ( path . join ( appPath , 'package.json' ) , JSON . stringify ( appPackage , null , 2 ) ) ; const readmeExists = await fs . pathExists ( path . join ( appPath , 'README.md' ) ) ; if ( readmeExists ) { await fs . rename ( path . join ( appPath , 'README.md' ) , path . join ( appPath , 'README.old.md' ) ) ; } const templatePath = path . join ( ownPath , 'template' , 'new' ) ; if ( await fs . pathExists ( templatePath ) ) { await fs . copy ( templatePath , appPath ) ; } else { console . error ( `Could not locate supplied template: ${ chalk . green ( templatePath ) }` ) ; return ; } let dependencies = [ ] ; const templateDependenciesPath = path . join ( appPath , '.template.dependencies.json' ) ; if ( await fs . pathExists ( templateDependenciesPath ) ) { const templateDependencies = require ( templateDependenciesPath ) . dependencies ; dependencies = dependencies . concat ( Object . keys ( templateDependencies ) . map ( key => { return `${ key }@${ templateDependencies [ key ] }` ; } ) ) ; await fs . unlink ( templateDependenciesPath ) ; } console . log ( `Installing dependencies...` ) ; console . log ( dependencies . join ( ', ' ) ) ; await install ( useYarn , dependencies , verbose ) ; let cdpath ; if ( originalDirectory && path . join ( originalDirectory , appName ) === appPath ) { cdpath = appName ; } else { cdpath = appPath ; } const displayedCommand = useYarn ? 'yarn' : 'npm' ; console . log ( ) ; console . log ( `Success! Created ${ appName } at ${ appPath }` ) ; console . log ( 'Inside that directory, you can run several commands:' ) ; console . log ( ) ; console . log ( chalk . cyan ( `  ${ displayedCommand } start` ) ) ; console . log ( '    Starts the development server.' ) ; console . log ( ) ; console . log ( chalk . cyan ( `  ${ displayedCommand } ${ useYarn ? '' : 'run ' }build` ) ) ; console . log ( '    Bundles the app into static files for production.' ) ; console . log ( ) ; console . log ( chalk . cyan ( `  ${ displayedCommand } ${ useYarn ? '' : 'run ' }serve` ) ) ; console . log ( '    Starts a server for the built output.' ) ; console . log ( ) ; console . log ( 'We suggest that you begin by typing:' ) ; console . log ( ) ; console . log ( chalk . cyan ( '  cd' ) , cdpath ) ; console . log ( `  ${ chalk . cyan ( `${ displayedCommand } start` ) }` ) ; if ( readmeExists ) { console . log ( ) ; console . log ( chalk . yellow ( 'You had a `README.md` file, we renamed it to `README.old.md`' ) ) ; } console . log ( ) ; console . log ( 'Happy hacking!' ) ; } ; export default init ;