@procore/core-scripts
Version:
A CLI to enhance your development experience
208 lines • 8.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = require("@oclif/command");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const cli_highlight_1 = require("cli-highlight");
const clearConsole_1 = tslib_1.__importDefault(require("react-dev-utils/clearConsole"));
const openBrowser_1 = tslib_1.__importDefault(require("react-dev-utils/openBrowser"));
const tsconfig = tslib_1.__importStar(require("tsconfig"));
const webpack_1 = tslib_1.__importDefault(require("webpack"));
const webpack_dev_server_1 = tslib_1.__importDefault(require("webpack-dev-server"));
const WebpackDevServerUtils_1 = require("react-dev-utils/WebpackDevServerUtils");
const BaseCommand_1 = require("../../BaseCommand");
const paths_1 = require("../../paths");
const app_1 = require("../../webpack/app");
const devServerConfig_1 = require("../../webpack/app/devServerConfig");
class AppStartCommand extends BaseCommand_1.BaseCommand {
async run() {
var _a, _b;
// eslint-disable-next-line no-shadow
const { flags } = this.parse(AppStartCommand);
if (this.workspace.isEnabled('skipValidateTsConfig')) {
this.workspace.debug(`Skipping TypeScript validation`);
}
else {
this.checkTypeScriptConfig();
}
// eslint-disable-next-line prefer-const
let devServer;
const defaultPort = parseInt(flags.port, 10);
const protocol = flags.https ? 'https' : 'http';
const isInteractive = process.stdout.isTTY;
const port = await (0, WebpackDevServerUtils_1.choosePort)(flags.host, defaultPort);
if (port === null) {
this.log(chalk_1.default.red('Can not find a port'));
this.exit(1);
}
const urls = (0, WebpackDevServerUtils_1.prepareUrls)(protocol, flags.host, port);
const publicPath = (0, paths_1.withSlash)(flags.publicPath || urls.localUrlForBrowser);
const config = (0, app_1.configFactory)({
env: 'development',
workspace: this.workspace,
hmr: {
host: flags.socketHost,
port,
},
publicPath,
railsMode: flags.railsMode,
});
if (flags.inspect) {
// @ts-ignore Fix: type spec
const output = config.toString({ verbose: false });
this.log((0, cli_highlight_1.highlight)(output, { language: 'js' }));
this.exit(0);
}
const webpackConfig = config.toConfig();
const compiler = (0, WebpackDevServerUtils_1.createCompiler)({
appName: this.workspace.packageJson.name,
config: webpackConfig,
devSocket: {
errors(errors) {
// @ts-ignore
devServer.sockWrite(devServer.sockets, 'errors', errors);
},
warnings(warnings) {
// @ts-ignore
devServer.sockWrite(devServer.sockets, 'warnings', warnings);
},
},
urls,
useTypeScript: true,
useYarn: true,
webpack: webpack_1.default,
});
const proxyConfig = (0, WebpackDevServerUtils_1.prepareProxy)((_a = this.workspace.procoreConfig.app) === null || _a === void 0 ? void 0 : _a.proxy, this.workspace.resolve('public'), publicPath);
const devServerOverride = (_b = this.workspace.procoreConfig.app) === null || _b === void 0 ? void 0 : _b.devServerOverride;
const serverConfig = (0, devServerConfig_1.devServerConfigFactory)(this.workspace, {
allowedHost: urls.lanUrlForConfig, // TODO consider asserting value is not undefined
dangerouslyDisableHostCheck: flags.dangerouslyDisableHostCheck,
devServerOverride,
host: flags.host,
sockHost: flags.socketHost,
port: port.toString(), // TODO align parameter types
protocol,
proxy: proxyConfig,
publicPath,
});
devServer = new webpack_dev_server_1.default(compiler, serverConfig);
// Launch WebpackDevServer.
devServer.listen(port, flags.host, (err) => {
if (err) {
throw err;
}
if (isInteractive) {
(0, clearConsole_1.default)();
}
this.log('Starting the development server...\n');
if (flags.openBrowser) {
(0, openBrowser_1.default)(urls.localUrlForBrowser);
}
});
['SIGINT', 'SIGTERM'].forEach((sig) => {
process.on(sig, () => {
devServer.close();
process.exit(0);
});
});
}
checkTypeScriptConfig() {
return this.workspace.existsSync('tsconfig.json')
? this.validateTypeScriptConfig()
: this.writeTypeScriptConfig();
}
writeTypeScriptConfig() {
const config = {
extends: './node_modules/@procore/core-scripts/configs/tsconfig.app.loose.json',
compilerOptions: {
baseUrl: '.',
},
include: ['src'],
};
this.workspace.writeFileSync('tsconfig.json', JSON.stringify(config, null, 2));
}
validateTypeScriptConfig() {
const config = tsconfig.readFileSync(this.workspace.resolve('tsconfig.json'));
this.validateTypeScriptConfigBaseUrl(config);
this.validateTypeScriptConfigPaths(config);
}
validateTypeScriptConfigBaseUrl(config) {
var _a, _b;
const baseUrl = (_b = (_a = config.compilerOptions) === null || _a === void 0 ? void 0 : _a.baseUrl) !== null && _b !== void 0 ? _b : undefined;
if (baseUrl === '.') {
return;
}
this.log(chalk_1.default.red());
this.log(chalk_1.default.red('Replace "baseUrl" with the following value:'));
this.log(chalk_1.default.red());
this.log(chalk_1.default.red('{'));
this.log(chalk_1.default.red(' "compilerOptions": {'));
this.log(chalk_1.default.red(' "baseUrl": "."'));
this.log(chalk_1.default.red(' }'));
this.log(chalk_1.default.red('}'));
this.log(chalk_1.default.red());
throw new Error('tsconfig.json misconfigured');
}
validateTypeScriptConfigPaths(config) {
var _a, _b, _c;
const paths = (_b = (_a = config.compilerOptions) === null || _a === void 0 ? void 0 : _a.paths) !== null && _b !== void 0 ? _b : undefined;
if (paths === undefined) {
return;
}
if (((_c = paths['@/*']) === null || _c === void 0 ? void 0 : _c.length) === 1 && paths['@/*'][0] === 'src/*') {
return;
}
this.log(chalk_1.default.red());
this.log(chalk_1.default.red('Make sure "paths" has the following value in it:'));
this.log(chalk_1.default.red());
this.log(chalk_1.default.red('{'));
this.log(chalk_1.default.red(' "compilerOptions": {'));
this.log(chalk_1.default.red(' "paths": {'));
this.log(chalk_1.default.red(' "@/*": ["src/*"]'));
this.log(chalk_1.default.red(' }'));
this.log(chalk_1.default.red(' }'));
this.log(chalk_1.default.red('}'));
this.log(chalk_1.default.red());
throw new Error('tsconfig.json misconfigured');
}
}
AppStartCommand.description = 'Starts the development server.';
AppStartCommand.flags = {
railsMode: command_1.flags.boolean({
default: false,
description: 'Enables Procore Rails integration.',
}),
inspect: command_1.flags.boolean({
default: false,
description: 'Prints the Webpack configuration.',
}),
openBrowser: command_1.flags.boolean({
default: false,
description: 'Open browser after initializing the server.',
}),
https: command_1.flags.boolean({
default: false,
description: 'Use HTTPS.',
}),
dangerouslyDisableHostCheck: command_1.flags.boolean({
default: false,
description: 'Disable host checking.',
}),
port: command_1.flags.string({
default: '5000',
description: 'Server port.',
}),
host: command_1.flags.string({
default: '0.0.0.0',
description: 'Server host.',
}),
socketHost: command_1.flags.string({
default: '0.0.0.0',
description: 'HMR websocket host',
}),
publicPath: command_1.flags.string({
description: 'Public asset path.',
}),
};
exports.default = AppStartCommand;
//# sourceMappingURL=start.js.map