@@ -71,14 +71,22 @@ function buildRepoUrlForPlugin(plugin) {
7171/**
7272 * @summary Clones a plugin in the api-plugins directory
7373 * @param {String } plugin - key of the plugin
74- * @return {void }
74+ * @return {Promise< void> } - promise that resolves when the plugin is cloned
7575 */
76- function clonePlugin ( plugin ) {
76+ async function clonePlugin ( plugin ) {
7777 const apiPluginRepoUrl = buildRepoUrlForPlugin ( plugin ) ;
7878
79- spawn ( "git" , [ "clone" , apiPluginRepoUrl ] , {
80- stdio : [ process . stdout , process . stderr , process . stdin ] ,
81- cwd : `${ process . cwd ( ) } /api-plugins`
79+ return new Promise ( ( resolve , reject ) => {
80+ spawn ( "git" , [ "clone" , apiPluginRepoUrl ] , {
81+ stdio : [ process . stdout , process . stderr , process . stdin ] ,
82+ cwd : `${ process . cwd ( ) } /api-plugins`
83+ } )
84+ . on ( "exit" , ( errorCode ) => {
85+ if ( errorCode === 0 ) {
86+ resolve ( ) ;
87+ }
88+ reject ( "Error cloning plugin" ) ;
89+ } ) ;
8290 } ) ;
8391}
8492
@@ -88,29 +96,56 @@ function clonePlugin(plugin) {
8896 * @return {Promise<String[]> } - list of the selected plugins
8997 */
9098async function getManuallySelectedPlugins ( allPlugins ) {
91- const { plugins } = await inquirer . prompt ( [
92- {
93- type : "checkbox" ,
94- message : "Select the plugins you want to clone:" ,
95- name : "plugins" ,
96- choices : allPlugins ,
97- validate : ( ans ) => {
98- if ( ans . length === 0 ) {
99- return "You must choose at least one plugin." ;
100- }
101- return true ;
99+ const { plugins } = await inquirer . prompt ( [ {
100+ type : "checkbox" ,
101+ message : "Select the plugins you want to clone:" ,
102+ name : "plugins" ,
103+ choices : allPlugins ,
104+ validate : ( ans ) => {
105+ if ( ans . length === 0 ) {
106+ return "You must choose at least one plugin." ;
102107 }
108+ return true ;
103109 }
104- ] ) ;
110+ } ] ) ;
105111 return plugins ;
106112}
107113
114+ /**
115+ * @summary Get the plugins.json file
116+ * @return {Object } - the plugins.json as an object
117+ */
118+ function getPluginsJson ( ) {
119+ const pluginsJson = fs . readFileSync ( `${ process . cwd ( ) } /plugins.json` , "utf8" ) ;
120+ return JSON . parse ( pluginsJson ) ;
121+ }
122+
123+ /**
124+ * @summary Link a local plugin in the plugins.json file
125+ * @param {String } plugin - name of the plugin
126+ * @return {void }
127+ */
128+ function linkLocalPlugin ( plugin ) {
129+ Logger . info ( `Linking local plugin ${ plugin } in plugins.json` ) ;
130+ const pluginsJson = getPluginsJson ( ) ;
131+ for ( const key in pluginsJson ) {
132+ if ( pluginsJson [ key ] === `@reactioncommerce/${ plugin } ` ) {
133+ pluginsJson [ key ] = `./api-plugins/${ plugin } /index.js` ;
134+ break ;
135+ }
136+ }
137+ fs . writeFileSync ( `${ process . cwd ( ) } /plugins.json` , JSON . stringify ( pluginsJson , null , 2 ) ) ;
138+ }
139+
108140/**
109141 * @summary Clone all official Open Commerce API plugins in the api project
110142 * @param {Object } options - Options for cloning api plugins command
111143 * @returns {Promise<boolean> } - return true if successful
112144 */
113- export default async function cloneApiPlugins ( { manualSelect } ) {
145+ export default async function cloneApiPlugins ( {
146+ manualSelect,
147+ link
148+ } ) {
114149 const isApiProject = await isProjectOfType ( "api" ) ;
115150 if ( ! isApiProject ) {
116151 return false ;
@@ -124,12 +159,20 @@ export default async function cloneApiPlugins({ manualSelect }) {
124159
125160 const pluginsToClone = manuallySelectedPlugins || allPlugins ;
126161
127- pluginsToClone
128- . forEach ( ( plugin ) => {
129- if ( ! isPluginAlreadyCloned ( plugin ) ) {
130- clonePlugin ( plugin ) ;
131- }
132- } ) ;
162+ const cloneAndLinkPromises = pluginsToClone . map ( async ( plugin ) => {
163+ if ( ! isPluginAlreadyCloned ( plugin ) ) {
164+ await clonePlugin ( plugin ) ;
165+ }
166+ if ( link ) {
167+ linkLocalPlugin ( plugin ) ;
168+ }
169+ } ) ;
133170
171+ try {
172+ await Promise . all ( cloneAndLinkPromises ) ;
173+ } catch ( error ) {
174+ Logger . error ( error ) ;
175+ return false ;
176+ }
134177 return true ;
135178}
0 commit comments