@@ -7,26 +7,46 @@ const isUrl = source => source.indexOf('http') === 0
77
88/**
99 * Get the data from a local or remote JSON file
10- * @param {string } source the local path or remote url of the file
10+ * @param {string } path the local path or remote url of the file
1111 * @returns {Promise<Object> } return the data from the source or an error
1212 */
13- const getDataFromSource = async ( source ) => {
14- if ( ! source ) {
13+ const getDataFromPath = async ( path ) => {
14+ if ( ! path ) {
1515 return { }
1616 }
17+ const sources = path . split ( ',' )
18+ const isList = sources . length > 1
1719
1820 try {
19- if ( isUrl ( source ) ) {
20- return ( await axios . get ( source ) ) . data
21+ if ( isUrl ( path ) ) {
22+ return ( await axios . get ( path ) ) . data
2123 } else {
22- return JSON . parse ( fs . readFileSync ( source , 'utf8' ) )
24+ if ( ! isList ) return JSON . parse ( fs . readFileSync ( sources [ 0 ] , 'utf8' ) )
25+
26+ const data = [ ]
27+ sources . forEach ( ( source ) => {
28+ data . push ( JSON . parse ( fs . readFileSync ( source , 'utf8' ) ) )
29+ } )
30+ return data
2331 }
2432 } catch ( err ) {
25- console . error ( `${ chalk . red ( 'X' ) } Can not load json file from ${ source } ` )
33+ console . error ( `${ chalk . red ( 'X' ) } Can not load json file from ${ path } ` )
2634 return Promise . reject ( err )
2735 }
2836}
2937
38+ /**
39+ * Creat an array based in the content parameter and the key provided
40+ * @param {object } content the data to create a list
41+ * @param {string } key key to serch in the content
42+ * @returns {Array } return the data from the source or an error
43+ */
44+ const createContentList = ( content , key ) => {
45+ if ( content [ key ] ) return content [ key ]
46+ else if ( Array . isArray ( content ) ) return [ ...content ]
47+ else return [ content ]
48+ }
49+
3050/**
3151 * Delete all components from your Space that occur in your Local JSON.
3252 * @param api {Object}
@@ -37,7 +57,8 @@ const getDataFromSource = async (source) => {
3757 */
3858const deleteComponents = async ( api , { source, reversed = false , dryRun = false } ) => {
3959 try {
40- const sourceComponents = ( await getDataFromSource ( source ) ) . components || [ ]
60+ const rawComponents = ( await getDataFromPath ( source ) ) || [ ]
61+ const sourceComponents = createContentList ( rawComponents , 'components' )
4162 if ( ! reversed ) {
4263 return deleteAllComponents ( api , sourceComponents , dryRun )
4364 }
@@ -71,19 +92,17 @@ const deleteAllComponents = async (api, components, dryrun) => {
7192 * @returns {Promise<void> }
7293 */
7394const deleteComponentsReversed = async ( api , components , spaceComponents , dryrun ) => {
74- const unifiedComps = components . concat ( [ ...spaceComponents ] )
75- const toDelete = unifiedComps
76- . filter ( ( value , index , self ) =>
77- self . findIndex ( ( o , i ) => o . id === value . id && i !== index ) < 0 )
95+ const toDeleteSpaceComponents = spaceComponents
96+ . filter ( spaceComponent => components . findIndex ( o => o . name === spaceComponent . name ) < 0 )
7897 console . log ( chalk . blue ( '-' ) + ' Deleting all components which do not appear in the given source.' )
79- for ( const c of toDelete ) {
98+ for ( const c of toDeleteSpaceComponents ) {
8099 await deleteComponentAndSkip ( api , c , dryrun )
81100 }
82101}
83102
84103const deleteComponentAndSkip = async ( api , c , dryrun ) => {
85104 try {
86- return await deleteComponent ( api , { comp : c . id , dryrun : dryrun } )
105+ return await deleteComponent ( api , { comp : c . name , dryrun : dryrun } )
87106 } catch ( e ) {
88107 console . log ( chalk . red ( '-' ) + ' Error deleting component ' + chalk . blue ( c . name ) + '! Skipped...' )
89108 }
0 commit comments