22import { Command } from 'commander' ;
33import fs from 'fs' ;
44import path from 'path' ;
5- import shell from 'shelljs' ;
65
7- import backupBootstrap from '@/bootstrap/backup.js' ;
8- import repairBootstrap from '@/bootstrap/repair.js' ;
9- import setupBootstrap from '@/bootstrap/setup.js' ;
10- import templateBootstrap from '@/bootstrap/template.js' ;
6+ import BackupBootstrap from '@/bootstrap/backup.js' ;
7+ import RepairBootstrap from '@/bootstrap/repair.js' ;
8+ import SetupBootstrap from '@/bootstrap/setup.js' ;
9+ import TemplateBootstrap from '@/bootstrap/template.js' ;
1110import DatabaseService from '@/service/database' ;
11+ import { isDirectory , isFile } from '@/util/path' ;
12+ import { isMySQLInstalled , isPHPInstalled } from '@/util/unix' ;
1213
1314const program = new Command ( ) ;
1415
@@ -18,7 +19,7 @@ program
1819 . version ( '1.0.0' , '-v, --version' ) ;
1920
2021program . command ( 'template' ) . action ( async function ( ) {
21- await templateBootstrap . handler ( ) ;
22+ await TemplateBootstrap . handler ( ) ;
2223} ) ;
2324
2425program
@@ -33,10 +34,10 @@ program
3334 . requiredOption ( '-d, --directory <directory>' , 'root directory for website' , '.' )
3435 . action ( async ( options ) => {
3536 const { template : templateFilePath , directory, host, port, username, password, dev } = options ;
36- if ( ! fs . existsSync ( templateFilePath ) ) {
37- throw new Error ( `File not exists in ${ templateFilePath } .` ) ;
37+ if ( ! isFile ( templateFilePath ) ) {
38+ throw new Error ( 'Please provide the correct file path.' ) ;
3839 }
39- if ( ! shell . which ( 'php' ) ) {
40+ if ( ! isPHPInstalled ( ) ) {
4041 throw new Error ( 'The host need to install php to make it works.' ) ;
4142 }
4243 if ( ! dev ) {
@@ -45,7 +46,7 @@ program
4546 }
4647 if (
4748 ( host === undefined || host === 'localhost' || host === '127.0.0.1' ) &&
48- ! shell . which ( 'mysqld' )
49+ ! isMySQLInstalled ( )
4950 ) {
5051 throw new Error ( 'Using localhost or 127.0.0.1 must be installed MySQL database on host.' ) ;
5152 }
@@ -55,7 +56,7 @@ program
5556 return ;
5657 }
5758 const template = JSON . parse ( fs . readFileSync ( templateFilePath , 'utf8' ) ) ;
58- await setupBootstrap . handler (
59+ await SetupBootstrap . handler (
5960 path . isAbsolute ( directory ) ? directory : path . join ( process . cwd ( ) , directory ) ,
6061 template ,
6162 dev ,
@@ -74,23 +75,23 @@ program
7475 . requiredOption ( '-d, --directory <directory>' , 'root directory for website' , '.' )
7576 . action ( async ( options ) => {
7677 const { domain, directory, host, port, username, password } = options ;
77- if ( ! shell . which ( 'php' ) ) {
78+ if ( ! isPHPInstalled ( ) ) {
7879 throw new Error ( 'The host need to install php to make it works.' ) ;
7980 }
8081 if ( process . getuid && process . getuid ( ) !== 0 ) {
8182 throw new Error ( 'The process needs root permission when production mode.' ) ;
8283 }
8384 if (
8485 ( host === undefined || host === 'localhost' || host === '127.0.0.1' ) &&
85- ! shell . which ( 'mysqld' )
86+ ! isMySQLInstalled ( )
8687 ) {
8788 throw new Error ( 'Using localhost or 127.0.0.1 must be installed MySQL database on host.' ) ;
8889 }
8990 const accessible = await new DatabaseService ( { host, port, username, password } ) . isAccessible ( ) ;
9091 if ( accessible ) {
9192 return ;
9293 }
93- await repairBootstrap . handler ( directory , domain , { host, port, username, password } ) ;
94+ await RepairBootstrap . handler ( directory , domain , { host, port, username, password } ) ;
9495 } ) ;
9596
9697program
@@ -103,13 +104,13 @@ program
103104 . requiredOption ( '-k, --secret-access-key <secretAccessKey>' , 'AWS s3 bucket secret access key' )
104105 . action ( async ( options ) => {
105106 const { username, password, directory, accessKeyId, secretAccessKey } = options ;
106- if ( ! fs . existsSync ( directory ) || ! fs . lstatSync ( directory ) . isDirectory ( ) ) {
107+ if ( ! isDirectory ( directory ) ) {
107108 throw new Error ( 'Root directory not exists.' ) ;
108109 }
109110 if ( directory === '/' ) {
110111 throw new Error ( 'Cannot archive the root directory.' ) ;
111112 }
112- await backupBootstrap . handler ( directory , username , password , accessKeyId , secretAccessKey ) ;
113+ await BackupBootstrap . handler ( directory , username , password , accessKeyId , secretAccessKey ) ;
113114 } ) ;
114115
115116program . parseAsync ( ) ;
0 commit comments