A stylesheet language written in TypeScript that compiles to CSS
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

moss.ts 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env node
  2. import * as minimist from 'minimist';
  3. import * as fs from 'fs';
  4. import * as path from 'path';
  5. import Compiler from './compiler';
  6. import { isError } from './error';
  7. import { EnvError } from './env';
  8. import Lexer, { LexerError } from './lexer';
  9. import Parser, { ParserError } from './parser';
  10. // import { inspect } from 'util';
  11. // const print = (obj: any) => {
  12. // console.log(inspect(obj, { depth: null }));
  13. // };
  14. const rawArgs = minimist(process.argv.slice(2));
  15. const args = {
  16. eval: rawArgs.e || rawArgs.eval,
  17. file: rawArgs.f || rawArgs.file,
  18. help: rawArgs.h || rawArgs.help,
  19. output: rawArgs.o || rawArgs.output,
  20. pretty: rawArgs.p || rawArgs.pretty,
  21. };
  22. const helpText = `Usage: moss [options]
  23. -e, --eval <source> Evaluate a string of source code
  24. -f, --file <path> Evaluate a file containing source code
  25. -h, --help Print this message
  26. -o, --output <path> Write output to a file
  27. -p, --pretty Pretty print the output`;
  28. const moss = (
  29. source: string,
  30. file?: string
  31. ): LexerError | ParserError | EnvError | string => {
  32. const lexer = new Lexer(source, file);
  33. const tokens = lexer.scan();
  34. if (tokens instanceof LexerError) return tokens;
  35. const parser = new Parser(tokens);
  36. const tree = parser.parse();
  37. // print(tree);
  38. if (tree instanceof ParserError) return tree;
  39. const compiler = new Compiler(tree, { prettyPrint: args.pretty });
  40. return compiler.compile();
  41. };
  42. const run = () => {
  43. if (args.help) {
  44. return helpText;
  45. } else if (args.file) {
  46. const filepath = path.join(process.cwd(), args.file);
  47. if (filepath !== null) {
  48. const source = fs.readFileSync(filepath, 'utf-8');
  49. return moss(source, args.file);
  50. }
  51. return '';
  52. } else if (args.eval) {
  53. return moss(args.eval);
  54. }
  55. return helpText;
  56. };
  57. const output = run();
  58. if (isError(output)) {
  59. const moduleAndLine = `${output.module ? output.module : '-e'}:${
  60. output.line
  61. }`;
  62. console.log(`Error ${moduleAndLine}
  63. ${output.message}`);
  64. } else {
  65. if (output.length) {
  66. if (args.output) {
  67. fs.writeFileSync(path.join(process.cwd(), args.output), output);
  68. } else {
  69. console.log(output);
  70. }
  71. }
  72. }