www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

copy.js (1683B)


      1 'use strict';
      2 
      3 const globby = require('globby');
      4 const path = require('path');
      5 const fs = require('fs-extra');
      6 const { getSignatures, writeSignatures, cleanUp, compareSignatures, getFileSignature, onSuccess, onError, onProgress } = require('./utils');
      7 const { copyDirs, ignoreMask } = require('./config');
      8 
      9 const ROOT = path.resolve(__dirname, '..');
     10 
     11 async function getCopy(source, options, signatures) {
     12 	const t1 = Date.now();
     13 	const files = await globby(source, Object.assign({ cwd: ROOT }, options ));
     14 	const totalCount = files.length;
     15 	var count = 0;
     16 	var f;
     17 
     18 	while ((f = files.pop()) != null) {
     19 		let newFileSignature = await getFileSignature(f);
     20 		const dest = path.join('build', f);
     21 
     22 		if (f in signatures) {
     23 			if (compareSignatures(newFileSignature, signatures[f])) {
     24 				try {
     25 					await fs.access(dest, fs.constants.F_OK);
     26 					continue;
     27 				} catch (_) {
     28 					// file does not exists in build, fallback to browserifing
     29 				}
     30 			}
     31 		}
     32 		try {
     33 			await fs.mkdirp(path.dirname(dest));
     34 			await fs.copy(f, dest);
     35 			onProgress(f, dest, 'cp');
     36 			signatures[f] = newFileSignature;
     37 			count++;
     38 		} catch (err) {
     39 			throw new Error(`Failed on ${f}: ${err}`);
     40 		}
     41 	}
     42 	
     43 	const t2 = Date.now();
     44 	return {
     45 		action: 'copy',
     46 		count,
     47 		totalCount,
     48 		processingTime: t2 - t1
     49 	};
     50 }
     51 
     52 module.exports = getCopy;
     53 
     54 if (require.main === module) {
     55 	(async () => {
     56 		try {
     57 			const signatures = await getSignatures();
     58 			onSuccess(await getCopy(copyDirs.map(d => `${d}/**`), { ignore: ignoreMask }, signatures));
     59 			onSuccess(await cleanUp(signatures));
     60 			await writeSignatures(signatures);
     61 		} catch (err) {
     62 			process.exitCode = 1;
     63 			global.isError = true;
     64 			onError(err);
     65 		}
     66 	})();
     67 }