Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codio-api-js",
"version": "0.0.9",
"version": "0.1.1",
"description": "JS client to Codio API",
"repository": "https://github.com/codio/codio-api-js",
"author": "Max Kraev <maxim.kraev@gmail.com>",
Expand Down
40 changes: 30 additions & 10 deletions src/lib/assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; }> {
Expand Down Expand Up @@ -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<string, Yaml> = 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<Yaml[]> {
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<void> {
Expand Down
42 changes: 32 additions & 10 deletions src/lib/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import copy from 'recursive-copy'

async function copyStripped(srcDir: string, bookStripped: Object, metadataStriped: Object, dstDir: string, paths: string[]): Promise<void> {
paths.push('.guides/**')
paths.push('.codio')
paths.push('.codio-menu')
paths.push('.settings')
paths.push('!.github/**')
paths.push('!.github')
await copy(srcDir, dstDir, {
Expand All @@ -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`)
}
Expand All @@ -46,9 +63,16 @@ 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)
if (!section) {
throw new Error(`${section} not found`)
}
children.push(section)
}
book.children = children
return book
}

Expand All @@ -60,18 +84,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<void> {
// 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<void> {
const bookJsonPath = path.join(srcDir, '.guides', 'book.json')
const metadataPath = path.join(srcDir, '.guides', 'metadata.json')

Expand Down
4 changes: 2 additions & 2 deletions src/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
const token = await auth(clientId, secretId, config.getDomain())
v1.setAuthToken(token)
Expand Down