1
- const path = require ( 'path' ) ;
2
- const fs = require ( 'fs-extra' ) ;
3
- const { machineIdSync } = require ( 'node-machine-id' ) ;
4
- const chalk = require ( 'chalk' ) ;
5
- const track = require ( './track' ) ;
1
+ import os from 'os' ;
2
+ import { spawn } from 'cross-spawn' ;
3
+ import fs from 'fs-extra' ;
4
+ import path from 'path' ;
5
+ import chalk from 'chalk' ;
6
+ import { machineIdSync } from 'node-machine-id' ;
7
+
8
+ import { track } from './track' ;
9
+
10
+ export const executeCommand = ( command : string , args : string [ ] ) => {
11
+ const child = spawn ( command , args , { stdio : 'inherit' } ) ;
12
+
13
+ return new Promise ( ( resolve , reject ) => {
14
+ child . on ( 'close' , ( code : number ) => {
15
+ if ( code !== 0 ) {
16
+ reject ( new Error ( `${ command } ${ args . join ( ' ' ) } failed with exit code ${ code } ` ) ) ;
17
+ return ;
18
+ }
19
+
20
+ resolve ( ) ;
21
+ } ) ;
22
+ } ) ;
23
+ } ;
24
+
25
+ export const writePackageJson = async ( json : any ) => fs . writeJson ( 'package.json' , json , {
26
+ spaces : 2 ,
27
+ EOL : os . EOL
28
+ } ) ;
29
+
30
+ export const npmInstall = ( dependencies : string [ ] , isDev ?: boolean ) => executeCommand (
31
+ 'npm' , [ 'install' , isDev ? '--save-dev' : '--save' ] . concat ( dependencies )
32
+ ) ;
6
33
7
34
const anonymousId = machineIdSync ( ) ;
8
35
9
- const event = async ( name , props ) => {
36
+ export const event = async ( name : string , props : any ) => {
10
37
try {
11
38
await track ( {
12
39
event : name ,
@@ -18,9 +45,7 @@ const event = async (name, props) => {
18
45
}
19
46
} ;
20
47
21
- exports . event = event ;
22
-
23
- const displayError = async ( text , options = { } ) => {
48
+ export const displayError = async ( text : string , options = { } ) => {
24
49
console . error ( '' ) ;
25
50
console . error ( chalk . cyan ( 'Cube.js Error ---------------------------------------' ) ) ;
26
51
console . error ( '' ) ;
@@ -39,27 +64,23 @@ const displayError = async (text, options = {}) => {
39
64
process . exit ( 1 ) ;
40
65
} ;
41
66
42
- exports . displayError = displayError ;
43
-
44
- exports . requireFromPackage = async ( module ) => {
67
+ export const requireFromPackage = async ( moduleName : string ) => {
45
68
if (
46
- ! ( await fs . pathExists ( path . join ( process . cwd ( ) , 'node_modules' , module ) ) ) &&
69
+ ! ( await fs . pathExists ( path . join ( process . cwd ( ) , 'node_modules' , moduleName ) ) ) &&
47
70
! ( await fs . pathExists ( path . join ( process . cwd ( ) , 'node_modules' , `${ module } .js` ) ) )
48
71
) {
49
72
await displayError (
50
- `${ module } dependency not found. Please run this command from project directory.`
73
+ `${ moduleName } dependency not found. Please run this command from project directory.`
51
74
) ;
52
75
}
53
76
54
77
// eslint-disable-next-line global-require,import/no-dynamic-require
55
- return require ( path . join ( process . cwd ( ) , 'node_modules' , module ) ) ;
78
+ return require ( path . join ( process . cwd ( ) , 'node_modules' , moduleName ) ) ;
56
79
} ;
57
80
58
- const logStage = async ( stage , eventName , props ) => {
81
+ const logStage = async ( stage : string , eventName : string , props : any ) => {
59
82
console . log ( `- ${ stage } ` ) ;
60
83
if ( eventName ) {
61
84
await event ( eventName , props ) ;
62
85
}
63
86
} ;
64
-
65
- exports . logStage = logStage ;
0 commit comments