Files
add-and-commit/node_modules/@node-minify/core/lib/compress.js
Federico Grandi f118062594 Update with working working version (#12)
* Fix workflow

* Trigger

* [auto] Update compiled version

* [auto] Commit modules

* Push Windows changes

* Fix

* [auto] Update compiled version

* Try removing cwd

* [auto] Update compiled version

* Try with path module

* [auto] Update compiled version

* Fix path

* [auto] Update compiled version

* Use raw path

* [auto] Update compiled version

* Other path

* [auto] Update compiled version

* Avoid @action/exec

* [auto] Update compiled version

* test

* [auto] Update compiled version

* test

* [auto] Update compiled version

* test

* [auto] Update compiled version

* test

* [auto] Update compiled version

* Try with shelljs

* [auto] Update compiled version

* Fix my stupidity

* Copy scripts to local dir

* [auto] Update compiled version

* Still use path

* [auto] Update compiled version

* Delete entrypoint.sh

* [auto] Update compiled version

* Make file executable

* [auto] Update compiled version

* Try using bash

* [auto] Update compiled version
2019-12-14 21:47:13 +01:00

178 lines
3.3 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.compress = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _mkdirp = _interopRequireDefault(require("mkdirp"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*!
* node-minify
* Copyright(c) 2011-2019 Rodolphe Stoclin
* MIT Licensed
*/
/**
* Module dependencies.
*/
/**
* Run compressor.
*
* @param {Object} settings
*/
const compress = settings => {
if (typeof settings.compressor !== 'function') {
throw new Error(`compressor should be a function, maybe you forgot to install the compressor`);
}
createDirectory(settings.output);
if (Array.isArray(settings.output)) {
return settings.sync ? compressArrayOfFilesSync(settings) : compressArrayOfFilesAsync(settings);
} else {
return compressSingleFile(settings);
}
};
/**
* Compress an array of files in sync.
*
* @param {Object} settings
*/
exports.compress = compress;
const compressArrayOfFilesSync = settings => {
return settings.input.forEach((input, index) => {
const content = getContentFromFiles(input);
return runSync({
settings,
content,
index
});
});
};
/**
* Compress an array of files in async.
*
* @param {Object} settings
*/
const compressArrayOfFilesAsync = settings => {
let sequence = Promise.resolve();
settings.input.forEach((input, index) => {
const content = getContentFromFiles(input);
sequence = sequence.then(() => runAsync({
settings,
content,
index
}));
});
return sequence;
};
/**
* Compress a single file.
*
* @param {Object} settings
*/
const compressSingleFile = settings => {
const content = getContentFromFiles(settings.input);
return settings.sync ? runSync({
settings,
content
}) : runAsync({
settings,
content
});
};
/**
* Run compressor in sync.
*
* @param {Object} settings
* @param {String} content
* @param {Number} index - index of the file being processed
* @return {String}
*/
const runSync = ({
settings,
content,
index
}) => settings.compressor({
settings,
content,
callback: null,
index
});
/**
* Run compressor in async.
*
* @param {Object} settings
* @param {String} content
* @param {Number} index - index of the file being processed
* @return {Promise}
*/
const runAsync = ({
settings,
content,
index
}) => {
return new Promise((resolve, reject) => {
settings.compressor({
settings,
content,
callback: (err, min) => {
if (err) {
return reject(err);
}
resolve(min);
},
index
});
});
};
/**
* Concatenate all input files and get the data.
*
* @param {String|Array} input
* @return {String}
*/
const getContentFromFiles = input => {
if (!Array.isArray(input)) {
return _fs.default.readFileSync(input, 'utf8');
}
return input.map(path => _fs.default.readFileSync(path, 'utf8')).join('\n');
};
/**
* Create folder of the target file.
*
* @param {String} file - Full path of the file
*/
const createDirectory = file => {
if (Array.isArray(file)) {
file = file[0];
}
_mkdirp.default.sync(file.substr(0, file.lastIndexOf('/')));
};
/**
* Expose `compress()`.
*/