Example #1
0
files.forEach(file => {
  Promise.all([
    prettier.resolveConfig(file, {
      config: prettierConfigPath,
    }),
    prettier.getFileInfo(file),
  ])
    .then(resolves => {
      const [options, fileInfo] = resolves;
      if (fileInfo.ignored) {
        return;
      }
      const input = fs.readFileSync(file, 'utf8');
      const withParserOptions = {
        ...options,
        parser: fileInfo.inferredParser,
      };
      const output = prettier.format(input, withParserOptions);
      if (output !== input) {
        fs.writeFileSync(file, output, 'utf8');
        console.log(chalk.green(`${file} is prettier`));
      }
    })
    .catch(e => {
      didError = true;
    })
    .finally(() => {
      if (didError) {
        process.exit(1);
      }
      console.log(chalk.hex('#1890FF')('prettier success!'));
    });
});
 return __awaiter(this, void 0, void 0, function* () {
     try {
         const config = yield bundledPrettier.resolveConfig(filePath, options);
         return { config };
     }
     catch (error) {
         return { config: null, error };
     }
 });
Example #3
0
 .then(({ ignored, inferredParser }) => {
   if (ignored || !inferredParser) {
     ignoredCount += 1;
     return;
   }
   return prettier.resolveConfig(filePath).then(fileOptions => {
     const options = { ...fileOptions, parser: inferredParser };
     return checkFileWithOptions(filePath, options);
   });
 });
 const transform = (file, encoding, callback) => {
     /* resolve from the projects config */
     prettier.resolveConfig(file.relative).then((options) => {
         const str = file.contents.toString('utf8');
         if (!options || Object.keys(options).length === 0) {
             options = defaultOptions;
         }
         // for better errors
         options.filepath = file.relative;
         const data = prettier.format(str, options);
         file.contents = Buffer.from(data);
         callback(null, file);
     });
 };
Example #5
0
files.forEach(file => {
  try {
    prettier
      .resolveConfig(file)
      .then(options => {
        const fileExtension = file.split('.').pop();
        Object.assign(options, {
          parser: config.parsers[fileExtension],
        });

        const input = fs.readFileSync(file, 'utf8');

        if (shouldSave) {
          const output = prettier.format(input, options);
          if (output !== input) {
            fs.writeFileSync(file, output, 'utf8');
            console.log(`Prettified : ${file}`);
          }
        } else if (!prettier.check(input, options)) {
          if (!didWarn) {
            console.log(
              '\n===============================\nGitLab uses Prettier to format all JavaScript code.\nPlease format each file listed below or run "yarn prettier-staged-save"\n===============================\n'
            );
            didWarn = true;
          }
          console.log(`Prettify Manually : ${file}`);
        }
      })
      .catch(e => {
        console.log(`Error on loading the Config File: ${e.message}`);
        process.exit(1);
      });
  } catch (error) {
    didError = true;
    console.log(`\n\nError with ${file}: ${error.message}`);
  }
});
  .then(() => {
    const prettierConfigPath = path.join(__dirname, '../../prettier.config.js');
    const prettierConfig = prettier.resolveConfig(process.cwd(), { config: prettierConfigPath });

    return Promise.all([getUnstagedGitFiles(), prettierConfig]);
  })