Bladeren bron

preemptively prettify code generated from templates

telamonian 5 jaren geleden
bovenliggende
commit
e03bf24869
6 gewijzigde bestanden met toevoegingen van 33 en 16 verwijderingen
  1. 2 0
      buildutils/package.json
  2. 21 9
      buildutils/src/ensure-package.ts
  3. 2 5
      buildutils/src/utils.ts
  4. 1 1
      package.json
  5. 1 0
      tests/test-notebook/package.json
  6. 6 1
      yarn.lock

+ 2 - 0
buildutils/package.json

@@ -49,6 +49,7 @@
     "mini-css-extract-plugin": "~0.6.0",
     "package-json": "^6.3.0",
     "path": "~0.12.7",
+    "prettier": "^1.18.2",
     "semver": "^6.1.0",
     "sort-package-json": "~1.22.1",
     "typescript": "~3.5.1",
@@ -60,6 +61,7 @@
     "@types/inquirer": "^6.0.3",
     "@types/mini-css-extract-plugin": "^0.2.0",
     "@types/node": "^12.0.2",
+    "@types/prettier": "^1.16.4",
     "@types/webpack": "^4.4.32",
     "rimraf": "~2.6.2"
   }

+ 21 - 9
buildutils/src/ensure-package.ts

@@ -6,6 +6,7 @@
 import * as fs from 'fs-extra';
 import * as glob from 'glob';
 import * as path from 'path';
+import * as prettier from 'prettier';
 import * as ts from 'typescript';
 import { getDependency } from './get-dependency';
 import * as utils from './utils';
@@ -181,7 +182,7 @@ export async function ensurePackage(
 
     // write out cssIndexContents, if needed
     const cssIndexPath = path.join(pkgPath, 'style/index.css');
-    messages.push(...ensureFile(cssIndexPath, cssIndexContents));
+    messages.push(...ensureFile(cssIndexPath, cssIndexContents, false));
   }
 
   // Look for unused packages
@@ -377,11 +378,9 @@ export async function ensureUiComponents(pkgPath: string): Promise<string[]> {
     _iconCSSUrls.push(
       `--${urlName}: url('${path.relative(iconCSSDir, svg)}');`
     );
-
-    // be sure to match the delinted syntax style
-    _iconCSSDeclarations.push(`.${className} {`);
-    _iconCSSDeclarations.push(`  background-image: var(--${urlName});`);
-    _iconCSSDeclarations.push(`}`);
+    _iconCSSDeclarations.push(
+      `.${className} {background-image: var(--${urlName})}`
+    );
   });
   const iconCSSUrls = _iconCSSUrls.join('\n');
   const iconCSSDeclarations = _iconCSSDeclarations.join('\n');
@@ -452,14 +451,22 @@ export interface IEnsurePackageOptions {
  * do nothing and return an empty array. If they don't match, overwrite the
  * file and return an array with an update message.
  *
- * @param path - The path to the file being checked. The file must exist,
+ * @param path: The path to the file being checked. The file must exist,
  * or else this function does nothing.
  *
- * @param contents - The desired file contents.
+ * @param contents: The desired file contents.
+ *
+ * @param prettify: default = true. If true, format the contents with
+ * `prettier` before comparing/writing. Set to false only if you already
+ * know your code won't be modified later by the `prettier` git commit hook.
  *
  * @returns a string array with 0 or 1 messages.
  */
-function ensureFile(path: string, contents: string): string[] {
+function ensureFile(
+  path: string,
+  contents: string,
+  prettify: boolean = true
+): string[] {
   let messages: string[] = [];
 
   if (!fs.existsSync(path)) {
@@ -470,6 +477,11 @@ function ensureFile(path: string, contents: string): string[] {
     return messages;
   }
 
+  // run the newly generated contents through prettier before comparing
+  if (prettify) {
+    contents = prettier.format(contents, { filepath: path, singleQuote: true });
+  }
+
   const prev = fs.readFileSync(path, {
     encoding: 'utf8'
   });

+ 2 - 5
buildutils/src/utils.ts

@@ -121,15 +121,12 @@ export function writeJSONFile(filePath: string, data: any): boolean {
  * Ex: `This header generated by {{funcName}}`
  *
  * @param subs: an object in which the parameter keys are the template
- * variables and the parameter values are the substitutions. Each value
- * can be either a single string or an array of strings. Arrays of strings
- * will be automatically joined.
+ * variables and the parameter values are the substitutions.
  *
  * @param options: function options.
  *
  * @param options.autoindent: default = true. If true, will try to match
- * indentation level of {{var}} in substituted template, if the substitution
- * value is supplied as an array of strings.
+ * indentation level of {{var}} in substituted template.
  *
  * @param options.end: default = '\n'. Inserted at the end of
  * a template post-substitution and post-trim.

+ 1 - 1
package.json

@@ -100,7 +100,7 @@
     "lerna": "^3.13.2",
     "lint-staged": "^8.1.5",
     "open-cli": "^5.0.0",
-    "prettier": "^1.17.0",
+    "prettier": "^1.18.2",
     "tslint": "^5.15.0",
     "tslint-config-prettier": "^1.18.0",
     "tslint-plugin-prettier": "^2.0.1",

+ 1 - 0
tests/test-notebook/package.json

@@ -12,6 +12,7 @@
     "test:debug": "python run-test.py  --browsers=Chrome --singleRun=false --debug=true --browserNoActivityTimeout=10000000 --browserDisconnectTimeout=10000000 karma.conf.js",
     "test:debug:chrome-headless": "python run-test.py  --browsers=ChromeHeadless --singleRun=false --debug=true --browserNoActivityTimeout=10000000 --browserDisconnectTimeout=10000000 karma.conf.js",
     "test:firefox": "python run-test.py --browsers=Firefox karma.conf.js",
+    "test:watch": "python run-test.py  --browsers=Chrome --singleRun=false --debug=true --watch --browserNoActivityTimeout=10000000 karma.conf.js",
     "test:ie": "python run-test.py  --browsers=IE karma.conf.js",
     "test:watch": "python run-test.py  --browsers=Chrome --singleRun=false --debug=true --watch --browserNoActivityTimeout=10000000 --browserDisconnectTimeout=10000000 karma.conf.js",
     "watch": "tsc -b --watch",

+ 6 - 1
yarn.lock

@@ -2018,6 +2018,11 @@
   resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
   integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==
 
+"@types/prettier@~1.16.4":
+  version "1.16.4"
+  resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-1.16.4.tgz#5e5e97702cb68498aaba7349b941648daaf2385c"
+  integrity sha512-MG7ExKBo7AQ5UrL1awyYLNinNM/kyXgE4iP4Ul9fB+T7n768Z5Xem8IZeP6Bna0xze8gkDly49Rgge2HOEw4xA==
+
 "@types/prop-types@*":
   version "15.7.1"
   resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.1.tgz#f1a11e7babb0c3cad68100be381d1e064c68f1f6"
@@ -9723,7 +9728,7 @@ prettier-linter-helpers@^1.0.0:
   dependencies:
     fast-diff "^1.1.2"
 
-prettier@^1.17.0:
+prettier@^1.17.0, prettier@~1.18.2:
   version "1.18.2"
   resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
   integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==