forked from synapticsim/mach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
100 lines (85 loc) · 4.15 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#! /usr/bin/env node
/*
* SPDX-FileCopyrightText: 2022 Synaptic Simulations and its contributors
* SPDX-License-Identifier: MIT
*/
import dotenv from 'dotenv';
import { Command } from 'commander';
import signale from 'signale';
import chalk from 'chalk';
import path from 'path';
import { description, version } from './package.json';
import { machBuild, machWatch } from './src';
import { MachConfig, MachConfigSchema } from './src/types';
try {
dotenv.config();
} catch {
// .env is optional, but dotenv throws an error if it cannot load it
}
interface ParsedCommandArgs {
config: MachConfig;
bundles: string;
out: string;
filter?: RegExp;
verbose?: boolean;
outputMetafile?: boolean;
skipSimulatorPackage?: boolean;
}
const cli = new Command();
const commandWithOptions = (name: string) => cli.command(name)
.option('-c, --config <filename>', 'specify path to configuration file', './mach.config.js')
.option('-d, --work-in-config-dir', 'use config directory as working directory')
.option('-b, --bundles <dirname>', 'bundles output directory', './bundles')
.option('-e, --werror', 'makes all warnings into errors')
.option('-f, --filter <regex>', 'regex filter of included instrument names')
.option('-m, --minify', 'minify bundle code')
.option('-s, --skip-simulator-package', 'skips writing simulator package templates')
.option('-t, --output-metafile', 'output `build_meta.json` file to bundles directory')
.option('-u, --output-sourcemaps', 'append sourcemaps to the end of bundle files')
.option('-v, --verbose', 'output additional build information')
.hook('preAction', async (thisCommand, actionCommand) => {
signale.info(`Welcome to ${chalk.cyanBright('Mach')}, v${version}`);
process.env.CONFIG_PATH = path.resolve(actionCommand.getOptionValue('config'));
process.env.BUNDLES_DIR = path.resolve(actionCommand.getOptionValue('bundles'));
process.env.WARNINGS_ERROR = actionCommand.getOptionValue('werror') ?? false;
process.env.MINIFY_BUNDLES = actionCommand.getOptionValue('minify') ?? false;
process.env.SKIP_SIM_PACKAGE = actionCommand.getOptionValue('skipSimulatorPackage') ?? false;
process.env.VERBOSE_OUTPUT = actionCommand.getOptionValue('verbose') ?? false;
process.env.OUTPUT_METAFILE = actionCommand.getOptionValue('outputMetafile') ?? false;
process.env.OUTPUT_SOURCEMAPS = actionCommand.getOptionValue('outputSourcemaps') ?? false;
process.env.WORK_IN_CONFIG_DIR = actionCommand.getOptionValue('workInConfigDir') ?? false;
actionCommand.setOptionValue('filter', new RegExp(actionCommand.getOptionValue('filter')));
// Load config
await import(process.env.CONFIG_PATH.replace(/\\/g, '/'))
.then((module) => {
// Check config integrity
const result = MachConfigSchema.safeParse(module.default);
if (result.success) {
actionCommand.setOptionValue('config', result.data);
signale.info('Loaded config file', chalk.cyanBright(process.env.CONFIG_PATH), '\n');
} else {
signale.error('Invalid config file', chalk.redBright(process.env.CONFIG_PATH));
process.exit(1);
}
if (process.env.WORK_IN_CONFIG_DIR) {
process.chdir(path.dirname(process.env.CONFIG_PATH));
}
})
.catch(() => {
signale.error('Unable to load config file', chalk.redBright(process.env.CONFIG_PATH));
process.exit(1);
});
});
cli
.name('mach')
.version(version)
.description(description);
commandWithOptions('build')
.description('compile instruments specified in configuration file')
.action(({ config, filter }: ParsedCommandArgs) => machBuild(config, filter));
commandWithOptions('watch')
.description('watch instruments for changes and re-compile bundles when updated')
.action(({ config, filter }: ParsedCommandArgs) => machWatch(config, filter));
cli.parse();
export { machBuild, machWatch } from './src';
export * from './src/types';