A templating language that looks like Lisp and compiles to HTML
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

oslo.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const args = require('minimist')(process.argv.slice(2))
  5. const oslo = require('../src/index')
  6. class OsloCLI {
  7. constructor(opts) {
  8. this.context = {}
  9. if (opts.l) {
  10. this.context = JSON.parse(fs.readFileSync(this.absolutePath(opts.l)))
  11. }
  12. let output
  13. if (opts.f && opts.f !== true) {
  14. output = this.file(path.join(process.cwd(), opts.f))
  15. } else if (opts.d && opts.d !== true) {
  16. if (!opts.o) {
  17. console.log(
  18. 'Output directory required. Invoke oslo with `-o path/to/output/directory`',
  19. )
  20. process.exit()
  21. }
  22. output = this.directory(
  23. this.absolutePath(opts.d),
  24. this.absolutePath(opts.o),
  25. )
  26. } else if (opts.e && opts.e !== true) {
  27. output = this.inline(opts.e)
  28. } else {
  29. this.printUsage()
  30. process.exit()
  31. }
  32. if (output.error) {
  33. this.error(output.error)
  34. }
  35. if (opts.o) {
  36. const absoluteOutputPath = this.absolutePath(opts.o)
  37. if (opts.f || opts.e) {
  38. fs.writeFile(absoluteOutputPath, output, function(err) {
  39. if (err) throw err
  40. })
  41. } else if (opts.d) {
  42. const absoluteInputPath = this.absolutePath(opts.d)
  43. Object.keys(output).forEach(function(file) {
  44. const absoluteOutputFilePath = file
  45. .replace(absoluteInputPath, absoluteOutputPath)
  46. .replace(/\.oslo$/, '.html')
  47. const parentDirectory = path.dirname(absoluteOutputFilePath)
  48. if (!fs.existsSync(parentDirectory)) {
  49. fs.mkdirSync(parentDirectory)
  50. }
  51. fs.writeFile(absoluteOutputFilePath, output[file], function(err) {
  52. if (err) throw err
  53. console.log(
  54. `Writing to ${absoluteOutputFilePath.replace(process.cwd(), '')}`,
  55. )
  56. })
  57. })
  58. }
  59. } else {
  60. process.stdout.write(output)
  61. process.stdout.write('\n')
  62. }
  63. }
  64. file(file) {
  65. const contents = fs.readFileSync(file).toString()
  66. return oslo(contents, this.context)
  67. }
  68. directory(directory) {
  69. let files = {}
  70. fs.readdirSync(directory).forEach(item => {
  71. const absolutePath = this.absolutePath(item, directory)
  72. if (fs.lstatSync(absolutePath).isDirectory()) {
  73. files = Object.assign(files, this.directory(absolutePath))
  74. } else {
  75. let output = this.file(absolutePath)
  76. files[absolutePath] = output
  77. }
  78. })
  79. return files
  80. }
  81. inline(source) {
  82. return oslo(source, this.context)
  83. }
  84. absolutePath(file, root = null) {
  85. if (!root) {
  86. root = process.cwd()
  87. }
  88. return path.join(root, file)
  89. }
  90. printUsage() {
  91. console.log(
  92. `USAGE
  93. -d The path to a directory. The directory will be recursively scanned. An
  94. output directory is required with this option.
  95. oslo -d templates
  96. -e Pass oslo code directly.
  97. oslo -e '(div "Hello world")'
  98. -f The path to a single file.
  99. oslo -f index.oslo
  100. -l The path to a json file to use as context for compilation
  101. oslo -f index.oslo -l data.json
  102. -o Where to direct the output. If no path is provided, output will be directed
  103. to stdout. When used with -f, the argument should be the path to a file.
  104. When used with -d, the argument should be the path to a directory, which
  105. will be created if it does not already exist.
  106. oslo -f index.oslo -o index.html
  107. oslo -d templates -o html`,
  108. )
  109. }
  110. error(err) {
  111. console.log(err.message)
  112. process.exit()
  113. }
  114. }
  115. new OsloCLI(args)