init.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import fs from 'fs-extra';
  2. import Handlebars from 'handlebars';
  3. import path from 'path';
  4. import { PromptObject } from 'prompts';
  5. import prompts from 'prompts';
  6. // tslint:disable:no-console
  7. export async function init(): Promise<string> {
  8. function defaultPort(_dbType: string) {
  9. switch (_dbType) {
  10. case 'mysql':
  11. return 3306;
  12. case 'postgres':
  13. return 5432;
  14. case 'mssql':
  15. return 1433;
  16. case 'oracle':
  17. return 1521;
  18. default:
  19. return 3306;
  20. }
  21. }
  22. function onSubmit(prompt: PromptObject, answer: any) {
  23. if (prompt.name === 'dbType') {
  24. dbType = answer;
  25. }
  26. }
  27. let dbType: string;
  28. console.log(`Let's get started with a new Vendure server!\n`);
  29. const answers = await prompts(
  30. [
  31. {
  32. type: 'select',
  33. name: 'dbType',
  34. message: 'Which database are you using?',
  35. choices: [
  36. { title: 'MySQL / MariaDB', value: 'mysql' },
  37. { title: 'Postgres', value: 'postgres' },
  38. { title: 'SQLite', value: 'sqlite' },
  39. { title: 'MS SQL Server', value: 'mssql' },
  40. { title: 'Oracle', value: 'oracle' },
  41. ],
  42. initial: 0 as any,
  43. },
  44. {
  45. type: (() => (dbType === 'sqlite' ? null : 'text')) as any,
  46. name: 'dbHost',
  47. message: `What's the database host address?`,
  48. initial: '192.168.99.100',
  49. },
  50. {
  51. type: (() => (dbType === 'sqlite' ? null : 'text')) as any,
  52. name: 'dbPort',
  53. message: `What port is the database listening on?`,
  54. initial: (() => defaultPort(dbType)) as any,
  55. },
  56. {
  57. type: 'text',
  58. name: 'dbName',
  59. message: () =>
  60. dbType === 'sqlite'
  61. ? `What is the path to the SQLite database file?`
  62. : `What's the name of the database?`,
  63. initial: (() =>
  64. dbType === 'sqlite' ? path.join(__dirname, 'vendure.sqlite') : 'vendure') as any,
  65. },
  66. {
  67. type: (() => (dbType === 'sqlite' ? null : 'text')) as any,
  68. name: 'dbUserName',
  69. message: `What's the database user name?`,
  70. initial: 'root',
  71. },
  72. {
  73. type: (() => (dbType === 'sqlite' ? null : 'password')) as any,
  74. name: 'dbPassword',
  75. message: `What's the database password?`,
  76. },
  77. {
  78. type: 'select',
  79. name: 'language',
  80. message: 'Which programming language will you be using?',
  81. choices: [{ title: 'TypeScript', value: 'ts' }, { title: 'JavaScript', value: 'js' }],
  82. initial: 0 as any,
  83. },
  84. ],
  85. {
  86. onSubmit,
  87. onCancel() {
  88. /* */
  89. },
  90. },
  91. );
  92. if (!answers.language) {
  93. console.log('Setup aborted. No changes made');
  94. process.exit(0);
  95. }
  96. await createDirectoryStructure();
  97. await copyEmailTemplates();
  98. return createFilesForBootstrap(answers);
  99. }
  100. /**
  101. * Generate the default directory structure for a new Vendure project
  102. */
  103. async function createDirectoryStructure() {
  104. const cwd = process.cwd();
  105. await fs.ensureDir(path.join(cwd, 'vendure', 'email', 'test-emails'));
  106. await fs.ensureDir(path.join(cwd, 'vendure', 'import-assets'));
  107. await fs.ensureDir(path.join(cwd, 'vendure', 'assets'));
  108. }
  109. /**
  110. * Copy the email templates into the app
  111. */
  112. async function copyEmailTemplates() {
  113. const templateDir = path.join(__dirname, 'assets', 'email-templates');
  114. try {
  115. await fs.copy(templateDir, path.join(process.cwd(), 'vendure', 'email', 'templates'));
  116. } catch (err) {
  117. console.error(`Failed to copy email templates.`);
  118. }
  119. }
  120. /**
  121. * Create the server index and config files based on the options specified by the CLI prompts.
  122. */
  123. async function createFilesForBootstrap(answers: any): Promise<string> {
  124. const cwd = process.cwd();
  125. const filePath = (fileName: string): string => path.join(cwd, `${fileName}.${answers.language}`);
  126. const templateContext = {
  127. ...answers,
  128. isTs: answers.language === 'ts',
  129. isSQLite: answers.dbType === 'sqlite',
  130. sessionSecret: Math.random()
  131. .toString(36)
  132. .substr(3),
  133. };
  134. const configTemplate = await fs.readFile(path.join(__dirname, 'assets', 'vendure-config.hbs'), 'utf-8');
  135. const configSource = Handlebars.compile(configTemplate)(templateContext);
  136. await fs.writeFile(filePath('vendure-config'), configSource);
  137. const indexTemplate = await fs.readFile(path.join(__dirname, 'assets', 'index.hbs'), 'utf-8');
  138. const indexSource = Handlebars.compile(indexTemplate)(templateContext);
  139. await fs.writeFile(filePath('index'), indexSource);
  140. return filePath('index');
  141. }