Skip to content

Commit

Permalink
Move function to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
msaggiorato committed Jun 25, 2021
1 parent e4b7f5b commit 93a33fc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
14 changes: 2 additions & 12 deletions scripts/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
const path = require( 'path' );
const { readdirSync, statSync, renameSync, rename } = require( 'fs' );
const { statSync, renameSync } = require( 'fs' );
const downloadGH = require( 'download-git-repo' );
const rimraf = require( 'rimraf' );
const replace = require( 'replace' );
Expand All @@ -11,7 +11,7 @@ const slugify = require( 'slugify' );
/**
* Internal dependencies
*/
const { getArgFromCLI } = require( '../utils' );
const { getArgFromCLI, walkDirectory } = require( '../utils' );

let data = {
branch: getArgFromCLI( '--branch' ) || 'master',
Expand Down Expand Up @@ -51,16 +51,6 @@ const shortify = function( name ) {
return newName;
}

const walkDirectory = function(dir, cb) {
cb = cb || ( (f) => f );
var files = readdirSync(dir)
.map( ( f ) => path.join( dir, f ) )
.map( ( f ) => statSync( f ).isDirectory() ? walkDirectory( f ) : f )
.flat()
.map( cb );
return files;
};

let pluginName = String(data.name).length ? data.name : 'Amazing Plugin';
let pluginSlug = String(data.slug).length ? String(data.slug).toLowerCase() : slugify( pluginName ).toLowerCase();
let pluginURI = String(data.uri).length ? data.uri : 'https://saucal.com/' ;
Expand Down
10 changes: 9 additions & 1 deletion utils/file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
const { existsSync, readdirSync } = require( 'fs' );
const { existsSync, readdirSync, statSync } = require( 'fs' );
const path = require( 'path' );

/**
Expand Down Expand Up @@ -29,11 +29,19 @@ const getScripts = () =>
.filter( ( f ) => path.extname( f ) === '.js' )
.map( ( f ) => path.basename( f, '.js' ) );

const walkDirectory = (dir, cb) =>
readdirSync(dir)
.map( ( f ) => path.join( dir, f ) )
.map( ( f ) => statSync( f ).isDirectory() ? walkDirectory( f ) : f )
.flat()
.map( cb || ( (f) => f ) );

module.exports = {
fromProjectRoot,
fromConfigRoot,
fromScriptsRoot,
getScripts,
hasProjectFile,
hasScriptFile,
walkDirectory,
};
3 changes: 2 additions & 1 deletion utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const {
hasPrettierConfig,
hasPostCSSConfig,
} = require( './config' );
const { fromProjectRoot, fromConfigRoot, hasProjectFile } = require( './file' );
const { fromProjectRoot, fromConfigRoot, hasProjectFile, walkDirectory } = require( './file' );
const { getPackageProp, hasPackageProp } = require( './package' );

module.exports = {
Expand All @@ -40,4 +40,5 @@ module.exports = {
hasPrettierConfig,
hasProjectFile,
spawnScript,
walkDirectory,
};

0 comments on commit 93a33fc

Please sign in to comment.