@@ -15,14 +15,16 @@ module.exports = function(grunt) {
1515 return {
1616 ...fileItem . item ,
1717 __index__ : fileItem . index ,
18- __path__ : unix ( fileItem . file . path )
18+ __path__ : unix ( fileItem . file . path ) ,
19+ __jsonext__ : fileItem . file . jsonext
1920 } ;
2021 }
2122
2223 function undressPathIndex ( object ) {
2324 const clone = { ...object } ;
2425 delete clone . __index__ ;
2526 delete clone . __path__ ;
27+ delete clone . __jsonext__ ;
2628 return clone ;
2729 }
2830
@@ -35,17 +37,48 @@ module.exports = function(grunt) {
3537 const migrations = await import ( 'adapt-migrations' ) ;
3638 const logger = migrations . Logger . getInstance ( ) ;
3739 const cwd = process . cwd ( ) ;
38- const outputPath = path . join ( cwd , './migrations/' ) ;
40+ const tempPath = path . join ( cwd , './migrations/' ) ;
3941 const cache = new migrations . CacheManager ( ) ;
4042 const cachePath = await cache . getCachePath ( {
4143 outputPath : buildConfig . outputdir ,
42- tempPath : outputPath
44+ tempPath
4345 } ) ;
4446
45- const framework = Helpers . getFramework ( ) ;
46- logger . debug ( `Using ${ framework . useOutputData ? framework . outputPath : framework . sourcePath } folder for course data...` ) ;
47+ if ( mode === 'capture' ) {
48+ const capturePath = grunt . option ( 'capturedir' ) || tempPath ;
49+ if ( ! fs . existsSync ( capturePath ) ) fs . mkdirSync ( capturePath ) ;
50+ const fromFramework = Helpers . getFramework ( {
51+ rootDir : path . resolve ( grunt . option ( 'rootdir' ) || process . cwd ( ) )
52+ } ) ;
53+ const fromPlugins = fromFramework . getPlugins ( ) . getAllPackageJSONFileItems ( ) . map ( fileItem => fileItem . item ) ;
54+ const languages = fromFramework . getData ( ) . languages . map ( ( language ) => language . name ) ;
55+ const languageFile = path . join ( capturePath , 'captureLanguages.json' ) ;
56+ fs . writeJSONSync ( languageFile , languages ) ;
57+ languages . forEach ( async ( language , index ) => {
58+ logger . debug ( `Migration -- Capture ${ language } ` ) ;
59+ const data = fromFramework . getData ( ) ;
60+ // get all items from config.json file and all language files, append __index__ and __path__ to each item
61+ const content = [
62+ ...data . configFile . fileItems ,
63+ ...data . languages [ index ] . getAllFileItems ( )
64+ ] . map ( dressPathIndex ) . map ( obj => {
65+ // reduce __path__ to relative paths about src/course/ or build/course/ so
66+ // that they're easier to restore elsewhere later
67+ obj . __path__ = obj . __path__ . slice ( data . coursePath . length ) . replace ( / ^ \/ / , '' ) ;
68+ return obj ;
69+ } ) ;
70+ const captured = await migrations . capture ( { content, fromPlugins, logger } ) ;
71+ const outputFile = path . join ( capturePath , `capture_${ language } .json` ) ;
72+ fs . writeJSONSync ( outputFile , captured ) ;
73+ } ) ;
4774
48- const plugins = framework . getPlugins ( ) . getAllPackageJSONFileItems ( ) . map ( fileItem => fileItem . item ) ;
75+ logger . output ( capturePath , 'capture' ) ;
76+ return next ( ) ;
77+ }
78+
79+ const toFramework = Helpers . getFramework ( ) ;
80+ logger . debug ( `Using ${ toFramework . useOutputData ? toFramework . outputPath : toFramework . sourcePath } folder for course data...` ) ;
81+ const plugins = toFramework . getPlugins ( ) . getAllPackageJSONFileItems ( ) . map ( fileItem => fileItem . item ) ;
4982 const migrationScripts = Array . from ( await new Promise ( resolve => {
5083 globs ( [
5184 '*/*/migrations/**/*.js' ,
@@ -67,40 +100,20 @@ module.exports = function(grunt) {
67100 logger
68101 } ) ;
69102
70- if ( mode === 'capture' ) {
71-
72- if ( ! fs . existsSync ( outputPath ) ) fs . mkdirSync ( outputPath ) ;
73- const languages = framework . getData ( ) . languages . map ( ( language ) => language . name ) ;
74- const languageFile = path . join ( outputPath , 'captureLanguages.json' ) ;
75- fs . writeJSONSync ( languageFile , languages ) ;
76- languages . forEach ( async ( language , index ) => {
77- logger . debug ( `Migration -- Capture ${ language } ` ) ;
78- const data = framework . getData ( ) ;
79- // get all items from config.json file and all language files, append __index__ and __path__ to each item
80- const content = [
81- ...data . configFile . fileItems ,
82- ...data . languages [ index ] . getAllFileItems ( )
83- ] . map ( dressPathIndex ) ;
84- const captured = await migrations . capture ( { content, fromPlugins : plugins , logger } ) ;
85- const outputFile = path . join ( outputPath , `capture_${ language } .json` ) ;
86- fs . writeJSONSync ( outputFile , captured ) ;
87- } ) ;
88-
89- logger . output ( outputPath , 'capture' ) ;
90- return next ( ) ;
91- }
92-
93103 if ( mode === 'migrate' ) {
104+ const capturePath = grunt . option ( 'capturedir' ) || tempPath ;
105+ const toFrameworkData = toFramework . getData ( { performLoad : false } ) ;
106+ const coursePath = toFrameworkData . coursePath ;
94107 try {
95- const languagesFile = path . join ( outputPath , 'captureLanguages.json' ) ;
108+ const languagesFile = path . join ( capturePath , 'captureLanguages.json' ) ;
96109 const languages = fs . readJSONSync ( languagesFile ) ;
97110
98111 for ( const language of languages ) {
99112 logger . debug ( `Migration -- Migrate ${ language } ` ) ;
100113 const Journal = migrations . Journal ;
101- if ( ! fs . existsSync ( outputPath ) ) fs . mkdirSync ( outputPath ) ;
102- const outputFile = path . join ( outputPath , `capture_${ language } .json` ) ;
103- const { content, fromPlugins } = fs . readJSONSync ( outputFile ) ;
114+ if ( ! fs . existsSync ( capturePath ) ) fs . mkdirSync ( capturePath ) ;
115+ const outputFile = path . join ( capturePath , `capture_${ language } .json` ) ;
116+ let { content, fromPlugins } = fs . readJSONSync ( outputFile ) ;
104117 const originalFromPlugins = JSON . parse ( JSON . stringify ( fromPlugins ) ) ;
105118 const journal = new Journal ( {
106119 logger,
@@ -113,6 +126,11 @@ module.exports = function(grunt) {
113126 } ) ;
114127 await migrations . migrate ( { journal, logger } ) ;
115128
129+ // change out jsonext
130+ content = content . map ( item => {
131+ item . __path__ = item . __path__ . replace ( '.' + item . __jsonext__ , '.' + buildConfig . jsonext ) ;
132+ return item ;
133+ } ) ;
116134 // group all content items by path
117135 const outputFilePathItems = _ . groupBy ( content , '__path__' ) ;
118136 // sort items inside each path
@@ -127,13 +145,17 @@ module.exports = function(grunt) {
127145 const stripped = isSingleObject
128146 ? undressPathIndex ( outputItems [ 0 ] ) // config.json, course.json
129147 : outputItems . map ( undressPathIndex ) ; // contentObjects.json, articles.json, blocks.json, components.json
130- fs . writeJSONSync ( outputPath , stripped , { replacer : null , spaces : 2 } ) ;
148+ // write files to specified --outputdir= location
149+ const outputFilePath = path . join ( coursePath , outputPath ) ;
150+ const outputDir = path . parse ( outputFilePath ) . dir ;
151+ fs . ensureDirSync ( outputDir ) ;
152+ fs . writeJSONSync ( outputFilePath , stripped , { replacer : null , spaces : 2 } ) ;
131153 } ) ;
132154 }
133155 } catch ( error ) {
134156 logger . error ( error . stack ) ;
135157 }
136- logger . output ( outputPath , 'migrate' ) ;
158+ logger . output ( capturePath , 'migrate' ) ;
137159 return next ( ) ;
138160 }
139161
0 commit comments