hydrogen/packages/cli/src/utilities/index.ts
2021-11-04 15:22:30 -07:00

43 lines
1 KiB
TypeScript

import {join, resolve} from 'path';
import minimist from 'minimist';
export * from './error';
export * from './feature';
export {merge} from './merge';
const DEFAULT_SUBCOMMANDS = {
create: 'app',
version: '',
};
export function parseCliArguments(rawInputs?: string[]) {
const inputs = minimist(rawInputs || []);
const command = inputs._[0];
const root =
command === 'create' && inputs._[2]
? join(process.cwd(), inputs._[2])
: process.cwd();
const subcommand =
inputs._[1] || DEFAULT_SUBCOMMANDS[command as 'create' | 'version'];
const {debug} = inputs;
return {
root: resolve(root),
mode: debug ? 'debug' : 'default',
command: [command, subcommand].join('/'),
};
}
export function formatFile(file: string) {
const match = file.match(/^[^\S\n]*(?=\S)/gm);
const indent = match && Math.min(...match.map((el) => el.length));
if (indent) {
const regexp = new RegExp(`^.{${indent}}`, 'gm');
return file.replace(regexp, '').trim() + '\n';
}
return file.trim() + '\n';
}