This repository was archived by the owner on Dec 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.js
127 lines (116 loc) · 4.01 KB
/
install.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
const fs = require('fs')
const chalk = require('chalk')
const inquirer = require('inquirer')
const { Printer, Executer, readJsonFile } = require('./scripts/lib')
const logo = fs.readFileSync('./assets/logo.art')
const config = require('./config.default')
const printer = new Printer()
async function install () {
// #region LOGO
printer.log(chalk.blue.bold(logo + chalk.underline('/ winwin-hexo-editor ') + '/'))
// #endregion
// #region Version
printer.printSection('Check Version')
const oldVersion = readJsonFile('./package.json').version
printer.info('Current Version ' + oldVersion)
if (oldVersion.indexOf('-') >= 0) {
printer.warn('This is a preview version!')
}
// #endregion
// #region Check Dependences
printer.printSection('Check Dependences')
const NODE = 'node'
const hasNode = await Executer.hasCommand(NODE + ' -v')
if (!hasNode) {
printer.error('Node is required! Please install node.js first')
process.exit(101)
} else {
printer.info(`${NODE} : ${chalk.green('pass')}`)
}
const NPM = 'npm'
const hasNPM = await Executer.hasCommand(NPM + ' -v')
if (!hasNPM) {
printer.error('npm not found, is there anything wrong with your node installation?')
process.exit(102)
} else {
printer.info(`${NPM} : ${chalk.green('pass')}`)
}
const YARN = 'yarn'
const hasYarn = await Executer.hasCommand(YARN + ' -v')
if (!hasYarn) {
printer.info(`${NODE} : ${chalk.red('pass')}`)
printer.warn('yarn not found, use npm instead.')
} else {
printer.info(`${YARN} : ${chalk.green('pass')}`)
}
// #endregion
// #region Fetch Submodules
printer.printSection('Fetch Submodules')
try {
printer.info('Fetching submodules updates')
await Executer.run('git submodule sync')
await Executer.run('git submodule update --init --recursive')
printer.success('Submodules updated')
} catch (err) {
const failed = new Printer('Installation Failed')
failed.error(err)
failed.error('error occured when fetching submodules')
process.exit(201)
}
// #endregion
// #region Install Dependences
printer.printSection('Install Dependences')
let cmd = 'yarn'
if (fs.existsSync('./package-lock.json') || !hasYarn) {
if (hasYarn) {
printer.info('package-lock.json found, use npm')
}
cmd = 'npm install'
}
try {
printer.info('Dependences installing')
await Executer.run(cmd)
printer.success('Dependences installed')
} catch (err) {
const failed = new Printer('Installation Failed')
failed.error(err)
failed.error(cmd, 'failed')
failed.error('error occured when install dependences')
process.exit(301)
}
// #endregion
// #region Configuation
printer.printSection('Configuation')
const answer = await inquirer
.prompt([{
name: 'port',
message: 'Which port do you like your hexo-editor running at?',
default: config.port || 5777,
validate (v) {
return !isNaN(v) || `number is required ${typeof v} given`
},
prefix: chalk.blue('?')
}])
fs.writeFileSync('./config.user.js', `module.exports = {\n port: ${answer.port}\n}\n`)
if (!fs.existsSync('./log')) {
fs.mkdirSync('./log')
}
// #endregion
// #region Finished
printer.clear()
printer.log(chalk.green.bold('Finished!'))
printer.log('Run ' + chalk.blue.bold('`npm start`') + ' to start with node')
printer.log('Run ' + chalk.blue.bold('`npm run prd`') + ' to start with pm2')
printer.log('Run ' + chalk.blue.bold('`npm run stop`') + ' to stop')
printer.log('Run ' + chalk.blue.bold('`npm run restart`') + ' to restart')
printer.log()
printer.log(chalk.green.bold('Remember to finish the following steps:'))
printer.log(chalk.blue.bold(' 1. (Re)Start your service manually!'))
printer.log(chalk.blue.bold(' 2. Finish installation via your browser'))
printer.log('Have fun :p')
printer.log(chalk.grey('For uninstall:'))
printer.log(chalk.grey('- Remove the following folder: ' + process.cwd()))
printer.log(chalk.grey('- Stop youre service manually.'))
// #endregion
}
install()