2016-07-21 08:47:58 +08:00
#!/usr/bin/env node
'use strict' ;
const chalk = require ( 'chalk' ) ;
const Vorpal = require ( 'vorpal' ) ;
const GitHubAPI = require ( 'github-api' ) ;
2016-09-07 06:22:59 +08:00
const untildify = require ( 'untildify' ) ;
2016-07-21 08:47:58 +08:00
const fs = require ( 'fs' ) ;
const path = require ( 'path' ) ;
2016-09-07 06:22:59 +08:00
const os = require ( 'os' ) ;
2016-07-21 08:47:58 +08:00
const child _process = require ( 'child_process' ) ;
const execSync = child _process . execSync ;
const vorpal = new Vorpal ( ) ;
// Expects to be in a checkout of react that is a sibling of the react checkout you want to operate on
// eg ~/code/react@release-manager/scripts/release-manager & ~/code/react
// TODO: Make this an argument to the script
2016-09-07 06:22:59 +08:00
let PATH _TO _REPO = null ;
const PATH _TO _CONFIG = path . resolve ( os . homedir ( ) , '.react-release-manager.json' ) ;
const DEFAULT _CONFIG = {
githubToken : null ,
reactPath : path . resolve ( '../../../react' ) ,
} ;
2016-07-21 08:47:58 +08:00
2016-07-22 06:57:12 +08:00
// Quick dry run opt-in. This allows quick debugging of execInRepo without
// actually running the command, ensuring no accidental publishing.
const DRY _RUN = false ;
2016-07-21 08:47:58 +08:00
2016-07-23 04:38:27 +08:00
// Enabled commands
const COMMANDS = [
'init' ,
'docs-prs' ,
'q' ,
'stable-prs' ,
'version' ,
'npm-publish' ,
2016-07-26 06:45:41 +08:00
'npm-check-access' ,
'npm-grant-access' ,
2016-09-09 06:18:20 +08:00
'start-release' ,
2016-07-23 04:38:27 +08:00
] ;
2016-07-21 08:47:58 +08:00
// HELPERS
// Simple helper to write out some JSON for debugging
function writeTo ( file , data ) {
2016-09-28 02:40:38 +08:00
var folder = path . join ( _ _dirname , 'data' ) ;
if ( ! fs . existsSync ( folder ) ) {
fs . mkdirSync ( folder ) ;
}
2016-07-21 08:47:58 +08:00
fs . writeFile (
2016-09-28 02:40:38 +08:00
path . join ( folder , file ) ,
2016-07-21 08:47:58 +08:00
JSON . stringify ( data , null , 2 )
) ;
}
// Wrapper around exec so we don't have to worry about paths
function execInRepo ( command ) {
2016-07-22 06:57:12 +08:00
vorpal . log ( chalk . gray ( ` Executing ${ chalk . underline ( command ) } ` ) ) ;
if ( DRY _RUN ) {
return '' ;
}
2016-07-21 08:47:58 +08:00
return execSync ( command , {
cwd : PATH _TO _REPO ,
encoding : 'utf8' ,
} ) . trim ( ) ;
}
2016-07-22 04:40:53 +08:00
function getReactVersion ( ) {
return ( JSON . parse ( fs . readFileSync ( path . join ( PATH _TO _REPO , 'package.json' ) , 'utf8' ) ) ) . version ;
}
2016-07-21 08:47:58 +08:00
const app = {
vorpal ,
updateConfig ( ) {
// TODO: write this. This should make it possible to start without a config
// and go through the init process to create one and then re-init the github
// setup.
2016-09-07 06:22:59 +08:00
this . config = this . loadConfig ( ) ;
2016-07-21 08:47:58 +08:00
} ,
2016-09-07 06:22:59 +08:00
loadConfig ( ) {
2016-07-21 08:47:58 +08:00
try {
2016-09-07 06:22:59 +08:00
// TODO: validate config
let config = JSON . parse ( fs . readFileSync ( PATH _TO _CONFIG , 'utf8' ) ) ;
config . reactPath = path . normalize ( untildify ( config . reactPath ) ) ;
PATH _TO _REPO = config . reactPath ;
return config ;
2016-07-21 08:47:58 +08:00
} catch ( e ) {
2016-09-07 06:22:59 +08:00
console . error ( 'Attempt to load config file failed. Please run `init` command for initial setup or make sure ~/.react-release-manager.json is valid JSON. Using a default config which may not work properly.' ) ;
return DEFAULT _CONFIG ;
2016-07-21 08:47:58 +08:00
}
2016-09-07 06:22:59 +08:00
} ,
init ( ) {
this . config = this . loadConfig ( ) ;
2016-07-21 08:47:58 +08:00
2016-09-07 06:22:59 +08:00
this . PATH _TO _CONFIG = PATH _TO _CONFIG ;
2016-07-21 08:47:58 +08:00
// GITHUB
this . github = new GitHubAPI ( {
2016-09-07 06:22:59 +08:00
token : this . config . githubToken ,
2016-07-21 08:47:58 +08:00
} ) ;
this . ghrepo = this . github . getRepo ( 'facebook' , 'react' ) ;
this . ghissues = this . github . getIssues ( 'facebook' , 'react' ) ;
// HELPERS
this . writeTo = writeTo ;
this . execInRepo = execInRepo ;
2016-07-22 04:40:53 +08:00
this . getReactVersion = getReactVersion ;
2016-07-21 08:47:58 +08:00
// Register commands
2016-07-23 04:38:27 +08:00
COMMANDS . forEach ( ( command ) => {
2016-07-21 08:47:58 +08:00
vorpal . use ( require ( ` ./commands/ ${ command } ` ) ( vorpal , app ) ) ;
} ) ;
2016-09-28 02:44:06 +08:00
var v = vorpal
2016-07-21 08:47:58 +08:00
. history ( 'react-release-manager' )
2016-09-28 02:44:06 +08:00
. delimiter ( 'rrm \u2234' ) ;
v . exec ( 'help' ) ;
v . show ( ) ;
2016-07-23 04:38:27 +08:00
} ,
} ;
2016-07-21 08:47:58 +08:00
app . init ( ) ;