pinstripe 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,6 +32,70 @@ export default {
32
32
  },
33
33
  }, null, 2));
34
34
  });
35
+
36
+ await generateFile(`pinstripe.config.js`, () => {
37
+ line();
38
+ line(`const environment = process.env.NODE_ENV || 'development';`);
39
+ line();
40
+ line(`let database;`);
41
+ line(`if(environment == 'production'){`)
42
+ indent(() => {
43
+ line(`database = {`);
44
+ indent(() => {
45
+ line(`adapter: 'mysql',`)
46
+ line(`host: 'localhost',`);
47
+ line(`user: 'root',`);
48
+ line(`password: '',`);
49
+ line(`database: \`${this.inflector.snakeify(name)}_\${environment}\``);
50
+ });
51
+ line(`};`);
52
+ });
53
+ line(`} else {`);
54
+ indent(() => {
55
+ line(`database = {`);
56
+ indent(() => {
57
+ line(`adapter: 'sqlite',`);
58
+ line(`filename: \`\${environment}.db\``)
59
+ });
60
+ line(`};`);
61
+ });
62
+ line(`}`);
63
+ line();
64
+ line(`let mail;`);
65
+ line(`if(environment == 'production'){`)
66
+ indent(() => {
67
+ line(`mail = {`);
68
+ indent(() => {
69
+ line(`adapter: 'smtp',`)
70
+ line(`host: "smtp.example.com",`)
71
+ line(`port: 465,`);
72
+ line(`secure: true, // use TLS`);
73
+ line(`auth: {`);
74
+ indent(() => {
75
+ line(`user: "username",`);
76
+ line(`pass: "password",`);
77
+ });
78
+ line(`}`);
79
+ });
80
+ line(`};`);
81
+ });
82
+ line(`} else {`);
83
+ indent(() => {
84
+ line(`mail = {`);
85
+ indent(() => {
86
+ line(`adapter: 'dummy'`);
87
+ });
88
+ line(`};`);
89
+ });
90
+ line(`}`);
91
+ line();
92
+ line(`export default {`);
93
+ indent(() => {
94
+ line(`database,`);
95
+ line(`mail`);
96
+ })
97
+ line(`};`);
98
+ });
35
99
 
