@@ -4,83 +4,92 @@ const fs = require('fs')
4
4
const path = require ( 'path' )
5
5
const walk = require ( 'walk-sync' )
6
6
const matter = require ( 'gray-matter' )
7
- const readlineSync = require ( 'readline-sync' )
7
+ const program = require ( 'commander' )
8
+ const { indexOf, nth } = require ( 'lodash' )
8
9
const removeLiquidStatements = require ( '../lib/remove-liquid-statements' )
9
10
const removeDeprecatedFrontmatter = require ( '../lib/remove-deprecated-frontmatter' )
10
- const versionToDeprecate = require ( '../lib/enterprise-server-releases' ) . deprecated [ 0 ]
11
- const nextOldestVersion = require ( '../lib/enterprise-server-releases' ) . oldestSupported
12
- const devCheckout = process . argv [ 2 ]
13
- const prompt = `This script will run in the current checkout of help.github.com.
14
- Is that what you want? Press Y to continue, or enter any other key to cancel: `
11
+ const enterpriseServerReleases = require ( '../lib/enterprise-server-releases' )
12
+ const contentPath = path . join ( __dirname , '../content' )
13
+ const dataPath = path . join ( __dirname , '../data' )
14
+ const removeUnusedAssetsScript = ' script/remove-unused-assets'
15
+ const elseifRegex = / { - ? % e l s i f /
15
16
16
17
// [start-readme]
17
18
//
18
19
// Run this script after an Enterprise deprecation to remove Liquid statements and frontmatter that contain the deprecated Enterprise version.
19
20
// See the Enterprise deprecation issue template for instructions.
20
21
//
21
- // You can run this script on either the help docs or the developer docs. To run it on the help docs, enter:
22
- //
23
- // `script/remove-deprecated-enterprise-version-markup.js`
24
- //
25
- // To run it on the developer docs, provide a path to your developer docs checkout as an argument. You can use a tilde to represent your home directory. For example:
26
- //
27
- // `script/remove-deprecated-enterprise-version-markup.js ~/Desktop/internal-developer.github.com/`
28
- //
29
22
// [end-readme]
30
23
31
- let contentDir = path . join ( __dirname , '../content' )
32
- let dataDir = path . join ( __dirname , '../data' )
33
-
34
- const elseifRegex = / { - ? % e l s i f /
24
+ program
25
+ . description ( 'Remove Liquid conditionals and update versions frontmatter for a given Enterprise Server release.' )
26
+ . option ( '-r, --release <NUMBER>' , 'Enterprise Server release number. Example: 2.19' )
27
+ . parse ( process . argv )
28
+
29
+ // verify CLI options
30
+ if ( ! program . release ) {
31
+ console . log ( program . description ( ) + '\n' )
32
+ program . options . forEach ( opt => {
33
+ console . log ( opt . flags )
34
+ console . log ( opt . description + '\n' )
35
+ } )
36
+ process . exit ( 1 )
37
+ }
35
38
36
- // determine whether to run the script on help docs or developer docs
37
- if ( devCheckout ) {
38
- try {
39
- process . chdir ( devCheckout )
40
- console . log ( 'OK, the script will run in ' + devCheckout )
41
- contentDir = path . join ( devCheckout , 'content' )
42
- dataDir = path . join ( devCheckout , 'data' )
43
- } catch ( err ) {
44
- console . log ( 'No such directory! ' + devCheckout )
45
- }
46
- } else {
47
- const answer = readlineSync . question ( prompt )
48
-
49
- if ( ! answer . match ( / ^ Y $ / mi) ) {
50
- console . log ( 'Exiting!' )
51
- process . exit ( )
52
- }
39
+ if ( ! enterpriseServerReleases . all . includes ( program . release ) ) {
40
+ console . log ( `You specified ${ program . release } ! Please specify a supported or deprecated release number from lib/enterprise-server-releases.js` )
41
+ process . exit ( 1 )
53
42
}
54
43
44
+ const versionToDeprecate = `enterprise-server@${ program . release } `
45
+ const currentIndex = indexOf ( enterpriseServerReleases . all , program . release )
46
+ const nextOldestRelease = nth ( enterpriseServerReleases . all , currentIndex - 1 )
47
+ const nextOldestVersion = `enterprise-server@${ nextOldestRelease } `
48
+
49
+ console . log ( `Deprecating ${ versionToDeprecate } !\n` )
50
+ console . log ( `Next oldest version: ${ nextOldestVersion } \n` )
51
+
55
52
// gather content and data files
56
- const contentFiles = walk ( contentDir , { includeBasePath : true } )
57
- . filter ( relativePath => relativePath . endsWith ( '.md' ) && ! relativePath . match ( / R E A D M E / i) )
53
+ const contentFiles = walk ( contentPath , { includeBasePath : true , directories : false } )
54
+ . filter ( file => file . endsWith ( '.md' ) )
55
+ . filter ( file => ! ( file . endsWith ( 'README.md' ) || file === 'LICENSE' ) )
58
56
59
- const dataFiles = walk ( dataDir , { includeBasePath : true } )
60
- . filter ( relativePath => relativePath . endsWith ( '.yml ') || relativePath . endsWith ( '.md ') )
61
- . filter ( relativePath => ! relativePath . includes ( '/graphql/ ') )
57
+ const dataFiles = walk ( dataPath , { includeBasePath : true , directories : false } )
58
+ . filter ( file => file . includes ( 'data/reusables ') || file . includes ( 'data/variables ') )
59
+ . filter ( file => ! file . endsWith ( 'README.md ') )
62
60
63
- const files = contentFiles . concat ( dataFiles )
61
+ const allFiles = contentFiles . concat ( dataFiles )
64
62
65
63
main ( )
66
- console . log ( `Removed ${ versionToDeprecate } markup from content and data files! Review and run script/test.` )
64
+ console . log ( `\nRunning ${ removeUnusedAssetsScript } ...` )
65
+ require ( `../${ removeUnusedAssetsScript } ` )
66
+
67
+ function printElseIfFoundWarning ( location ) {
68
+ console . log ( `${ location } has an 'elsif' condition! Resolve all elsifs by hand, then rerun the script.` )
69
+ }
67
70
68
71
function main ( ) {
69
- files . forEach ( file => {
72
+ allFiles . forEach ( file => {
70
73
const oldContents = fs . readFileSync ( file , 'utf8' )
71
74
const { content, data } = matter ( oldContents )
72
75
73
- // can't safely handle elseifs programmatically, too many possible outcomes
74
- // (only intro and title frontmatter are likely to contain elseif tags)
75
- if ( content . match ( elseifRegex ) || ( data . intro && data . intro . match ( elseifRegex ) ) || ( data . title && data . title . match ( elseifRegex ) ) ) {
76
- console . log ( `${ file } has an 'elsif' condition! Resolve all elsifs by hand, then rerun the script.` )
76
+ // we can't safely handle elseifs programmatically, too many possible outcomes
77
+ if ( elseifRegex . test ( content ) ) {
78
+ printElseIfFoundWarning ( `content in ${ file } ` )
77
79
process . exit ( )
78
80
}
79
81
80
- // update frontmatter data (i.e., productVersions field)
81
- const newData = removeDeprecatedFrontmatter ( data , devCheckout , versionToDeprecate , nextOldestVersion )
82
+ Object . keys ( data ) . forEach ( key => {
83
+ if ( elseifRegex . test ( data [ key ] ) ) {
84
+ printElseIfFoundWarning ( `frontmatter '${ key } ' in ${ file } ` )
85
+ process . exit ( )
86
+ }
87
+ } )
88
+
89
+ // update frontmatter versions prop
90
+ removeDeprecatedFrontmatter ( file , data . versions , versionToDeprecate , nextOldestVersion )
82
91
83
- // update liquid statements in body content
92
+ // update liquid statements in content and data
84
93
const newContent = removeLiquidStatements ( content , versionToDeprecate , nextOldestVersion )
85
94
86
95
// make sure any intro fields that exist and are empty return an empty string, not null
@@ -89,8 +98,10 @@ function main () {
89
98
}
90
99
91
100
// put it all back together
92
- const newContents = matter . stringify ( newContent , newData , { lineWidth : 10000 } )
101
+ const newContents = matter . stringify ( newContent , data , { lineWidth : 10000 } )
93
102
94
103
fs . writeFileSync ( file , newContents )
95
104
} )
105
+
106
+ console . log ( `Removed ${ versionToDeprecate } markup from content and data files! Review and run script/test.` )
96
107
}
0 commit comments