Skip to content

Commit

Permalink
Merge pull request #34 from ulkajs/0.6.6
Browse files Browse the repository at this point in the history
0.6.6
  • Loading branch information
coderosh-zz authored Oct 22, 2020
2 parents ec300ac + 54c9450 commit 3ecf805
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"prettier/prettier": ["error"],
"no-console": "off",
"no-undef": "error",
"linebreak-style": "off"
"linebreak-style": "off",
"no-const-assign": "error"
}
}
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 0.6.6

- Assets with format \*.ignore.[ext] are ignored while copying.
- More tests and coverage
- Supported beforeSetup plugin (beforeSetup plugins run before setting map contents map and pages array).
- Removed support for getting prefix from domain in siteMetaData.
- Bug fixes
- Now it's more easy to add templating engines
- Supported use of templating files inside contents (also supports frontmatter).

## 0.6.5

- Added prefix link support
Expand Down
15 changes: 14 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
module.exports = {
collectCoverage: true
collectCoverage: true,
collectCoverageFrom: [
"src/utils/**/*.js",
"src/index.js",
"src/ulka-cli/build.js"
],
coverageThreshold: {
global: {
branches: 60,
functions: 70,
lines: 70,
statements: 70
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ulka",
"version": "0.6.6-rc.5",
"version": "0.6.6",
"description": "Ulka - A simpler static site generator written in JavaScript",
"main": "./src/index.js",
"bin": {
Expand Down
74 changes: 53 additions & 21 deletions src/utils/build-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function createContentsMap(info) {
const contentsMap = {}

for (const content of contents) {
const files = allFiles(content.path, ".md")
const files = allFiles(content.path, info.contentsExtesnions || ".md")

const contentArr = []

Expand Down Expand Up @@ -89,7 +89,10 @@ function createContentsMap(info) {
* @return {Array} pages
*/
function createPagesArray(info, contents) {
const files = allFiles(info.configs.pagesPath, ".ulka")
const files = allFiles(
info.configs.pagesPath,
info.pagesExtensions || ".ulka"
)

const pagesArray = []
for (const file of files) {
Expand Down Expand Up @@ -276,10 +279,19 @@ async function contentToHtml(contentData, contents, info) {
}

if (contentData.type === "raw") {
const html = renderMarkdown(contentData.content, info)
const base = contentData.source || info.cwd
let context = { ...contentData, info }
const filePath = contentData.source || info.cwd

contentData.html = renderUlka(html, { ...contentData, info }, base, info)
const ext = path.parse(filePath).ext
const extRenderer = info.renderer[ext]

if (ext === "" || ext === ".md" || typeof extRenderer !== "function") {
const html = renderMarkdown(contentData.content, info)
contentData.html = renderUlka(html, context, filePath, info)
} else {
context = createContext(context, filePath, info)
contentData.html = extRenderer(contentData.content, context, filePath)
}
} else {
contentData.html = contentData.content
}
Expand Down Expand Up @@ -316,14 +328,16 @@ async function contentToHtml(contentData, contents, info) {
info
)
} else {
context = {
...context,
$assets: rPath => $assets(rPath, filePath, info),
$import: (rPath, $values = {}) => {
return $import(rPath, { ...context, ...$values }, filePath, info)
}
}
html = info.renderer[ext](contentData.template, context, info)
context = createContext(
context,
contentData.templatePath || filePath,
info
)
html = info.renderer[ext](
contentData.template,
context,
contentData.templatePath || filePath
)
}

const parsedBuildPath = path.parse(contentData.buildPath)
Expand Down Expand Up @@ -377,14 +391,8 @@ async function pageToHtml(pageData, pages, contents, info) {
if (ext === "" || ext === ".ulka" || typeof extRenderer !== "function") {
pageData.html = renderUlka(pageData.content, context, filePath, info)
} else {
context = {
...context,
$assets: rPath => $assets(rPath, filePath, info),
$import: (rPath, $values = {}) => {
return $import(rPath, { ...context, ...$values }, filePath, info)
}
}
pageData.html = extRenderer(pageData.content, context, info)
context = createContext(context, filePath, info)
pageData.html = extRenderer(pageData.content, context, filePath)
}
} else {
pageData.html = pageData.content
Expand Down Expand Up @@ -433,10 +441,15 @@ function createInfo(cwd, task) {
prefix += "/"
}

const pagesExtensions = [".ulka"]
const contentsExtensions = [".md"]

return {
configs,
cwd,
task,
pagesExtensions,
contentsExtensions,
ignoreExtensions: [".ulka", ".md"],
renderer: {},
prefix
Expand All @@ -447,6 +460,25 @@ function createInfo(cwd, task) {
}
}

/**
* All values to contet
* @param {Object} context
* @param {String} filePath
* @param {Object} info
* @return {Object} context
*/
function createContext(context, filePath, info) {
context = {
...context,
$assets: rPath => $assets(rPath, filePath, info),
$import: (rPath, $values = {}) => {
return $import(rPath, { ...context, ...$values }, filePath, info)
}
}

return context
}

module.exports = {
createContentsMap,
createPagesArray,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function copyAssets(info) {
for (const file of allFilesinSrc) {
const parsed = path.parse(file)

if (!parsed.name.endsWith("ulka") && !ignoreExt.includes(parsed.ext)) {
if (!parsed.name.endsWith("ignore") && !ignoreExt.includes(parsed.ext)) {
try {
const strToHash = path.relative(info.cwd, file).split(path.sep).join("")

Expand Down
11 changes: 9 additions & 2 deletions src/utils/ulka-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function mkdir(pathToDirectory) {
* Find all files is a directory using recursion
*
* @param {String} dirPath path to the directory to search files.
* @param {String} [ext] extension of the files to search.
* @param {String|String[]} [ext] extension of the files to search.
* @param {String[]} [arrayOfFiles]
* @return {String[]} Array of files
*/
Expand All @@ -53,7 +53,14 @@ function allFiles(dirPath, ext, arrayOfFiles = []) {
if (fs.statSync(pathTo).isDirectory()) {
arrayOfFiles = allFiles(pathTo, ext, arrayOfFiles)
} else {
if (!ext || file.endsWith(ext)) arrayOfFiles.push(pathTo)
const fileExt = path.parse(file).ext

const shouldPushToFilesArray =
!ext ||
(typeof ext === "string" && ext === fileExt) ||
(Array.isArray(ext) && ext.includes(fileExt))

if (shouldPushToFilesArray) arrayOfFiles.push(pathTo)
}
})

Expand Down
6 changes: 6 additions & 0 deletions tests/e2e/basic.e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require("path")
const cheerio = require("cheerio")
const build = require("../../src/ulka-cli/build")
const { createInfo } = require("../../src/utils/build-utils")
const { rmdir } = require("../../src/utils/ulka-fs")

beforeAll(async () => {
const cwd = path.join(__dirname, "resources", "basic")
Expand All @@ -11,6 +12,11 @@ beforeAll(async () => {
await build(info)
})

afterAll(async () => {
const buildDir = path.join(__dirname, "resources", "basic", "build")
rmdir(buildDir)
})

describe("pages - index.html", () => {
let $
beforeAll(() => {
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/resources/basic/src/pages/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/e2e/resources/basic/static/my-html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>This is my html</title>
</head>
<body>
hehe
</body>
</html>
Empty file.
8 changes: 7 additions & 1 deletion tests/e2e/resources/with-plugin/plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ module.exports = {
name: "Roshan Acharya",
age: 20
}
}
},
beforeBuild() {},
afterBuild() {},
beforeSetup() {},
remarkablePlugin() {},
afterContentRender() {},
afterPageRender() {}
}
14 changes: 14 additions & 0 deletions tests/unit/utils/helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const helpers = require("../../../src/utils/helpers")
const path = require("path")
const { spinner } = require("../../../src/utils/helpers")

const cwd = process.cwd()

Expand Down Expand Up @@ -98,3 +99,16 @@ describe("getConfigs function", () => {
spy.mockRestore()
})
})

describe("spinner function", () => {
test("should be called after 1s of calling it", done => {
const stop = spinner()
const spy = jest.spyOn(process.stdout, "write")
setTimeout(() => {
stop()
expect(spy.mock.calls[0].length).toEqual(1)
spy.mockRestore()
done()
}, 100)
})
})
20 changes: 20 additions & 0 deletions tests/unit/utils/ulka-fs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ describe("all files function", () => {
`)
})

test("should return the list of all files in ext array", () => {
const files = allFiles(allFilesTestDir, [".js", ".json"]).map(
replaceSepWithSlash
)
expect(files).toMatchInlineSnapshot(`
Array [
"resources/test-all-files/1-dir/1-1-dir/1-1.js",
"resources/test-all-files/1-dir/1-1-dir/1-1.json",
"resources/test-all-files/1-dir/1.js",
"resources/test-all-files/1-dir/1.json",
"resources/test-all-files/2-dir/2.js",
"resources/test-all-files/2-dir/2.json",
]
`)
})

test("should return filePath in a string of path to file is provided", () => {
const files = allFiles(path.join(allFilesTestDir, "1-dir", "1.js")).map(
replaceSepWithSlash
Expand All @@ -47,6 +63,10 @@ describe("all files function", () => {
]
`)
})

test("should throw error on fail", () => {
expect(() => allFiles(path.join(allFilesTestDir, "hehe"))).toThrowError()
})
})

describe("mkdir and rmdir function", () => {
Expand Down

0 comments on commit 3ecf805

Please sign in to comment.