BVB Source Codes

prettier Show sync-flow-tests.js Source code

Return Download prettier: download sync-flow-tests.js Source code - Download prettier Source code - Type:.js
  1. "use strict";
  2.  
  3. const fs = require("fs");
  4. const flowParser = require("flow-parser");
  5. const glob = require("glob");
  6. const mkdirp = require("mkdirp");
  7. const path = require("path");
  8. const rimraf = require("rimraf");
  9.  
  10. const DEFAULT_SPEC_CONTENT = "run_spec(__dirname);\n";
  11. const SPEC_FILE_NAME = "jsfmt.spec.js";
  12. const FLOW_TESTS_DIR = path.join(__dirname, "..", "tests", "flow");
  13.  
  14. function tryParse(file, content) {
  15.   const ast = flowParser.parse(content, {
  16.     esproposal_class_instance_fields: true,
  17.     esproposal_class_static_fields: true,
  18.     esproposal_export_star_as: true
  19.   });
  20.  
  21.   if (ast.errors.length > 0) {
  22.     const line = ast.errors[0].loc.start.line;
  23.     const column = ast.errors[0].loc.start.column;
  24.     const message = ast.errors[0].message;
  25.     return `${file}:${line}:${column}: ${message}`;
  26.   }
  27.  
  28.   return null;
  29. }
  30.  
  31. function syncTests(syncDir) {
  32.   const specFiles = glob.sync(path.join(FLOW_TESTS_DIR, "**", SPEC_FILE_NAME));
  33.   const filesToCopy = glob.sync(path.join(syncDir, "**/*.js"));
  34.  
  35.   if (filesToCopy.length === 0) {
  36.     throw new Error(
  37.       [
  38.         "Couldn't find any files to copy.",
  39.         `Please make sure that \`${syncDir}\` exists and contains the flow tests.`
  40.       ].join("\n")
  41.     );
  42.   }
  43.  
  44.   const specContents = specFiles.reduce((obj, specFile) => {
  45.     obj[specFile] = fs.readFileSync(specFile, "utf8");
  46.     return obj;
  47.   }, {});
  48.  
  49.   const skipped = [];
  50.  
  51.   rimraf.sync(FLOW_TESTS_DIR);
  52.  
  53.   filesToCopy.forEach(file => {
  54.     const content = fs.readFileSync(file, "utf8");
  55.     const parseError = tryParse(file, content);
  56.  
  57.     if (parseError) {
  58.       skipped.push(parseError);
  59.       return;
  60.     }
  61.  
  62.     const newFile = path.join(FLOW_TESTS_DIR, path.relative(syncDir, file));
  63.     const dirname = path.dirname(newFile);
  64.     const specFile = path.join(dirname, SPEC_FILE_NAME);
  65.     const specContent = specContents[specFile] || DEFAULT_SPEC_CONTENT;
  66.  
  67.     mkdirp.sync(dirname);
  68.     fs.writeFileSync(newFile, content);
  69.     fs.writeFileSync(specFile, specContent);
  70.   });
  71.  
  72.   return skipped;
  73. }
  74.  
  75. function run(argv) {
  76.   if (argv.length !== 1) {
  77.     console.error(
  78.       [
  79.         "You must provide the path to a flow tests directory to sync from!",
  80.         "Example: node scripts/sync-flow-tests.js ../flow/tests/"
  81.       ].join("\n")
  82.     );
  83.     return 1;
  84.   }
  85.  
  86.   const syncDir = argv[0];
  87.   let skipped = [];
  88.  
  89.   try {
  90.     skipped = syncTests(syncDir);
  91.   } catch (error) {
  92.     console.error(`Failed to sync.\n${error}`);
  93.     return 1;
  94.   }
  95.  
  96.   if (skipped.length > 0) {
  97.     console.log(
  98.       [
  99.         "Some files were skipped due to syntax errors.",
  100.         "This is expected since flow tests for handling invalid code,",
  101.         "but that's not interesting for Prettier's tests.",
  102.         "This is the skipped stuff:",
  103.         ""
  104.       ]
  105.         .concat(skipped, "")
  106.         .join("\n")
  107.     );
  108.   }
  109.  
  110.   console.log(
  111.     [
  112.       "Done syncing! Now you need to:",
  113.       "",
  114.       `1. Optional: Adjust some ${SPEC_FILE_NAME} files.`,
  115.       "2. Run `jest -u` to create snapshots.",
  116.       "3. Run `git diff` to check how tests and snapshots have changed",
  117.       "4. Take a look at new snapshots to see if they're OK."
  118.     ].join("\n")
  119.   );
  120.  
  121.   return 0;
  122. }
  123.  
  124. if (require.main === module) {
  125.   const exitCode = run(process.argv.slice(2));
  126.   process.exit(exitCode);
  127. }
  128.  
downloadsync-flow-tests.js Source code - Download prettier Source code
Related Source Codes/Software:
storybook - 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
parse-server - Parse-compatible API server module for Node/Expres... 2017-06-07
inferno - An extremely fast, React-like JavaScript library f... 2017-06-08
guetzli - Perceptual JPEG encoder 2017-06-08
cs-video-courses - List of Computer Science courses with video lectur... 2017-06-08
interviews - Everything you need to know to get the job. 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top