UNPKG

4.87 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13/* eslint no-console: off */
14/* eslint global-require: off */
15
16const path = require('path');
17const yargsBuild = require('./yargs-build.js');
18const yargsParams = require('./yargs-params.js');
19const yargsGithub = require('./yargs-github.js');
20const yargsAlgolia = require('./yargs-algolia.js');
21const { getOrCreateLogger } = require('./log-common.js');
22
23module.exports = function up() {
24 let executor;
25 return {
26 set executor(value) {
27 executor = value;
28 },
29 command: 'up [files...]',
30 description: 'Run a Helix development server',
31 builder: (yargs) => {
32 yargsBuild(yargs);
33 yargsGithub(yargs);
34 yargsAlgolia(yargs);
35 yargsParams(yargs, {
36 name: 'dev-default',
37 describe: 'Additional action parameters',
38 alias: ['devDefault'],
39 type: 'array',
40 default: [],
41 });
42 yargs
43 .option('open', {
44 describe: 'Open a browser window',
45 type: 'boolean',
46 default: true,
47 })
48 .option('no-open', {
49 // negation of the open option (resets open default)
50 // see https://github.com/yargs/yargs/blob/master/docs/tricks.md#negating-boolean-arguments
51 alias: 'noOpen',
52 describe: 'Disable automatic opening of browser window',
53 type: 'boolean',
54 })
55 .option('livereload', {
56 describe: 'Enable automatic reloading of modified sources in browser.',
57 type: 'boolean',
58 default: true,
59 })
60 .option('no-livereload', {
61 // negation of the livereload option (resets open default)
62 // see https://github.com/yargs/yargs/blob/master/docs/tricks.md#negating-boolean-arguments
63 alias: 'noLiveReload',
64 describe: 'Disable live-reload',
65 type: 'boolean',
66 })
67 .option('host', {
68 describe: 'Override request.host',
69 type: 'string',
70 })
71 .option('local-repo', {
72 alias: 'localRepo',
73 describe: 'Emulates a GitHub repository for the specified local git repository.',
74 type: 'string',
75 array: true,
76 default: '.',
77 })
78 // allow for comma separated values
79 .coerce('localRepo', (value) => value.reduce((acc, curr) => {
80 if (curr) {
81 acc.push(...curr.split(/\s*,\s*/));
82 }
83 return acc;
84 }, []))
85 .option('no-local-repo', {
86 // negation of the local-repo option (resets local-repo default)
87 // see https://github.com/yargs/yargs/blob/master/docs/tricks.md#negating-boolean-arguments
88 alias: 'noLocalRepo',
89 describe: 'Ignore local checkout, always fetch from GitHub',
90 type: 'boolean',
91 })
92 .option('save-config', {
93 alias: 'saveConfig',
94 describe: 'Saves the default config.',
95 type: 'boolean',
96 default: false,
97 })
98 .option('port', {
99 describe: 'Start development server on port',
100 type: 'int',
101 default: 3000,
102 })
103 .group(['port', 'open', 'no-open', 'host', 'local-repo', 'no-local-repo'], 'Server options')
104 .help();
105 },
106 handler: async (argv) => {
107 if (!executor) {
108 // eslint-disable-next-line global-require
109 const UpCommand = require('./up.cmd'); // lazy load the handler to speed up execution time
110 executor = new UpCommand(getOrCreateLogger(argv));
111 }
112
113 await executor
114 .withTargetDir(argv.target)
115 .withFiles(argv.files)
116 .withOverrideHost(argv.host)
117 .withSaveConfig(argv.saveConfig)
118 .withHttpPort(argv.port)
119 .withLocalRepo(argv.localRepo)
120 .withDevDefault(argv.devDefault)
121 .withDevDefaultFile(argv.devDefaultFile)
122 .withGithubToken(argv.githubToken)
123 // only open browser window when executable is `hlx`
124 // this prevents the window to be opened during integration tests
125 .withOpen(argv.open && path.basename(argv.$0) === 'hlx')
126 .withLiveReload(argv.livereload)
127 .withCustomPipeline(argv.customPipeline)
128 .withAlgoliaAppID(argv.algoliaAppID)
129 .withAlgoliaAPIKey(argv.algoliaAPIKey)
130 .run();
131 },
132 };
133};