|
| 1 | +#!/usr/bin/env node |
| 2 | +import program from 'commander'; |
| 3 | +import path from 'path'; |
| 4 | + |
| 5 | +/* tslint:disable:no-require-imports no-var-requires */ |
| 6 | +const pkg = require('../../package.json'); |
| 7 | + |
| 8 | +const contextHelp: string = `Path to context or project root directory. Defaults to current working directory. It is recommended to use absolute path, else it is calculated from current working directory. The path you mention here should be what the URL 'localhost/wp-content/<themes|plugins>/<slug>/' map to. In most cases, you should leave it, because calling the program from npm or yarn script should automatically set it.`; |
| 9 | +let isValidCommand = false; |
| 10 | + |
| 11 | +// Declare version and stuff |
| 12 | +program |
| 13 | + .version(pkg.version) |
| 14 | + .description('Start the development server or create production builds.') |
| 15 | + .usage('command [options]'); |
| 16 | + |
| 17 | +program.on('--help', () => { |
| 18 | + console.log(''); |
| 19 | + console.log('Examples:'); |
| 20 | + console.log(''); |
| 21 | + console.log( |
| 22 | + ' %s start -p /path/to/wpackio.project.js', |
| 23 | + path.basename(process.argv[1]) |
| 24 | + ); |
| 25 | + console.log( |
| 26 | + ' %s build -c /path/to/project/root', |
| 27 | + path.basename(process.argv[1]) |
| 28 | + ); |
| 29 | +}); |
| 30 | + |
| 31 | +// Commands |
| 32 | + |
| 33 | +// Start the server |
| 34 | +program |
| 35 | + .command('start') |
| 36 | + .description('Start the development server.') |
| 37 | + .option('-c, --context [path]', contextHelp) |
| 38 | + .option( |
| 39 | + '-p, --project-config [path]', |
| 40 | + 'Path to project config. If it differs from ./wpackio.project.js' |
| 41 | + ) |
| 42 | + .option( |
| 43 | + '-s, --server-config [path]', |
| 44 | + 'Path to server config. If it differs from ./wpackio.server.js' |
| 45 | + ) |
| 46 | + .action(options => { |
| 47 | + isValidCommand = true; |
| 48 | + console.log('should start script.', options.context); |
| 49 | + // Set process.env.NODE_ENV to development |
| 50 | + // Set process.env.BABEL_ENV to development |
| 51 | + // Get project and server config JSONs. |
| 52 | + // Start the webpack/browserSync server |
| 53 | + // Listen for SIGTERM and quit properly |
| 54 | + // Listen for keyinput <r> and invalidate webpack builds. |
| 55 | + }); |
| 56 | + |
| 57 | +// Build the script |
| 58 | +program |
| 59 | + .command('build') |
| 60 | + .description('Build production files.') |
| 61 | + .option('-c, --context', contextHelp) |
| 62 | + .option( |
| 63 | + '-p, --project-config [path]', |
| 64 | + 'Path to project config. If it differs from ./wpackio.project.js' |
| 65 | + ) |
| 66 | + .option( |
| 67 | + '-s, --server-config [path]', |
| 68 | + 'Path to server config. If it differs from ./wpackio.server.js' |
| 69 | + ) |
| 70 | + .action(options => { |
| 71 | + isValidCommand = true; |
| 72 | + console.log('should build the script', options.context); |
| 73 | + // Set process.env.NODE_ENV to production |
| 74 | + // Set process.env.BABEL_ENV to production |
| 75 | + // Get project and server config JSONs. |
| 76 | + // Compile scripts using webpack |
| 77 | + }); |
| 78 | + |
| 79 | +// Init |
| 80 | +program.parse(process.argv); |
| 81 | + |
| 82 | +// error on unknown commands |
| 83 | +if (!isValidCommand) { |
| 84 | + console.error( |
| 85 | + 'Invalid command: %s\nSee --help for a list of available commands.', |
| 86 | + program.args.join(' ') |
| 87 | + ); |
| 88 | + process.exit(1); |
| 89 | +} |
0 commit comments