@@ -100,32 +100,11 @@ const createPluginDir = () => {
100100 }
101101} ;
102102
103- // Copy template to the plugin dir.
104- const copyTemplateToPluginDir = ( ) => {
105- const resolvePkg = require ( 'resolve-pkg' ) ;
106- const template = resolvePkg ( 'cgb-scripts/template' , { cwd : __dirname } ) ;
107-
108- return new Promise ( resolve => {
109- shell . cd ( blockDir ) ;
110- shell . cp ( '-RL' , `${ template } /*` , './' ) ;
111- shell . cp ( '-RL' , `${ template } /.*` , './' ) ;
112-
113- // Replace dynamic content for block name in the code.
114- shell . ls ( '**/**.*' ) . forEach ( function ( file ) {
115- shell . sed ( '-i' , '<% blockName %>' , `${ blockName } ` , file ) ;
116- shell . sed ( '-i' , '<% blockName % >' , `${ blockName } ` , file ) ;
117- shell . sed ( '-i' , '<% blockNamePHPLower %>' , `${ blockNamePHPLower } ` , file ) ;
118- shell . sed ( '-i' , '<% blockNamePHPLower % >' , `${ blockNamePHPLower } ` , file ) ;
119- shell . sed ( '-i' , '<% blockNamePHPUpper %>' , `${ blockNamePHPUpper } ` , file ) ;
120- shell . sed ( '-i' , '<% blockNamePHPUpper % >' , `${ blockNamePHPUpper } ` , file ) ;
121- } ) ;
122-
123- resolve ( ) ;
124- } ) ;
125- } ;
126-
127103// NPM install and npm run build to build the block.
128- const npmInstallBuild = ( ) => {
104+ const npmInstallScripts = ( ) => {
105+ shell . cd ( blockDir ) ;
106+ shell . touch ( 'package.json' ) ;
107+
129108 // Write a package.json file since npm install needs it.
130109 const appPackage = {
131110 name : `${ blockName } -cgb-guten-block` ,
@@ -137,26 +116,61 @@ const npmInstallBuild = () => {
137116 eject : 'cgb-scripts eject' ,
138117 } ,
139118 } ;
119+
120+ // Write the file.
140121 fs . writeFileSync (
141122 path . join ( process . cwd ( ) , 'package.json' ) ,
142123 JSON . stringify ( appPackage , null , 2 ) + '\n'
143124 ) ;
125+
126+ // Install latest cgb-scripts.
144127 return new Promise ( async resolve => {
145- // Install.
146- // await execa( 'npm', [ 'install', '--slient' ] );
128+ await execa ( 'npm' , [ 'install' , 'cgb-scripts' , '--save' , '--slient' ] ) ;
129+ resolve ( true ) ;
130+ } ) ;
131+ } ;
132+
133+ // Copy template to the plugin dir.
134+ const copyTemplateFiles = ( ) => {
135+ // Since we ran npm install cgb-scripts we have it in the node_modules now.
136+ const template = path . resolve (
137+ blockDir ,
138+ './node_modules/cgb-scripts/template'
139+ ) ;
147140
148- // Install latest cgb-scripts.
149- await execa ( 'npm' , [ 'install' , 'cgb-scripts' , '--slient' ] ) ;
150- resolve ( ) ;
141+ return new Promise ( resolve => {
142+ shell . cd ( blockDir ) ;
143+ // Create a tmp folder to avoid globbing through node_modules.
144+ shell . exec ( 'mkdir -p ./tmp' ) ;
145+ shell . cp ( '-RL' , `${ template } /*` , './tmp' ) ;
146+ shell . cp ( '-RL' , `${ template } /.*` , './tmp' ) ;
147+
148+ // Replace dynamic content for block name in the code.
149+ shell . ls ( 'tmp/**/**.*' ) . forEach ( function ( file ) {
150+ shell . sed ( '-i' , '<% blockName %>' , `${ blockName } ` , file ) ;
151+ shell . sed ( '-i' , '<% blockName % >' , `${ blockName } ` , file ) ;
152+ shell . sed ( '-i' , '<% blockNamePHPLower %>' , `${ blockNamePHPLower } ` , file ) ;
153+ shell . sed ( '-i' , '<% blockNamePHPLower % >' , `${ blockNamePHPLower } ` , file ) ;
154+ shell . sed ( '-i' , '<% blockNamePHPUpper %>' , `${ blockNamePHPUpper } ` , file ) ;
155+ shell . sed ( '-i' , '<% blockNamePHPUpper % >' , `${ blockNamePHPUpper } ` , file ) ;
156+ } ) ;
157+
158+ // Move all processed files back to the main folder.
159+ shell . cp ( '-RL' , `${ blockDir } /tmp/*` , './' ) ;
160+ shell . cp ( '-RL' , `${ blockDir } /tmp/.*` , './' ) ;
161+
162+ // Thank you, tmpΒ β you've been a useful friend.
163+ shell . rm ( '-rf' , './tmp' ) ;
164+ resolve ( true ) ;
151165 } ) ;
152166} ;
153167
154168// Final npm run build to build the block.
155- const finalNpmBuild = ( ) => {
169+ const npmBuildBlock = ( ) => {
156170 return new Promise ( async resolve => {
157171 // Build.
158172 await execa ( 'npm' , [ 'run' , 'build' , '--slient' ] ) ;
159- resolve ( ) ;
173+ resolve ( true ) ;
160174 } ) ;
161175} ;
162176
@@ -174,14 +188,14 @@ const prePrint = () => {
174188 chalk . dim ( 'This might take a couple of minutes.\n' )
175189 ) ;
176190} ;
191+
177192// Prints next steps.
178193const printNextSteps = ( ) => {
179194 console . log (
180195 '\n\nβ
' ,
181196 chalk . black . bgGreen ( ' All done! Go build some Gutenberg blocks. \n' )
182197 ) ;
183198 console . log (
184- // chalk.black.bgWhite( ' create-guten-block ' ),
185199 `CGB ${ chalk . dim (
186200 '(create-guten-block)'
187201 ) } has created a WordPress plugin called `,
@@ -229,15 +243,15 @@ const run = async() => {
229243 spinner . succeed ( ) ;
230244
231245 spinner . start ( '2. Installing npm packages...' ) ;
232- await npmInstallBuild ( ) ;
246+ await npmInstallScripts ( ) ;
233247 spinner . succeed ( ) ;
234248
235249 spinner . start ( '3. Creating plugin files...' ) ;
236- await copyTemplateToPluginDir ( ) ;
250+ await copyTemplateFiles ( ) ;
237251 spinner . succeed ( ) ;
238252
239253 spinner . start ( '4. Finally building the block...' ) ;
240- finalNpmBuild ( ) ;
254+ await npmBuildBlock ( ) ;
241255 spinner . succeed ( ) ;
242256
243257 await printNextSteps ( ) ;
0 commit comments