Add Prettier Formatting (#258)

* Add Prettier formatting to source files and example files, and check that Prettier formatting is maintained on PRs

* Remove trailing-comma as we are using Node 6 on Circle

* Use latest Node 6 LTS version in Circle

* Remove unused test
This commit is contained in:
Héctor Ramos 2017-12-04 19:21:02 -08:00 committed by Joel Marcey
parent 0cead4b6f9
commit 65421db62e
50 changed files with 1376 additions and 1350 deletions

View file

@ -6,32 +6,32 @@
*/
const CWD = process.cwd();
const glob = require("glob");
const fs = require("fs");
const path = require("path");
const assert = require("assert");
const glob = require('glob');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const siteConfig = require(CWD + "/siteConfig.js");
const siteConfig = require(CWD + '/siteConfig.js');
const ENABLE_TRANSLATION = fs.existsSync(CWD + "/languages.js");
const ENABLE_TRANSLATION = fs.existsSync(CWD + '/languages.js');
let versions;
if (fs.existsSync(CWD + "/versions.json")) {
versions = require(CWD + "/versions.json");
if (fs.existsSync(CWD + '/versions.json')) {
versions = require(CWD + '/versions.json');
} else {
versions = [];
}
let languages;
if (fs.existsSync(CWD + "/languages.js")) {
languages = require(CWD + "/languages.js");
if (fs.existsSync(CWD + '/languages.js')) {
languages = require(CWD + '/languages.js');
} else {
languages = [
{
enabled: true,
name: "English",
tag: "en"
}
name: 'English',
tag: 'en',
},
];
}
@ -40,16 +40,16 @@ if (fs.existsSync(CWD + "/languages.js")) {
// included to prevent cyclical dependency with readMetadata.js
function splitHeader(content) {
const lines = content.split("\n");
const lines = content.split('\n');
let i = 1;
for (; i < lines.length - 1; ++i) {
if (lines[i] === "---") {
if (lines[i] === '---') {
break;
}
}
return {
header: lines.slice(1, i + 1).join("\n"),
content: lines.slice(i + 1).join("\n")
header: lines.slice(1, i + 1).join('\n'),
content: lines.slice(i + 1).join('\n'),
};
}
@ -59,27 +59,27 @@ function extractMetadata(content) {
const both = splitHeader(content);
// if no content returned, then that means there was no header, and both.header is the content
if (!both.content) {
return { metadata, rawContent: both.header };
return {metadata, rawContent: both.header};
}
const lines = both.header.split("\n");
const lines = both.header.split('\n');
for (let i = 0; i < lines.length - 1; ++i) {
const keyvalue = lines[i].split(":");
const keyvalue = lines[i].split(':');
const key = keyvalue[0].trim();
let value = keyvalue
.slice(1)
.join(":")
.join(':')
.trim();
try {
value = JSON.parse(value);
} catch (e) {}
metadata[key] = value;
}
return { metadata, rawContent: both.content };
return {metadata, rawContent: both.content};
}
/*****************************************************************/
const versionFolder = CWD + "/versioned_docs/";
const versionFolder = CWD + '/versioned_docs/';
// available stores doc ids of documents that are available for
// each version
@ -87,39 +87,47 @@ const available = {};
// versionFiles is used to keep track of what file to use with a
// given version/id of a document
const versionFiles = {};
let files = glob.sync(versionFolder + "**");
let files = glob.sync(versionFolder + '**');
files.forEach(file => {
const ext = path.extname(file);
if (ext !== ".md" && ext !== ".markdown") {
if (ext !== '.md' && ext !== '.markdown') {
return;
}
const res = extractMetadata(fs.readFileSync(file, "utf8"));
const res = extractMetadata(fs.readFileSync(file, 'utf8'));
const metadata = res.metadata;
if (!metadata.original_id) {
console.error(
`No 'original_id' field found in ${file}. Perhaps you forgot to add it when importing prior versions of your docs?`
`No 'original_id' field found in ${
file
}. Perhaps you forgot to add it when importing prior versions of your docs?`
);
throw new Error(
`No 'original_id' field found in ${file}. Perhaps you forgot to add it when importing prior versions of your docs?`
`No 'original_id' field found in ${
file
}. Perhaps you forgot to add it when importing prior versions of your docs?`
);
}
if (!metadata.id) {
console.error(`No 'id' field found in ${file}.`);
throw new Error(`No 'id' field found in ${file}.`);
} else if (metadata.id.indexOf("version-") === -1) {
} else if (metadata.id.indexOf('version-') === -1) {
console.error(
`The 'id' field in ${file} is missing the expected 'version-XX-' prefix. Perhaps you forgot to add it when importing prior versions of your docs?`
`The 'id' field in ${
file
} is missing the expected 'version-XX-' prefix. Perhaps you forgot to add it when importing prior versions of your docs?`
);
throw new Error(
`The 'id' field in ${file} is missing the expected 'version-XX-' prefix. Perhaps you forgot to add it when importing prior versions of your docs?`
`The 'id' field in ${
file
} is missing the expected 'version-XX-' prefix. Perhaps you forgot to add it when importing prior versions of your docs?`
);
}
if (!(metadata.original_id in available)) {
available[metadata.original_id] = new Set();
}
const version = metadata.id.split("-")[1];
const version = metadata.id.split('-')[1];
available[metadata.original_id].add(version);
if (!(version in versionFiles)) {
@ -133,7 +141,9 @@ files.forEach(file => {
function docVersion(id, req_version) {
if (!available[id]) {
throw new Error(
`Document with id '${id}' was requested but no document with that id could be located.`
`Document with id '${
id
}' was requested but no document with that id could be located.`
);
}
// iterate through versions until a version less than or equal to the requested
@ -179,40 +189,40 @@ function diffLatestDoc(file, id) {
}
return (
extractMetadata(fs.readFileSync(latestFile, "utf8")).rawContent.trim() !==
extractMetadata(fs.readFileSync(file, "utf8")).rawContent.trim()
extractMetadata(fs.readFileSync(latestFile, 'utf8')).rawContent.trim() !==
extractMetadata(fs.readFileSync(file, 'utf8')).rawContent.trim()
);
}
// return metadata for a versioned file given the file, its version (requested),
// the version of the file to be used, and its language
function processVersionMetadata(file, version, useVersion, language) {
const metadata = extractMetadata(fs.readFileSync(file, "utf8")).metadata;
metadata.source = "version-" + useVersion + "/" + path.basename(file);
const metadata = extractMetadata(fs.readFileSync(file, 'utf8')).metadata;
metadata.source = 'version-' + useVersion + '/' + path.basename(file);
const latestVersion = versions[0];
if (!ENABLE_TRANSLATION && !siteConfig.useEnglishUrl) {
metadata.permalink =
"docs/" +
(version !== latestVersion ? version + "/" : "") +
'docs/' +
(version !== latestVersion ? version + '/' : '') +
metadata.original_id +
".html";
'.html';
} else {
metadata.permalink =
"docs/" +
'docs/' +
language +
"/" +
(version !== latestVersion ? version + "/" : "") +
'/' +
(version !== latestVersion ? version + '/' : '') +
metadata.original_id +
".html";
'.html';
}
metadata.id = metadata.id.replace(
"version-" + useVersion + "-",
"version-" + version + "-"
'version-' + useVersion + '-',
'version-' + version + '-'
);
metadata.localized_id = metadata.id;
metadata.id = language + "-" + metadata.id;
metadata.id = language + '-' + metadata.id;
metadata.language = language;
metadata.version = version;
@ -269,14 +279,16 @@ function sidebarVersion(req_version) {
}
if (
fs.existsSync(
CWD + "/versioned_sidebars/version-" + versions[i] + "-sidebars.json"
CWD + '/versioned_sidebars/version-' + versions[i] + '-sidebars.json'
)
) {
return versions[i];
}
}
throw new Error(
`No sidebar file available to use for version ${req_version}. Verify that 'version-${req_version}-sidebars.json' exists.`
`No sidebar file available to use for version ${
req_version
}. Verify that 'version-${req_version}-sidebars.json' exists.`
);
}
@ -290,11 +302,11 @@ function diffLatestSidebar() {
const version = sidebarVersion(latest);
const latestSidebar =
CWD + "/versioned_sidebars/version-" + version + "-sidebars.json";
CWD + '/versioned_sidebars/version-' + version + '-sidebars.json';
if (!fs.existsSync(latestSidebar)) {
return true;
}
const currentSidebar = CWD + "/sidebars.json";
const currentSidebar = CWD + '/sidebars.json';
// if no current sidebar file, return false so no sidebar file gets copied
if (!fs.existsSync(currentSidebar)) {
return false;
@ -303,10 +315,10 @@ function diffLatestSidebar() {
// compare for equality between latest version sidebar with version prefixes
// stripped and current sidebar
return (
JSON.stringify(JSON.parse(fs.readFileSync(latestSidebar, "utf8"))).replace(
new RegExp("version-" + version + "-", "g"),
""
) !== JSON.stringify(JSON.parse(fs.readFileSync(currentSidebar, "utf8")))
JSON.stringify(JSON.parse(fs.readFileSync(latestSidebar, 'utf8'))).replace(
new RegExp('version-' + version + '-', 'g'),
''
) !== JSON.stringify(JSON.parse(fs.readFileSync(currentSidebar, 'utf8')))
);
}
@ -319,12 +331,12 @@ function sidebarData() {
const sidebar = JSON.parse(
fs
.readFileSync(
CWD + "/versioned_sidebars/version-" + version + "-sidebars.json",
"utf8"
CWD + '/versioned_sidebars/version-' + version + '-sidebars.json',
'utf8'
)
.replace(
new RegExp("version-" + version + "-", "g"),
"version-" + versions[i] + "-"
new RegExp('version-' + version + '-', 'g'),
'version-' + versions[i] + '-'
)
);
Object.assign(allSidebars, sidebar);
@ -339,5 +351,5 @@ module.exports = {
docData,
sidebarVersion,
diffLatestSidebar,
sidebarData
sidebarData,
};