1 | const fs = require('fs')
|
2 | const path = require('path')
|
3 | const prompt = require('prompt-promise')
|
4 | const randomString = require('./lib/random-string')
|
5 | const root = path.join(__dirname, '..')
|
6 | const pkg = require(path.join(root, 'package.json'))
|
7 |
|
8 | const createFolder = require('./lib/create-folder')
|
9 |
|
10 | const { promisify } = require('util');
|
11 | const exec = promisify(require('child_process').exec)
|
12 |
|
13 | async function initInsights ({ dev = false, login = undefined }) {
|
14 | console.log(`Welcome to Insights v${pkg.version}!`)
|
15 | console.log('')
|
16 |
|
17 | const configFolder = path.join(process.cwd(), '.insights')
|
18 |
|
19 | if (fs.existsSync(configFolder)) {
|
20 | console.error('!! Fatal Error! The folder ".insights" already exists. ')
|
21 | console.error(' Could not complete init.')
|
22 | process.exit(1)
|
23 | }
|
24 |
|
25 | console.log('This script will create a folder ".insights", which will be used to store')
|
26 | console.log('config data. Please add it to .gitignore if you\'re running this script')
|
27 | console.log('inside a project folder!')
|
28 | console.log('')
|
29 |
|
30 | if (login === undefined) {
|
31 | let repeat = true
|
32 |
|
33 | console.log('Do you wish to setup Insights in "login" mode, requiring authentication to')
|
34 | console.log('access the interface or in standalone "loginless" mode, which is practical')
|
35 | console.log('only when running in localhost.')
|
36 | console.log('')
|
37 |
|
38 | while (repeat) {
|
39 | const answer = (await prompt('Setup Insights in "login" mode? (Y/n): ')).trim()
|
40 | const letter = answer.toLowerCase()[0]
|
41 |
|
42 | if (answer === '' || letter === 'y' || letter === 'n') {
|
43 | repeat = false
|
44 | login = letter !== 'n'
|
45 | } else {
|
46 | console.error('!! Please answer either "y" or "n" or hit CTRL+C to cancel')
|
47 | }
|
48 | console.log('')
|
49 | }
|
50 | }
|
51 |
|
52 | createFolder(configFolder)
|
53 | process.env.NODE_CONFIG_DIR = configFolder
|
54 |
|
55 | console.log('')
|
56 |
|
57 | if (login) {
|
58 | console.log('In order to log in to Insights you must create at least one user account.')
|
59 | console.log('Run "insights createsuperuser" or use the web interface to create more later.')
|
60 | console.log('')
|
61 | }
|
62 |
|
63 | const secretKey = randomString(64)
|
64 |
|
65 | if (dev) {
|
66 | let developmentTemplate = require('./templates/development.json')
|
67 | developmentTemplate.authentication.secret = secretKey
|
68 | developmentTemplate.authentication.authStrategies = ['jwt', login ? 'local' : 'noLogin']
|
69 | fs.writeFileSync(path.join(configFolder, 'development.json'), JSON.stringify(developmentTemplate, null, 2))
|
70 | }
|
71 |
|
72 | let productionTemplate = require('./templates/production.json')
|
73 | productionTemplate.authentication.secret = secretKey
|
74 | productionTemplate.authentication.authStrategies = ['jwt', login ? 'local' : 'noLogin']
|
75 | fs.writeFileSync(path.join(configFolder, 'production.json'), JSON.stringify(productionTemplate, null, 2))
|
76 |
|
77 | const defaultTemplate = require('./templates/default.json')
|
78 | fs.writeFileSync(path.join(configFolder, 'default.json'), JSON.stringify(defaultTemplate, null, 2))
|
79 |
|
80 | if (!login) {
|
81 | const user = (await exec(`whoami`)).stdout.trim() || 'anonymous'
|
82 | const host = (await exec(`hostname`)).stdout.trim() || 'localhost'
|
83 | process.env.INSIGHTS_SUPERUSER_EMAIL = `${user}@${host}`
|
84 | }
|
85 |
|
86 | const createSuperuser = require('./create-superuser')
|
87 | await createSuperuser({ login, exit: false })
|
88 |
|
89 | console.log('')
|
90 | console.log('Success! Run "insights start" to start the server!')
|
91 |
|
92 | process.exit(0)
|
93 | }
|
94 |
|
95 | module.exports = initInsights
|