@resoc/create-img
Advanced tools
Comparing version 0.3.0 to 0.3.1
import { ImageTemplate, ImageResolution, ParamValues } from '@resoc/core'; | ||
export declare const compileLocalTemplate: (templateManifestPath: string, paramValues: ParamValues, resolution: ImageResolution, imagePath: string) => Promise<void>; | ||
declare type LocalTemplateOptions = { | ||
cache: boolean; | ||
}; | ||
export declare const fileExists: (path: string) => Promise<boolean>; | ||
export declare const cachedImageName: (templateDir: string, values: ParamValues, imagePath: string) => Promise<string>; | ||
export declare const compileLocalTemplate: (templateManifestPath: string, paramValues: ParamValues, resolution: ImageResolution, imagePath: string, options?: LocalTemplateOptions | undefined) => Promise<string>; | ||
export declare const compileTemplate: (template: ImageTemplate, paramValues: ParamValues, resolution: ImageResolution, imagePath: string, resourcePath?: string | undefined) => Promise<void>; | ||
export declare const urlToImage: (url: string, outputPath: string) => Promise<void>; | ||
export {}; |
@@ -1,3 +0,3 @@ | ||
export { compileTemplate, compileLocalTemplate } from "./compile"; | ||
export { compileTemplate, compileLocalTemplate, cachedImageName, fileExists } from "./compile"; | ||
export { loadLocalTemplate } from "./local"; | ||
export { parseParameters } from "./parse-parameters"; |
@@ -7,2 +7,5 @@ import { renderTemplateToHtml } from '@resoc/core'; | ||
import copy from 'recursive-copy'; | ||
import { hashElement } from 'folder-hash'; | ||
import sha256 from 'sha256'; | ||
import Mustache from 'mustache'; | ||
@@ -141,10 +144,70 @@ /*! ***************************************************************************** | ||
var compileLocalTemplate = function (templateManifestPath, paramValues, resolution, imagePath) { return __awaiter(void 0, void 0, void 0, function () { | ||
var template; | ||
var imageFingerprint = function (templateDir, values) { return __awaiter(void 0, void 0, void 0, function () { | ||
var hash, sha; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, loadLocalTemplate(templateManifestPath)]; | ||
case 0: return [4 /*yield*/, hashElement(templateDir)]; | ||
case 1: | ||
hash = _a.sent(); | ||
return [4 /*yield*/, sha256(JSON.stringify(hash) + JSON.stringify(values))]; | ||
case 2: | ||
sha = _a.sent(); | ||
return [2 /*return*/, sha.substr(0, 8)]; | ||
} | ||
}); | ||
}); }; | ||
var fileExists = function (path) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, fs.access(path)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, true]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/, false]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
var cachedImageName = function (templateDir, values, imagePath) { return __awaiter(void 0, void 0, void 0, function () { | ||
var hash; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, imageFingerprint(templateDir, values)]; | ||
case 1: | ||
hash = _a.sent(); | ||
return [2 /*return*/, Mustache.render(imagePath, { hash: hash })]; | ||
} | ||
}); | ||
}); }; | ||
var compileLocalTemplate = function (templateManifestPath, paramValues, resolution, imagePath, options) { return __awaiter(void 0, void 0, void 0, function () { | ||
var templateDir, template; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
options = Object.assign({}, { | ||
cache: false | ||
}, options); | ||
templateDir = path.resolve(path.dirname(templateManifestPath)); | ||
if (!options.cache) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, cachedImageName(templateDir, paramValues, imagePath)]; | ||
case 1: | ||
imagePath = _a.sent(); | ||
return [4 /*yield*/, fileExists(imagePath)]; | ||
case 2: | ||
if (_a.sent()) { | ||
return [2 /*return*/, imagePath]; | ||
} | ||
_a.label = 3; | ||
case 3: return [4 /*yield*/, loadLocalTemplate(templateManifestPath)]; | ||
case 4: | ||
template = _a.sent(); | ||
return [2 /*return*/, compileTemplate(template, paramValues, resolution, imagePath, path.resolve(path.dirname(templateManifestPath)))]; | ||
return [4 /*yield*/, compileTemplate(template, paramValues, resolution, imagePath, templateDir)]; | ||
case 5: | ||
_a.sent(); | ||
return [2 /*return*/, imagePath]; | ||
} | ||
@@ -222,3 +285,3 @@ }); | ||
export { compileLocalTemplate, compileTemplate, loadLocalTemplate, parseParameters }; | ||
export { cachedImageName, compileLocalTemplate, compileTemplate, fileExists, loadLocalTemplate, parseParameters }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -11,2 +11,5 @@ 'use strict'; | ||
var copy = require('recursive-copy'); | ||
var folderHash = require('folder-hash'); | ||
var sha256 = require('sha256'); | ||
var Mustache = require('mustache'); | ||
@@ -20,2 +23,4 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var copy__default = /*#__PURE__*/_interopDefaultLegacy(copy); | ||
var sha256__default = /*#__PURE__*/_interopDefaultLegacy(sha256); | ||
var Mustache__default = /*#__PURE__*/_interopDefaultLegacy(Mustache); | ||
@@ -154,10 +159,70 @@ /*! ***************************************************************************** | ||
var compileLocalTemplate = function (templateManifestPath, paramValues, resolution, imagePath) { return __awaiter(void 0, void 0, void 0, function () { | ||
var template; | ||
var imageFingerprint = function (templateDir, values) { return __awaiter(void 0, void 0, void 0, function () { | ||
var hash, sha; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, loadLocalTemplate(templateManifestPath)]; | ||
case 0: return [4 /*yield*/, folderHash.hashElement(templateDir)]; | ||
case 1: | ||
hash = _a.sent(); | ||
return [4 /*yield*/, sha256__default['default'](JSON.stringify(hash) + JSON.stringify(values))]; | ||
case 2: | ||
sha = _a.sent(); | ||
return [2 /*return*/, sha.substr(0, 8)]; | ||
} | ||
}); | ||
}); }; | ||
var fileExists = function (path) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, fs__default['default'].access(path)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, true]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/, false]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
var cachedImageName = function (templateDir, values, imagePath) { return __awaiter(void 0, void 0, void 0, function () { | ||
var hash; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, imageFingerprint(templateDir, values)]; | ||
case 1: | ||
hash = _a.sent(); | ||
return [2 /*return*/, Mustache__default['default'].render(imagePath, { hash: hash })]; | ||
} | ||
}); | ||
}); }; | ||
var compileLocalTemplate = function (templateManifestPath, paramValues, resolution, imagePath, options) { return __awaiter(void 0, void 0, void 0, function () { | ||
var templateDir, template; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
options = Object.assign({}, { | ||
cache: false | ||
}, options); | ||
templateDir = path__default['default'].resolve(path__default['default'].dirname(templateManifestPath)); | ||
if (!options.cache) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, cachedImageName(templateDir, paramValues, imagePath)]; | ||
case 1: | ||
imagePath = _a.sent(); | ||
return [4 /*yield*/, fileExists(imagePath)]; | ||
case 2: | ||
if (_a.sent()) { | ||
return [2 /*return*/, imagePath]; | ||
} | ||
_a.label = 3; | ||
case 3: return [4 /*yield*/, loadLocalTemplate(templateManifestPath)]; | ||
case 4: | ||
template = _a.sent(); | ||
return [2 /*return*/, compileTemplate(template, paramValues, resolution, imagePath, path__default['default'].resolve(path__default['default'].dirname(templateManifestPath)))]; | ||
return [4 /*yield*/, compileTemplate(template, paramValues, resolution, imagePath, templateDir)]; | ||
case 5: | ||
_a.sent(); | ||
return [2 /*return*/, imagePath]; | ||
} | ||
@@ -235,6 +300,8 @@ }); | ||
exports.cachedImageName = cachedImageName; | ||
exports.compileLocalTemplate = compileLocalTemplate; | ||
exports.compileTemplate = compileTemplate; | ||
exports.fileExists = fileExists; | ||
exports.loadLocalTemplate = loadLocalTemplate; | ||
exports.parseParameters = parseParameters; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@resoc/create-img", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Create an image based on a Resoc image template", | ||
@@ -31,4 +31,7 @@ "main": "build/index.js", | ||
"commander": "^8.1.0", | ||
"folder-hash": "^4.0.1", | ||
"mustache": "^4.2.0", | ||
"puppeteer": "^10.2.0", | ||
"recursive-copy": "^2.0.13" | ||
"recursive-copy": "^2.0.13", | ||
"sha256": "^0.2.0" | ||
}, | ||
@@ -43,3 +46,6 @@ "devDependencies": { | ||
"@rollup/plugin-json": "^4.1.0", | ||
"@types/folder-hash": "^4.0.1", | ||
"@types/jest": "^27.0.1", | ||
"@types/mustache": "^4.1.2", | ||
"@types/sha256": "^0.2.0", | ||
"jest": "^27.0.6", | ||
@@ -54,3 +60,3 @@ "nodemon": "^2.0.12", | ||
}, | ||
"gitHead": "e7a11a17f5baa8e9b99c40aa2d1bdb42ab9455d2" | ||
"gitHead": "f53e448ace457d0b37debc70f2daf2f755224dcb" | ||
} |
@@ -8,7 +8,47 @@ import { ImageTemplate, ImageResolution, loadRemoteTemplate, ParamValues, renderTemplateToHtml } from '@resoc/core' | ||
import { loadLocalTemplate } from './local' | ||
import { imageFingerprint } from './fingerprint' | ||
import Mustache from 'mustache' | ||
export const compileLocalTemplate = async (templateManifestPath: string, paramValues: ParamValues, resolution: ImageResolution, imagePath: string): Promise<void> => { | ||
type LocalTemplateOptions = { | ||
cache: boolean; | ||
}; | ||
export const fileExists = async (path: string): Promise<boolean> => { | ||
try { | ||
await fs.access(path); | ||
return true; | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
}; | ||
export const cachedImageName = async (templateDir: string, values: ParamValues, imagePath: string): Promise<string> => { | ||
const hash = await imageFingerprint(templateDir, values); | ||
return Mustache.render(imagePath, { hash }); | ||
}; | ||
export const compileLocalTemplate = async ( | ||
templateManifestPath: string, | ||
paramValues: ParamValues, | ||
resolution: ImageResolution, | ||
imagePath: string, | ||
options?: LocalTemplateOptions | ||
): Promise<string> => { | ||
options = Object.assign({}, { | ||
cache: false | ||
}, options); | ||
const templateDir = path.resolve(path.dirname(templateManifestPath)); | ||
if (options.cache) { | ||
imagePath = await cachedImageName(templateDir, paramValues, imagePath); | ||
if (await fileExists(imagePath)) { | ||
return imagePath; | ||
} | ||
} | ||
const template = await loadLocalTemplate(templateManifestPath); | ||
return compileTemplate( | ||
await compileTemplate( | ||
template, | ||
@@ -18,4 +58,6 @@ paramValues, | ||
imagePath, | ||
path.resolve(path.dirname(templateManifestPath)) | ||
templateDir | ||
); | ||
return imagePath; | ||
}; | ||
@@ -22,0 +64,0 @@ |
@@ -1,3 +0,3 @@ | ||
export { compileTemplate, compileLocalTemplate } from "./compile"; | ||
export { compileTemplate, compileLocalTemplate, cachedImageName, fileExists } from "./compile"; | ||
export { loadLocalTemplate } from "./local"; | ||
export { parseParameters } from "./parse-parameters"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
145232
37
1257
7
19
+ Addedfolder-hash@^4.0.1
+ Addedmustache@^4.2.0
+ Addedsha256@^0.2.0
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedconvert-hex@0.1.0(transitive)
+ Addedconvert-string@0.1.0(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addedfolder-hash@4.0.4(transitive)
+ Addedminimatch@5.1.6(transitive)
+ Addedms@2.1.3(transitive)
+ Addedsha256@0.2.0(transitive)