build-elm-assets
Advanced tools
Comparing version 1.0.0 to 1.1.0
51
index.js
@@ -7,5 +7,11 @@ const dive = require("dive"); | ||
const handlebars = require("handlebars"); | ||
const isString = x => typeof x === "string"; | ||
const isFunction = x => typeof x === "function"; | ||
function collectAssets(config, callback) { | ||
const { assetsPath, replacePath, buildUrl } = config; | ||
const { | ||
assetsPath, | ||
replacePath, | ||
buildUrl | ||
} = config; | ||
let assets = []; | ||
@@ -15,10 +21,11 @@ try { | ||
assetsPath, | ||
(err, file) => { | ||
(err, copyFrom) => { | ||
if (err) throw new Error(err); | ||
const fileName = replacePath(file); | ||
const hash = md5.sync(file); | ||
const url = buildUrl(fileName, hash); | ||
const elmName = createElmName(fileName); | ||
assets.push({ url, elmName }); | ||
const copyTo = replacePath(copyFrom); | ||
const hash = md5.sync(copyFrom); | ||
const urlWithHash = buildUrl(copyTo, hash); | ||
const elmName = createElmName(copyTo); | ||
moveAssets(copyFrom, copyTo, urlWithHash, config); | ||
assets.push({ urlWithHash, elmName }); | ||
}, | ||
@@ -32,2 +39,12 @@ () => checkForDuplications(assets, callback) | ||
function moveAssets(copyFrom, copyTo, link, { assetsOutputPath, assetsLink }) { | ||
if (isString(assetsOutputPath)) { | ||
const destination = path.join(assetsOutputPath, copyTo); | ||
fs.copySync(copyFrom, destination); | ||
if (isString(assetsLink)) { | ||
fs.ensureSymlinkSync(destination, path.join(assetsLink, link)); | ||
} | ||
} | ||
} | ||
function checkForDuplications(assets, callback) { | ||
@@ -98,6 +115,5 @@ const dups = findDuplicatesBy(assets, x => x.elmName); | ||
} | ||
function validateConfig(config) { | ||
const errors = []; | ||
if (typeof config.assetsPath !== "string") { | ||
if (!isString(config.assetsPath)) { | ||
errors.push( | ||
@@ -107,3 +123,3 @@ 'You need to define `assetsPath` as a string. i.e. "app/assets/images"' | ||
} | ||
if (typeof config.outputPath !== "string") { | ||
if (!isString(config.outputPath)) { | ||
errors.push( | ||
@@ -113,6 +129,6 @@ 'You need to define `outputPath` as a string. i.e. "src/generated/"' | ||
} | ||
if (typeof config.outputPath !== "string") { | ||
if (!isString(config.outputPath)) { | ||
errors.push('You need to define `moduleNamespace` as a string. i.e. "Nri"'); | ||
} | ||
if (typeof config.replacePath !== "function") { | ||
if (!isFunction(config.replacePath)) { | ||
errors.push( | ||
@@ -122,3 +138,3 @@ "You need to define `replacePath` as a string. i.e. \"fileName => fileName.replace(/app/assets/, '')\"" | ||
} | ||
if (typeof config.buildUrl !== "function") { | ||
if (!isFunction(config.buildUrl)) { | ||
errors.push( | ||
@@ -146,3 +162,3 @@ "You need to define `buildUrl` as a string. i.e. \"(fileName, hash) => 'assets/' + hash + '-' + fileName\"" | ||
{{elmName}} = | ||
AssetPath "{{url}}" | ||
AssetPath "{{urlWithHash}}" | ||
{{/each}} | ||
@@ -158,6 +174,7 @@ `; | ||
}, | ||
collectAssets, | ||
createElmName, | ||
collectAssets, | ||
writeElmFile, | ||
validateConfig | ||
moveAssets, | ||
validateConfig, | ||
writeElmFile | ||
}; |
{ | ||
"name": "build-elm-assets", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "builds a elm file containing all paths of a assets directory", | ||
@@ -36,5 +36,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"ava": "^0.19.0", | ||
"ava": "^0.19.1", | ||
"mock-require": "^2.0.2" | ||
} | ||
} |
@@ -16,2 +16,7 @@ Build Elm Assets | ||
assetsPath: path.join("app", "assets", "images"), | ||
// asset gets copied to this folder if provided | ||
assetsOutputPath: path.join("public/copies"), | ||
// link to assetsOutputPath with hash goes here | ||
// this is only useful when you copy the file using assetsOutputPath | ||
assetsLink: path.join("public"), | ||
// function to modify the asset path this path will be passed to buildUrl | ||
@@ -18,0 +23,0 @@ replacePath: fileName => fileName.replace(/.*\/app\/assets\/images\//, ""), |
50
test.js
import test from "ava"; | ||
import path from "path"; | ||
import mock from "mock-require"; | ||
@@ -48,7 +49,7 @@ import buildElmAssets from "./index.js"; | ||
elmName: "foo_png", | ||
url: "foo-HASH.png" | ||
urlWithHash: "foo-HASH.png" | ||
}, | ||
{ | ||
elmName: "bar_png", | ||
url: "bar-HASH.png" | ||
urlWithHash: "bar-HASH.png" | ||
} | ||
@@ -79,2 +80,43 @@ ]; | ||
test("moveAssets", t => { | ||
mock("fs-extra", { | ||
copySync: (s, d) => { | ||
t.deepEqual(s, "app/assets/foo.png"); | ||
t.deepEqual(d, "public/test/assets/foo.png"); | ||
}, | ||
ensureSymlinkSync: (s, d) => { | ||
t.deepEqual(s, "public/test/assets/foo.png"); | ||
t.deepEqual(d, "public/assets/foo-hash.png"); | ||
} | ||
}); | ||
const { moveAssets } = mock.reRequire("./index.js"); | ||
const config = { | ||
assetsOutputPath: "public/test", | ||
assetsLink: "public/" | ||
}; | ||
moveAssets( | ||
"app/assets/foo.png", | ||
"assets/foo.png", | ||
"assets/foo-hash.png", | ||
config | ||
); | ||
}); | ||
test("moveAssets doesn't link or copy if not configured", t => { | ||
mock("fs-extra", { | ||
copySync: () => t.fail(), | ||
ensureSymlinkSync: () => t.fail() | ||
}); | ||
const { moveAssets } = mock.reRequire("./index.js"); | ||
const config = {}; | ||
moveAssets( | ||
"app/assets/foo.png", | ||
"assets/foo.png", | ||
"assets/foo-hash.png", | ||
config | ||
); | ||
t.pass(); | ||
}); | ||
test("writeElmFile", t => { | ||
@@ -99,4 +141,4 @@ mock("fs-extra", { | ||
const assets = [ | ||
{ elmName: "img1_png", url: "assets/img1.png" }, | ||
{ elmName: "img2_png", url: "assets/img2.png" } | ||
{ elmName: "img1_png", urlWithHash: "assets/img1.png" }, | ||
{ elmName: "img2_png", urlWithHash: "assets/img2.png" } | ||
]; | ||
@@ -103,0 +145,0 @@ const done = (err, actual) => t.is(actual, expected); |
12562
312
38