Browse Source

build: auto-fix whitespace in docs in pre-commit hook (#17490)

Samuel Attard 6 years ago
parent
commit
784f9742bc

+ 1 - 1
docs/api/structures/custom-scheme.md

@@ -7,4 +7,4 @@
   * `bypassCSP` Boolean (optional) - Default false.
   * `allowServiceWorkers` Boolean (optional) - Default false.
   * `supportFetchAPI` Boolean (optional) - Default false.
-  * `corsEnabled` Boolean (optional) - Default false.
+  * `corsEnabled` Boolean (optional) - Default false.

+ 1 - 1
docs/api/structures/ipc-main-event.md

@@ -5,4 +5,4 @@
 * `sender` WebContents - Returns the `webContents` that sent the message
 * `reply` Function - A function that will send an IPC message to the renderer frame that sent the original message that you are currently handling.  You should use this method to "reply" to the sent message in order to guaruntee the reply will go to the correct process and frame.
   * `...args` any[]
-IpcRenderer
+IpcRenderer

+ 1 - 1
docs/api/structures/product.md

@@ -7,4 +7,4 @@
 * `contentLengths` Number[] - The total size of the content, in bytes.
 * `price` Number - The cost of the product in the local currency.
 * `formattedPrice` String - The locale formatted price of the product.
-* `downloadable` Boolean - A Boolean value that indicates whether the App Store has downloadable content for this product.
+* `downloadable` Boolean - A Boolean value that indicates whether the App Store has downloadable content for this product.

+ 1 - 1
docs/api/structures/web-source.md

@@ -2,4 +2,4 @@
 
 * `code` String
 * `url` String (optional)
-* `startLine` Integer (optional) - Default is 1.
+* `startLine` Integer (optional) - Default is 1.

+ 1 - 1
docs/tutorial/in-app-purchases.md

@@ -118,4 +118,4 @@ inAppPurchase.getProducts(PRODUCT_IDS).then(products => {
     console.log('The payment has been added to the payment queue.')
   })
 })
-```
+```

+ 1 - 1
docs/tutorial/linux-desktop-actions.md

@@ -38,4 +38,4 @@ parameters. You can find these in your app in the global variable
 
 [unity-launcher]: https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles#Adding_shortcuts_to_a_launcher
 [audacious-launcher]: https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles?action=AttachFile&do=get&target=shortcuts.png
-[spec]: https://specifications.freedesktop.org/desktop-entry-spec/1.1/ar01s11.html
+[spec]: https://specifications.freedesktop.org/desktop-entry-spec/1.1/ar01s11.html

+ 1 - 1
docs/tutorial/updates.md

@@ -149,4 +149,4 @@ autoUpdater.on('error', message => {
 [electron-release-server]: https://github.com/ArekSredzki/electron-release-server
 [nucleus]: https://github.com/atlassian/nucleus
 [update.electronjs.org]: https://github.com/electron/update.electronjs.org
-[update-electron-app]: https://github.com/electron/update-electron-app
+[update-electron-app]: https://github.com/electron/update-electron-app

+ 2 - 1
package.json

@@ -116,8 +116,9 @@
       "node script/lint.js --py --fix --only --",
       "git add"
     ],
-    "docs/api/*.md": [
+    "docs/api/**/*.md": [
       "node script/gen-filenames.js",
+      "python script/check-trailing-whitespace.py --fix",
       "git add filenames.auto.gni"
     ]
   }

+ 20 - 3
script/check-trailing-whitespace.py

@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import argparse
 import os
 import sys
 
@@ -9,6 +10,8 @@ DOCS_DIR = os.path.join(SOURCE_ROOT, 'docs')
 def main():
   os.chdir(SOURCE_ROOT)
 
+  args = parse_args()
+
   filepaths = []
   totalDirs = 0
   try:
@@ -23,7 +26,7 @@ def main():
 
   trailingWhiteSpaceFiles = 0
   for path in filepaths:
-    trailingWhiteSpaceFiles += hasTrailingWhiteSpace(path)
+    trailingWhiteSpaceFiles += hasTrailingWhiteSpace(path, args.fix)
 
   print('Parsed through ' + str(len(filepaths)) +
         ' files within docs directory and its ' +
@@ -32,7 +35,7 @@ def main():
         ' files with trailing whitespace.')
   return trailingWhiteSpaceFiles
 
-def hasTrailingWhiteSpace(filepath):
+def hasTrailingWhiteSpace(filepath, fix):
   try:
     f = open(filepath, 'r')
     lines = f.read().splitlines()
@@ -41,12 +44,26 @@ def hasTrailingWhiteSpace(filepath):
   finally:
     f.close()
 
+  fixed_lines = []
   for line in lines:
-    if line != line.rstrip():
+    fixed_lines.append(line.rstrip() + '\n')
+    if not fix and line != line.rstrip():
       print "Trailing whitespace in: " + filepath
       return True
+  if fix:
+    with open(filepath, 'w') as f:
+      print(fixed_lines)
+      f.writelines(fixed_lines)
 
   return False
 
+def parse_args():
+  parser = argparse.ArgumentParser(
+                      description='Check for trailing whitespace in md files')
+  parser.add_argument('-f', '--fix',
+                      help='Automatically fix trailing whitespace issues',
+                      action='store_true')
+  return parser.parse_known_args()[0]
+
 if __name__ == '__main__':
   sys.exit(main())