authCode.js 596 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //@flow
  2. const rand = require("randomatic");
  3. const fs = require("fs");
  4. const path = require("path");
  5. const fn = path.join(process.cwd(), "authCode");
  6. class Auth {
  7. code: string;
  8. constructor() {
  9. if (fs.existsSync(fn)) {
  10. this.code = fs.readFileSync(fn, "utf8");
  11. } else {
  12. this.generate();
  13. }
  14. }
  15. generate() {
  16. this.code = [
  17. rand("A", 4),
  18. rand("0", 4),
  19. rand("Aa", 4),
  20. rand("0", 4),
  21. ].join("-")
  22. fs.writeFileSync(fn, this.code, {
  23. flag: "w"
  24. });
  25. }
  26. get(): string {
  27. return this.code;
  28. }
  29. }
  30. module.exports = new Auth();