Skip to content

Commit d5636f7

Browse files
committed
Interpret portal/localasset
1 parent 3dceadb commit d5636f7

File tree

5 files changed

+137
-19
lines changed

5 files changed

+137
-19
lines changed

src/compiler2/ontologies/portal/_maybe_models/localAsset.js

-17
This file was deleted.

src/compiler2/ontologies/portal/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Ontology = require('../../lib/Ontology')
22
const SubOntologies = require('./subontology')
33
const Homepage = require('./homepage')
44
const Subpages = require('./subpage')
5+
const LocalAssets = require('./localasset')
56

67
module.exports = ({ ontologies }) => {
78
class Portal extends Ontology {
@@ -35,11 +36,17 @@ module.exports = ({ ontologies }) => {
3536
return withSubpages
3637
}
3738

38-
// localAssets
39+
const withLocalAssets = LocalAssets.reduce(
40+
(withSubpages || withHomepage || withSubOntologies || contentModel),
41+
entry
42+
)
43+
if (withLocalAssets) {
44+
return withLocalAssets
45+
}
3946

4047
return contentModel
4148
}, {})
42-
console.log('portal.contentModel', JSON.stringify(this.contentModel, null, 2))
49+
// console.log('portal.contentModel', JSON.stringify(this.contentModel, null, 2))
4350
}
4451

4552
async render(renderer) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const model = require('./model')
2+
const view = require('./view')
3+
4+
const render = (renderer, contentModel) => {
5+
return view(renderer, contentModel)
6+
}
7+
8+
const reduce = (contentModel, entry) => {
9+
if (!model.match(entry)) {
10+
return undefined
11+
}
12+
return {
13+
...contentModel,
14+
localAssets: [
15+
...(contentModel.localAssets || []),
16+
model.create(entry).data
17+
]
18+
}
19+
}
20+
21+
module.exports = {
22+
model,
23+
view,
24+
render,
25+
reduce
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const Model = require('../../../lib/Model')
2+
const contentTypes = require('../contentTypes')
3+
4+
const LocalAsset = new Model({
5+
schema: (entry) => ({
6+
type: 'object'
7+
}),
8+
9+
create(entry) {
10+
return {
11+
type: contentTypes.LOCAL_ASSET,
12+
data: {
13+
name: entry.data.name.data,
14+
path: entry.data.path.data,
15+
stats: entry.data.stats.data,
16+
depth: entry.data.depth.data,
17+
format: entry.data.format.data
18+
}
19+
}
20+
}
21+
})
22+
23+
module.exports = LocalAsset
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const { cp, stat } = require('fs/promises')
2+
const { join, dirname, sep } = require('path')
3+
const Settings = require('../../../../settings')
4+
const { contentRoot, getSlug } = require('../../../../helpers')
5+
const { debugLog } = require('../../../../debug')
6+
7+
const all = Promise.all.bind(Promise)
8+
9+
const withBasePath = (basePath) => (asset) => ({ ...asset, basePath })
10+
11+
const copyAsset = async ({ basePath, destPath, path, name, isFolder }) => {
12+
const { out } = Settings.getSettings()
13+
const outputDir = dirname(destPath || path)
14+
const dirnameSlug = outputDir === '.' ?
15+
outputDir :
16+
outputDir.split(sep).map(getSlug).join(sep)
17+
const outPath = join(out, join(dirnameSlug, name))
18+
debugLog('copying:', path)
19+
try {
20+
return await cp(join(basePath, path), outPath, { recursive: !!isFolder })
21+
} catch (e) {
22+
if (e.code === 'ENOENT') {
23+
debugLog('failed copying asset that no longer exists', e)
24+
} else {
25+
debugLog('failed copying asset', e)
26+
}
27+
return Promise.resolve()
28+
}
29+
}
30+
31+
const copyLocalAssets = async (contentModel) => {
32+
const { rootDirectory, contentDirectory, pagesDirectory } = Settings.getSettings()
33+
const basePath = await contentRoot(rootDirectory, contentDirectory)
34+
const { localAssets, homepage, posts, subpages, categories } = contentModel
35+
36+
console.log('copyLocalAssets CM', contentModel)
37+
38+
const copyRootAssets = all(
39+
localAssets
40+
.map(withBasePath(basePath))
41+
.map(copyAsset)
42+
)
43+
44+
const copyHomepageAssets = all(
45+
(homepage.localAssets || []).map((localAsset) => {
46+
const assetWithBasePath = withBasePath(basePath)(localAsset)
47+
return copyAsset({
48+
...assetWithBasePath,
49+
destPath: '.'
50+
})
51+
})
52+
)
53+
54+
const copySubpageAssets = all(
55+
(subpages || []).map(({ localAssets = [] }) => {
56+
return all(
57+
localAssets
58+
.map(withBasePath(basePath))
59+
.map(a => ({
60+
...a,
61+
destPath: a.path.replace(new RegExp('^' + pagesDirectory), '')
62+
}))
63+
.map(copyAsset)
64+
)
65+
})
66+
)
67+
68+
return all([
69+
copyRootAssets,
70+
copyHomepageAssets,
71+
copySubpageAssets
72+
])
73+
}
74+
75+
const renderLocalAssets = (renderer, contentModel) => {
76+
return copyLocalAssets(contentModel)
77+
}
78+
79+
module.exports = renderLocalAssets

0 commit comments

Comments
 (0)