copy.js (1839B)
1 /** 2 * React Starter Kit (https://www.reactstarterkit.com/) 3 * 4 * Copyright © 2014-present Kriasoft, LLC. All rights reserved. 5 * 6 * This source code is licensed under the MIT license found in the 7 * LICENSE.txt file in the root directory of this source tree. 8 */ 9 10 import path from 'path'; 11 import chokidar from 'chokidar'; 12 import { writeFile, copyFile, makeDir, copyDir, cleanDir } from './lib/fs'; 13 import pkg from '../package.json'; 14 import { format } from './run'; 15 16 /** 17 * Copies static files such as robots.txt, favicon.ico to the 18 * output (build) folder. 19 */ 20 async function copy() { 21 await makeDir('build'); 22 await Promise.all([ 23 writeFile('build/package.json', JSON.stringify({ 24 private: true, 25 engines: pkg.engines, 26 dependencies: pkg.dependencies, 27 scripts: { 28 start: 'node server.js', 29 }, 30 }, null, 2)), 31 copyFile('LICENSE.txt', 'build/LICENSE.txt'), 32 copyDir('public', 'build/public'), 33 ]); 34 35 if (process.argv.includes('--watch')) { 36 const watcher = chokidar.watch([ 37 'public/**/*', 38 ], { ignoreInitial: true }); 39 40 watcher.on('all', async (event, filePath) => { 41 const start = new Date(); 42 const src = path.relative('./', filePath); 43 const dist = path.join('build/', src.startsWith('src') ? path.relative('src', src) : src); 44 switch (event) { 45 case 'add': 46 case 'change': 47 await makeDir(path.dirname(dist)); 48 await copyFile(filePath, dist); 49 break; 50 case 'unlink': 51 case 'unlinkDir': 52 cleanDir(dist, { nosort: true, dot: true }); 53 break; 54 default: 55 return; 56 } 57 const end = new Date(); 58 const time = end.getTime() - start.getTime(); 59 console.log(`[${format(end)}] ${event} '${dist}' after ${time} ms`); 60 }); 61 } 62 } 63 64 export default copy;