@@ -3,128 +3,114 @@ import * as fs from "fs"
33import * as path from "path"
44import { stdout } from "process"
55
6- import { staticbuild } from "."
6+ import type { StaticBuildOptions } from "./staticbuild "
77
8- interface Args {
9- watch ? : boolean
10- dryRun ? : boolean
11- port ? : string
12- check ? : boolean
13- }
8+ import { staticbuild } from "."
9+ import { isKeyValuePair , isStringLiteral , parseArgv } from "./args"
1410
1511const ERROR_CODE = {
1612 SUCCESS : 0 ,
1713 CALLED_WITH_ILLEGAL_PARAMETERS : 1 ,
1814 FAILED_T0_READ_LOCAL_FILE : 11 ,
1915}
2016
21- const DEFAULT_ARGS : Required < Args > = {
22- watch : false ,
23- dryRun : false ,
24- port : "8082" ,
25- check : false ,
26- }
27-
2817function logUsage ( ) {
29- stdout . write ( `Usage: staticbuild <inputDirectory> <outputDirectory> [--watch, --dry-run, --port, --check]\n` )
18+ stdout . write ( `🙅🏻♀️ staticbuild - a static site generator that isn't for you!\n\n` )
19+ stdout . write ( `Usage: staticbuild <inputDirectory> <outputDirectory> [--watch, --dry-run, --check]\n` )
3020
3121 stdout . write ( `\nArguments:\n` )
3222 stdout . write ( `<inputDirectory> Path to directory containing content.\n` )
3323 stdout . write ( `<outputDirectory> Path to directory for built site assets.\n` )
3424 stdout . write ( `--watch, -w Watch the input directory and rebuild if there are changes.\n` )
3525 stdout . write ( `--dry-run Prevent writing files to disk, instead logging the file list out.\n` )
36- stdout . write ( `--port, -p The expected port the site will be served from.\n` )
3726 stdout . write ( `--check, -c Perform a dead link check on the output files.\n` )
3827}
3928
4029async function main ( ) {
41- // Remove the first 2 arguments that nodejs provides.
42- const args = process . argv . splice ( 2 , process . argv . length )
43-
44- if ( args . length === 0 ) {
45- logUsage ( )
46- return ERROR_CODE . CALLED_WITH_ILLEGAL_PARAMETERS
30+ const options : StaticBuildOptions = {
31+ inputDirectory : "" ,
32+ outputDirectory : "" ,
33+ baseURL :
34+ ( process . env . context === "production" ? process . env . URL : process . env . DEPLOY_PRIME_URL ) ||
35+ "http://localhost:8082" ,
36+ logger : {
37+ info : console . log ,
38+ warn : console . warn ,
39+ error : console . error ,
40+ time : console . time ,
41+ timeEnd : console . timeEnd ,
42+ } ,
4743 }
4844
49- const inputDirectory = args[0]
50- const outputDirectory = args[1]
45+ const argv = parseArgv ( process . argv )
5146
52- // Parse options that user has provided as an args object.
53- const options = args.reduce(
54- (mem, arg) => {
55- if ( arg === "--watch" || arg === "-w" ) {
56- mem [ "watch" ] = true
57- }
47+ if ( argv . length === 0 ) {
48+ return logUsage ( )
49+ }
5850
59- if (arg === "--dry-run") {
60- mem [ "dryRun" ] = true
61- }
51+ for ( const arg of argv ) {
52+ if ( isStringLiteral ( arg ) && arg . position === 0 ) {
53+ options . inputDirectory = path . join ( process . cwd ( ) , arg . value )
54+ }
55+
56+ if ( isStringLiteral ( arg ) && arg . position === 1 ) {
57+ options . outputDirectory = path . join ( process . cwd ( ) , arg . value )
58+ }
59+
60+ if ( isStringLiteral ( arg ) && arg . position > 1 ) {
61+ stdout . write ( `Error: Unknown argument ${ arg . value } .\n` )
62+ return ERROR_CODE . CALLED_WITH_ILLEGAL_PARAMETERS
63+ }
64+
65+ if ( isKeyValuePair ( arg ) ) {
66+ switch ( arg . key ) {
67+ case "watch" :
68+ case "w" : {
69+ options . watch = true
70+ continue
71+ }
6272
63- if (arg === "--check" || arg === "-c") {
64- mem [ "check" ] = true
65- }
73+ case "dry-run" : {
74+ options . dryRun = true
75+ continue
76+ }
6677
67- if (arg.startsWith("--port") || arg.startsWith("-p")) {
68- const splitValue = arg . split ( "=" )
78+ case "check" :
79+ case "c" : {
80+ options . check = true
81+ continue
82+ }
6983
70- if ( splitValue . length === 2 ) {
71- mem [ "port" ] = splitValue [ 1 ] . trim ( )
84+ default : {
85+ stdout . write ( `Error: Unknown argument ${ arg . key } .\n` )
86+ return ERROR_CODE . CALLED_WITH_ILLEGAL_PARAMETERS
7287 }
7388 }
74-
75- return mem
76- } ,
77- { ...DEFAULT_ARGS } ,
78- )
89+ }
90+ }
7991
8092 // Check that the first argument is a path and not a command.
81- if (!inputDirectory) {
93+ if ( ! options . inputDirectory ) {
8294 stdout . write ( `Error: Missing input directory.\n` )
8395 return ERROR_CODE . CALLED_WITH_ILLEGAL_PARAMETERS
8496 }
8597
86- if (inputDirectory.slice(0, 1) === "-") {
87- stdout . write ( `Error: Invalid input directory.\n> ${ inputDirectory } ` )
88- return ERROR_CODE . CALLED_WITH_ILLEGAL_PARAMETERS
89- }
90-
9198 // Check that the second argument is a path and not a command.
92- if (!outputDirectory) {
99+ if ( ! options . outputDirectory ) {
93100 stdout . write ( `Error: Missing output directory.\n` )
94101 return ERROR_CODE . CALLED_WITH_ILLEGAL_PARAMETERS
95102 }
96103
97- if (outputDirectory.slice(0, 1) === "-") {
98- stdout . write ( `Error: Invalid output directory.\n> ${ inputDirectory } ` )
99- return ERROR_CODE . CALLED_WITH_ILLEGAL_PARAMETERS
100- }
101-
102- if (!fs.existsSync(inputDirectory)) {
103- stdout . write ( `Error: Input directory "${ inputDirectory } " does not exist.\n` )
104+ if ( ! fs . existsSync ( options . inputDirectory ) ) {
105+ stdout . write ( `Error: Input directory "${ options . inputDirectory } " does not exist.\n` )
104106 return ERROR_CODE . FAILED_T0_READ_LOCAL_FILE
105107 }
106108
107- if (!fs.existsSync(outputDirectory)) {
108- fs . mkdirSync ( outputDirectory )
109+ if ( ! fs . existsSync ( options . outputDirectory ) ) {
110+ fs . mkdirSync ( options . outputDirectory )
109111 }
110112
111- const baseURL =
112- (process.env.context === "production" ? process.env.URL : process.env.DEPLOY_PRIME_URL) ||
113- `http://localhost:${ options ?. port || DEFAULT_ARGS . port } `
114-
115- await staticbuild({
116- inputDirectory : path . join ( process . cwd ( ) , inputDirectory ) ,
117- outputDirectory : path . join ( process . cwd ( ) , outputDirectory ) ,
118- baseURL ,
119- ...options ,
120- logger : {
121- info : console . log ,
122- warn : console . warn ,
123- error : console . error ,
124- time : console . time ,
125- timeEnd : console . timeEnd ,
126- } ,
127- } )
113+ await staticbuild ( options )
128114}
129115
130116main ( )
0 commit comments