36
100
  await generateFile(`lib/index.js`, () => {
37
101
  line();
@@ -0,0 +1,6 @@
1
+
2
+ export default {
3
+ async run(){
4
+ console.log(JSON.stringify(await this.config, null, 2));
5
+ }
6
+ };
@@ -1,33 +1,47 @@
1
1
 
2
+ import { existsSync } from 'fs';
3
+
2
4
  let createConfigPromise;
3
5
 
4
6
  export default {
5
7
  create(){
6
8
  return this.defer(() => {
7
9
  if(!createConfigPromise){
8
- createConfigPromise = (async () => ({
9
- database: await this.createDatabaseConfig()
10
- }))();
10
+ createConfigPromise = this.createConfig();
11
11
  }
12
12
  return createConfigPromise;
13
13
  });
14
14
  },
15
15
 
16
- async createDatabaseConfig(){
17
- const out = { adapter: 'sqlite' };
16
+ async createConfig(){
17
+ let out = {};
18
+ const candidateConfigFilePath = `${await this.project.rootPath}/pinstripe.config.js`;
19
+ if(existsSync(candidateConfigFilePath)){
20
+ out = await (await import(candidateConfigFilePath)).default;
21
+ }
18
22
 
19
- Object.keys(process.env).forEach(name => {
20
- const matches = name.match(/^DATABASE_(.+)$/);
21
- if(!matches) return;
22
- const normalizedName = matches[1].toLowerCase().split(/_+/).map((s, i) => i > 0 ? s[0].toUpperCase() + s.slice(1) : s).join('');
23
- out[normalizedName] = process.env[name];
24
- });
23
+ const {
24
+ database = { adapter: 'sqlite' },
25
+ mail = { adapter: 'dummy' }
26
+ } = out;
27
+
28
+ return {
29
+ ...out,
30
+ database: await this.normalizeDatabaseConfig(database),
31
+ mail
32
+ };
33
+ },
34
+
35
+ async normalizeDatabaseConfig(config){
36
+ const out = { ...config };
25
37
 
26
38
  const { adapter } = out;
27
39
 
40
+ const environment = process.env.NODE_ENV || 'development';
41
+
28
42
  if(adapter == 'sqlite'){
29
43
  return Object.assign({
30
- filename: `${await this.project.rootPath}/${process.env.NODE_ENV || 'development'}.db`
44
+ filename: `${await this.project.rootPath}/${environment}.db`
31
45
  }, out);
32
46
  }
33
47
 
@@ -35,6 +49,7 @@ export default {
35
49
  host: 'localhost',
36
50
  user: 'root',
37
51
  password: '',
52
+ database: `${await this.project.name}_${environment}`
38
53
  }, out);
39
54
  }
40
55
  };
@@ -6,31 +6,41 @@ export default {
6
6
  create(){
7
7
  return this.defer(async () => {
8
8
  const { mail: mailConfig = {} } = await this.config;
9
- const transport = createTransport(mailConfig);
10
- return (mailOptions = {}) => {
11
- if(process.env.NODE_ENV == 'production') return transport.sendMail(mailOptions);
12
- const { text, html, ...otherMailOptions } = mailOptions;
13
- console.log('');
14
- console.log('----------------------------------------');
15
- console.log(chalk.green('sendMail'));
16
- console.log('----------------------------------------');
17
- Object.keys(otherMailOptions).forEach(name => {
18
- console.log(`${name}: ${JSON.stringify(otherMailOptions[name])}`)
19
- });
20
- if(text){
21
- console.log('text:')
22
- console.log(text.replace(/(^|\n)/g, '$1 '));
23
- }
24
- if(html){
25
- console.log('html:')
26
- console.log(html.replace(/(^|\n)/g, '$1 '));
27
- }
28
- if(!Object.keys(mailOptions).length){
29
- console.log('No mail options have been provided.')
30
- }
31
- console.log('----------------------------------------');
32
- console.log('');
33
- }
9
+ const { adapter = 'dummy', ...adapterConfig } = mailConfig;
10
+ if(adapter == 'dummy') return this.createDummy();
11
+ if(adapter == 'smtp') return this.createSmtp(adapterConfig);
12
+ throw new Error(`No such mail adapter '${adapter}' exists.`);
34
13
  });
14
+ },
15
+
16
+ createDummy(){
17
+ return (mailOptions = {}) => {
18
+ const { text, html, ...otherMailOptions } = mailOptions;
19
+ console.log('');
20
+ console.log('----------------------------------------');
21
+ console.log(chalk.green('sendMail'));
22
+ console.log('----------------------------------------');
23
+ Object.keys(otherMailOptions).forEach(name => {
24
+ console.log(`${name}: ${JSON.stringify(otherMailOptions[name])}`)
25
+ });
26
+ if(text){
27
+ console.log('text:')
28
+ console.log(text.replace(/(^|\n)/g, '$1 '));
29
+ }
30
+ if(html){
31
+ console.log('html:')
32
+ console.log(html.replace(/(^|\n)/g, '$1 '));
33
+ }
34
+ if(!Object.keys(mailOptions).length){
35
+ console.log('No mail options have been provided.')
36
+ }
37
+ console.log('----------------------------------------');
38
+ console.log('');
39
+ }
40
+ },
41
+
42
+ createSmtp(config){
43
+ const transport = createTransport(config);
44
+ return (mailOptions = {}) => transport.sendMail(mailOptions);
35
45
  }
36
46
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "name": "pinstripe",
4
4
  "description": "A slick web framework for Node.js.",
5
- "version": "0.17.0",
5
+ "version": "0.18.0",
6
6
  "author": "Jody Salt",
7
7
  "license": "MIT",
8
8
  "exports": {
@@ -48,5 +48,5 @@
48
48
  "tmp": "^0.2.1",
49
49
  "unionfs": "^4.4.0"
50
50
  },
51
- "gitHead": "ccfaad0d841d6404b4eec608f8c6e04ce83dcec5"
51
+ "gitHead": "b638218b06f4cdea77f3ba0a4f9c082989528ccf"
52
52
  }