Skip to content

Commit c071bc1

Browse files
authored
refactor(docs): internal command signatures (#615)
* refactor: slight readDoc signature change * chore: remove unnecessary comment * chore: rename variable * refactor: return content and data, rather than matter object
1 parent fb5047c commit c071bc1

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

src/lib/pushDoc.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import crypto from 'crypto';
2-
31
import chalk from 'chalk';
42
import config from 'config';
53
import { Headers } from 'node-fetch';
@@ -29,28 +27,27 @@ export default async function pushDoc(
2927
filepath: string,
3028
type: CommandCategories
3129
) {
32-
const { content, matter, slug } = readDoc(filepath);
33-
const hash = crypto.createHash('sha1').update(content).digest('hex');
30+
const { content, data, hash, slug } = readDoc(filepath);
3431

35-
let data: {
32+
let payload: {
3633
body?: string;
3734
html?: string;
3835
htmlmode?: boolean;
3936
lastUpdatedHash: string;
40-
} = { body: matter.content, ...matter.data, lastUpdatedHash: hash };
37+
} = { body: content, ...data, lastUpdatedHash: hash };
4138

4239
if (type === CommandCategories.CUSTOM_PAGES) {
4340
if (filepath.endsWith('.html')) {
44-
data = { html: matter.content, htmlmode: true, ...matter.data, lastUpdatedHash: hash };
41+
payload = { html: content, htmlmode: true, ...data, lastUpdatedHash: hash };
4542
} else {
46-
data = { body: matter.content, htmlmode: false, ...matter.data, lastUpdatedHash: hash };
43+
payload = { body: content, htmlmode: false, ...data, lastUpdatedHash: hash };
4744
}
4845
}
4946

5047
function createDoc() {
5148
if (dryRun) {
5249
return `🎭 dry run! This will create '${slug}' with contents from ${filepath} with the following metadata: ${JSON.stringify(
53-
matter.data
50+
data
5451
)}`;
5552
}
5653

@@ -65,14 +62,14 @@ export default async function pushDoc(
6562
),
6663
body: JSON.stringify({
6764
slug,
68-
...data,
65+
...payload,
6966
}),
7067
})
7168
.then(res => handleRes(res))
7269
.then(res => `🌱 successfully created '${res.slug}' (ID: ${res.id}) with contents from ${filepath}`);
7370
}
7471

75-
function updateDoc(existingDoc: typeof data) {
72+
function updateDoc(existingDoc: typeof payload) {
7673
if (hash === existingDoc.lastUpdatedHash) {
7774
return `${dryRun ? '🎭 dry run! ' : ''}\`${slug}\` ${
7875
dryRun ? 'will not be' : 'was not'
@@ -81,7 +78,7 @@ export default async function pushDoc(
8178

8279
if (dryRun) {
8380
return `🎭 dry run! This will update '${slug}' with contents from ${filepath} with the following metadata: ${JSON.stringify(
84-
matter.data
81+
data
8582
)}`;
8683
}
8784

@@ -96,7 +93,7 @@ export default async function pushDoc(
9693
),
9794
body: JSON.stringify(
9895
Object.assign(existingDoc, {
99-
...data,
96+
...payload,
10097
})
10198
),
10299
})

src/lib/readDoc.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import type matter from 'gray-matter';
2-
1+
import crypto from 'crypto';
32
import fs from 'fs';
43
import path from 'path';
54

65
import grayMatter from 'gray-matter';
76

87
import { debug } from './logger';
98

10-
type DocMetadata = {
9+
type ReadDocMetadata = {
10+
/** The contents of the file below the YAML front matter */
1111
content: string;
12-
matter: matter.GrayMatterFile<string>;
12+
/** A JSON object with the YAML front matter */
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14+
data: Record<string, any>;
15+
/**
16+
* A hash of the file contents (including the front matter)
17+
*/
18+
hash: string;
19+
/** The page slug */
1320
slug: string;
1421
};
1522

@@ -18,15 +25,17 @@ type DocMetadata = {
1825
*
1926
* @param {String} filepath path to the HTML/Markdown file
2027
* (file extension must end in `.html`, `.md`., or `.markdown`)
21-
* @returns {DocMetadata} an object containing the file's content, matter, and slug
2228
*/
23-
export default function readDoc(filepath: string): DocMetadata {
29+
export default function readDoc(filepath: string): ReadDocMetadata {
2430
debug(`reading file ${filepath}`);
25-
const content = fs.readFileSync(filepath, 'utf8');
26-
const matter = grayMatter(content);
27-
debug(`frontmatter for ${filepath}: ${JSON.stringify(matter)}`);
31+
const rawFileContents = fs.readFileSync(filepath, 'utf8');
32+
const matter = grayMatter(rawFileContents);
33+
const { content, data } = matter;
34+
debug(`front matter for ${filepath}: ${JSON.stringify(matter)}`);
2835

2936
// Stripping the subdirectories and markdown extension from the filename and lowercasing to get the default slug.
3037
const slug = matter.data.slug || path.basename(filepath).replace(path.extname(filepath), '').toLowerCase();
31-
return { content, matter, slug };
38+
39+
const hash = crypto.createHash('sha1').update(rawFileContents).digest('hex');
40+
return { content, data, hash, slug };
3241
}

0 commit comments

Comments
 (0)