Skip to content
Open
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
32 changes: 24 additions & 8 deletions scripts/format.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const fs = require('fs').promises
const path = require('path')

const REPLACES = [
[/([^/])\bjavascript/gi, '$1JavaScript'], // stylise javascript to JavaScript
Expand Down Expand Up @@ -37,12 +36,29 @@ function applyReplaces(text) {
return REPLACES.reduce((prev, [a, b]) => prev.replace(a, b), text)
}

// Returns an array of the filenames of all the episodes
async function getTranscripts() {
const { stdout } = await exec('find **/*.md')
return stdout
.split('\n') // make an array
.filter(x => x.endsWith('.md')) // remove blanks
// Returns an array of the filenames of all the episodes.
// Transcripts live one directory deep (e.g. afk/away-from-keyboard-1.md), so we
// read each top-level podcast directory and collect its Markdown files. This is
// cross-platform (no reliance on a Unix `find`) and skips hidden/tooling folders.
async function getTranscripts(dir = '.') {
const entries = await fs.readdir(dir, { withFileTypes: true })
const perDirectory = await Promise.all(
entries
.filter(entry =>
entry.isDirectory() &&
!entry.name.startsWith('.') &&
entry.name !== 'node_modules' &&
entry.name !== 'scripts'
)
.map(async entry => {
const subdir = path.join(dir, entry.name)
const files = await fs.readdir(subdir)
return files
.filter(name => name.endsWith('.md'))
.map(name => path.join(subdir, name))
})
)
return perDirectory.flat()
}

// Read a textfile to a string, pass the string through processor, and write the results out to a file
Expand Down
6 changes: 5 additions & 1 deletion scripts/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ test("WebAssembly is a single word proper noun", () => {

test('getTranscripts works as expected', async () => {
const transcripts = await getTranscripts()
// TODO: What are good ways to test this function?
expect(Array.isArray(transcripts)).toBe(true)
expect(transcripts.length).toBeGreaterThan(0)
expect(transcripts.every(name => name.endsWith('.md'))).toBe(true)
// Transcripts live one directory deep, never at the repo root
expect(transcripts.every(name => name.includes(path.sep))).toBe(true)
})


Expand Down