prepack/website/scripts/prettier/index.js
James Nolan 3c9c6ac2a5 Make master/docs a single source of truth for gh-pages
Summary:
Release note: none

To address issue #956 .

The idea is to have all documentation (aka "the website") in a single `docs/` directory on the `master` branch like [Jest](https://github.com/facebook/jest/tree/master/docs) and [Relay](https://github.com/facebook/relay/tree/master/docs) do. This directory then serves as a single source of truth to publish the `gh-pages` branch.

This PR:
- Moves the current content of the `gh-pages` branch to a `docs/` directory in `master`
- Provides a bash script for maintainers to easily publish the `gh-pages` branch from the `docs/` directory. The script also builds `prepack.min.js` from master to make sure gh-pages always has the latest version of prepack.

Additionally, I noticed the `gh-pages` branch had two `prepack.min.js` files (one in `./` and one in `./js/`). I removed the first one because it is apparently not used but it may break other websites if they references that file.

0. Fork the repo to avoid modifying the real website

1. Change the site
`> git checkout master`
`> echo ".nav { background-color: chartreuse; }" >> docs/css/style.css`
`> git add docs/css/style.css`
`> commit -m "The navigation bar looks better in green"`
`> git push`

2. Run the publication script:
`> ./scripts/publish-gh-pages.sh`

3. Verify that the gh-pages has been updated:
`> git checkout gh-pages`
`> git pull`
`> git diff HEAD~ # should show the line you added`
Closes https://github.com/facebook/prepack/pull/1223

Differential Revision: D6547395

Pulled By: j-nolan

fbshipit-source-id: 6e6d3aec97c0bcc2555c421d6f4a889bcd8df208
2017-12-12 12:10:05 -08:00

103 lines
2.7 KiB
JavaScript

/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
// Based on similar script in Jest
// https://github.com/facebook/jest/blob/master/scripts/prettier.js
const chalk = require('chalk');
const glob = require('glob');
const path = require('path');
const execFileSync = require('child_process').execFileSync;
const mode = process.argv[2] || 'check';
const shouldWrite = mode === 'write' || mode === 'write-changed';
const onlyChanged = mode === 'check-changed' || mode === 'write-changed';
const isWindows = process.platform === 'win32';
const prettier = isWindows ? 'prettier.cmd' : 'prettier';
const prettierCmd = path.resolve(
__dirname,
'../../node_modules/.bin/' + prettier
);
const defaultOptions = {
'bracket-spacing': 'true',
'single-quote': 'true',
'jsx-bracket-same-line': 'true',
'trailing-comma': 'all',
'print-width': 120,
};
const config = {
default: {
patterns: ['js/*.js'],
ignore: ['js/*.min.js', 'js/prism.js'],
},
scripts: {
patterns: ['scripts/**/*.js'],
},
};
function exec(command, args) {
console.log('> ' + [command].concat(args).join(' '));
const options = {};
return execFileSync(command, args, options).toString();
}
const mergeBase = exec('git', ['merge-base', 'HEAD', 'gh-pages']).trim();
const changedFiles = new Set(
exec('git', [
'diff',
'-z',
'--name-only',
'--diff-filter=ACMRTUB',
mergeBase,
]).match(/[^\0]+/g)
);
Object.keys(config).forEach(key => {
const patterns = config[key].patterns;
const options = config[key].options;
const ignore = config[key].ignore;
const globPattern = patterns.length > 1
? `{${patterns.join(',')}}`
: `${patterns.join(',')}`;
const files = glob
.sync(globPattern, {ignore})
.filter(f => !onlyChanged || changedFiles.has(f));
if (!files.length) {
return;
}
const args = Object.keys(defaultOptions).map(
k => `--${k}=${(options && options[k]) || defaultOptions[k]}`
);
args.push(`--${shouldWrite ? 'write' : 'l'}`);
try {
exec(prettierCmd, [...args, ...files]);
} catch (e) {
if (!shouldWrite) {
console.log(
'\n' +
chalk.red(
` This project uses prettier to format all JavaScript code.\n`
) +
chalk.dim(` Please run `) +
chalk.reset('yarn prettier') +
chalk.dim(` and add changes to files listed above to your commit.`) +
`\n`
);
process.exit(1);
}
throw e;
}
});