vendure-cli.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env node
  2. import * as fs from 'fs-extra';
  3. import * as Handlebars from 'handlebars';
  4. import * as path from 'path';
  5. import * as prompts from 'prompts';
  6. // tslint:disable:no-console
  7. console.log(
  8. '\x1b[36m%s\x1b[0m',
  9. `
  10. _
  11. | |
  12. __ _____ _ __ __| |_ _ _ __ ___
  13. \\ \\ / / _ \\ '_ \\ / _\` | | | | '__/ _ \\
  14. \\ V / __/ | | | (_| | |_| | | | __/
  15. \\_/ \\___|_| |_|\\__,_|\\__,_|_| \\___|
  16. `,
  17. );
  18. console.log(`Let's get started with a new Vendure server!\n`);
  19. prompts([
  20. {
  21. type: 'select',
  22. name: 'dbType',
  23. message: 'Which database are you using?',
  24. choices: [
  25. { title: 'MySQL / MariaDB', value: 'mysql' },
  26. { title: 'Postgres', value: 'postgres' },
  27. { title: 'SQLite', value: 'sqlite' },
  28. { title: 'MS SQL Server', value: 'mssql' },
  29. { title: 'Oracle', value: 'oracle' },
  30. ],
  31. initial: 0 as any,
  32. },
  33. {
  34. type: 'text',
  35. name: 'dbHost',
  36. message: `What's the database host address?`,
  37. initial: '192.168.99.100',
  38. },
  39. {
  40. type: 'text',
  41. name: 'dbName',
  42. message: `What's the name of the database?`,
  43. initial: 'vendure',
  44. },
  45. {
  46. type: 'text',
  47. name: 'dbUserName',
  48. message: `What's the database user name?`,
  49. initial: 'root',
  50. },
  51. {
  52. type: 'password',
  53. name: 'dbPassword',
  54. message: `What's the database password?`,
  55. },
  56. {
  57. type: 'select',
  58. name: 'language',
  59. message: 'Which language will you be using?',
  60. choices: [{ title: 'TypeScript', value: 'ts' }, { title: 'JavaScript', value: 'js' }],
  61. initial: 0 as any,
  62. },
  63. ]).then(
  64. async answers => {
  65. if (!answers.language) {
  66. console.log('Setup aborted. No changes made');
  67. process.exit(0);
  68. }
  69. await createDirectoryStructure();
  70. await copyEmailTemplates();
  71. await createIndexFile(answers);
  72. console.log(
  73. '\x1b[36m%s\x1b[0m',
  74. `\nAll done! You can now execute the index.${answers.language} file to start the server.`,
  75. );
  76. },
  77. err => console.log('error', err),
  78. );
  79. /**
  80. * Generate the default directory structure for a new Vendure project
  81. */
  82. async function createDirectoryStructure() {
  83. const cwd = process.cwd();
  84. await fs.ensureDir(path.join(cwd, 'vendure', 'email', 'test-emails'));
  85. await fs.ensureDir(path.join(cwd, 'vendure', 'import-assets'));
  86. await fs.ensureDir(path.join(cwd, 'vendure', 'assets'));
  87. }
  88. /**
  89. * Copy the email templates into the app
  90. */
  91. async function copyEmailTemplates() {
  92. const templateDir = path.join(__dirname, 'assets', 'email-templates');
  93. await fs.copy(templateDir, path.join(process.cwd(), 'vendure', 'email', 'templates'));
  94. }
  95. /**
  96. * Create the server index file based on the options specified by the CLI prompts.
  97. */
  98. async function createIndexFile(answers: any) {
  99. const cwd = process.cwd();
  100. const templateContext = {
  101. ...answers,
  102. isTs: answers.language === 'ts',
  103. sessionSecret: Math.random()
  104. .toString(36)
  105. .substr(3),
  106. };
  107. const template = await fs.readFile(path.join(__dirname, 'assets', 'index.hbs'), 'utf-8');
  108. const indexSource = Handlebars.compile(template)(templateContext);
  109. await fs.writeFile(path.join(cwd, 'index.' + answers.language), indexSource);
  110. }