From 9e15e1ddda1210b86fd7d71e606a756e0dd66da9 Mon Sep 17 00:00:00 2001 From: Max Kraev Date: Thu, 3 Jun 2021 16:15:16 +0100 Subject: [PATCH 1/4] more exclude, allow multi definition --- src/lib/assignment.ts | 40 ++++++++++++++++++++++++++++++---------- src/lib/tools.ts | 39 +++++++++++++++++++++++++++++---------- src/v1.ts | 4 ++-- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/src/lib/assignment.ts b/src/lib/assignment.ts index 4faae2a..e270e8f 100644 --- a/src/lib/assignment.ts +++ b/src/lib/assignment.ts @@ -9,10 +9,17 @@ import tools from './tools' import config from './config' import _ from 'lodash' +type YamlRaw = { + assignment: string + paths: string[] | undefined + section: string | string[] +} + + type Yaml = { assignment: string paths: string[] - section: string | string[] + section: string[][] } async function archiveTar(src: string): Promise<{ file: string; dir: string; }> { @@ -79,29 +86,42 @@ async function publishArchive (courseId: string, assignmentId:string, archivePat } } -function validityState(ymls: Yaml[]): void { - const assignmentIds: string[] = [] +function validityState(ymls: YamlRaw[]): Yaml[] { + const map: Map = new Map() for(const yml of ymls) { - if (assignmentIds.includes(yml.assignment)) { - throw new Error(`assignment ${yml.assignment} defined twice`) + const section = _.isString(yml.section)? [yml.section] : yml.section + if (map.has(yml.assignment)) { + const item = map.get(yml.assignment) + if (!item) { + continue + } + item.section.push(section) + item.paths = item.paths.concat(yml.paths || []) + } else { + map.set(yml.assignment, { + assignment: yml.assignment, + paths: yml.paths || [], + section: [section] + }) } - assignmentIds.push(yml.assignment) } + + return Array.from(map.values()) } async function loadYaml(yamlDir: string): Promise { - let res: Yaml[] = [] + let res: YamlRaw[] = [] const files = await glob('*.+(yml|yaml)', {cwd: yamlDir, nodir: true}) for (var file of files) { const ymlText = await fs.promises.readFile(path.join(yamlDir, file), {encoding: 'utf-8'}) - let ymls: Yaml[] | Yaml = YAML.parse(ymlText) + let ymls: YamlRaw[] = YAML.parse(ymlText) if (!_.isArray(ymls)) { ymls = [ymls] } res = res.concat(ymls) } - validityState(res) - return res + + return validityState(res) } async function reducePublish(courseId: string, srcDir: string, yamlDir: string, changelog: string): Promise { diff --git a/src/lib/tools.ts b/src/lib/tools.ts index f6e0b23..835cc09 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -5,6 +5,9 @@ import copy from 'recursive-copy' async function copyStripped(srcDir: string, bookStripped: Object, metadataStriped: Object, dstDir: string, paths: string[]): Promise { paths.push('.guides/**') + paths.push('.codio') + paths.push('.codio-menu') + paths.push('.settings') paths.push('!.github/**') paths.push('!.github') await copy(srcDir, dstDir, { @@ -19,9 +22,23 @@ async function copyStripped(srcDir: string, bookStripped: Object, metadataStripe await fs.writeFile(metadataPath, JSON.stringify(metadataStriped, undefined, ' ')) } +// case-insensitive search for title +function findSection(children: any[], title: string): any | undefined { + const capitalTitle = _.upperCase(title) + for(const item of children) { + if (_.upperCase(item.title) === capitalTitle) { + return item + } + } + return undefined +} + function traverseBook(book: any, sections: string[]): any { const sectionName = sections.shift() - const section = _.find(book.children, {title: sectionName}) + if (!sectionName) { + return + } + const section = findSection(book.children, sectionName) if (!section) { throw new Error(`section "${sectionName}" is not found`) } @@ -46,9 +63,13 @@ function getSectionIds(book: any): string[] { return ids } -function stripBook(book: any, sections: string[]): any { - const section = traverseBook(book, sections) - book.children = [section] +function stripBook(book: any, sections: string[][]): any { + const children: any[] = [] + for (const sectionPath of sections ) { + const section = traverseBook(book, sectionPath) + children.push(section) + } + book.children = children return book } @@ -60,18 +81,16 @@ function stripMetadata(metadata: any, book: any): string[] { if (ids.includes(section['id'])) { newSections.push(section) } else { - excludePaths.push(`!${section['content-file']}`) + if (section['content-file']) { + excludePaths.push(`!${section['content-file']}`) + } } } metadata.sections = newSections return excludePaths } -async function reduce(srcDir: string, dstDir: string, sections: string | string[], paths: string[]): Promise { - // const assessmentsJson = path.join(srcDir, '.guides', 'assessments.json') - if (_.isString(sections)) { - sections = [sections] - } +async function reduce(srcDir: string, dstDir: string, sections: string[][], paths: string[]): Promise { const bookJsonPath = path.join(srcDir, '.guides', 'book.json') const metadataPath = path.join(srcDir, '.guides', 'metadata.json') diff --git a/src/v1.ts b/src/v1.ts index aa387f7..576254b 100644 --- a/src/v1.ts +++ b/src/v1.ts @@ -4,8 +4,8 @@ import tools from './lib/tools' import assignment from './lib/assignment' export const v1 = { - setDomain: (domain: string) => config.setDomain(domain), - setAuthToken: (token: string) => config.setToken(token), + setDomain: (_: string) => config.setDomain(_), + setAuthToken: (_: string) => config.setToken(_), auth: async (clientId: string, secretId: string): Promise => { const token = await auth(clientId, secretId, config.getDomain()) v1.setAuthToken(token) From 3517eb070b66b37aa6590032535cc9a774e1dd0a Mon Sep 17 00:00:00 2001 From: Max Kraev Date: Thu, 3 Jun 2021 16:30:19 +0100 Subject: [PATCH 2/4] version pump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ac72197..d4545b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codio-api-js", - "version": "0.0.9", + "version": "0.1.0", "description": "JS client to Codio API", "repository": "https://github.com/codio/codio-api-js", "author": "Max Kraev ", From 7b40ee5ff2e38fcd09f4d4264047199e3254f215 Mon Sep 17 00:00:00 2001 From: Max Kraev Date: Thu, 3 Jun 2021 16:50:53 +0100 Subject: [PATCH 3/4] v0.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d4545b7..30a9169 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codio-api-js", - "version": "0.1.0", + "version": "0.1.1", "description": "JS client to Codio API", "repository": "https://github.com/codio/codio-api-js", "author": "Max Kraev ", From d5e44644d63a0265301d8f127f7e8d4be6b2047f Mon Sep 17 00:00:00 2001 From: Max Kraev Date: Thu, 3 Jun 2021 16:51:06 +0100 Subject: [PATCH 4/4] add section find check --- src/lib/tools.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 835cc09..57f468a 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -67,6 +67,9 @@ function stripBook(book: any, sections: string[][]): any { const children: any[] = [] for (const sectionPath of sections ) { const section = traverseBook(book, sectionPath) + if (!section) { + throw new Error(`${section} not found`) + } children.push(section) } book.children = children