#!/usr/bin/env node const cmdline = require('commander'); const fs = require('fs-extra'); const path = require('path'); const prettier = require('../src'); const { version } = require('../package.json'); const defaults = require('../src/defaultOptions'); const glob = require('glob'); cmdline .version(version) .arguments('[PATH...]') .option('--write', 'Edit the files in-place. (Beware!)') .option('--tab-width ', `Specify the number of spaces per indentation-level. Defaults to ${defaults.tabWidth}.`, (value) => parseInt(value, 10), defaults.tabWidth) .option('--use-tabs', 'Indent lines with tabs instead of spaces.') .option('--expand-users', 'Expand author and contributors into objects.') .option('--key-order ', 'Sort order for keys in package.json', (val) => val.split(','), defaults.keyOrder) .option('-l, --list-different', 'Print filenames of files that are different from prettier-package-json formatting.') .parse(process.argv); const globs = cmdline.args.length > 0 ? cmdline.args : [path.join(process.cwd(), 'package.json')]; const files = Promise.all(globs.map(globToPaths)).then((matches) => { return matches.reduce((state, match) => [...state, ...match], []); }); if (cmdline.listDifferent) { files.then((paths) => { return Promise.all(paths.map((filepath) => check(filepath, cmdline))); }).then((details) => { details.forEach(({ filepath, isDifferent, error }) => { if (error) { console.error(error); } else if (isDifferent) { console.log(filepath); } }); }); } else { files.then((paths) => { return Promise.all(paths.map((filepath) => format(filepath, cmdline))); }).then((details) => { details.forEach(({ filepath, json, error }) => { if (error) { console.error(error); } else { if (cmdline.write) { return fs.writeFile(filepath, json); } else { return console.log(json); } } }); }) } function globToPaths(arg) { return new Promise((resolve, reject) => { glob(arg, (err, files) => err ? reject(err) : resolve(files)); }); } function format(filepath, options) { return fs.pathExists(filepath).then((exists) => { if (exists) { return fs.readJson(filepath); } else { return Promise.reject(`${filepath} doesn't exist`); } }).then((json) => { return prettier.format(json, options); }).then((json) => { return { filepath, json }; }).catch((error) => { return { filepath, error }; }); } function check(filepath, options) { return fs.pathExists(filepath).then((exists) => { if (exists) { return fs.readJson(filepath); } else { return Promise.reject(`${filepath} doesn't exist`); } }).then((json) => { return prettier.check(json, options); }).then((isSame) => { return { filepath, isDifferent: !isSame }; }).catch((error) => { return { filepath, error }; }); }