/**
 * Invoke 'fractive <command> <args>' to execute command-line tools
 */ require ( "source-map-support" ) . install ( ) ; import * as cp from "child_process" ; import * as fs from "fs" ; import * as path from "path" ; import * as clc from "cli-color" ; import * as commandLineArgs from "command-line-args" ; import * as commandLineUsage from "command-line-usage" ; import { Compiler , CompilerOptions , ProjectDefaults } from "./Compiler" ; var isWindows = /^win/ . test ( process . platform ) ; var isMac = /^darwin/ . test ( process . platform ) ; param args function Compile ( args ) { let optionDefinitions = [ { name : 'buildPath' , type : String , defaultOption : true , defaultValue : '.' } , { name : 'help' , type : Boolean } , { name : 'dry-run' , type : Boolean } , { name : 'verbose' , alias : 'v' , type : Boolean } , { name : 'debug' , type : Boolean } ] ; let options = null ; try { options = commandLineArgs ( optionDefinitions , { argv : args } ) ; } catch ( err ) { CompileUsage ( ) ; } if ( options . help ) { CompileUsage ( ) ; } else { Compiler . Compile ( options ) ; } } function CompileUsage ( ) { let sections = [ { header : 'Compiling Fractive Stories' , content : '$ fractive compile <options>' } , { header : 'Options' , optionList : [ { name : 'buildPath (optional)' , description : 'Either the root directory of a Fractive project, or the path to a valid Fractive project file.' } , { name : 'dry-run' , description : "Log what would have been done, but do not actually change any files." } , { name : 'verbose' , description : 'Log more detailed build information.' } , { name : 'debug' , description : 'Log debugging information during the build.' } , ] } ] ; let usage = commandLineUsage ( sections ) ; console . log ( usage ) ; } param args function Create ( args ) { let optionDefinitions = [ { name : "storyDirectory" , type : String , defaultOption : true } ] ; let options = commandLineArgs ( optionDefinitions , { argv : args } ) ; let projectDir = options . storyDirectory ; if ( fs . existsSync ( projectDir ) ) { let files = fs . readdirSync ( projectDir , "utf8" ) ; if ( files . length > 0 ) { console . error ( clc . red ( `Target directory "${ projectDir }" already exists and is not empty` ) ) ; process . exit ( 1 ) ; } } else { fs . mkdirSync ( projectDir ) ; } let projectFilePath = path . resolve ( projectDir , "fractive.json" ) ; fs . writeFileSync ( projectFilePath , JSON . stringify ( ProjectDefaults , null , 4 ) , "utf8" ) ; let sourceDir = path . resolve ( projectDir , "source" ) ; fs . mkdirSync ( sourceDir ) ; fs . writeFileSync ( path . resolve ( sourceDir , "text.md" ) , "{{Start}}\n\nYour story begins here." , "utf8" ) ; fs . writeFileSync ( path . resolve ( sourceDir , "script.js" ) , "// Your Javascript goes here" , "utf8" ) ; fs . mkdirSync ( path . resolve ( projectDir , "assets" ) ) ; fs . copyFileSync ( path . resolve ( __dirname , "../templates/basic.html" ) , path . resolve ( projectDir , "template.html" ) ) ; console . log ( clc . green ( `Project created at ${ projectDir }` ) ) ; } function Docs ( ) { let docPath = path . join ( __dirname , "../doc/build/index.html" ) ; if ( isWindows ) { cp . execSync ( `start "" "${ docPath }"` ) ; } else if ( isMac ) { cp . execSync ( `open ${ docPath }` ) ; } else { console . log ( `To view the Fractive user guide, open ${ docPath } in your web browser.` ) ; } return ; } function Examples ( ) { let examplesPath = path . join ( __dirname , "../examples" ) ; if ( isWindows ) { cp . execSync ( `start "" "${ examplesPath }"` ) ; } else if ( isMac ) { cp . execSync ( `open ${ examplesPath }` ) ; } else { console . log ( `To view the Fractive examples guide, navigate to ${ examplesPath } in your file browser.` ) ; } return ; } function ShowUsage ( ) { let sections = [ { header : 'Fractive' , content : 'Fractive is a hypertext authoring tool, primarily intended for the creation of interactive fiction.' } , { header : 'Commands' , content : [ { name : 'help' , summary : 'Show this command-line usage guide' } , { name : 'docs' , summary : 'Launch the Fractive user guide (Web browser required)' } , { name : 'examples' , summary : 'Browse Fractive example projects (Web browser required)' } , { name : 'create' , summary : 'Create a new Fractive project' } , { name : 'compile [--help]' , summary : 'Compile an existing Fractive project' } ] } ] ; let usage = commandLineUsage ( sections ) ; console . log ( usage ) ; } let commands = { 'create' : Create , 'compile' : Compile , 'docs' : Docs , 'help' : ShowUsage , 'examples' : Examples } ; if ( process . argv . length < 3 ) { ShowUsage ( ) ; process . exit ( 1 ) ; } else { if ( process . argv [ 2 ] in commands ) { commands [ process . argv [ 2 ] ] ( process . argv . slice ( 3 ) ) ; } else { let message = `'${ process . argv [ 2 ] }' is not a valid fractive command.` ; console . log ( `\n${ clc . red ( message ) }` ) ; ShowUsage ( ) ; process . exit ( 1 ) ; } }