import path from 'path' ; import fs from 'fs-extra' ; import execa from 'execa' ; import chalk from 'chalk' ; import readPkgUp from 'read-pkg-up' ; import * as messages from './messages' ; import getInstallCmd from './get-install-cmd' ; export default async function createWebScriptsLibrary ( projectName ? ) { if ( ! projectName ) { throw new Error ( messages . missingProjectName ( ) ) ; } if ( fs . existsSync ( projectName ) && projectName !== '.' ) { throw new Error ( messages . alreadyExists ( projectName ) ) ; } const { packageJson : cwslPkg } = readPkgUp . sync ( { cwd : __dirname , } ) || { } ; const projectPath = path . resolve ( process . cwd ( ) , projectName ) ; const templatePath = path . resolve ( __dirname , '..' , 'template' ) ; console . log ( chalk . gray ( 'Copying template...' ) ) ; await fs . copy ( templatePath , projectPath ) ; console . log ( chalk . gray ( 'Writing over necessary files...' ) ) ; process . chdir ( projectPath ) ; const newPkgPath = path . join ( projectPath , 'package.json' ) ; const newPkg = require ( newPkgPath ) ; newPkg . name = projectName ; newPkg . devDependencies [ '@spotify/web-scripts' ] = ( cwslPkg ?. devDependencies || { } ) [ '@spotify/web-scripts' ] || newPkg . devDependencies [ '@spotify/web-scripts' ] ; await fs . writeFile ( newPkgPath , JSON . stringify ( newPkg , null , 2 ) ) ; console . log ( chalk . gray ( 'Installing dependencies...' ) ) ; process . chdir ( projectPath ) ; await execa ( getInstallCmd ( ) , [ 'install' ] ) ; console . log ( messages . start ( projectName ) ) ; }