Skip to content

Commit 9c678a8

Browse files
committed
store config per section
1 parent b49d998 commit 9c678a8

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/lib/assignment.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ type YamlRaw = {
2323
withChildren: boolean | undefined
2424
}
2525

26+
export type SectionConfig = {
27+
withChildren: boolean
28+
}
29+
30+
export function sectionToKey(section: string[]) {
31+
return section.join('\n')
32+
}
33+
2634
type Yaml = {
2735
assignment: string | undefined
2836
assignmentName: string | undefined
2937
paths: (string | PathMap)[]
3038
section: string[][]
31-
withChildren: boolean
39+
sectionConfig: Map<string, SectionConfig>
3240
}
3341

3442
export type TimeExtension = {
@@ -194,21 +202,24 @@ function validityState(ymls: YamlRaw[]): Yaml[] {
194202
if (assignmentId === undefined) {
195203
throw new Error('assignment and assignmentName does not exist')
196204
}
205+
const withChildren = yml.withChildren !== false
197206
if (map.has(assignmentId)) {
198207
const item = map.get(assignmentId)
199208
if (!item) {
200209
continue
201210
}
202211
item.section.push(section)
203212
item.paths = item.paths.concat(yml.paths || [])
213+
item.sectionConfig.set(sectionToKey(section), {withChildren: withChildren})
204214
} else {
205-
const withChildren = yml.withChildren !== false
215+
const sectionConfig: Map<string, SectionConfig> = new Map()
216+
sectionConfig.set(sectionToKey(section), {withChildren: withChildren})
206217
map.set(assignmentId, {
207218
assignment: yml.assignment,
208219
assignmentName: yml.assignmentName,
209220
paths: yml.paths || [],
210221
section: [section],
211-
withChildren: withChildren
222+
sectionConfig: sectionConfig
212223
})
213224
}
214225
}
@@ -232,13 +243,14 @@ function validateYmlCfg(ymls: Yaml[]): Yaml[] {
232243
}
233244
item.section = item.section.concat(section)
234245
item.paths = item.paths.concat(yml.paths || [])
246+
item.sectionConfig = yml.sectionConfig
235247
} else {
236248
map.set(assignmentId, {
237249
assignment: yml.assignment,
238250
assignmentName: yml.assignmentName,
239251
paths: yml.paths || [],
240252
section: section,
241-
withChildren: yml.withChildren
253+
sectionConfig: yml.sectionConfig
242254
})
243255
}
244256
}
@@ -301,7 +313,7 @@ async function reducePublish(courseId: string, srcDir: string, yamlDir: string,
301313
if (!item.assignment) {
302314
throw new Error(`assignment not found with name "${item.assignmentName}"`)
303315
}
304-
await tools.reduce(srcDir, tmpDstDir, item.section, _.compact(paths), item.withChildren)
316+
await tools.reduce(srcDir, tmpDstDir, item.section, _.compact(paths), item.sectionConfig)
305317
await assignment.publish(courseId, item.assignment, tmpDstDir, changelogOrOptions)
306318
fs.rmSync(tmpDstDir, {recursive: true})
307319
}

src/lib/tools.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {excludePaths} from './config'
77
import tar from 'tar'
88
import { ZSTDCompress } from 'simple-zstd'
99
import config from './config'
10-
import { PathMap } from './assignment'
10+
import { PathMap, SectionConfig, sectionToKey } from './assignment'
1111

1212
const CONVERTER_VERSION = '4ca4944ddf9d4fe4df9697bec06cbd0a6c170419'
1313
const GUIDES_CONTENT_DIR = '.guides/content'
@@ -22,14 +22,15 @@ export async function fixGuidesVersion(projectPath: string) {
2222
}
2323

2424
export async function reduce(
25-
srcDir: string, dstDir: string, yaml_sections: string[][], paths: (string | PathMap)[], withChildren=true
25+
srcDir: string, dstDir: string, yaml_sections: string[][],
26+
paths: (string | PathMap)[], sectionConfig: Map<string, SectionConfig> = new Map()
2627
): Promise<void> {
2728
await fixGuidesVersion(srcDir)
2829
const contentDir = path.join(srcDir, GUIDES_CONTENT_DIR)
2930
const rootMetadataPath = path.join(contentDir, INDEX_METADATA_FILE)
3031
const rootMetadata = readMetadataFile(rootMetadataPath)
3132
const guidesStructure = getGuidesStructure(rootMetadata, srcDir, '')
32-
const filter = collectFilter(guidesStructure, _.cloneDeep(yaml_sections), withChildren)
33+
const filter = collectFilter(guidesStructure, _.cloneDeep(yaml_sections), sectionConfig)
3334
const strippedStructure = stripStructure(guidesStructure, filter)
3435
const strippedSectionsIds = getStrippedSectionIds(strippedStructure)
3536
const excludePaths = getExcludedPaths(guidesStructure, strippedSectionsIds)
@@ -84,7 +85,7 @@ const DEFAULT_ALL_SECTION: Section = {
8485
children: {}
8586
}
8687

87-
function collectFilter(guidesStructure, yaml_sections, withChildren: boolean) {
88+
function collectFilter(guidesStructure, yaml_sections: string[][], sectionConfig: Map<string, SectionConfig>) {
8889
const filterMap = {
8990
all: false,
9091
children: {}
@@ -94,7 +95,8 @@ function collectFilter(guidesStructure, yaml_sections, withChildren: boolean) {
9495
if (sectionPath.length === 0) {
9596
continue
9697
}
97-
const section = traverseItems(guidesStructure, sectionPath, filterMap, withChildren)
98+
const withChildren = sectionConfig.has(sectionToKey(sectionPath)) ? sectionConfig.get(sectionToKey(sectionPath))?.withChildren : true
99+
const section = traverseItems(guidesStructure, sectionPath, filterMap, withChildren??true)
98100
if (!section) {
99101
throw new Error(`${section} not found`)
100102
}

0 commit comments

Comments
 (0)