Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add automated formatting
  • Loading branch information
simeydk committed Oct 14, 2021
1 parent 31d9db4 commit 9b1d2b9
Show file tree
Hide file tree
Showing 5 changed files with 4,870 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -14,3 +14,11 @@ Thanks for wanting to help out! These pointers below _should_ help you fall into
### Conventions 馃

- Please review the [conventions](https://github.com/thechangelog/transcripts#conventions) section of the README before opening a PR

### Formatting 馃摃

This repo has an automatic formatter that applies some style conventions. To run this:
* Ensure you have the latest stable version of [NodeJS](https://nodejs.org) installed.
* run `npm install` to install dependencies
* run `npm run format` to apply formatting changes to the repo
* run `npm run format:test` to run the tests for the formatter
55 changes: 55 additions & 0 deletions format.js
@@ -0,0 +1,55 @@
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const fs = require('fs').promises


const REPLACES = [
[/([^/])\bjavascript/gi, '$1JavaScript'], // stylise javascript to JavaScript
[/\bML ops/gi, 'MLOps'],
[/\bdev ops/gi, 'DevOps'],
[/\bclick[ -]ops/g, 'ClickOps'],

[/\[00:(\d{2}:\d{2}).\d{2}\]/g, '\[$1]'], // Remove leading 00: and fractions in timestamps
[/\[(\d{2}:\d{2}).\d{2}\]/g, '\[$1]'], // Remove fractions in timestamps
[/\\`(.*?)\\`/g, '`$1`'], // Remove escaping backslashes before backticks
]


// Perform multiple rounds of replaces on a string
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
}

// Read a textfile to a string, pass the string through processor, and write the results out to a file
async function processFile(filename, processor, newname = '') {
const inText = await fs.readFile(filename).then(buffer => buffer.toString())
const outText = processor(inText)
if (outText != inText) {
await fs.writeFile(newname || filename, outText)
}
}



async function main() {
const transcripts = await getTranscripts()
transcripts.forEach(filename => processFile(filename, applyReplaces))
}



if (require.main === module) {
main();
}

module.exports = {
getTranscripts, processFile, applyReplaces
}
38 changes: 38 additions & 0 deletions format.test.js
@@ -0,0 +1,38 @@
const fs = require('fs').promises
const os = require('os')
const path = require('path')

const {applyReplaces, getTranscripts, processFile}= require('./format')


test('applyReplaces works as expected', () => {
expect(applyReplaces('I love javascript')).toBe('I love JavaScript')
expect(applyReplaces('r/javascript')).toBe('r/javascript')

expect(applyReplaces('\[00:04:06.27\]')).toBe('\[04:06\]')
expect(applyReplaces('\[00:10:17.78\]')).toBe('\[10:17\]')
expect(applyReplaces('\[03:43.78\]')).toBe('\[03:43\]')

expect(applyReplaces("\\`console.log($TOKEN)\\`")).toBe('`console.log($TOKEN)`')
expect(applyReplaces("The difference between \\`foo\\` and \\`bar\\`")).toBe('The difference between `foo` and `bar`')

expect(applyReplaces("ML ops is ML ops, but OCAML ops is not")).toBe('MLOps is MLOps, but OCAML ops is not')
})


test('getTranscripts works as expected', async () => {
const transcripts = await getTranscripts()
// TODO: What are good ways to test this function?
})


test('processFile works as expected', async () => {
const location = await fs.mkdtemp(path.join(os.tmpdir(), 'format-'))
const filename = path.join(location, 'temp.txt')
const text = "The Quick Brown Fox Jumps Over the Lazy dog.\nI wander lonely as a cloud.\n"
await fs.writeFile(filename, text)
await processFile(filename, x => x.toLowerCase())
const outText = await fs.readFile(filename).then(x => x.toString())
expect(outText).toBe(text.toLowerCase())
await fs.rm(location, {recursive: true})
})

0 comments on commit 9b1d2b9

Please sign in to comment.