forked from kreteshq/kretes
/
init.js
220 lines (193 loc) · 6.02 KB
/
init.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2016 Zaiste & contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs-extra'));
const { join, resolve, delimiter } = require('path');
const { spawn } = require('child_process');
const cwd = process.cwd();
async function init({ dir, dbengine }) {
const themeDir = join(resolve(__dirname, '..'), 'template');
const name = dir.replace(/-/g, '_');
try {
console.log(`Initialising '${dir}'...`);
console.log(`Database engine: '${dbengine}'`);
// dynamic files
const databaseConfig = join(cwd, dir, 'config', 'database.json');
await fs.outputJson(databaseConfig, generateDatabaseConfig(name), { spaces: 2 });
const packageJSON = join(cwd, dir, 'package.json');
await fs.outputJson(packageJSON, generatePackageJSON(dir, dbengine), { spaces: 2 });
const sql = join(cwd, dir, 'db', 'tasks.sql');
await fs.outputFile(sql, generateSQL(name, dbengine));
await fs.ensureFile(join(cwd, dir, 'db', 'development.sqlite3'));
await fs.ensureFile(join(cwd, dir, 'db', 'test.sqlite3'));
await fs.ensureFile(join(cwd, dir, 'db', 'production.sqlite3'));
// static files
await fs.copyAsync(themeDir, join(cwd, dir));
const isYarnInstalled = await hasbin('yarn');
if (isYarnInstalled) {
spawn('yarn', { cwd: dir, stdio: 'inherit' });
} else {
console.error('\nError: `yarn` is not installed. Please check their installation guide at https://yarnpkg.com/en/docs/install to learn how to install `yarn` on your platform');
}
} catch (error) {
console.log('error: ' + error.message);
}
}
async function hasbin(name) {
return await Promise.resolve(process.env.PATH.split(delimiter).map(_ => join(_, name)))
.map(exists)
.reduce((a, b) => a || b);
}
async function exists(pathname) {
try {
await fs.accessAsync(pathname);
return true;
} catch (error) {
return false;
}
}
module.exports = {
handler: init,
builder: _ => _
.option('dbengine', { alias: 'd', default: 'sqlite3' })
.default('dir', '.')
};
// TODO: generalize this function as ~ `generate(...)`
function generateDatabaseConfig(database, dbengine) {
switch (dbengine) {
case 'postgresql':
return {
development: {
client: 'pg',
host: 'localhost', port: 5432, database
},
test: {
client: 'pg',
host: 'localhost', 'port': 5432, database
},
production: {
client: 'pg',
host: 'localhost', port: 5432, database
}
};
default:
return {
development: {
client: 'sqlite3',
filename: './db/development.sqlite3'
},
test: {
client: 'sqlite3',
filename: './db/test.sqlite3'
},
production: {
client: 'sqlite3',
filename: './db/production.sqlite3'
},
};
}
}
function generatePackageJSON(name, dbengine) {
const dependencies = {
'apollo-cache-inmemory': '^1.2.1',
'apollo-client': '^2.3.1',
'apollo-link-http': '^1.5.4',
'axios': '^0.18.0',
'graphql': '^0.13.2',
'graphql-tag': '^2.9.2',
'huncwot': '^0.21.0',
'knex': '^0.14.6',
'sqlite3': '^4.0.0',
'validate': '^4.4.1',
'vue': '^2.5.16',
'vue-apollo': '^3.0.0-beta.10',
'vue-router': '^3.0.1',
'vuex': '^3.0.1'
};
const devDependencies = {
'@types/node': '^10.0.8',
'babel-core': '^6.26.3',
'babel-loader': '^7.1.4',
'babel-preset-env': '^1.7.0',
'babel-preset-stage-3': '^6.24.1',
'concurrently': '^3.5.1',
'cross-env': '^5.1.5',
'css-loader': '^0.28.11',
'file-loader': '^1.1.11',
'friendly-errors-webpack-plugin': '^1.7.0',
'html-webpack-plugin': '^3.2.0',
'node-sass': '^4.9.0',
'nodemon': '^1.17.4',
'sass-loader': '^7.0.1',
'typescript': '^2.8.3',
'vue-loader': '^15.0.10',
'vue-style-loader': '^4.1.0',
'vue-template-compiler': '^2.5.16',
'webpack': '4.8.3',
'webpack-cli': '^2.1.3',
'webpack-dev-server': '^3.1.4',
'webpack-merge': '^4.1.2'
};
switch (dbengine) {
case 'postgresql':
Object.assign(dependencies, { 'pg': 'latest' });
break;
default:
Object.assign(dependencies, { 'sqlite3': 'latest' });
break;
}
const browserslist = [
'> 1%',
'last 2 versions',
'not ie <= 8'
];
const scripts = {
'client': 'webpack-dev-server --mode development --open --config config/webpack.dev.js',
'build': 'webpack --progress --hide-modules --config config/webpack.prod.js',
'server': 'huncwot server',
'watch-ts': 'tsc -w',
'start': 'concurrently -k -p "[{name}]" -n "Client,TypeScript,Server" -c "yellow.bold,cyan.bold,green.bold" "yarn run client" "yarn run watch-ts" "yarn run server"'
};
return { name, version: '0.0.1', scripts, dependencies, devDependencies, browserslist };
}
function generateSQL(name, dbengine) {
switch (dbengine) {
case 'postgresql':
return `DROP DATABASE IF EXISTS ${name}_dev;
CREATE DATABASE ${name}_dev;
\\c ${name}_dev;
CREATE TABLE tasks (
ID SERIAL PRIMARY KEY,
name VARCHAR,
done BOOLEAN
);
INSERT INTO tasks (name, done)
VALUES
('Share the love about Huncwot', false),
('Build a fantastic web application', false),
('Give back to the community', false);
`;
case 'sqlite3':
return `CREATE TABLE tasks (
id INTEGER PRIMARY KEY,
name VARCHAR,
done INTEGER
);
INSERT INTO tasks (name, done)
VALUES
('Share the love about Huncwot', 0),
('Build a fantastic web application', 0),
('Give back to the community', 0);
`;
}
}