1
+ #!/usr/bin/env node
2
+
1
3
import chalk from 'chalk' ;
2
4
import commandExists from 'command-exists' ;
3
5
import execa from 'execa' ;
4
6
import fsExtra from 'fs-extra' ;
5
- import minimist from 'minimist' ;
6
7
import path from 'path' ;
7
8
8
9
const { copyFileSync, ensureFileSync, existsSync, removeSync } = fsExtra ;
9
10
10
11
const docsPath = '../ember-api-docs-data' ;
11
12
12
- const argv = minimist ( process . argv . slice ( 2 ) ) ;
13
-
14
- const { project, version, install } = argv ;
13
+ import { program , Option , InvalidArgumentError } from 'commander' ;
15
14
16
- const exit = function exit ( ) {
15
+ function exit ( ) {
17
16
console . log ( ...arguments ) ;
18
17
process . exit ( 1 ) ;
19
- } ;
18
+ }
19
+
20
+ function semverVersion ( value ) {
21
+ if ( ! / ^ \d + \. \d + \. \d + $ / . test ( value ) ) {
22
+ throw new InvalidArgumentError ( 'Not a correctly defined semver version i.e. major.minor.patch' ) ;
23
+ }
24
+ return value ;
25
+ }
26
+
27
+ program
28
+ . addOption (
29
+ new Option ( '-p, --project <project>' , 'the project that you want to run this for' )
30
+ . choices ( [ 'ember' , 'ember-data' ] )
31
+ . makeOptionMandatory ( ) ,
32
+ )
33
+ . requiredOption ( '-v, --version <version>' , 'project version' , semverVersion ) ;
34
+
35
+ program . parse ( ) ;
36
+
37
+ const options = program . opts ( ) ;
38
+
39
+ const { project, version } = options ;
20
40
21
41
async function runCmd ( cmd , path , args = [ ] ) {
22
42
console . log ( chalk . underline ( `Running '${ chalk . green ( cmd ) } ' in ${ path } ` ) ) ;
@@ -31,80 +51,55 @@ async function runCmd(cmd, path, args = []) {
31
51
console . log ( executedCmd . stdout + '\n' ) ;
32
52
}
33
53
34
- ( async ( ) => {
35
- if ( ! project || ! version ) {
36
- exit (
37
- chalk . red ( 'Both project and version args are required.\n' ) ,
38
- chalk . yellow ( ' e.g., yarn gen --project ember --version 3.10.1' ) ,
39
- ) ;
40
- }
54
+ try {
55
+ await commandExists ( 'yarn' ) ;
56
+ } catch ( e ) {
57
+ exit ( chalk . red ( 'We need yarn installed globally for this script to work' ) ) ;
58
+ }
59
+
60
+ let emberProjectPath = path . join ( '../' , 'ember.js' ) ;
61
+ let emberDataProjectPath = path . join ( '../' , 'data' ) ;
41
62
42
- if ( ! [ 'ember' , 'ember-data' ] . includes ( project ) ) {
43
- exit ( chalk . red ( `Project has to be either 'ember' or 'ember-data'. (was given ${ project } )\n` ) ) ;
63
+ let checkIfProjectDirExists = dirPath => {
64
+ if ( ! existsSync ( dirPath ) ) {
65
+ exit ( chalk . yellow ( `Please checkout the ${ project } project at ${ dirPath } ` ) ) ;
44
66
}
67
+ } ;
68
+
69
+ let buildDocs = async projDirPath => {
70
+ checkIfProjectDirExists ( projDirPath ) ;
45
71
46
- try {
47
- await commandExists ( ' yarn') ;
48
- } catch ( e ) {
49
- exit ( chalk . red ( 'We need yarn installed globally for this script to work' ) ) ;
72
+ if ( project === 'ember' ) {
73
+ await runCmd ( 'volta' , projDirPath , [ 'run' , ' yarn'] ) ;
74
+ } else {
75
+ await runCmd ( 'corepack' , projDirPath , [ 'pnpm' , 'install' ] ) ;
50
76
}
51
77
52
- let emberProjectPath = path . join ( '../' , 'ember.js' ) ;
53
- let emberDataProjectPath = path . join ( '../' , 'data' ) ;
54
-
55
- let checkIfProjectDirExists = dirPath => {
56
- if ( ! existsSync ( dirPath ) ) {
57
- exit ( chalk . yellow ( `Please checkout the ${ project } project at ${ dirPath } ` ) ) ;
58
- }
59
- } ;
60
-
61
- let buildDocs = async projDirPath => {
62
- checkIfProjectDirExists ( projDirPath ) ;
63
-
64
- if ( project === 'ember' ) {
65
- await runCmd ( 'volta' , projDirPath , [ 'run' , 'yarn' ] ) ;
66
- } else {
67
- await runCmd ( 'corepack' , projDirPath , [ 'pnpm' , 'install' ] ) ;
68
- }
69
-
70
- if ( install ) {
71
- await runCmd ( project === 'ember' ? 'yarn' : 'pnpm install' , projDirPath ) ;
72
- console . log ( '\n\n' ) ;
73
- }
74
-
75
- await runCmd (
76
- project === 'ember' ? 'volta run yarn docs' : 'corepack pnpm run build:docs' ,
77
- projDirPath ,
78
- ) ;
79
-
80
- let destination = `${ docsPath } /s3-docs/v${ version } /${ project } -docs.json` ;
81
- ensureFileSync ( destination ) ;
82
- const projYuiDocFile = destination ;
83
- removeSync ( projYuiDocFile ) ;
84
- removeSync ( `${ docsPath } /json-docs/${ project } /${ version } ` ) ;
85
-
86
- const yuiDocFile = path . join (
87
- projDirPath ,
88
- project === 'ember' ? 'docs/data.json' : 'packages/-ember-data/dist/docs/data.json' ,
89
- ) ;
90
- copyFileSync ( yuiDocFile , projYuiDocFile ) ;
91
- } ;
92
-
93
- let dirMap = {
94
- ember : emberProjectPath ,
95
- 'ember-data' : emberDataProjectPath ,
96
- } ;
97
-
98
- await buildDocs ( dirMap [ project ] ) ;
99
-
100
- await execa ( 'volta' , [
101
- 'run' ,
102
- 'yarn' ,
103
- 'start' ,
104
- '--project' ,
105
- project ,
106
- '--version' ,
107
- version ,
108
- '--no-sync' ,
109
- ] ) . stdout . pipe ( process . stdout ) ;
110
- } ) ( ) ;
78
+ await runCmd (
79
+ project === 'ember' ? 'volta run yarn docs' : 'corepack pnpm run build:docs' ,
80
+ projDirPath ,
81
+ ) ;
82
+
83
+ let destination = `${ docsPath } /s3-docs/v${ version } /${ project } -docs.json` ;
84
+ ensureFileSync ( destination ) ;
85
+ const projYuiDocFile = destination ;
86
+ removeSync ( projYuiDocFile ) ;
87
+ removeSync ( `${ docsPath } /json-docs/${ project } /${ version } ` ) ;
88
+
89
+ const yuiDocFile = path . join (
90
+ projDirPath ,
91
+ project === 'ember' ? 'docs/data.json' : 'packages/-ember-data/dist/docs/data.json' ,
92
+ ) ;
93
+ copyFileSync ( yuiDocFile , projYuiDocFile ) ;
94
+ } ;
95
+
96
+ let dirMap = {
97
+ ember : emberProjectPath ,
98
+ 'ember-data' : emberDataProjectPath ,
99
+ } ;
100
+
101
+ await buildDocs ( dirMap [ project ] ) ;
102
+
103
+ await execa ( 'volta' , [ 'run' , 'yarn' , 'start' , '--projects' , project , '--version' , version ] , {
104
+ stdio : 'inherit' ,
105
+ } ) ;
0 commit comments