gatsby-source-netlify-lfs
Version:
Use Netlify's Git LFS Transform Image API with gatsby-image
85 lines • 4.92 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const glob_1 = __importDefault(require("glob"));
const defaultConfig_1 = require("./defaultConfig");
const generateBase64_1 = require("./generateBase64");
const cli_progress_1 = __importDefault(require("cli-progress"));
const gatsbyConfigFilePath = path.resolve('./gatsby-config');
const netlifyLfsImageDataPath = path.resolve('./src/netlifyLfs');
const netlifyLfsImageDataFilename = 'netlifyLfsImageData.json';
const gatsbyConfig = require(gatsbyConfigFilePath);
const userConfig = gatsbyConfig.plugins.find(plugin => plugin.resolve === 'gatsby-source-netlify-lfs') || {};
let config = Object.assign(Object.assign(Object.assign({}, defaultConfig_1.defaultConfig), userConfig.options), { blurredOptions: Object.assign(Object.assign({}, defaultConfig_1.defaultConfig.blurredOptions), (_a = userConfig.options) === null || _a === void 0 ? void 0 : _a.blurredOptions) });
if (config.paths == null) {
config.paths = gatsbyConfig.plugins
.filter(plugin => plugin.resolve === 'gatsby-source-filesystem')
.map(sourceFilesystem => sourceFilesystem.options.path);
}
const globPaths = config.paths.length > 1
? `{${config.paths.join(',')}}`
: config.paths[0];
(0, glob_1.default)(`${globPaths}/*.{${config.formats.join(',')}}`, (error, matches) => __awaiter(void 0, void 0, void 0, function* () {
console.log(`Recording LFS Data of ${matches.length} files from directories:`, config.paths);
const progressBar = new cli_progress_1.default.SingleBar({}, cli_progress_1.default.Presets.shades_classic);
progressBar.start(matches.length, 0);
const imageData = {};
yield Promise.all(matches.map((file, index) => __awaiter(void 0, void 0, void 0, function* () {
var fileName = path.basename(file);
const data = yield (0, generateBase64_1.processImage)(file, config);
if (imageData[fileName] != null)
console.warn(`All media files must have a unique name. '${fileName}' is duplicated and is being overwritten`);
const usePlaceholderImage = config.placeholder === 'blurred' || config.placeholder === 'tracedSVG';
const useDominantColor = config.placeholder === 'dominantColor';
imageData[fileName] = {
h: data.height,
w: data.width,
p: usePlaceholderImage ? data.src : undefined,
b: useDominantColor ? data.dominantColor : undefined,
};
progressBar.increment();
})));
progressBar.stop();
yield fs.mkdir(netlifyLfsImageDataPath, { recursive: true }, () => undefined);
yield fs.writeFile(netlifyLfsImageDataPath + '/' + netlifyLfsImageDataFilename, JSON.stringify(imageData), 'utf8', () => {
console.log(`DONE! Mapped dimensions for ${Object.keys(imageData).length} media files in:\n`, path.resolve(netlifyLfsImageDataPath), `\n`, `Please commit this file.`);
});
}));
//# sourceMappingURL=recordNetlifyLfsImageData.js.map