forked from stripe/stripe-billing-typographic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
146 lines (136 loc) · 3.92 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* setup.js
* Stripe Billing demo. Created by Michael Glukhovsky (@mglukhovsky).
*
* This setup script creates the Plans and Products required for Typographic.
*/
'use strict';
const path = require('path');
const fs = require('fs');
// Make sure Node.js packages are installed
let exists;
try {
exists = fs.accessSync(path.join(process.cwd(), 'node_modules'));
} catch (e) {
console.log(
'⚠️ Missing Node.js packages. Run `npm install` before running setup.'
);
process.exit(0);
}
// Make sure .env file exists
try {
const exists = fs.accessSync(path.join(process.cwd(), '.env'));
} catch (e) {
console.log(
'⚠️ Missing `.env` file. Copy `.env.example` and include your Stripe credentials.'
);
process.exit(0);
}
const config = require('./config');
const stripe = require('stripe')(config.stripe.secretKey);
const Plan = require('./server/models/Plan');
const knex = require('./server/database');
const {exec} = require('child_process');
module.exports = {
running: false,
async checkTables() {
for (let table of ['accounts', 'customers', 'subscriptions', 'plans']) {
const hasTable = await knex.schema.hasTable(table);
if (!hasTable) {
return false;
}
}
return true;
},
async run() {
if (this.running) {
console.log('⚠️ Setup already in progress.');
return;
}
this.running = true;
try {
// Build our Vue frontend with Rollup
await rollup();
// Recreate our database tables
await dropTables();
await createTables();
// Create the required plans on our Stripe account (and our database)
await Plan.setupPlans();
console.log('Typographic is ready: run `npm start` to start the server.');
} catch (e) {
console.log(e);
}
this.running = false;
process.exit(0);
},
};
// Runs when this file is called directly from the command line (i.e.
// it's the main module)
if (require.main === module) {
module.exports.run();
}
// Use Rollup to bundle our frontend Vue components into a single JS file
async function rollup() {
console.log('🌍 Building Vue frontend');
await exec('./node_modules/rollup/bin/rollup -c');
}
// Drop all Typographic tables from the database
async function dropTables() {
console.log('🔻 Dropping existing database tables');
await Promise.all([
knex.schema.dropTableIfExists('accounts'),
knex.schema.dropTableIfExists('customers'),
knex.schema.dropTableIfExists('subscriptions'),
knex.schema.dropTableIfExists('plans'),
]);
}
// Create tables on our database
async function createTables() {
console.log('✨ Creating database tables for Typographic');
await Promise.all([
knex.schema.createTable('accounts', t => {
t.increments();
t
.string('email')
.unique()
.notNullable();
t.integer('createdAt');
t.string('password').notNullable();
}),
knex.schema.createTable('customers', t => {
t.increments();
t.string('stripeId');
t.string('accountId');
t.string('email');
t.integer('createdAt');
t.string('fonts');
t.string('paymentMethodId');
t.string('paymentMethodLast4');
t.string('paymentMethodBrand');
}),
knex.schema.createTable('subscriptions', t => {
t.increments();
t.string('stripeId');
t.string('customerId');
t.integer('createdAt');
t.string('status');
t.string('plan');
t.string('collectionMethod');
t.integer('currentPeriodEnd');
t.integer('currentPeriodStart');
t.float('meteredUsage').unsigned();
t.string('stripeMonthlySubId');
t.string('stripeMeteredSubId');
}),
knex.schema.createTable('plans', t => {
t.increments();
t.string('stripeId');
t.string('name');
t.string('nickname');
t.string('type');
t.integer('amount');
t.float('included');
}),
]);
console.log('✅ Database is ready');
}