|
| 1 | +import { BlockFactory, CssBlockError } from "@css-blocks/core"; |
| 2 | +import chalk = require("chalk"); |
| 3 | +import fs = require("fs"); |
| 4 | +import fse = require("fs-extra"); |
| 5 | +import path = require("path"); |
| 6 | +import util = require("util"); |
| 7 | +import yargs = require("yargs"); |
| 8 | + |
| 9 | +const writeFile = util.promisify(fs.writeFile); |
| 10 | + |
| 11 | +interface GlobalArgs { |
| 12 | + preprocessors: string | undefined; |
| 13 | + [k: string]: unknown; |
| 14 | +} |
| 15 | + |
| 16 | +interface ValidateArgs extends GlobalArgs { |
| 17 | + blocks: unknown; |
| 18 | +} |
| 19 | + |
| 20 | +interface ValidateOptions { |
| 21 | + preprocessors?: string; |
| 22 | +} |
| 23 | + |
| 24 | +export class CLI { |
| 25 | + constructor() { |
| 26 | + } |
| 27 | + |
| 28 | + run(args: Array<string>): Promise<void> { |
| 29 | + let argv = this.argumentParser().parse(args); |
| 30 | + if (argv.promise) { |
| 31 | + return argv.promise as Promise<void>; |
| 32 | + } else { |
| 33 | + return Promise.resolve(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + get chalk() { |
| 38 | + return chalk.default; |
| 39 | + } |
| 40 | + |
| 41 | + argumentParser() { |
| 42 | + return yargs |
| 43 | + .scriptName("css-blocks") |
| 44 | + .usage("$0 <cmd> [options] block-dir-or-file...") |
| 45 | + .version() |
| 46 | + .strict() |
| 47 | + .option("preprocessors", { |
| 48 | + type: "string", |
| 49 | + global: true, |
| 50 | + description: "A JS file that exports an object that maps extensions to a preprocessor function for that type.", |
| 51 | + nargs: 1, |
| 52 | + }) |
| 53 | + .command<ValidateArgs>( |
| 54 | + "validate <blocks..>", |
| 55 | + "Validate block file syntax.", (y) => |
| 56 | + y.positional("blocks", { |
| 57 | + description: "files or directories containing css blocks.", |
| 58 | + }), |
| 59 | + (argv: ValidateArgs) => { |
| 60 | + let { preprocessors } = argv; |
| 61 | + argv.promise = this.validate(argv.blocks as Array<string>, { |
| 62 | + preprocessors, |
| 63 | + }); |
| 64 | + }, |
| 65 | + ) |
| 66 | + .demandCommand(1, "No command was provided.") |
| 67 | + .help(); |
| 68 | + } |
| 69 | + |
| 70 | + async validate(blockFiles: Array<string>, options: ValidateOptions) { |
| 71 | + let preprocessors = options.preprocessors ? require(options.preprocessors) : {}; |
| 72 | + let factory = new BlockFactory({preprocessors}); |
| 73 | + let errorCount = 0; |
| 74 | + for (let blockFile of blockFiles) { |
| 75 | + try { |
| 76 | + await factory.getBlockFromPath(path.resolve(blockFile)); |
| 77 | + this.println(`${this.chalk.green("ok")}\t${blockFile}`); |
| 78 | + } catch (e) { |
| 79 | + errorCount++; |
| 80 | + if (e instanceof CssBlockError) { |
| 81 | + let loc = e.location; |
| 82 | + if (loc) { |
| 83 | + this.println(`${this.chalk.red("error")}\t${this.chalk.whiteBright(blockFile)}:${loc.line}:${loc.column} ${e.origMessage}`); |
| 84 | + } else { |
| 85 | + this.println(`${this.chalk.red("error")}\t${this.chalk.whiteBright(blockFile)} ${e.origMessage}`); |
| 86 | + } |
| 87 | + } else { |
| 88 | + console.error(e); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + this.exit(errorCount); |
| 93 | + } |
| 94 | + |
| 95 | + println(...args: Array<string>) { |
| 96 | + console.log(...args); |
| 97 | + } |
| 98 | + |
| 99 | + async writeFile(filename: string, contents: string): Promise<void> { |
| 100 | + await fse.mkdirp(path.dirname(filename)); |
| 101 | + return writeFile(filename, contents, "utf8"); |
| 102 | + } |
| 103 | + |
| 104 | + exit(code = 0) { |
| 105 | + process.exit(code); |
| 106 | + } |
| 107 | +} |
0 commit comments