mirror of https://github.com/grafana/grafana
parent
cee5f030dc
commit
73ef864979
@ -1,33 +1,47 @@ |
||||
import program from 'commander'; |
||||
import chalk from 'chalk'; |
||||
import { execTask } from './utils/execTask'; |
||||
import chalk from 'chalk'; |
||||
import { startTask } from './tasks/core.start'; |
||||
import { buildTask } from './tasks/grafanaui.build'; |
||||
import { releaseTask } from './tasks/grafanaui.release'; |
||||
|
||||
export type Task<T> = (options: T) => Promise<void>; |
||||
program.option('-d, --depreciate <scripts>', 'Inform about npm script deprecation', v => v.split(',')); |
||||
|
||||
// TODO: Refactor to commander commands
|
||||
// This will enable us to have command scoped options and limit the ifs below
|
||||
program |
||||
.option('-h, --hot', 'Runs front-end with hot reload enabled') |
||||
.option('-t, --theme', 'Watches for theme changes and regenerates variables.scss files') |
||||
.option('-d, --depreciate <scripts>', 'Inform about npm script deprecation', v => v.split(',')) |
||||
.option('-b, --build', 'Created @grafana/ui build') |
||||
.option('-r, --release', 'Releases @grafana/ui to npm') |
||||
.parse(process.argv); |
||||
.command('core:start') |
||||
.option('-h, --hot', 'Run front-end with HRM enabled') |
||||
.option('-t, --watchTheme', 'Watch for theme changes and regenerate variables.scss files') |
||||
.description('Starts Grafana front-end in development mode with watch enabled') |
||||
.action(async cmd => { |
||||
await execTask(startTask)({ |
||||
watchThemes: cmd.theme, |
||||
hot: cmd.hot, |
||||
}); |
||||
}); |
||||
|
||||
if (program.build) { |
||||
execTask('grafanaui.build'); |
||||
} else if (program.release) { |
||||
execTask('grafanaui.release'); |
||||
} else { |
||||
if (program.depreciate && program.depreciate.length === 2) { |
||||
console.log( |
||||
chalk.yellow.bold( |
||||
`[NPM script depreciation] ${program.depreciate[0]} is deprecated! Use ${program.depreciate[1]} instead!` |
||||
) |
||||
); |
||||
} |
||||
execTask('core.start', { |
||||
watchThemes: !!program.theme, |
||||
hot: !!program.hot, |
||||
program |
||||
.command('gui:build') |
||||
.description('Builds @grafana/ui package to packages/grafana-ui/dist') |
||||
.action(async cmd => { |
||||
await execTask(buildTask)(); |
||||
}); |
||||
|
||||
program |
||||
.command('gui:release') |
||||
.description('Prepares @grafana/ui release (and publishes to npm on demand)') |
||||
.option('-p, --publish', 'Publish @grafana/ui to npm registry') |
||||
.action(async cmd => { |
||||
await execTask(releaseTask)({ |
||||
publishToNpm: !!cmd.publish, |
||||
}); |
||||
}); |
||||
|
||||
program.parse(process.argv); |
||||
|
||||
if (program.depreciate && program.depreciate.length === 2) { |
||||
console.log( |
||||
chalk.yellow.bold( |
||||
`[NPM script depreciation] ${program.depreciate[0]} is deprecated! Use ${program.depreciate[1]} instead!` |
||||
) |
||||
); |
||||
} |
||||
|
||||
@ -0,0 +1,23 @@ |
||||
export type TaskRunner<T> = (options: T) => Promise<void>; |
||||
|
||||
export class Task<TOptions> { |
||||
name: string; |
||||
runner: (options: TOptions) => Promise<void>; |
||||
options: TOptions; |
||||
|
||||
setName = name => { |
||||
this.name = name; |
||||
}; |
||||
|
||||
setRunner = (runner: TaskRunner<TOptions>) => { |
||||
this.runner = runner; |
||||
}; |
||||
|
||||
setOptions = options => { |
||||
this.options = options; |
||||
}; |
||||
|
||||
exec = () => { |
||||
return this.runner(this.options); |
||||
}; |
||||
} |
||||
@ -1,6 +1,15 @@ |
||||
import { Task } from '..'; |
||||
import { Task } from '../tasks/task'; |
||||
import chalk from 'chalk'; |
||||
|
||||
export const execTask = async <T>(taskName, options?: T) => { |
||||
const task = await import(`${__dirname}/../tasks/${taskName}.ts`); |
||||
return task.default(options) as Task<T>; |
||||
export const execTask = <TOptions>(task: Task<TOptions>) => async (options: TOptions) => { |
||||
console.log(chalk.yellow(`Running ${chalk.bold(task.name)} task`)); |
||||
task.setOptions(options); |
||||
try { |
||||
console.group(); |
||||
await task.exec(); |
||||
console.groupEnd(); |
||||
} catch (e) { |
||||
console.log(e); |
||||
process.exit(1); |
||||
} |
||||
}; |
||||
|
||||
@ -1,7 +0,0 @@ |
||||
import ora from 'ora'; |
||||
|
||||
export const startSpinner = (label: string) => { |
||||
const spinner = new ora(label); |
||||
spinner.start(); |
||||
return spinner; |
||||
}; |
||||
@ -0,0 +1,20 @@ |
||||
import ora from 'ora'; |
||||
|
||||
type FnToSpin<T> = (options: T) => Promise<void>; |
||||
|
||||
export const useSpinner = <T>(spinnerLabel: string, fn: FnToSpin<T>, killProcess = true) => { |
||||
return async (options: T) => { |
||||
const spinner = new ora(spinnerLabel); |
||||
spinner.start(); |
||||
try { |
||||
await fn(options); |
||||
spinner.succeed(); |
||||
} catch (e) { |
||||
spinner.fail(); |
||||
console.log(e); |
||||
if (killProcess) { |
||||
process.exit(1); |
||||
} |
||||
} |
||||
}; |
||||
}; |
||||
Loading…
Reference in new issue